Skip to content

Commit f41efb7

Browse files
authored
[Refactor] Remove global config references (coinbase#317)
* Remove global config from client specs * Remove global configuration from client and connection * Remove global configuration from integration specs * Remove global config from error messages and comments * Remove global config from specs * Remove global config from worker spec * Add README section on global vs local configuration
1 parent c73a07e commit f41efb7

26 files changed

+129
-83
lines changed

README.md

+41
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,47 @@ Temporal.configure do |config|
179179
end
180180
```
181181

182+
## Configuration
183+
184+
This gem is optimised for the smoothest out-of-the-box experience, which is achieved using a global
185+
configuration:
186+
187+
```ruby
188+
Temporal.configure do |config|
189+
config.host = '127.0.0.1' # sets global host
190+
...
191+
end
192+
193+
Temporal::Worker.new # uses global host
194+
Temporal.start_workflow(...) # uses global host
195+
```
196+
197+
This will work just fine for simpler use-cases, however at some point you might need to setup
198+
multiple clients and workers within the same instance of your app (e.g. you have different Temporal
199+
hosts, need to use different codecs/converters for different parts of your app, etc). Should this be
200+
the case we recommend using explicit local configurations for each client/worker:
201+
202+
```ruby
203+
config_1 = Temporal::Configuration.new
204+
config_1.host = 'temporal-01'
205+
206+
config_2 = Temporal::Configuration.new
207+
config_2.host = 'temporal-01'
208+
209+
worker_1 = Temporal::Worker.new(config_1)
210+
worker_2 = Temporal::Worker.new(config_2)
211+
212+
client_1 = Temporal::Client.new(config_1)
213+
client_1.start_workflow(...)
214+
215+
client_2 = Temporal::Client.new(config_2)
216+
client_2.start_workflow(...)
217+
```
218+
219+
*NOTE: Almost all the methods on the `Temporal` module are delegated to the default client that's
220+
initialized using global configuration. The same methods can be used directly on your own client
221+
instances.*
222+
182223
## Workflows
183224

184225
A workflow is defined using pure Ruby code, however it should contain only a high-level

examples/init.rb

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88

99
metrics_logger = Logger.new(STDOUT, progname: 'metrics')
1010

11+
DEFAULT_NAMESPACE = 'ruby-samples'.freeze
12+
DEFAULT_TASK_QUEUE = 'general'.freeze
13+
1114
Temporal.configure do |config|
1215
config.host = ENV.fetch('TEMPORAL_HOST', 'localhost')
1316
config.port = ENV.fetch('TEMPORAL_PORT', 7233).to_i
14-
config.namespace = ENV.fetch('TEMPORAL_NAMESPACE', 'ruby-samples')
15-
config.task_queue = ENV.fetch('TEMPORAL_TASK_QUEUE', 'general')
17+
config.namespace = ENV.fetch('TEMPORAL_NAMESPACE', DEFAULT_NAMESPACE)
18+
config.task_queue = ENV.fetch('TEMPORAL_TASK_QUEUE', DEFAULT_TASK_QUEUE)
1619
config.metrics_adapter = Temporal::MetricsAdapters::Log.new(metrics_logger)
1720
end

examples/spec/helpers.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def wait_for_workflow_completion(workflow_id, run_id)
2121
def fetch_history(workflow_id, run_id, options = {})
2222
connection = Temporal.send(:default_client).send(:connection)
2323
options = {
24-
namespace: Temporal.configuration.namespace,
24+
namespace: integration_spec_namespace,
2525
workflow_id: workflow_id,
2626
run_id: run_id,
2727
}.merge(options)
@@ -30,6 +30,10 @@ def fetch_history(workflow_id, run_id, options = {})
3030
end
3131

3232
def integration_spec_namespace
33-
ENV.fetch('TEMPORAL_NAMESPACE', 'ruby-samples')
33+
ENV.fetch('TEMPORAL_NAMESPACE', DEFAULT_NAMESPACE)
34+
end
35+
36+
def integration_spec_task_queue
37+
ENV.fetch('TEMPORAL_TASK_QUEUE', DEFAULT_TASK_QUEUE)
3438
end
3539
end

examples/spec/integration/converter_spec.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
end
1313

1414
around(:each) do |example|
15-
task_queue = Temporal.configuration.task_queue
16-
1715
Temporal.configure do |config|
1816
config.task_queue = 'crypt'
1917
config.payload_codec = codec
@@ -22,7 +20,7 @@
2220
example.run
2321
ensure
2422
Temporal.configure do |config|
25-
config.task_queue = task_queue
23+
config.task_queue = integration_spec_task_queue
2624
config.payload_codec = Temporal::Configuration::DEFAULT_PAYLOAD_CODEC
2725
end
2826
end

examples/spec/integration/create_schedule_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"Test",
2626
options: {
2727
workflow_id: workflow_id,
28-
task_queue: Temporal.configuration.task_queue
28+
task_queue: integration_spec_task_queue
2929
}
3030
),
3131
policies: Temporal::Schedule::SchedulePolicies.new(
@@ -74,7 +74,7 @@
7474
action: Temporal::Schedule::StartWorkflowAction.new(
7575
"HelloWorldWorkflow",
7676
"Test",
77-
options: {task_queue: Temporal.configuration.task_queue}
77+
options: {task_queue: integration_spec_task_queue}
7878
)
7979
)
8080

examples/spec/integration/delete_schedule_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"HelloWorldWorkflow",
1616
"Test",
1717
options: {
18-
task_queue: Temporal.configuration.task_queue
18+
task_queue: integration_spec_task_queue
1919
}
2020
)
2121
)

examples/spec/integration/handling_structured_error_workflow_spec.rb

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
# That worker runs a task queue, error_serialization_v2. This setup code will
66
# route workflow requests to that task queue.
77
around(:each) do |example|
8-
task_queue = Temporal.configuration.task_queue
9-
108
Temporal.configure do |config|
119
config.task_queue = 'error_serialization_v2'
1210
end
1311

1412
example.run
1513
ensure
1614
Temporal.configure do |config|
17-
config.task_queue = task_queue
15+
config.task_queue = integration_spec_task_queue
1816
end
1917
end
2018

examples/spec/integration/list_schedules_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"HelloWorldWorkflow",
2323
"Test",
2424
options: {
25-
task_queue: Temporal.configuration.task_queue
25+
task_queue: integration_spec_task_queue
2626
}
2727
)
2828
)

examples/spec/integration/metadata_workflow_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
run_id: run_id,
1717
)
1818

19-
expect(actual_result.task_queue).to eq(Temporal.configuration.task_queue)
19+
expect(actual_result.task_queue).to eq(integration_spec_task_queue)
2020
end
2121

2222
it 'workflow can retrieve its headers' do

examples/spec/integration/pause_schedule_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"HelloWorldWorkflow",
1818
"Test",
1919
options: {
20-
task_queue: Temporal.configuration.task_queue
20+
task_queue: integration_spec_task_queue
2121
}
2222
)
2323
)

examples/spec/integration/reset_workflow_spec.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require 'workflows/query_workflow'
33
require 'temporal/reset_reapply_type'
44

5-
describe 'Temporal.reset_workflow' do
5+
describe 'Temporal.reset_workflow', :integration do
66
it 'can reset a closed workflow to the beginning' do
77
workflow_id = SecureRandom.uuid
88
original_run_id = Temporal.start_workflow(
@@ -19,7 +19,7 @@
1919
expect(original_result).to eq('Hello World, Test')
2020

2121
new_run_id = Temporal.reset_workflow(
22-
Temporal.configuration.namespace,
22+
integration_spec_namespace,
2323
workflow_id,
2424
original_run_id,
2525
strategy: Temporal::ResetStrategy::FIRST_WORKFLOW_TASK
@@ -36,7 +36,7 @@
3636
def reset_hello_world_workflow_twice(workflow_id, original_run_id, request_id:)
3737
2.times.map do
3838
new_run_id = Temporal.reset_workflow(
39-
Temporal.configuration.namespace,
39+
integration_spec_namespace,
4040
workflow_id,
4141
original_run_id,
4242
strategy: Temporal::ResetStrategy::FIRST_WORKFLOW_TASK,
@@ -130,7 +130,7 @@ def start_query_workflow_and_signal_three_times
130130
workflow_id, original_run_id = start_query_workflow_and_signal_three_times.values_at(:workflow_id, :run_id)
131131

132132
new_run_id = Temporal.reset_workflow(
133-
Temporal.configuration.namespace,
133+
integration_spec_namespace,
134134
workflow_id,
135135
original_run_id,
136136
strategy: Temporal::ResetStrategy::FIRST_WORKFLOW_TASK,
@@ -147,7 +147,7 @@ def start_query_workflow_and_signal_three_times
147147
workflow_id, original_run_id = start_query_workflow_and_signal_three_times.values_at(:workflow_id, :run_id)
148148

149149
new_run_id = Temporal.reset_workflow(
150-
Temporal.configuration.namespace,
150+
integration_spec_namespace,
151151
workflow_id,
152152
original_run_id,
153153
strategy: Temporal::ResetStrategy::FIRST_WORKFLOW_TASK,
@@ -160,4 +160,4 @@ def start_query_workflow_and_signal_three_times
160160
Temporal.terminate_workflow(workflow_id, run_id: new_run_id)
161161
end
162162
end
163-
163+

examples/spec/integration/start_workflow_spec.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'workflows/hello_world_workflow'
22
require 'workflows/long_workflow'
33

4-
describe 'Temporal.start_workflow' do
4+
describe 'Temporal.start_workflow', :integration do
55
let(:workflow_id) { SecureRandom.uuid }
66

77
it 'starts a workflow using a class reference' do
@@ -21,15 +21,15 @@
2121
it 'starts a workflow using a string reference' do
2222
run_id = Temporal.start_workflow('HelloWorldWorkflow', 'Test', options: {
2323
workflow_id: workflow_id,
24-
namespace: Temporal.configuration.namespace,
25-
task_queue: Temporal.configuration.task_queue
24+
namespace: integration_spec_namespace,
25+
task_queue: integration_spec_task_queue
2626
})
2727

2828
result = Temporal.await_workflow_result(
2929
'HelloWorldWorkflow',
3030
workflow_id: workflow_id,
3131
run_id: run_id,
32-
namespace: Temporal.configuration.namespace
32+
namespace: integration_spec_namespace
3333
)
3434

3535
expect(result).to eq('Hello World, Test')
@@ -82,11 +82,11 @@
8282
})
8383

8484
execution_1 = Temporal.fetch_workflow_execution_info(
85-
Temporal.configuration.namespace,
85+
integration_spec_namespace,
8686
workflow_id,
8787
run_id_1)
8888
execution_2 = Temporal.fetch_workflow_execution_info(
89-
Temporal.configuration.namespace,
89+
integration_spec_namespace,
9090
workflow_id,
9191
run_id_2)
9292

examples/spec/integration/trigger_schedule_spec.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"HelloWorldWorkflow",
1818
"Test",
1919
options: {
20-
task_queue: Temporal.configuration.task_queue
20+
task_queue: integration_spec_task_queue
2121
}
2222
)
2323
)

examples/spec/integration/update_schedule_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"HelloWorldWorkflow",
1919
"Test",
2020
options: {
21-
task_queue: Temporal.configuration.task_queue
21+
task_queue: integration_spec_task_queue
2222
}
2323
),
2424
policies: Temporal::Schedule::SchedulePolicies.new(
@@ -42,7 +42,7 @@
4242
"HelloWorldWorkflow",
4343
"UpdatedInput",
4444
options: {
45-
task_queue: Temporal.configuration.task_queue
45+
task_queue: integration_spec_task_queue
4646
}
4747
),
4848
policies: Temporal::Schedule::SchedulePolicies.new(

lib/temporal/client.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ def reset_workflow(namespace, workflow_id, run_id, strategy: nil, workflow_task_
331331
# for reference
332332
# @param details [String, Array, nil] optional details to be stored in history
333333
def terminate_workflow(workflow_id, namespace: nil, run_id: nil, reason: nil, details: nil)
334-
namespace ||= Temporal.configuration.namespace
334+
namespace ||= config.namespace
335335

336336
connection.terminate_workflow_execution(
337337
namespace: namespace,

lib/temporal/configuration.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def for_connection
126126
credentials: credentials,
127127
identity: identity || default_identity,
128128
converter: converter,
129-
connection_options: connection_options.merge(use_error_serialization_v2: @use_error_serialization_v2)
129+
connection_options: connection_options.merge(use_error_serialization_v2: use_error_serialization_v2)
130130
).freeze
131131
end
132132

lib/temporal/connection/grpc.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def respond_activity_task_completed_by_id(namespace:, activity_id:, workflow_id:
315315
end
316316

317317
def respond_activity_task_failed(namespace:, task_token:, exception:)
318-
serialize_whole_error = options.fetch(:use_error_serialization_v2, Temporal.configuration.use_error_serialization_v2)
318+
serialize_whole_error = options.fetch(:use_error_serialization_v2)
319319
request = Temporalio::Api::WorkflowService::V1::RespondActivityTaskFailedRequest.new(
320320
namespace: namespace,
321321
identity: identity,

lib/temporal/errors.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ChildWorkflowTerminatedError < Error; end
2626

2727
# A superclass for activity exceptions raised explicitly
2828
# with the intent to propagate to a workflow
29-
# With v2 serialization (set with Temporal.configuration set with use_error_serialization_v2=true) you can
29+
# With v2 serialization (set with Temporal::Configuration#use_error_serialization_v2=true) you can
3030
# throw any exception from an activity and expect that it can be handled by the workflow.
3131
class ActivityException < ClientError; end
3232

lib/temporal/workflow/errors.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def self.generate_error(failure, converter, default_exception_class = StandardEr
2626
exception_or_message = converter.from_details_payloads(details)
2727
# v1 serialization only supports StandardErrors with a single "message" argument.
2828
# v2 serialization supports complex errors using our converters to serialize them.
29-
# enable v2 serialization in activities with Temporal.configuration.use_error_serialization_v2
29+
# enable v2 serialization in activities with Temporal::Configuration#use_error_serialization_v2
3030
if exception_or_message.is_a?(Exception)
3131
exception = exception_or_message
3232
else
@@ -37,7 +37,7 @@ def self.generate_error(failure, converter, default_exception_class = StandardEr
3737
exception = default_exception_class.new(message)
3838
Temporal.logger.error(
3939
"Could not instantiate original error. Defaulting to StandardError. Make sure the worker running " \
40-
"your activities is setting Temporal.configuration.use_error_serialization_v2. If so, make sure the " \
40+
"your activities is configured with use_error_serialization_v2. If so, make sure the " \
4141
"original error serialized by searching your logs for 'unserializable_error'. If not, you're using "\
4242
"legacy serialization, and it's likely that "\
4343
"your error's initializer takes something other than exactly one positional argument.",

spec/config/temporal.rb

-5
This file was deleted.

0 commit comments

Comments
 (0)