Slide 82
Slide 82 text
SIGNALS: SERVICES
class PayoutWorkflow < Temporalio::Workflow::Definition
attr_accessor :status, :payment
Temporal
workflow_signal
def payment_received(token, status, amount)
self.payment = {token:, status:, amount:}
end
Damn, you write
Ruby like that?!
def execute(payout_id, wallet_id, amount)
self.status = "withdrawing"
Temporalio::Workflow.execute_activity(WithdrawFunds, payout_id, wallet_id, amount,
start_to_close_timeout: 60)
self.status = "waiting_for_payment"
Temporalio::Workflow.wait_condition { payment.present? }
raise Temporalio::Error::ApplicationError.new("payment failed", non_retryable: true) if payment[:status]
== "failed"
self.status = "crediting"
Temporalio::Workflow.execute_activity(CreditLedger, payout_id, payment[:amount], start_to_close_timeout:
60)
self.status = "completed"
end
end
class WithdrawFunds < Temporalio::Activity::Definition
def execute(payout_id, wallet_id, amount)
Wallet::Withdraw.(wallet_id:, amount:, payout: Payout.find(payout_id))
end
end
class CreditLedger < Temporalio::Activity::Definition
def execute(payout_id, amount)
payout = Payout.find(payout_id)
Ledger.credit!(payout.account, amount)
payout.completed!
end
end