Skip to content

Commit 378630a

Browse files
authored
[Refactor] Use local configuration instead of a global one (coinbase#92)
1 parent 973eaf5 commit 378630a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+378
-297
lines changed

examples/lib/cryptconverter.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'openssl'
22

33
module Temporal
4-
class CryptConverter < Temporal::Client::Converter::Base
4+
class CryptConverter < Temporal::Connection::Converter::Base
55
CIPHER = 'aes-256-gcm'.freeze
66
GCM_NONCE_SIZE = 12
77
GCM_TAG_SIZE = 16

examples/spec/helpers.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def wait_for_workflow_completion(workflow_id, run_id)
2323
end
2424

2525
def fetch_history(workflow_id, run_id, options = {})
26-
client = Temporal.send(:client)
26+
connection = Temporal.send(:connection)
2727

28-
result = client.get_workflow_execution_history(
28+
result = connection.get_workflow_execution_history(
2929
{
3030
namespace: Temporal.configuration.namespace,
3131
workflow_id: workflow_id,

lib/temporal.rb

+28-23
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
require 'securerandom'
55
require 'temporal/configuration'
66
require 'temporal/execution_options'
7-
require 'temporal/client'
7+
require 'temporal/connection'
88
require 'temporal/activity'
99
require 'temporal/activity/async_token'
1010
require 'temporal/workflow'
@@ -21,10 +21,10 @@ def start_workflow(workflow, *input, **args)
2121
options = args.delete(:options) || {}
2222
input << args unless args.empty?
2323

24-
execution_options = ExecutionOptions.new(workflow, options)
24+
execution_options = ExecutionOptions.new(workflow, options, config.default_execution_options)
2525
workflow_id = options[:workflow_id] || SecureRandom.uuid
2626

27-
response = client.start_workflow_execution(
27+
response = connection.start_workflow_execution(
2828
namespace: execution_options.namespace,
2929
workflow_id: workflow_id,
3030
workflow_name: execution_options.name,
@@ -45,10 +45,10 @@ def schedule_workflow(workflow, cron_schedule, *input, **args)
4545
options = args.delete(:options) || {}
4646
input << args unless args.empty?
4747

48-
execution_options = ExecutionOptions.new(workflow, options)
48+
execution_options = ExecutionOptions.new(workflow, options, config.default_execution_options)
4949
workflow_id = options[:workflow_id] || SecureRandom.uuid
5050

51-
response = client.start_workflow_execution(
51+
response = connection.start_workflow_execution(
5252
namespace: execution_options.namespace,
5353
workflow_id: workflow_id,
5454
workflow_name: execution_options.name,
@@ -69,13 +69,13 @@ def schedule_workflow(workflow, cron_schedule, *input, **args)
6969
end
7070

7171
def register_namespace(name, description = nil)
72-
client.register_namespace(name: name, description: description)
72+
connection.register_namespace(name: name, description: description)
7373
end
7474

7575
def signal_workflow(workflow, signal, workflow_id, run_id, input = nil)
76-
execution_options = ExecutionOptions.new(workflow)
76+
execution_options = ExecutionOptions.new(workflow, {}, config.default_execution_options)
7777

78-
client.signal_workflow_execution(
78+
connection.signal_workflow_execution(
7979
namespace: execution_options.namespace, # TODO: allow passing namespace instead
8080
workflow_id: workflow_id,
8181
run_id: run_id,
@@ -95,11 +95,11 @@ def signal_workflow(workflow, signal, workflow_id, run_id, input = nil)
9595
# namespace: if nil, choose the one declared on the Workflow, or the global default
9696
def await_workflow_result(workflow, workflow_id:, run_id: nil, timeout: nil, namespace: nil)
9797
options = namespace ? {namespace: namespace} : {}
98-
execution_options = ExecutionOptions.new(workflow, options)
99-
max_timeout = Temporal::Client::GRPCClient::SERVER_MAX_GET_WORKFLOW_EXECUTION_HISTORY_POLL
98+
execution_options = ExecutionOptions.new(workflow, options, config.default_execution_options)
99+
max_timeout = Temporal::Connection::GRPC::SERVER_MAX_GET_WORKFLOW_EXECUTION_HISTORY_POLL
100100
history_response = nil
101101
begin
102-
history_response = client.get_workflow_execution_history(
102+
history_response = connection.get_workflow_execution_history(
103103
namespace: execution_options.namespace,
104104
workflow_id: workflow_id,
105105
run_id: run_id,
@@ -143,7 +143,7 @@ def reset_workflow(namespace, workflow_id, run_id, workflow_task_id: nil, reason
143143
workflow_task_id ||= get_last_completed_workflow_task_id(namespace, workflow_id, run_id)
144144
raise Error, 'Could not find a completed workflow task event' unless workflow_task_id
145145

146-
response = client.reset_workflow_execution(
146+
response = connection.reset_workflow_execution(
147147
namespace: namespace,
148148
workflow_id: workflow_id,
149149
run_id: run_id,
@@ -157,7 +157,7 @@ def reset_workflow(namespace, workflow_id, run_id, workflow_task_id: nil, reason
157157
def terminate_workflow(workflow_id, namespace: nil, run_id: nil, reason: nil, details: nil)
158158
namespace ||= Temporal.configuration.namespace
159159

160-
client.terminate_workflow_execution(
160+
connection.terminate_workflow_execution(
161161
namespace: namespace,
162162
workflow_id: workflow_id,
163163
run_id: run_id,
@@ -167,7 +167,7 @@ def terminate_workflow(workflow_id, namespace: nil, run_id: nil, reason: nil, de
167167
end
168168

169169
def fetch_workflow_execution_info(namespace, workflow_id, run_id)
170-
response = client.describe_workflow_execution(
170+
response = connection.describe_workflow_execution(
171171
namespace: namespace,
172172
workflow_id: workflow_id,
173173
run_id: run_id
@@ -179,7 +179,7 @@ def fetch_workflow_execution_info(namespace, workflow_id, run_id)
179179
def complete_activity(async_token, result = nil)
180180
details = Activity::AsyncToken.decode(async_token)
181181

182-
client.respond_activity_task_completed_by_id(
182+
connection.respond_activity_task_completed_by_id(
183183
namespace: details.namespace,
184184
activity_id: details.activity_id,
185185
workflow_id: details.workflow_id,
@@ -191,7 +191,7 @@ def complete_activity(async_token, result = nil)
191191
def fail_activity(async_token, exception)
192192
details = Activity::AsyncToken.decode(async_token)
193193

194-
client.respond_activity_task_failed_by_id(
194+
connection.respond_activity_task_failed_by_id(
195195
namespace: details.namespace,
196196
activity_id: details.activity_id,
197197
workflow_id: details.workflow_id,
@@ -201,19 +201,20 @@ def fail_activity(async_token, exception)
201201
end
202202

203203
def configure(&block)
204-
yield configuration
204+
yield config
205205
end
206206

207207
def configuration
208-
@configuration ||= Configuration.new
208+
warn '[DEPRECATION] This method is now deprecated without a substitution'
209+
config
209210
end
210211

211212
def logger
212-
configuration.logger
213+
config.logger
213214
end
214215

215216
def metrics
216-
@metrics ||= Metrics.new(configuration.metrics_adapter)
217+
@metrics ||= Metrics.new(config.metrics_adapter)
217218
end
218219

219220
class ResultConverter
@@ -222,13 +223,17 @@ class ResultConverter
222223
private_constant :ResultConverter
223224

224225
private
226+
227+
def config
228+
@config ||= Configuration.new
229+
end
225230

226-
def client
227-
@client ||= Temporal::Client.generate
231+
def connection
232+
@connection ||= Temporal::Connection.generate(config.for_connection)
228233
end
229234

230235
def get_last_completed_workflow_task_id(namespace, workflow_id, run_id)
231-
history_response = client.get_workflow_execution_history(
236+
history_response = connection.get_workflow_execution_history(
232237
namespace: namespace,
233238
workflow_id: workflow_id,
234239
run_id: run_id

lib/temporal/activity/context.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
module Temporal
88
class Activity
99
class Context
10-
def initialize(client, metadata)
11-
@client = client
10+
def initialize(connection, metadata)
11+
@connection = connection
1212
@metadata = metadata
1313
@async = false
1414
end
@@ -32,7 +32,7 @@ def async_token
3232

3333
def heartbeat(details = nil)
3434
logger.debug("Activity heartbeat", metadata.to_h)
35-
client.record_activity_task_heartbeat(task_token: task_token, details: details)
35+
connection.record_activity_task_heartbeat(task_token: task_token, details: details)
3636
end
3737

3838
def heartbeat_details
@@ -58,7 +58,7 @@ def headers
5858

5959
private
6060

61-
attr_reader :client, :metadata
61+
attr_reader :connection, :metadata
6262

6363
def task_token
6464
metadata.task_token

lib/temporal/activity/poller.rb

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'temporal/client'
1+
require 'temporal/connection'
22
require 'temporal/thread_pool'
33
require 'temporal/middleware/chain'
44
require 'temporal/activity/task_processor'
@@ -11,10 +11,11 @@ class Poller
1111
thread_pool_size: 20
1212
}.freeze
1313

14-
def initialize(namespace, task_queue, activity_lookup, middleware = [], options = {})
14+
def initialize(namespace, task_queue, activity_lookup, config, middleware = [], options = {})
1515
@namespace = namespace
1616
@task_queue = task_queue
1717
@activity_lookup = activity_lookup
18+
@config = config
1819
@middleware = middleware
1920
@shutting_down = false
2021
@options = DEFAULT_OPTIONS.merge(options)
@@ -31,7 +32,7 @@ def stop_polling
3132
end
3233

3334
def cancel_pending_requests
34-
client.cancel_polling_request
35+
connection.cancel_polling_request
3536
end
3637

3738
def wait
@@ -41,10 +42,10 @@ def wait
4142

4243
private
4344

44-
attr_reader :namespace, :task_queue, :activity_lookup, :middleware, :options, :thread
45+
attr_reader :namespace, :task_queue, :activity_lookup, :config, :middleware, :options, :thread
4546

46-
def client
47-
@client ||= Temporal::Client.generate
47+
def connection
48+
@connection ||= Temporal::Connection.generate(config.for_connection)
4849
end
4950

5051
def shutting_down?
@@ -73,7 +74,7 @@ def poll_loop
7374
end
7475

7576
def poll_for_task
76-
client.poll_activity_task_queue(namespace: namespace, task_queue: task_queue)
77+
connection.poll_activity_task_queue(namespace: namespace, task_queue: task_queue)
7778
rescue StandardError => error
7879
Temporal.logger.error("Unable to poll activity task queue", { namespace: namespace, task_queue: task_queue, error: error.inspect })
7980

@@ -85,7 +86,7 @@ def poll_for_task
8586
def process(task)
8687
middleware_chain = Middleware::Chain.new(middleware)
8788

88-
TaskProcessor.new(task, namespace, activity_lookup, client, middleware_chain).process
89+
TaskProcessor.new(task, namespace, activity_lookup, middleware_chain, config).process
8990
end
9091

9192
def thread_pool

lib/temporal/activity/task_processor.rb

+15-9
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
require 'temporal/errors'
44
require 'temporal/activity/context'
55
require 'temporal/concerns/payloads'
6-
require 'temporal/client/retryer'
6+
require 'temporal/connection/retryer'
7+
require 'temporal/connection'
78

89
module Temporal
910
class Activity
1011
class TaskProcessor
1112
include Concerns::Payloads
1213

13-
def initialize(task, namespace, activity_lookup, client, middleware_chain)
14+
def initialize(task, namespace, activity_lookup, middleware_chain, config)
1415
@task = task
1516
@namespace = namespace
1617
@metadata = Metadata.generate(Metadata::ACTIVITY_TYPE, task, namespace)
1718
@task_token = task.task_token
1819
@activity_name = task.activity_type.name
1920
@activity_class = activity_lookup.find(activity_name)
20-
@client = client
2121
@middleware_chain = middleware_chain
22+
@config = config
2223
end
2324

2425
def process
@@ -27,7 +28,7 @@ def process
2728
Temporal.logger.debug("Processing Activity task", metadata.to_h)
2829
Temporal.metrics.timing('activity_task.queue_time', queue_time_ms, activity: activity_name)
2930

30-
context = Activity::Context.new(client, metadata)
31+
context = Activity::Context.new(connection, metadata)
3132

3233
if !activity_class
3334
raise ActivityNotRegistered, 'Activity is not registered with this worker'
@@ -51,7 +52,12 @@ def process
5152

5253
private
5354

54-
attr_reader :task, :namespace, :task_token, :activity_name, :activity_class, :client, :middleware_chain, :metadata
55+
attr_reader :task, :namespace, :task_token, :activity_name, :activity_class,
56+
:middleware_chain, :metadata, :config
57+
58+
def connection
59+
@connection ||= Temporal::Connection.generate(config.for_connection)
60+
end
5561

5662
def queue_time_ms
5763
scheduled = task.current_attempt_scheduled_time.to_f
@@ -64,8 +70,8 @@ def respond_completed(result)
6470
log_retry = proc do
6571
Temporal.logger.debug("Failed to report activity task completion, retrying", metadata.to_h)
6672
end
67-
Temporal::Client::Retryer.with_retries(on_retry: log_retry) do
68-
client.respond_activity_task_completed(task_token: task_token, result: result)
73+
Temporal::Connection::Retryer.with_retries(on_retry: log_retry) do
74+
connection.respond_activity_task_completed(task_token: task_token, result: result)
6975
end
7076
rescue StandardError => error
7177
Temporal.logger.error("Unable to complete Activity", metadata.to_h.merge(error: error.inspect))
@@ -78,8 +84,8 @@ def respond_failed(error)
7884
log_retry = proc do
7985
Temporal.logger.debug("Failed to report activity task failure, retrying", metadata.to_h)
8086
end
81-
Temporal::Client::Retryer.with_retries(on_retry: log_retry) do
82-
client.respond_activity_task_failed(task_token: task_token, exception: error)
87+
Temporal::Connection::Retryer.with_retries(on_retry: log_retry) do
88+
connection.respond_activity_task_failed(task_token: task_token, exception: error)
8389
end
8490
rescue StandardError => error
8591
Temporal.logger.error("Unable to fail Activity task", metadata.to_h.merge(error: error.inspect))

lib/temporal/client.rb

-21
This file was deleted.

0 commit comments

Comments
 (0)