|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative 'activities' |
| 4 | +require_relative 'greeting_workflow' |
| 5 | +require 'optparse' |
| 6 | +require 'temporalio/client' |
| 7 | +require 'temporalio/worker' |
| 8 | + |
| 9 | +# Define options parser to handle certificate paths and other parameters |
| 10 | +options = { |
| 11 | + target_host: 'localhost:7233', |
| 12 | + namespace: 'default', |
| 13 | + task_queue: 'mtls-task-queue', |
| 14 | + server_root_ca_cert: nil, |
| 15 | + client_cert: nil, |
| 16 | + client_key: nil |
| 17 | +} |
| 18 | + |
| 19 | +OptionParser.new do |opts| |
| 20 | + opts.banner = 'Usage: worker.rb [options]' |
| 21 | + |
| 22 | + opts.on('--target-host HOST', 'Host:port for the server (default: localhost:7233)') do |v| |
| 23 | + options[:target_host] = v |
| 24 | + end |
| 25 | + |
| 26 | + opts.on('--namespace NAMESPACE', 'Namespace to use (default: default)') do |v| |
| 27 | + options[:namespace] = v |
| 28 | + end |
| 29 | + |
| 30 | + opts.on('--task-queue QUEUE', 'Task queue to use (default: mtls-task-queue)') do |v| |
| 31 | + options[:task_queue] = v |
| 32 | + end |
| 33 | + |
| 34 | + opts.on('--server-root-ca-cert PATH', 'Path to the server root CA certificate') do |v| |
| 35 | + options[:server_root_ca_cert] = v |
| 36 | + end |
| 37 | + |
| 38 | + opts.on('--client-cert PATH', 'Path to the client certificate (required for mTLS)') do |v| |
| 39 | + options[:client_cert] = v |
| 40 | + end |
| 41 | + |
| 42 | + opts.on('--client-key PATH', 'Path to the client key (required for mTLS)') do |v| |
| 43 | + options[:client_key] = v |
| 44 | + end |
| 45 | +end.parse! |
| 46 | + |
| 47 | +# Check for required certificates for mTLS |
| 48 | +if options[:client_cert].nil? || options[:client_key].nil? |
| 49 | + puts 'Error: Client certificate and key are required for mTLS' |
| 50 | + puts 'Usage: ruby worker.rb --client-cert PATH --client-key PATH' |
| 51 | + exit 1 |
| 52 | +end |
| 53 | + |
| 54 | +puts "Connecting to Temporal Server at #{options[:target_host]} with mTLS..." |
| 55 | +puts "Using namespace: #{options[:namespace]}" |
| 56 | + |
| 57 | +# Create client with mTLS configuration |
| 58 | +tls_options = Temporalio::Client::Connection::TLSOptions.new( |
| 59 | + client_cert: File.read(options[:client_cert]), |
| 60 | + client_private_key: File.read(options[:client_key]) |
| 61 | +) |
| 62 | + |
| 63 | +# Add server root CA cert if provided |
| 64 | +if options[:server_root_ca_cert] |
| 65 | + tls_options = tls_options.with(server_root_ca_cert: File.read(options[:server_root_ca_cert])) |
| 66 | +end |
| 67 | + |
| 68 | +# Connect to Temporal server |
| 69 | +client = Temporalio::Client.connect( |
| 70 | + options[:target_host], |
| 71 | + options[:namespace], |
| 72 | + tls: tls_options |
| 73 | +) |
| 74 | + |
| 75 | +# Start worker with activities and workflows registered |
| 76 | +puts 'Starting worker connected with mTLS...' |
| 77 | +puts "Task queue: #{options[:task_queue]}" |
| 78 | +Temporalio::Worker.new( |
| 79 | + client:, |
| 80 | + task_queue: options[:task_queue], |
| 81 | + workflows: [ClientMtls::GreetingWorkflow], |
| 82 | + activities: [ClientMtls::Activities::ComposeGreeting] |
| 83 | +).run(shutdown_signals: ['SIGINT']) |
0 commit comments