Skip to content

Commit 0f8d82c

Browse files
Merge coinbase#73; update with client->connection refactor
2 parents 101ec13 + f984a3b commit 0f8d82c

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Temporal.configure do |config|
5959
config.port = 7233
6060
config.namespace = 'ruby-samples'
6161
config.task_queue = 'hello-world'
62+
config.credentials = :this_channel_is_insecure
6263
end
6364

6465
begin
@@ -114,6 +115,24 @@ curl -O https://raw.githubusercontent.com/temporalio/docker-compose/main/docker-
114115
docker-compose up
115116
```
116117

118+
### Connecting via SSL
119+
120+
In many production deployments you will end up connecting to your Temporal Services via SSL. In which
121+
case you must read the public cert of the CA that issued your Temporal server's SSL cert and create
122+
an instance of GRPC Channel Credentials.
123+
124+
Configure your Temporal connection:
125+
126+
```ruby
127+
Temporal.configure do |config|
128+
config.host = 'temporal-prod.mycompany.com'
129+
config.port = 7233
130+
config.namespace = 'ruby-samples'
131+
config.task_queue = 'hello-world'
132+
config.credentials = GRPC::Core::ChannelCredentials.new(CA_CERT)
133+
end
134+
```
135+
117136
## Workflows
118137

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

lib/temporal/client.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def await_workflow_result(workflow, workflow_id:, run_id: nil, timeout: nil, nam
195195
timeout: timeout || max_timeout,
196196
)
197197
rescue GRPC::DeadlineExceeded => e
198-
message = if timeout
198+
message = if timeout
199199
"Timed out after your specified limit of timeout: #{timeout} seconds"
200200
else
201201
"Timed out after #{max_timeout} seconds, which is the maximum supported amount."

lib/temporal/configuration.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
module Temporal
99
class Configuration
10-
Connection = Struct.new(:type, :host, :port, keyword_init: true)
10+
Connection = Struct.new(:type, :host, :port, :credentials, keyword_init: true)
1111
Execution = Struct.new(:namespace, :task_queue, :timeouts, :headers, keyword_init: true)
1212

1313
attr_reader :timeouts, :error_handlers
1414
attr_writer :converter
15-
attr_accessor :connection_type, :host, :port, :logger, :metrics_adapter, :namespace, :task_queue, :headers
15+
attr_accessor :connection_type, :host, :port, :credentials, :logger, :metrics_adapter, :namespace, :task_queue, :headers
1616

1717
# See https://docs.temporal.io/blog/activity-timeouts/ for general docs.
1818
# We want an infinite execution timeout for cron schedules and other perpetual workflows.
@@ -79,7 +79,8 @@ def for_connection
7979
Connection.new(
8080
type: connection_type,
8181
host: host,
82-
port: port
82+
port: port,
83+
credentials: credentials,
8384
).freeze
8485
end
8586

lib/temporal/connection.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ def self.generate(configuration)
1010
connection_class = CLIENT_TYPES_MAP[configuration.type]
1111
host = configuration.host
1212
port = configuration.port
13+
credentials = :this_channel_is_insecure
14+
15+
unless configuration.credentials.nil?
16+
credentials = configuration.credentials
17+
end
1318

1419
hostname = `hostname`
1520
thread_id = Thread.current.object_id
1621
identity = "#{thread_id}@#{hostname}"
1722

18-
connection_class.new(host, port, identity)
23+
if configuration.type == :grpc
24+
connection_class.new(host, port, identity, credentials)
25+
else
26+
connection_class.new(host, port, identity)
1927
end
2028
end
2129
end

lib/temporal/connection/grpc.rb

+5-4
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ class GRPC
2323
close: Temporal::Api::Enums::V1::HistoryEventFilterType::HISTORY_EVENT_FILTER_TYPE_CLOSE_EVENT,
2424
}.freeze
2525

26-
def initialize(host, port, identity)
26+
def initialize(host, port, identity, credentials)
2727
@url = "#{host}:#{port}"
2828
@identity = identity
29+
@credentials = credentials
2930
@poll = true
3031
@poll_mutex = Mutex.new
3132
@poll_request = nil
@@ -133,7 +134,7 @@ def get_workflow_execution_history(
133134
event_type: :all,
134135
timeout: nil
135136
)
136-
if wait_for_new_event
137+
if wait_for_new_event
137138
if timeout.nil?
138139
# This is an internal error. Wrappers should enforce this.
139140
raise "You must specify a timeout when wait_for_new_event = true."
@@ -464,12 +465,12 @@ def cancel_polling_request
464465

465466
private
466467

467-
attr_reader :url, :identity, :poll_mutex, :poll_request
468+
attr_reader :url, :credentials, :identity, :poll_mutex, :poll_request
468469

469470
def client
470471
@client ||= Temporal::Api::WorkflowService::V1::WorkflowService::Stub.new(
471472
url,
472-
:this_channel_is_insecure,
473+
credentials,
473474
timeout: 60
474475
)
475476
end

spec/unit/lib/temporal/grpc_client_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe Temporal::Connection::GRPC do
2-
subject { Temporal::Connection::GRPC.new(nil, nil, nil) }
2+
subject { Temporal::Connection::GRPC.new(nil, nil, nil, :this_channel_is_insecure) }
33
let(:grpc_stub) { double('grpc stub') }
44
let(:namespace) { 'test-namespace' }
55
let(:workflow_id) { SecureRandom.uuid }
@@ -8,7 +8,7 @@
88

99
before do
1010
allow(subject).to receive(:client).and_return(grpc_stub)
11-
11+
1212
allow(Time).to receive(:now).and_return(now)
1313
end
1414

@@ -35,7 +35,7 @@
3535
end
3636
end
3737
end
38-
38+
3939
describe '#signal_with_start_workflow' do
4040
let(:temporal_response) do
4141
Temporal::Api::WorkflowService::V1::SignalWithStartWorkflowExecutionResponse.new(run_id: 'xxx')
@@ -122,7 +122,7 @@
122122
end
123123
end
124124

125-
it 'demands a timeout to be specified' do
125+
it 'demands a timeout to be specified' do
126126
expect do
127127
subject.get_workflow_execution_history(
128128
namespace: namespace,
@@ -135,7 +135,7 @@
135135
end
136136
end
137137

138-
it 'disallows a timeout larger than the server timeout' do
138+
it 'disallows a timeout larger than the server timeout' do
139139
expect do
140140
subject.get_workflow_execution_history(
141141
namespace: namespace,

0 commit comments

Comments
 (0)