-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinterceptor.rb
More file actions
149 lines (121 loc) · 3.64 KB
/
interceptor.rb
File metadata and controls
149 lines (121 loc) · 3.64 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# frozen_string_literal: true
require 'temporalio/client/interceptor'
require 'temporalio/worker/interceptor'
module ContextPropagation
class Interceptor
include Temporalio::Client::Interceptor
include Temporalio::Worker::Interceptor::Workflow
include Temporalio::Worker::Interceptor::Activity
def initialize(*keys_to_propagate)
@keys_to_propagate = keys_to_propagate
end
def intercept_client(next_interceptor)
ClientOutbound.new(self, next_interceptor)
end
def intercept_workflow(next_interceptor)
WorkflowInbound.new(self, next_interceptor)
end
def intercept_activity(next_interceptor)
ActivityInbound.new(self, next_interceptor)
end
def context_to_headers(input)
@keys_to_propagate.each do |key|
value = Thread.current[key]
input.headers[key.to_s] = value unless value.nil?
end
end
def with_context_from_headers(input)
# Grab all original values
orig_values = @keys_to_propagate.map { |key| [key, Thread.current[key]] }
# Replace values, even if they are nil
@keys_to_propagate.each { |key| Thread.current[key] = input.headers[key.to_s] }
begin
yield
ensure
# Put them all back, even if they were nil
orig_values.each { |key, val| Thread.current[key] = val }
end
end
class ClientOutbound < Temporalio::Client::Interceptor::Outbound
def initialize(root, next_interceptor)
super(next_interceptor)
@root = root
end
def start_workflow(input)
@root.context_to_headers(input)
super
end
def signal_workflow(input)
@root.context_to_headers(input)
super
end
def query_workflow(input)
@root.context_to_headers(input)
super
end
def start_workflow_update(input)
@root.context_to_headers(input)
super
end
end
class WorkflowInbound < Temporalio::Worker::Interceptor::Workflow::Inbound
def initialize(root, next_interceptor)
super(next_interceptor)
@root = root
end
def init(outbound)
super(WorkflowOutbound.new(@root, outbound))
end
def execute(input)
@root.with_context_from_headers(input) { super }
end
def handle_signal(input)
@root.with_context_from_headers(input) { super }
end
def handle_query(input)
@root.with_context_from_headers(input) { super }
end
def validate_update(input)
@root.with_context_from_headers(input) { super }
end
def handle_update(input)
@root.with_context_from_headers(input) { super }
end
end
class WorkflowOutbound < Temporalio::Worker::Interceptor::Workflow::Outbound
def initialize(root, next_interceptor)
super(next_interceptor)
@root = root
end
def execute_activity(input)
@root.context_to_headers(input)
super
end
def execute_local_activity(input)
@root.context_to_headers(input)
super
end
def signal_child_workflow(input)
@root.context_to_headers(input)
super
end
def signal_external_workflow(input)
@root.context_to_headers(input)
super
end
def start_child_workflow(input)
@root.context_to_headers(input)
super
end
end
class ActivityInbound < Temporalio::Worker::Interceptor::Activity::Inbound
def initialize(root, next_interceptor)
super(next_interceptor)
@root = root
end
def execute(input)
@root.with_context_from_headers(input) { super }
end
end
end
end