Skip to content

Commit b4b66ce

Browse files
authored
env config sample (#49)
* env config sample * upgrade temporalio dependency
1 parent 5c21ec6 commit b4b66ce

5 files changed

Lines changed: 164 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ Prerequisites:
2323
* [context_propagation](context_propagation) - Use interceptors to propagate thread/fiber local data from clients
2424
through workflows/activities.
2525
* [encryption](encryption) - Demonstrates how to make a codec for end-to-end encryption.
26+
* [env_config](env_config) - Load client configuration from TOML files with programmatic overrides.
2627
* [message_passing_simple](message_passing_simple) - Simple workflow that accepts signals, queries, and updates.
2728
* [polling/infrequent](polling/infrequent) - Implement an infrequent polling mechanism using Temporal's automatic Activity Retry feature.
2829
* [rails_app](rails_app) - Basic Rails API application using Temporal workflows and activities.
2930
* [saga](saga) - Using undo/compensation using a very simplistic Saga pattern.
3031
* [sorbet_generic](sorbet_generic) - Proof of concept of how to do _advanced_ Sorbet typing with the SDK.
3132
* [worker_specific_task_queues](worker_specific_task_queues) - Use a unique Task Queue for each Worker to run a sequence of Activities on the same Worker.
33+
* [worker_versioning](worker_versioning) - Use the Worker Versioning feature to more easily version your workflows & other code.
3234

3335
## Development
3436

env_config/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Temporal External Client Configuration Samples
2+
3+
This directory contains Ruby samples that demonstrate how to use the Temporal SDK's external client configuration feature. This feature allows you to configure a `Temporalio::Client` using a TOML file and/or programmatic overrides, decoupling connection settings from your application code.
4+
5+
## Prerequisites
6+
7+
To run, first see [README.md](../README.md) for prerequisites.
8+
9+
## Configuration File
10+
11+
The `config.toml` file defines three profiles for different environments:
12+
13+
- `[profile.default]`: A working configuration for local development.
14+
- `[profile.staging]`: A configuration with an intentionally **incorrect** address (`localhost:9999`) to demonstrate how it can be corrected by an override.
15+
- `[profile.prod]`: A non-runnable, illustrative-only configuration showing a realistic setup for Temporal Cloud with placeholder credentials. This profile is not used by the samples but serves as a reference.
16+
17+
## Samples
18+
19+
The following Ruby scripts demonstrate different ways to load and use these configuration profiles. Each runnable sample highlights a unique feature.
20+
21+
### `load_from_file.rb`
22+
23+
This sample shows the most common use case: loading the `default` profile from the `config.toml` file.
24+
25+
**To run this sample:**
26+
27+
28+
```bash
29+
bundle exec ruby load_from_file.rb
30+
```
31+
32+
### `load_profile.rb`
33+
34+
This sample demonstrates loading the `staging` profile by name (which has an incorrect address) and then correcting the address programmatically. This highlights the recommended approach for overriding configuration values at runtime.
35+
36+
**To run this sample:**
37+
38+
```bash
39+
bundle exec ruby load_profile.rb
40+
```
41+
42+
## Running the Samples
43+
44+
You can run each sample script directly from the root of the `samples-ruby` repository. Ensure you have the necessary dependencies installed by running `bundle install` from the repository root.

env_config/config.toml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This is a sample configuration file for demonstrating Temporal's environment
2+
# configuration feature. It defines multiple profiles for different environments,
3+
# such as local development, production, and staging.
4+
5+
# Default profile for local development
6+
[profile.default]
7+
address = "localhost:7233"
8+
namespace = "default"
9+
10+
# Optional: Add custom gRPC headers
11+
[profile.default.grpc_meta]
12+
my-custom-header = "development-value"
13+
trace-id = "dev-trace-123"
14+
15+
# Staging profile with inline certificate data
16+
[profile.staging]
17+
address = "localhost:9999"
18+
namespace = "staging"
19+
20+
# An example production profile for Temporal Cloud
21+
[profile.prod]
22+
address = "your-namespace.a1b2c.tmprl.cloud:7233"
23+
namespace = "your-namespace"
24+
# Replace with your actual Temporal Cloud API key
25+
api_key = "your-api-key-here"
26+
27+
# TLS configuration for production
28+
[profile.prod.tls]
29+
# TLS is auto-enabled when an API key is present, but you can configure it
30+
# explicitly.
31+
# disabled = false
32+
33+
# Use certificate files for mTLS. Replace with actual paths.
34+
client_cert_path = "/etc/temporal/certs/client.pem"
35+
client_key_path = "/etc/temporal/certs/client.key"
36+
37+
# Custom headers for production
38+
[profile.prod.grpc_meta]
39+
environment = "production"
40+
service-version = "v1.2.3"

env_config/load_from_file.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/client'
4+
require 'temporalio/env_config'
5+
6+
def main
7+
puts '--- Loading default profile from config.toml ---'
8+
9+
# For this sample to be self-contained, we explicitly provide the path to
10+
# the config.toml file included in this directory.
11+
# By default though, the config.toml file will be loaded from
12+
# ~/.config/temporalio/temporal.toml (or the equivalent standard config directory on your OS).
13+
config_file = File.join(__dir__, 'config.toml')
14+
15+
# load_client_connect_options is a helper that loads a profile and prepares
16+
# the configuration for Client.connect. By default, it loads the
17+
# "default" profile.
18+
args, kwargs = Temporalio::EnvConfig::ClientConfig.load_client_connect_options(
19+
config_source: Pathname.new(config_file)
20+
)
21+
22+
puts "Loaded 'default' profile from #{config_file}."
23+
puts " Address: #{args[0]}"
24+
puts " Namespace: #{args[1]}"
25+
puts " gRPC Metadata: #{kwargs[:rpc_metadata]}"
26+
27+
puts "\nAttempting to connect to client..."
28+
begin
29+
client = Temporalio::Client.connect(*args, **kwargs)
30+
puts '✅ Client connected successfully!'
31+
sys_info = client.workflow_service.get_system_info(Temporalio::Api::WorkflowService::V1::GetSystemInfoRequest.new)
32+
puts "✅ Successfully verified connection to Temporal server!\n#{sys_info}"
33+
rescue StandardError => e
34+
puts "❌ Failed to connect: #{e}"
35+
end
36+
end
37+
38+
main if $PROGRAM_NAME == __FILE__

env_config/load_profile.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require 'temporalio/client'
4+
require 'temporalio/env_config'
5+
6+
def main
7+
puts "--- Loading 'staging' profile with programmatic overrides ---"
8+
9+
config_file = File.join(__dir__, 'config.toml')
10+
profile_name = 'staging'
11+
12+
puts "The 'staging' profile in config.toml has an incorrect address (localhost:9999)."
13+
puts "We'll programmatically override it to the correct address."
14+
15+
# Load the 'staging' profile.
16+
args, kwargs = Temporalio::EnvConfig::ClientConfig.load_client_connect_options(
17+
profile: profile_name,
18+
config_source: Pathname.new(config_file)
19+
)
20+
21+
# Override the target host to the correct address.
22+
# This is the recommended way to override configuration values.
23+
args[0] = 'localhost:7233'
24+
25+
puts "\nLoaded '#{profile_name}' profile from #{config_file} with overrides."
26+
puts " Address: #{args[0]} (overridden from localhost:9999)"
27+
puts " Namespace: #{args[1]}"
28+
29+
puts "\nAttempting to connect to client..."
30+
begin
31+
client = Temporalio::Client.connect(*args, **kwargs)
32+
puts '✅ Client connected successfully!'
33+
sys_info = client.workflow_service.get_system_info(Temporalio::Api::WorkflowService::V1::GetSystemInfoRequest.new)
34+
puts "✅ Successfully verified connection to Temporal server!\n#{sys_info}"
35+
rescue StandardError => e
36+
puts "❌ Failed to connect: #{e}"
37+
end
38+
end
39+
40+
main if $PROGRAM_NAME == __FILE__

0 commit comments

Comments
 (0)