-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathworkflow.rb
More file actions
65 lines (56 loc) · 2.2 KB
/
workflow.rb
File metadata and controls
65 lines (56 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true
# @@@SNIPSTART money-transfer-project-template-ruby-workflow
require_relative 'activities'
require_relative 'shared'
require 'temporalio/retry_policy'
require 'temporalio/workflow'
module MoneyTransfer
# Temporal Workflow that withdraws the specified amount from the source
# account and deposits it into the target account, refunding the source
# account if the deposit cannot be completed.
class MoneyTransferWorkflow < Temporalio::Workflow::Definition
def execute(details)
retry_policy = Temporalio::RetryPolicy.new(
max_interval: 10,
non_retryable_error_types: [
'InvalidAccountError',
'InsufficientFundsError'
]
)
Temporalio::Workflow.logger.info("Starting workflow (#{details})")
withdraw_result = Temporalio::Workflow.execute_activity(
BankActivities::Withdraw,
details,
start_to_close_timeout: 5,
retry_policy: retry_policy
)
Temporalio::Workflow.logger.info("Withdrawal confirmation: #{withdraw_result}")
begin
deposit_result = Temporalio::Workflow.execute_activity(
BankActivities::Deposit,
details,
start_to_close_timeout: 5,
retry_policy: retry_policy
)
Temporalio::Workflow.logger.info("Deposit confirmation: #{deposit_result}")
"Transfer complete (transaction IDs: #{withdraw_result}, #{deposit_result})"
rescue Temporalio::Error::ActivityError => e
Temporalio::Workflow.logger.error("Deposit failed: #{e}")
# Since the deposit failed, attempt to recover by refunding the withdrawal
begin
refund_result = Temporalio::Workflow.execute_activity(
BankActivities::Refund,
details,
start_to_close_timeout: 5,
retry_policy: retry_policy
)
Temporalio::Workflow.logger.info("Refund confirmation: #{refund_result}")
"Transfer complete (transaction IDs: #{withdraw_result}, #{refund_result})"
rescue Temporalio::Error::ActivityError => refund_error
Temporalio::Workflow.logger.error("Refund failed: #{refund_error}")
end
end
end
end
end
# @@@SNIPEND