Skip to main content

Schedule workflows

A workflow schedule starts a workflow repeatedly on a cron schedule, for example every night at 02:00 or every 15 minutes. Each time the schedule matches, Diagrid starts a new, independent execution of the workflow: a run. The schedule keeps track of when it runs next, how many runs it has started and how the last run went, so you no longer need a job handler that starts the workflow, or a workflow that sleeps in a loop, to run something on a timer.

You create, inspect, trigger and delete schedules from your app with the workflow client of its SDK, with the diagrid workflow schedule commands, or in the Diagrid Console. This page covers the SDK and the CLI; for the console, see Operate workflow schedules.

note

The workflow client methods on this page ship in the next release of each Dapr SDK. Until then, create and manage schedules with the Diagrid CLI or in the Diagrid Console. The runs they start are ordinary workflow executions, which your current SDK already handles.

How a schedule runs​

Each schedule is run by Diagrid itself, as a small internal workflow that belongs to your App ID. It waits on a durable timer until the next time the schedule matches, then starts your workflow as a new execution. Because it is built on the same durable timers and state as any other workflow, a schedule survives restarts and redeployments of your app, and needs no extra component.

A schedule and its runsDiagridClientconsole, CLI or SDKApp IDreportsSchedulenightly-reportRunnightly-report_8bfcbd_20261130T020000ZWorkflow workeryour appstartsruns on
A schedule and its runsThe schedule waits in Diagrid until its next time, then starts a run. The run executes on your workflow worker like any other execution.
  • Runs are ordinary workflow executions. Each run has the instance ID <schedule name>_<owner>_<time>. The owner is a tag of the App ID that owns the schedule: the first 6 hex characters of the SHA-256 of <namespace>/<App ID>. It keeps apart the runs of same-named schedules in two apps that target one app. The time is when the run was due, in UTC, formatted as 20060102T150405Z. A run started by triggering the schedule has the ID <schedule name>_<owner>_<time>_manual, where the time has milliseconds.
  • Runs are independent of the schedule. A run that fails does not stop the schedule, and deleting a schedule does not touch the runs it already started. Manage runs as you would any other execution, in the console or with diagrid workflow.
  • Runs are not child workflows. The schedule's own workflow continues as new after every run, so it could not receive a child's result, and terminating it would reach only the runs of its latest generation. Instead, the schedule's status carries the last run's status, output and failure, and the <schedule name>_ prefix of the instance IDs ties every run to its schedule.
  • The schedule needs a connected worker. As when starting any workflow, creating a schedule needs your app's workflow worker to be connected. While no worker is connected, the schedule waits.
  • A schedule never misses a time. After downtime, every time the schedule missed starts its own run, unless you set the skip missed option. See When the app is down.
  • The schedule lives with your App ID. Its runs start on the same App ID unless you set a target App ID. A target app that has a workflow access policy must grant the owning app schedule on the workflow, and get for the schedule to read the last run's status. A run the policy refuses never starts, but still counts as a run, and the schedule carries on.

Create a schedule​

The following examples create a schedule named nightly-report that starts the GenerateReport workflow at 02:00 UTC every day, then read back when it runs next and how its last run went.

from dapr.ext.workflow import DaprWorkflowClient

client = DaprWorkflowClient()

client.create_workflow_schedule(
"nightly-report",
generate_report,
"0 0 2 * * *",
input={"region": "eu"},
)

schedule = client.get_workflow_schedule("nightly-report")
print("next run:", schedule.status.next_run_time)
if schedule.status.last_run:
print("last run:", schedule.status.last_run.instance_id, schedule.status.last_run.runtime_status)

The same schedule from the Diagrid CLI, on the reports App ID:

diagrid workflow schedule create nightly-report --id reports \
--workflow GenerateReport --schedule "0 0 2 * * *" --data '{"region":"eu"}'

diagrid workflow schedule get nightly-report --id reports

Schedule format​

The schedule uses the same format as Dapr jobs:

FormatExampleMeaning
Cron expression with seconds0 30 9 * * MON-FRI09:30:00 on weekdays
@every <duration>@every 15mEvery 15 minutes. The minimum is 1 second.
@yearly, @monthly, @weekly, @daily, @hourly@dailyOnce a year, month, week, day or hour

The six fields of a cron expression are seconds, minutes, hours, day of month, month and day of week. Cron expressions use UTC unless they start with a time zone, for example CRON_TZ=Europe/London 0 0 9 * * *.

Options​

OptionCLI flagDescription
NameargumentIdentifies the schedule within the App ID. 1 to 30 letters, digits or -, not starting with dapr-internal-schedule-.
Workflow--workflowThe name of the workflow each run starts.
Schedule--scheduleWhen runs start. See Schedule format.
Input--dataThe input every run is given, as JSON. Text that is not JSON is passed as a JSON string.
Due time--due-timeThe time of the first run, in RFC 3339 format. Without it, the first run is at the first time the schedule matches.
Expiration time--expiration-timeNo run starts after this time. The schedule then completes.
Repeats--repeatsThe number of runs, scheduled or triggered, after which the schedule completes.
Target App ID and namespace--target-id, --target-namespaceThe App ID that hosts the workflow each run starts, when it is not the App ID that owns the schedule. SDK and CLI only; the console creates schedules on the owning App ID.
Overwrite--overwriteReplace an existing schedule of the same name. Without it, creating a schedule whose name is taken fails.
Skip missed--skip-missedStart one run for the times the schedule missed while the app was down, rather than one run for each, and carry on from the next time after now. Off by default.

Status​

Getting or listing schedules returns each schedule with its status:

FieldDescription
Stateactive while runs are left, completed once repeats or the expiration time are reached, and failed if the schedule itself failed, with a failure message.
Next run timeWhen the next run starts, unless the schedule is triggered sooner. Absent once the schedule is no longer active.
Last run timeThe time the last run was started for, scheduled or triggered.
Run countHow many runs the schedule has started.
Created atWhen the schedule was created.
Last runThe last run's instance ID, runtime status, created and last updated times, and its output or failure message. Only the instance ID is set when the run's execution can no longer be read, for example because it was purged.

List, trigger and delete​

Listing returns the App ID's schedules with their status. Triggering a schedule starts a run at once, without changing when the next scheduled run starts, and returns the run's instance ID. A triggered run counts towards the schedule's repeats, and a schedule that has completed or failed cannot be triggered. Deleting a schedule stops it; runs it already started carry on, and keep their instance IDs and history.

diagrid workflow schedule list --id reports
diagrid workflow schedule trigger nightly-report --id reports
diagrid workflow schedule delete nightly-report --id reports

list shows every schedule in the project unless you pass --id, with when each runs next, when it last ran, how that run went and the start of its output or failure. Pass -o json or -o yaml for the full status.

In code, the workflow client offers the same operations:

OperationPython.NETJava and JavaScriptGo
Listlist_workflow_schedulesListWorkflowSchedulesAsynclistWorkflowSchedulesListWorkflowSchedules
Triggertrigger_workflow_scheduleTriggerWorkflowScheduleAsynctriggerWorkflowScheduleTriggerWorkflowSchedule
Deletedelete_workflow_scheduleDeleteWorkflowScheduleAsyncdeleteWorkflowScheduleDeleteWorkflowSchedule

Listing from an SDK returns a page at a time. A page can hold fewer schedules than the page size while more remain, so keep listing while a continuation token is returned.

To change a schedule, create it again with the overwrite option. That replaces the schedule and resets its run count; runs it already started are not affected.

Find a schedule's runs​

Every run's instance ID starts with the schedule name and _, so list the workflow executions with that prefix:

for instance_id in client.iter_workflow_instance_ids(instance_id_prefix="nightly-report_"):
print(instance_id)

The other SDKs offer the same prefix filter on their instance ID listing. In the Diagrid Console, View runs on a schedule opens the executions list filtered to its runs. From the CLI, diagrid workflow list has no prefix filter. diagrid workflow list --id reports --name GenerateReport -o json returns the App ID's executions of that workflow, with the instance ID in each entry. It stops at 250 executions unless you pass a higher --limit, so raise it on a busy App ID before concluding that a run never started.

A schedule with a target App ID starts its runs on the target, so look for them there. Other apps can own a schedule of the same name that targets the same app, so add the owning app's tag to the prefix. For nightly-report, owned by reports in the default namespace:

printf 'default/reports' | sha256sum | cut -c1-6
# 8bfcbd

The prefix is then nightly-report_8bfcbd_.

Errors​

ProblemSDK status codeCLI and console
The name, workflow name, schedule, repeats, expiration time or target namespace is not valid.INVALID_ARGUMENT400 Bad Request
Creating a schedule whose name is taken, without overwrite.ALREADY_EXISTS409 Conflict
Getting, triggering or deleting a schedule that does not exist. In .NET, GetWorkflowScheduleAsync returns null instead.NOT_FOUND404 Not Found
Triggering a completed or failed schedule, or no workflow worker is connected for the App ID.FAILED_PRECONDITION409 Conflict, with the message App ID <app-id> has no connected workflow worker. Start the app and retry. when the worker is missing.
The App ID's runtime does not support workflow schedules yet.UNIMPLEMENTED501 Not Implemented. The CLI reports that the selected region is running an older version.

Next steps​

Was this page helpful?