Skip to main content

Schedules - Ruby SDK

This page shows how to do the following:

Schedule a Workflow

Scheduling Workflows is a crucial aspect of automation. By scheduling a Workflow, you can automate repetitive tasks, reduce manual intervention, and ensure timely execution.

Use the following actions to manage Scheduled Workflows.

Create a Scheduled Workflow

The create action enables you to create a new Schedule. When you create a new Schedule, a unique Schedule ID is generated, which you can use to reference the Schedule in other Schedule commands.

To create a Scheduled Workflow Execution in Ruby, use the create_schedule method on the Client. Then pass the Schedule ID and the Schedule object to the method to create a Scheduled Workflow Execution. Set the Schedule's action member to an instance of Temporalio::Client::Schedule::Action::StartWorkflow to schedule a Workflow Execution.

handle = my_client.create_schedule(
'my_schedule_id',
Temporalio::Client::Schedule.new(
action: Temporalio::Client::Schedule::Action::StartWorkflow.new(
MyWorkflow, 'some-input',
id: 'my-workflow-id', task_queue: 'my-task-queue'
),
spec: Temporalio::Client::Schedule::Spec.new(
intervals: [
Temporalio::Client::Schedule::Spec::Interval.new(
every: 5 * 24 * 60 * 60.0, # 5 days
)
]
)
)
)
Schedule Auto-Deletion

Once a Schedule has completed creating all its Workflow Executions, the Temporal Service deletes it since it won’t fire again. The Temporal Service doesn't guarantee when this removal will happen.

Backfill a Scheduled Workflow

The backfill action executes Actions ahead of their specified time range. This command is useful when you need to execute a missed or delayed Action, or when you want to test the Workflow before its scheduled time.

To backfill a Scheduled Workflow Execution in Ruby, use the backfill method on the Schedule Handle.

handle = my_client.schedule_handle('my-schedule-id')
now = Time.now(in: 'UTC')
handle.backfill(
Temporalio::Client::Schedule::Backfill.new(
start_at: now - (4 * 60),
end_at: now - (2 * 60),
overlap: Temporalio::Client::Schedule::OverlapPolicy::ALLOW_ALL
)
)

Delete a Scheduled Workflow

The delete action enables you to delete a Schedule. When you delete a Schedule, it does not affect any Workflows that were started by the Schedule.

To delete a Scheduled Workflow Execution in Ruby, use the delete method on the Schedule Handle.

handle = my_client.schedule_handle('my-schedule-id')
handle.delete

Describe a Scheduled Workflow

The describe action shows the current Schedule configuration, including information about past, current, and future Workflow Runs. This command is helpful when you want to get a detailed view of the Schedule and its associated Workflow Runs.

To describe a Scheduled Workflow Execution in Ruby, use the describe method on the Schedule Handle.

handle = my_client.schedule_handle('my-schedule-id')
desc = handle.describe
puts "Schedule info: #{desc.info}"

List a Scheduled Workflow

The list action lists all the available Schedules. This command is useful when you want to view a list of all the Schedules and their respective Schedule IDs.

To list all schedules, use the list_schedules asynchronous method on the Client. This returns an enumerator/enumerable. If a schedule is added or deleted, it may not be available in the list immediately.

my_client.list_schedules.each do |sched|
puts "Schedule info: #{sched}"
end

Pause a Scheduled Workflow

The pause action enables you to pause and unpause a Schedule. When you pause a Schedule, all the future Workflow Runs associated with the Schedule are temporarily stopped. This command is useful when you want to temporarily halt a Workflow due to maintenance or any other reason.

To pause a Scheduled Workflow Execution in Ruby, use the pause method on the Schedule Handle. You can pass a note to the pause method to provide a reason for pausing the schedule.

handle = my_client.schedule_handle('my-schedule-id')
handle.pause(note: 'Pausing the schedule for now')

Trigger a Scheduled Workflow

The trigger action triggers an immediate action with a given Schedule. By default, this action is subject to the Overlap Policy of the Schedule. This command is helpful when you want to execute a Workflow outside of its scheduled time.

To trigger a Scheduled Workflow Execution in Ruby, use the trigger method on the Schedule Handle.

handle = my_client.schedule_handle('my-schedule-id')
handle.trigger

Update a Scheduled Workflow

The update action enables you to update an existing Schedule. This command is useful when you need to modify the Schedule's configuration, such as changing the start time, end time, or interval.

To update a Scheduled Workflow Execution in Ruby, use the update method on the Schedule Handle. This method accepts a block which itself accepts an update input object and is expected to return an update with a new schedule to update, or nil to not update.

handle = my_client.schedule_handle('my-schedule-id')
handle.update do |input|
# Return a new schedule with the action updated
Temporalio::Client::Schedule::Update.new(
schedule: input.description.schedule.with(
# Update the action
action: Temporalio::Client::Schedule::Action::StartWorkflow.new(
MyNewWorkflow, 'some-new-input',
id: 'my-workflow-id', task_queue: 'my-task-queue'
)
)
)
end

Use Start Delay

Use the start_delay to schedule a Workflow Execution at a specific one-time future point rather than on a recurring schedule.

Use the start_delay parameter on either the start_workflow or execute_workflow methods in the Client.

handle = my_client.start_workflow(
MyWorkflow, 'some-input',
id: 'my-workflow-id', task_queue: 'my-task-queue',
start_delay: 3 * 60 * 60 # 3 hours
)