### Example: Start, Query, Signal, and Get Result Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md Demonstrates how to start a workflow, interact with its handle by querying and signaling, and finally retrieve the workflow's result. ```ruby # Start workflow without waiting handle = client.start_workflow( MyWorkflow, 'arg1', 'arg2', id: 'workflow-123', task_queue: 'my-task-queue', execution_timeout: 3600 ) # Query the workflow result = handle.query(MyWorkflow.my_query_method) # Send a signal handle.signal(MyWorkflow.my_signal_method, 'signal-arg') # Get result result = handle.result ``` -------------------------------- ### Starting a Standalone Activity Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md This example demonstrates how to start a standalone activity using the Client#start_activity method. It includes setting the activity class, arguments, ID, task queue, and a start-to-close timeout. The returned handle can be used to fetch the activity's result. ```ruby # Start a standalone activity handle = client.start_activity( MyActivity, 'arg1', id: 'activity-1', task_queue: 'default', start_to_close_timeout: 60 ) result = handle.result ``` -------------------------------- ### Log Activity Start Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/activity.md Example of using the contextual logger to log a message indicating the start of an activity, with the message automatically tagged with activity details. ```ruby Temporalio::Activity::Context.current.logger.info('Activity started') ``` -------------------------------- ### Execute a Temporal Workflow and Get Result Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Initiate a workflow execution using the Temporalio::Client and wait for its result. This example demonstrates how to start the SayHelloWorkflow and print its output. ```ruby require 'temporalio/client' require_relative 'say_hello_workflow' # Create a client client = Temporalio::Client.connect('localhost:7233', 'my-namespace') # Run workflow result = client.execute_workflow( SayHelloWorkflow, 'Temporal', # This is the input to the workflow id: 'my-workflow-id', task_queue: 'my-task-queue' ) puts "Result: #{result}" ``` -------------------------------- ### Minimal Example Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/quick-reference.md A basic example demonstrating how to define and run a simple workflow and activity using the Temporal Ruby SDK. ```APIDOC ## Minimal Example This example demonstrates defining a workflow and activity, connecting a client, and running a worker and workflow. ```ruby require 'temporalio/client' require 'temporalio/worker' require 'temporalio/workflow' require 'temporalio/activity' # Define activity class GreetActivity < Temporalio::Activity::Definition def execute(name) "Hello, #{name}!" end end # Define workflow class GreetWorkflow < Temporalio::Workflow::Definition def execute(name) Temporalio::Workflow.execute_activity( GreetActivity, name, schedule_to_close_timeout: 300 ) end end # Connect client client = Temporalio::Client.connect('localhost:7233', 'default') # Create and run worker (in separate process/thread) worker = Temporalio::Worker.new( client: client, task_queue: 'default', workflows: [GreetWorkflow], activities: [GreetActivity] ) worker.run { sleep } # Execute workflow (from different process) result = client.execute_workflow( GreetWorkflow, 'Temporal', id: 'greet-1', task_queue: 'default' ) puts result # "Hello, Temporal!" ``` ``` -------------------------------- ### Start Local Workflow Environment Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/testing.md Starts a local Temporal server subprocess for testing. Provides a client connected to the server. Cleanup is automatic upon block completion. ```ruby Temporalio::Testing::WorkflowEnvironment.start_local do |env| # Create worker worker = Temporalio::Worker.new( client: env.client, task_queue: 'test-queue', workflows: [MyWorkflow] ) # Run worker and test worker.run do result = env.client.execute_workflow( MyWorkflow, id: 'wf-1', task_queue: 'test-queue' ) puts "Result: #{result}" end end ``` -------------------------------- ### Install Temporalio Gem via gem install Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Install the Temporalio gem directly using the gem install command. ```bash gem install temporalio ``` -------------------------------- ### Start Time-Skipping Workflow Environment Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/testing.md Starts a time-skipping test environment for fast execution without real-time waits. Not thread-safe and not supported on ARM processors. ```ruby Temporalio::Testing::WorkflowEnvironment.start_time_skipping do |env| worker = Temporalio::Worker.new( client: env.client, task_queue: 'test-queue', workflows: [WaitWorkflow], workflow_executor: Temporalio::Worker::WorkflowExecutor::ThreadPool.default ) worker.run do # This completes instantly even though workflow sleeps for 1 hour result = env.client.execute_workflow( WaitWorkflow, id: 'wf-1', task_queue: 'test-queue' ) puts result # "done" (after automatic time skip) end end ``` -------------------------------- ### Activity Definition Example Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/activity.md A basic example of defining an activity by extending `Temporalio::Activity::Definition` and implementing the `execute` method. ```APIDOC ## Activity Definition Example ### Description This example demonstrates the fundamental structure of an activity definition using the `Temporalio::Activity::Definition` base class. ### Code ```ruby class MyActivity < Temporalio::Activity::Definition def execute(arg1, arg2) # Activity work "result" end end ``` ``` -------------------------------- ### Install Dependencies Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Installs project dependencies using Bundler. Optionally configures the local bundle path. ```ruby # Optional: Change bundler install path to be local bundle config --local path $(pwd)/.bundle bundle install ``` -------------------------------- ### Future Example Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Demonstrates how to run multiple activities concurrently using Futures and wait for their completion. ```APIDOC ### Example ```ruby # Run 3 activities concurrently f1 = Temporalio::Workflow::Future.new do Temporalio::Workflow.execute_activity(Activity1, schedule_to_close_timeout: 300) end f2 = Temporalio::Workflow::Future.new do Temporalio::Workflow.execute_activity(Activity2, schedule_to_close_timeout: 300) end f3 = Temporalio::Workflow::Future.new do Temporalio::Workflow.execute_activity(Activity3, schedule_to_close_timeout: 300) end Temporalio::Workflow::Future.all_of(f1, f2, f3).wait puts "Results: #{f1.result}, #{f2.result}, #{f3.result}" ``` ``` -------------------------------- ### Start a Workflow Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/00-START-HERE.md Connect to a Temporal server and execute a workflow. Ensure the Temporal server is running and accessible. ```ruby client = Temporalio::Client.connect('localhost:7233', 'default') result = client.execute_workflow(MyWorkflow, arg, id: 'wf-1', task_queue: 'default') ``` -------------------------------- ### workflow_init Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Marks the `initialize` method to be used for receiving workflow start arguments. This allows for dependency injection or initial setup using arguments passed when the workflow starts. ```APIDOC ## workflow_init ### Description Mark the `initialize` method to receive workflow start arguments. ### Usage ```ruby class MyWorkflow < Temporalio::Workflow::Definition workflow_init def initialize(config) @config = config end def execute # Use @config end end ``` ``` -------------------------------- ### Start and Execute Workflow Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/quick-reference.md Initiates a workflow execution. `start_workflow` returns a handle immediately, while `execute_workflow` waits for the result. ```ruby # Without waiting handle = client.start_workflow( MyWorkflow, 'arg1', 'arg2', id: 'wf-id', task_queue: 'queue' ) result = handle.result ``` ```ruby # With waiting (shortcut) result = client.execute_workflow( MyWorkflow, 'arg1', 'arg2', id: 'wf-id', task_queue: 'queue' ) ``` -------------------------------- ### Reusable Activity with Dependency Injection Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/advanced-patterns.md Demonstrates how to create reusable activities by injecting dependencies, such as a database connection, during worker setup. ```ruby class DatabaseActivity < Temporalio::Activity def initialize(db_connection) @db = db_connection end def execute(query) @db.query(query) end end # Usage class MyWorkflow < Temporalio::Workflow::Definition def execute(sql) Temporalio::Workflow.execute_activity( DatabaseActivity, sql, schedule_to_close_timeout: 300 ) end end # Create worker with activity instance db = OpenDatabase() worker = Temporalio::Worker.new( client:, task_queue: 'default', workflows: [MyWorkflow], activities: [DatabaseActivity.new(db)] ) ``` -------------------------------- ### Connect and Start Workflow with Ruby Client Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Create a client to interact with Temporal, start a workflow, and retrieve its result. Clients are thread-safe and fiber-compatible. TLS can be enabled via the `tls` option. ```ruby require 'temporalio/client' # Create a client client = Temporalio::Client.connect('localhost:7233', 'my-namespace') # Start a workflow handle = client.start_workflow( MyWorkflow, 'arg1', 'arg2', id: 'my-workflow-id', task_queue: 'my-task-queue' ) # Wait for result result = handle.result puts "Result: #{result}" ``` -------------------------------- ### Start Workflow Environment and Run Worker Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/quick-reference.md Use Temporalio::Testing::WorkflowEnvironment to start a test environment. This allows you to run a worker and execute workflows within a controlled test context, simulating time progression. ```ruby Temporalio::Testing::WorkflowEnvironment.start_time_skipping do |env| worker = Temporalio::Worker.new( client: env.client, task_queue: 'test', workflows: [MyWorkflow], workflow_executor: Temporalio::Worker::WorkflowExecutor::ThreadPool.default ) worker.run do result = env.client.execute_workflow( MyWorkflow, id: 'wf-1', task_queue: 'test' ) assert_equal 'expected', result end end ``` -------------------------------- ### Start Workflow Signature Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md This snippet shows the signature for the Client#start_workflow method, detailing all available parameters for starting a workflow. ```ruby start_workflow( workflow, *args, id: nil, task_queue:, workflow_name: nil, arg_hints: [], result_hint: nil, memo: {}, search_attributes: {}, start_signal: nil, start_signal_args: [], execution_timeout: nil, run_timeout: nil, task_timeout: nil, retry_policy: nil, cron_schedule: nil, parent_close_policy: nil, versioning_override: nil, with_start_workflow_operation: nil, rpc_options: nil ) ``` -------------------------------- ### Start Standalone Activity with Timeouts Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/activity.md Use this to start a standalone activity with specified timeouts. This includes schedule-to-close, start-to-close, and heartbeat timeouts. ```ruby client.start_activity( MyActivity, 'arg', id: 'activity-123', task_queue: 'default', schedule_to_close_timeout: 3600, start_to_close_timeout: 1800, heartbeat_timeout: 60 ) ``` -------------------------------- ### Client#start_activity Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md Starts a standalone activity. This method is experimental and allows for the execution of activities outside of a workflow context. ```APIDOC ## Client#start_activity ### Description Starts a standalone activity (experimental). ### Method `start_activity` ### Parameters #### Path Parameters - **activity** (Class, String, Symbol) - Required - Activity class or name to execute - **args** (Array) - Optional - Positional arguments for the activity - **id** (String) - Required - Activity execution ID - **task_queue** (String) - Required - Task queue for the activity - **activity_name** (String, nil) - Optional - Override the activity name - **arg_hints** (Array) - Optional - Hints for converter - **result_hint** (Object, nil) - Optional - Hint for result conversion - **schedule_to_close_timeout** (Integer, nil) - Optional - Timeout from scheduling to close in seconds - **start_to_close_timeout** (Integer, nil) - Optional - Timeout from start to close in seconds - **heartbeat_timeout** (Integer, nil) - Optional - Timeout between heartbeats in seconds - **retry_policy** (RetryPolicy, nil) - Optional - Retry policy for the activity - **versioning_override** (VersioningOverride, nil) - Optional - Versioning information - **rpc_options** (RPCOptions, nil) - Optional - Advanced RPC options ### Returns #### Success Response - **Type:** `Client::AsyncActivityHandle` - **Description:** Handle for the started activity ### Example ```ruby # Start a standalone activity handle = client.start_activity( MyActivity, 'arg1', id: 'activity-1', task_queue: 'default', start_to_close_timeout: 60 ) result = handle.result ``` ``` -------------------------------- ### Execute Workflow and Wait for Result Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md Starts a workflow and waits for its completion. This is a shortcut for starting a workflow and then retrieving its result. ```ruby execute_workflow( workflow, *args, id: nil, task_queue:, workflow_name: nil, arg_hints: [], result_hint: nil, memo: {}, search_attributes: {}, start_signal: nil, start_signal_args: [], execution_timeout: nil, run_timeout: nil, task_timeout: nil, retry_policy: nil, cron_schedule: nil, parent_close_policy: nil, versioning_override: nil, with_start_workflow_operation: nil, rpc_options: nil ) ``` ```ruby # Execute workflow and wait for result result = client.execute_workflow( MyWorkflow, 'Temporal', id: 'wf-1', task_queue: 'default' ) puts "Workflow result: #{result}" ``` -------------------------------- ### Start Workflow Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/quick-reference.md Methods for starting a workflow execution, either asynchronously or by waiting for the result. ```APIDOC ## Start Workflow Initiate a workflow execution. You can either get a handle to start asynchronously or execute and wait for the result. ### Start workflow without waiting for result ```ruby handle = client.start_workflow( MyWorkflow, 'arg1', 'arg2', id: 'wf-id', task_queue: 'queue' ) result = handle.result ``` ### Execute workflow and wait for result ```ruby result = client.execute_workflow( MyWorkflow, 'arg1', 'arg2', id: 'wf-id', task_queue: 'queue' ) ``` ``` -------------------------------- ### Initialize Workflow with Arguments Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Use the `workflow_init` decorator to designate the `initialize` method for receiving workflow start arguments. ```ruby class MyWorkflow < Temporalio::Workflow::Definition workflow_init def initialize(config) @config = config end def execute # Use @config end end ``` -------------------------------- ### Client#execute_workflow Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md Starts a workflow and waits for its completion. This is a shortcut for starting a workflow and then retrieving its result. ```APIDOC ## Client#execute_workflow ### Description Start a workflow and wait for its completion. Shortcut for `start_workflow` + `handle.result`. ### Method Not specified (SDK method) ### Parameters Same as `start_workflow`. See `start_workflow` parameters table above. ### Returns - **Type:** Object - **Description:** The result of the workflow execution (type depends on workflow) ### Raises - `Temporalio::Error::WorkflowFailedError` — If the workflow fails - `Temporalio::Error::WorkflowContinuedAsNewError` — If the workflow continues as new - `Temporalio::Error::RPCError` — If RPC call fails ### Example ```ruby # Execute workflow and wait for result result = client.execute_workflow( MyWorkflow, 'Temporal', id: 'wf-1', task_queue: 'default' ) puts "Workflow result: #{result}" ``` ``` -------------------------------- ### Install RBS Collection Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Installs the necessary RBS collection for type checking with Steep. ```ruby bundle exec rake rbs:install_collection ``` -------------------------------- ### Example: Replay Workflow from JSON History Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/worker.md Load a workflow history from a JSON file and replay it using `WorkflowReplayer` to check for non-determinism. Ensure `temporalio/workflow_history` is required. ```ruby # Load history from JSON require 'temporalio/workflow_history' history_json = File.read('workflow_history.json') history = Temporalio::WorkflowHistory.from_history_json(history_json) # Replay to check for non-determinism replayer = Temporalio::Worker::WorkflowReplayer.new(workflows: [MyWorkflow]) replayer.replay_workflow(history) puts "Replay successful" ``` -------------------------------- ### Set Up a Temporal Worker Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/README.md Connect to a Temporal server and start a worker that polls for tasks and executes workflows and activities. This is typically run as a long-lived process. ```ruby client = Temporalio::Client.connect('localhost:7233', 'default') worker = Temporalio::Worker.new( client:, task_queue: 'default', workflows: [MyWorkflow], activities: [MyActivity] ) worker.run(shutdown_signals: ['SIGINT']) ``` -------------------------------- ### Start and Wait for Multiple Activities Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Initiates three activities in the background using Futures and then waits for all of them to complete. The results are logged upon completion. ```ruby fut1 = Temporalio::Workflow::Future.new do Temporalio::Workflow.execute_activity(MyActivity1, schedule_to_close_timeout: 300) end fut2 = Temporalio::Workflow::Future.new do Temporalio::Workflow.execute_activity(MyActivity2, schedule_to_close_timeout: 300) end fut3 = Temporalio::Workflow::Future.new do Temporalio::Workflow.execute_activity(MyActivity3, schedule_to_close_timeout: 300) end Temporalio::Workflow::Future.all_of(fut1, fut2, fut3).wait Temporalio::Workflow.logger.debug("Got: #{fut1.result}, #{fut2.result}, #{fut3.result}") ``` -------------------------------- ### Manage Child Workflows Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/quick-reference.md Start, wait for results, or signal child workflows. Use `execute_child_workflow` to start and wait for completion in one step. ```ruby # Start handle = Temporalio::Workflow.start_child_workflow( ChildWorkflow, 'arg', id: 'child-1' ) # Wait result = handle.result # Signal child handle.signal(ChildWorkflow.signal_method) # Or execute (start + wait) result = Temporalio::Workflow.execute_child_workflow( ChildWorkflow, 'arg' ) ``` -------------------------------- ### Get Test Client Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/testing.md Retrieves the client connected to the test environment. ```ruby env.client ``` -------------------------------- ### Client#start_workflow Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md Starts a workflow and returns a handle without waiting for completion. This method allows for asynchronous initiation of workflow executions. ```APIDOC ## Client#start_workflow ### Description Start a workflow and return a handle without waiting for completion. ### Method POST (Assumed, as it initiates a resource) ### Endpoint `/start_workflow` (Assumed, based on method name) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **workflow** (Class, String, Symbol) - Required - Workflow class or name to execute - **args** (Array) - Optional - Positional arguments passed to workflow execute method - **id** (String) - Required - Workflow execution ID - **task_queue** (String) - Required - Task queue to execute workflow on - **workflow_name** (String, nil) - Optional - Override the workflow name. If unset, uses workflow definition name - **arg_hints** (Array) - Optional - Hints for converter when deserializing arguments - **result_hint** (Object, nil) - Optional - Hint for converter when deserializing result - **memo** (Hash) - Optional - Memo to set on workflow. Keys are strings, values are serializable - **search_attributes** (Hash, SearchAttributes) - Optional - Search attributes for filtering workflows - **start_signal** (String, Symbol, nil) - Optional - Signal to send when starting the workflow - **start_signal_args** (Array) - Optional - Arguments for the start signal - **execution_timeout** (Integer, nil) - Optional - Timeout for the entire workflow execution in seconds - **run_timeout** (Integer, nil) - Optional - Timeout for a single workflow run in seconds - **task_timeout** (Integer, nil) - Optional - Timeout for a single workflow task in seconds - **retry_policy** (RetryPolicy, nil) - Optional - Retry policy for the workflow - **cron_schedule** (String, nil) - Optional - Cron schedule for recurring workflows - **parent_close_policy** (ParentClosePolicy, nil) - Optional - How the parent workflow behaves when this workflow completes - **versioning_override** (VersioningOverride, nil) - Optional - Versioning information for deployment - **with_start_workflow_operation** (WithStartWorkflowOperation, nil) - Optional - Nexus operation for starting - **rpc_options** (RPCOptions, nil) - Optional - Advanced RPC options ### Request Example ```ruby client.start_workflow( MyWorkflow, 'arg1', 'arg2', id: 'workflow-123', task_queue: 'my-task-queue', execution_timeout: 3600 ) ``` ### Response #### Success Response (200) - **Type:** `Client::WorkflowHandle` - Handle for interacting with the started workflow #### Response Example (A `Client::WorkflowHandle` object) ### Error Handling - `Temporalio::Error::RPCError` — If RPC call fails ``` -------------------------------- ### WorkflowEnvironment#client Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/testing.md Get the test client connected to the environment. ```APIDOC ## #client ### Description Get the test client. ### Returns `Temporalio::Client` — Connected test client ### Example ```ruby env.client ``` ``` -------------------------------- ### Start a Standalone Activity Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/activity.md Initiate a standalone activity execution directly from a client. This is useful for activities that do not need to be part of a workflow. Requires specifying activity ID and task queue. ```ruby handle = client.start_activity( MyActivity, 'arg1', id: 'activity-123', task_queue: 'default', start_to_close_timeout: 300 ) result = handle.result ``` -------------------------------- ### Define an Activity Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/00-START-HERE.md Define an activity by inheriting from Temporalio::Activity::Definition and implementing the execute method. This example returns a simple string. ```ruby class MyActivity < Temporalio::Activity::Definition def execute(arg) "result" end end ``` -------------------------------- ### Start a Temporal Testing Environment Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/README.md Initializes a test environment that allows for time-skipping, facilitating faster and more controlled testing of workflows. See testing.md for all testing APIs. ```ruby require 'temporalio/testing' Temporalio::Testing::WorkflowEnvironment.start_time_skipping do |env| # Test code end ``` -------------------------------- ### Workflow Execution Output Example Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md This is the expected output when the 'execute_workflow.rb' script is run after the worker has successfully completed the workflow. ```text Result: Hello, Temporal! ``` -------------------------------- ### Configure Worker with Tuner Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/worker.md Example of creating a worker and configuring its concurrency limits using a fixed tuner. This sets the maximum number of concurrent activities and workflow tasks for the worker. ```ruby worker = Temporalio::Worker.new( client:, task_queue: 'default', workflows: [MyWorkflow], activities: [MyActivity], tuner: Temporalio::Worker::Tuner.create_fixed( max_concurrent_activities: 50, max_concurrent_workflow_tasks: 20 ) ) ``` -------------------------------- ### WorkflowEnvironment.start_local Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/testing.md Start a local test environment with a real Temporal server. Downloads and runs a Temporal server subprocess, provides a client connected to the server, and cleans up automatically when the block completes. ```APIDOC ## WorkflowEnvironment.start_local ### Description Start a local test environment with a real Temporal server. ### Parameters - Block — Executed with the test environment ### Behavior - Downloads and runs a Temporal server subprocess - Provides a client connected to the server - Cleanup is automatic when the block completes ### Returns Block return value ### Example ```ruby Temporalio::Testing::WorkflowEnvironment.start_local do |env| # Create worker worker = Temporalio::Worker.new( client: env.client, task_queue: 'test-queue', workflows: [MyWorkflow] ) # Run worker and test worker.run do result = env.client.execute_workflow( MyWorkflow, id: 'wf-1', task_queue: 'test-queue' ) puts "Result: #{result}" end end ``` ``` -------------------------------- ### Start and Get Result of Standalone Activity Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Starts a standalone activity and blocks until it completes to retrieve the result. Ensure the activity ID, task queue, and timeouts are correctly specified. ```ruby handle = client.start_activity( MyActivity, 'some-arg', id: 'my-activity-id', task_queue: 'my-task-queue', start_to_close_timeout: 60 ) result = handle.result # blocks until the activity completes ``` -------------------------------- ### Creating an ActivityEnvironment Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/testing.md Demonstrates how to create an instance of ActivityEnvironment with various configuration options. ```APIDOC ## Creating an ActivityEnvironment This section details the creation of an `ActivityEnvironment` object, which allows for isolated testing of activities. ### Constructor Parameters | Parameter | Type | Default | Description | |-----------|------|---------|-------------| | task_queue | String | 'test-queue' | Task queue name | | heartbeat_details | Array | [] | Heartbeat details from previous attempts | | attempt | Integer | 1 | Activity attempt number | | started_at | Time | Time.now | When activity started | | scheduled_at | Time | Time.now | When activity was scheduled | | deadline | Time | +1 hour | Activity deadline | | is_local | Boolean | false | Whether this is a local activity | | is_workflow | Boolean | true | Whether part of a workflow | | workflow_id | String, nil | nil | Parent workflow ID | | workflow_run_id | String, nil | nil | Parent workflow run ID | | workflow_type | String, nil | nil | Parent workflow type | ### Example Usage ```ruby require "temporalio/testing" env = Temporalio::Testing::ActivityEnvironment.new( task_queue: 'test-queue', heartbeat_details: [], attempt: 1, started_at: Time.now, scheduled_at: Time.now, deadline: Time.now + 3600, is_local: false, is_workflow: true, workflow_id: 'test-workflow', workflow_run_id: 'test-run', workflow_type: 'TestWorkflow' ) ``` ``` -------------------------------- ### Get immutable workflow information Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Retrieves immutable details about the current workflow execution, such as its ID, type, and start time. ```ruby Temporalio::Workflow.info ``` -------------------------------- ### Temporalio::Workflow.memo Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Get the workflow memo (read-only). This method retrieves the memo associated with the workflow execution, which is immutable after the workflow starts. ```APIDOC ## Temporalio::Workflow.memo ### Description Get the workflow memo (read-only). ### Method `Temporalio::Workflow.memo` ### Response #### Success Response (`Hash`) - **Description:** Read-only memo set at workflow start ``` -------------------------------- ### Get the workflow memo Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Retrieves the read-only memo associated with the workflow execution. Memos are key-value pairs set at workflow start. ```ruby Temporalio::Workflow.memo ``` -------------------------------- ### Temporalio::Workflow.search_attributes Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Get search attributes (read-only). This method retrieves the search attributes associated with the workflow execution, which are immutable after the workflow starts. ```APIDOC ## Temporalio::Workflow.search_attributes ### Description Get search attributes (read-only). ### Method `Temporalio::Workflow.search_attributes` ### Response #### Success Response (`Temporalio::SearchAttributes`) - **Description:** Read-only search attributes ``` -------------------------------- ### Implement a Simple Temporal Activity Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Define a Ruby class that inherits from Temporalio::Activity::Definition to implement a workflow activity. This example activity takes a name and returns a greeting. ```ruby require 'temporalio/activity' # Implementation of a simple activity class SayHelloActivity < Temporalio::Activity::Definition def execute(name) "Hello, #{name}!" end end ``` -------------------------------- ### Connect to Temporal Client Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/README.md Establishes a connection to the Temporal server. This client is used to interact with Temporal, such as starting workflows or querying their status. See client.md for all `Temporalio::Client` methods. ```ruby require 'temporalio/client' client = Temporalio::Client.connect('localhost:7233', 'default') # Use client methods ``` -------------------------------- ### Client#start_activity Method Signature Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md This snippet shows the signature of the Client#start_activity method, detailing all available parameters and their types. It is useful for understanding the full range of options when starting an activity. ```ruby start_activity( activity, *args, id:, task_queue:, activity_name: nil, arg_hints: [], result_hint: nil, schedule_to_close_timeout: nil, start_to_close_timeout: nil, heartbeat_timeout: nil, retry_policy: nil, versioning_override: nil, rpc_options: nil ) ``` -------------------------------- ### Temporalio::Workflow.info Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Get immutable workflow information. This method provides access to details about the current workflow execution, such as its ID, type, and start time. ```APIDOC ## Temporalio::Workflow.info ### Description Get immutable workflow information. ### Method `Temporalio::Workflow.info` ### Response #### Success Response (`Temporalio::Workflow::Info`) - **Description:** Workflow execution details including ID, type, start time, etc. ``` -------------------------------- ### Initialize WorkflowReplayer Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/worker.md Create a new WorkflowReplayer instance to replay workflow histories for testing and validation. Pass the list of workflows to be replayed. ```ruby replayer = Temporalio::Worker::WorkflowReplayer.new(workflows: [MyWorkflow]) ``` -------------------------------- ### Create and Run a Worker Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/quick-reference.md Instantiate a worker with client, task queue, workflows, and activities, then run it with signal handling. ```ruby worker = Temporalio::Worker.new( client:, task_queue: 'default', workflows: [Workflow1, Workflow2], activities: [Activity1, Activity2], tuner: Temporalio::Worker::Tuner.create_fixed( max_concurrent_activities: 100 ) ) # Run with signal handling worker.run(shutdown_signals: ['SIGINT', 'SIGTERM']) # Run multiple workers Temporalio::Worker.run_all( worker1, worker2, shutdown_signals: ['SIGINT'] ) ``` -------------------------------- ### Use Converter Hints for Custom Serialization Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/configuration.md Use hints to customize serialization for workflow arguments and results, and activity arguments and results. This example shows how to hint that `MyModel` should be used for deserialization. ```ruby class MyModel attr_accessor :value end # In workflow definition class MyWorkflow < Temporalio::Workflow::Definition workflow_arg_hint MyModel workflow_result_hint MyModel def execute(arg) # arg hint helps deserialize to MyModel end end # In activity definition class MyActivity < Temporalio::Activity::Definition activity_arg_hint MyModel activity_result_hint MyModel def execute(arg) # arg hint helps deserialize to MyModel end end ``` -------------------------------- ### Execute Standalone Activity and Wait for Result Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md A convenience method to start a standalone activity and immediately wait for its completion and result. This is a shorthand for starting and then calling `.result`. ```ruby result = client.execute_activity( MyActivity, 'some-arg', id: 'my-activity-id', task_queue: 'my-task-queue', start_to_close_timeout: 60 ) ``` -------------------------------- ### Client#options Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md Returns a frozen copy of the client's current configuration options. ```APIDOC ## Client#options ### Description Get a frozen copy of this client's options. ### Method `options` ### Returns - **Type:** `Client::Options` - **Description:** Immutable copy of client options ### Example ```ruby options = client.options new_client = Temporalio::Client.new(**options.to_h.merge(namespace: 'other-namespace')) ``` ``` -------------------------------- ### Build and Test Project Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Lints, builds, and tests the entire project. ```ruby bundle exec rake ``` -------------------------------- ### Client Connect Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/quick-reference.md Demonstrates how to connect to the Temporal service using the client, including options for TLS and API keys. ```APIDOC ## Client Connect Connect to the Temporal service. Supports plain connection, TLS, and API key authentication. ### Connect to default namespace ```ruby client = Temporalio::Client.connect('localhost:7233', 'namespace') ``` ### Connect with TLS ```ruby client = Temporalio::Client.connect( 'cloud.tmprl.cloud:7233', 'namespace.cloud', tls: Temporalio::Client::Connection::TLSOptions.new( client_cert: File.read('cert.pem'), client_private_key: File.read('key.pem') ) ) ``` ### Connect with API Key and TLS ```ruby client = Temporalio::Client.connect( 'cloud.tmprl.cloud:7233', 'namespace.cloud', api_key: 'my-key', tls: true ) ``` ``` -------------------------------- ### Temporalio::Workflow.execute_child_workflow Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Starts a child workflow and waits for its completion, returning its result directly. ```APIDOC ## Temporalio::Workflow.execute_child_workflow ### Description Start and wait for a child workflow. ### Method Signature ```ruby execute_child_workflow(workflow, *args, **options) ``` ### Details Shortcut for `start_child_workflow` followed by `handle.result`. ### Parameters Same as `start_child_workflow`. See parameters table above. ### Returns - **Type:** Object - **Description:** Child workflow result ``` -------------------------------- ### Worker::ActivityExecutor::ThreadPool.default Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/worker.md Gets the default activity executor, which runs activities in threads. ```APIDOC ## Worker::ActivityExecutor::ThreadPool ### Description Default executor that runs activities in threads. ### ActivityExecutor::ThreadPool.default ```ruby Temporalio::Worker::ActivityExecutor::ThreadPool.default ``` ``` -------------------------------- ### Get Worker Options Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/worker.md Retrieves a frozen copy of the worker's configuration options. ```ruby worker.options ``` -------------------------------- ### Start and Interact with a Workflow from a Client Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Initiates a workflow, queries its current state, updates parameters, signals completion, and retrieves the final result. Requires workflow ID and task queue. ```ruby # Start the workflow handle = my_client.start_workflow( GreetingWorkflow, { salutation: 'Hello', name: 'Temporal' }, id: 'my-workflow-id', task_queue: 'my-task-queue' ) # Check current greeting via query puts "Current greeting: #{handle.query(GreetingWorkflow.current_greeting)}" # Change the params via update handle.execute_update( GreetingWorkflow.update_greeting_params, { salutation: 'Aloha', name: 'John' } ) # Tell it to complete via signal handle.signal(GreetingWorkflow.complete_with_greeting) # Wait for workflow result puts "Final greeting: #{handle.result}" ``` -------------------------------- ### Run Tests by Name Prefix Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md Executes all tests whose names start with a specified prefix. ```ruby bundle exec rake test TESTOPTS="--name=/^test_some_method_prefix/" ``` -------------------------------- ### Handle Activity Cancellation Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/errors.md Demonstrates how to configure an activity to not automatically raise on cancellation and how to manually check for and handle cancellation within the activity's execution loop. ```ruby class MyActivity < Temporalio::Activity::Definition activity_cancel_raise false def execute ctx = Temporalio::Activity::Context.current loop do if ctx.cancellation.canceled? raise Temporalio::Error::CanceledError.new('Activity canceled') end do_work() end end end ``` -------------------------------- ### Connect to Local Temporal Server Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/configuration.md Connects to a local Temporal server instance. Ensure the server is running and accessible. ```ruby client = Temporalio::Client.connect( 'localhost:7233', 'default', tls: false, logger: Logger.new($stdout, level: Logger::DEBUG) ) ``` -------------------------------- ### Get Worker Task Queue Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/worker.md Retrieves the task queue name associated with the worker instance. ```ruby worker.task_queue ``` -------------------------------- ### #run Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/testing.md Executes a given activity within the ActivityEnvironment and returns its result. ```APIDOC ## #run Execute an activity in the test environment. ### Method Signature ```ruby env.run(activity, *args) ``` ### Description This method executes a specified activity class with provided arguments within the isolated `ActivityEnvironment`. It returns the result of the activity execution. ### Parameters #### Arguments - **activity** (Class) - Required - The Activity class to execute. - **args** (Array) - Optional - Arguments to pass to the activity's `execute` method. ### Returns - Activity result: The value returned by the activity's `execute` method. ### Raises - Exception raised by the activity: If the activity execution throws an exception, it will be propagated. ### Example ```ruby require 'temporalio/testing' class MyActivity < Temporalio::Activity::Definition def execute(name) "Hello, #{name}!" end end env = Temporalio::Testing::ActivityEnvironment.new result = env.run(MyActivity, 'Temporal') puts result # Output: "Hello, Temporal!" ``` ``` -------------------------------- ### Create Deployment Options Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/configuration.md Create deployment options for a worker, specifying the deployment name and build ID for versioning. ```ruby Temporalio::Worker::DeploymentOptions.new( version: Temporalio::WorkerDeploymentVersion.new( deployment_name: 'my-app', # Deployment identifier build_id: 'build-123' # Build/version identifier ) ) ``` -------------------------------- ### Temporalio::Workflow.start_child_workflow Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Starts a child workflow execution without waiting for its completion, returning a handle to manage it. ```APIDOC ## Temporalio::Workflow.start_child_workflow ### Description Start a child workflow without waiting for completion. ### Method Signature ```ruby start_child_workflow( workflow, *args, id: nil, workflow_name: nil, task_queue: nil, arg_hints: [], result_hint: nil, execution_timeout: nil, run_timeout: nil, task_timeout: nil, retry_policy: nil, cancellation: nil, cancellation_type: 'WAIT_CANCELLATION_COMPLETED', parent_close_policy: nil ) ``` ### Parameters #### Workflow - **workflow** (Class, String, Symbol) - Required - Workflow class or name #### Arguments - **args** (Array) - Optional - Workflow arguments #### Identification & Routing - **id** (String, nil) - Optional - Child workflow ID - **workflow_name** (String, nil) - Optional - Override workflow name - **task_queue** (String, nil) - Optional - Task queue (inherited from parent if not set) #### Hints - **arg_hints** (Array) - Optional - Conversion hints - **result_hint** (Object, nil) - Optional - Result conversion hint #### Timeouts - **execution_timeout** (Integer, nil) - Optional - Execution timeout in seconds - **run_timeout** (Integer, nil) - Optional - Run timeout in seconds - **task_timeout** (Integer, nil) - Optional - Task timeout in seconds #### Behavior - **retry_policy** (RetryPolicy, nil) - Optional - Retry policy - **cancellation** (Cancellation, nil) - Optional - Cancellation - **cancellation_type** (String) - Optional - Cancellation handling - **parent_close_policy** (ParentClosePolicy, nil) - Optional - Parent close policy ### Returns - **Type:** `Temporalio::Workflow::ChildWorkflowHandle` - **Description:** Handle for the child workflow ### Example ```ruby handle = Temporalio::Workflow.start_child_workflow( ChildWorkflow, 'arg1', id: 'child-1' ) result = handle.result ``` ``` -------------------------------- ### Get the workflow cancellation Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Retrieves the workflow-level cancellation token. This token can be used to check if the workflow has been canceled. ```ruby Temporalio::Workflow.cancellation ``` -------------------------------- ### Configure Worker with Deployment Options Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/worker.md Initializes a worker with specific deployment options, including deployment name and build ID. This allows for version-aware worker management. ```ruby deployment_options = Temporalio::Worker::DeploymentOptions.new( version: Temporalio::WorkerDeploymentVersion.new( deployment_name: 'my-app-v2', build_id: 'build-20240101' ) ) worker = Temporalio::Worker.new( client:, task_queue: 'default', workflows: [MyWorkflow], deployment_options: ) ``` -------------------------------- ### Configure DNS Load Balancing Options for Temporal Client Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/configuration.md Instantiate DnsLoadBalancingOptions to configure DNS load balancing for Temporal client connections. No specific parameters are shown in this basic example. ```ruby Temporalio::Client::Connection::DnsLoadBalancingOptions.new ``` -------------------------------- ### Define a Greeting Workflow Source: https://github.com/temporalio/sdk-ruby/blob/main/README.md This example shows how to define a workflow class in Ruby, including the main execute method, handling updates, and completing the workflow. Workflow code must be deterministic. ```ruby require 'temporalio/workflow' class GreetingWorkflow < Temporalio::Workflow::Definition workflow_query_attr_reader :current_greeting def execute(params) loop do # Call activity called CreateGreeting to create greeting and store as attribute @current_greeting = Temporalio::Workflow.execute_activity( CreateGreeting, params, schedule_to_close_timeout: 300 ) Temporalio::Workflow.logger.debug("Greeting set to #{@current_greeting}") # Wait for param update or complete signal. Note, cancellation can occur by default # on wait_condition calls, so Cancellation object doesn't need to be passed # explicitly. Temporalio::Workflow.wait_condition { @greeting_params_update || @complete } # If there was an update, exchange and rerun. If it's _only_ a complete, finish # workflow with the greeting. if @greeting_params_update params, @greeting_params_update = @greeting_params_update, nil else return @current_greeting end end end workflow_update def update_greeting_params(greeting_params_update) @greeting_params_update = greeting_params_update end workflow_signal def complete_with_greeting @complete = true end end ``` -------------------------------- ### Create and Run a Worker Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/00-START-HERE.md Create a worker instance with a client and task queue, then run it to process workflows. The worker can be configured to shut down on specific signals. ```ruby worker = Temporalio::Worker.new(client:, task_queue: 'default', workflows: [MyWorkflow]) worker.run(shutdown_signals: ['SIGINT']) ``` -------------------------------- ### Get Workflow Handle Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md Retrieves a handle to an existing workflow by its ID. This handle can be used to query or signal the workflow. ```ruby workflow_handle( id, run_id: nil, first_execution_run_id: nil, result_hint: nil, rpc_options: nil ) ``` ```ruby # Get handle to existing workflow handle = client.workflow_handle('workflow-123') ``` ```ruby # Get handle with specific run handle = client.workflow_handle('workflow-123', run_id: 'run-456') ``` ```ruby # Query or signal status = handle.query(MyWorkflow.status_query) handle.signal(MyWorkflow.update_signal, new_value) ``` -------------------------------- ### Test Workflow with Signals Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/testing.md Tests a workflow that receives and processes signals. It starts the workflow, sends a signal, and then asserts the result. ```ruby def test_workflow_with_signal Temporalio::Testing::WorkflowEnvironment.start_time_skipping do |env| worker = Temporalio::Worker.new( client: env.client, task_queue: 'test-queue', workflows: [SignalWorkflow], workflow_executor: Temporalio::Worker::WorkflowExecutor::ThreadPool.default ) worker.run do handle = env.client.start_workflow( SignalWorkflow, id: 'wf-1', task_queue: 'test-queue' ) # Send signal to workflow handle.signal(SignalWorkflow.complete) # Get result result = handle.result assert_equal 'completed', result end end end ``` -------------------------------- ### Temporalio::Workflow.cancellation Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/workflow.md Get the workflow cancellation. This method returns a cancellation object that can be used to monitor or signal workflow cancellation. ```APIDOC ## Temporalio::Workflow.cancellation ### Description Get the workflow cancellation. ### Method `Temporalio::Workflow.cancellation` ### Response #### Success Response (`Temporalio::Cancellation`) - **Description:** Workflow-level cancellation that fires when workflow is canceled ``` -------------------------------- ### Get Activity Handle Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/client.md Retrieves a handle for an existing standalone activity. Use this to interact with an activity that is already running or has completed. ```ruby activity_handle(id, activity_run_id: nil) ``` -------------------------------- ### Get External Workflow Handle Source: https://github.com/temporalio/sdk-ruby/blob/main/_autodocs/quick-reference.md Obtain a handle to an external workflow by its ID. This allows signaling or querying the external workflow. ```ruby external = Temporalio::Workflow.external_workflow_handle('wf-id') external.signal(signal_method, 'value') ```