This page shows you how to use Cloud Scheduler to run a workflow on a particular schedule, such as every Monday at 9 AM or every 15 minutes. You can configure the schedule either in the Google Cloud console or by using the Google Cloud CLI.
Before you begin
- If you do not already have a workflow that you want to schedule, create and deploy one.
-
Enable the Cloud Scheduler API.
Or, in a terminal, enter the following command:
gcloud services enable cloudscheduler.googleapis.com
Schedule a workflow
Create a service account so that Cloud Scheduler can make requests to the Workflows API:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name that is between 6 and 30 characters. It can contain lowercase alphanumeric characters and dashes. After you create a service account, you cannot change its name.To allow the principal that will run your Cloud Scheduler commands the ability to act as an Identity and Access Management (IAM) service account, grant a role that allows the principal to impersonate the service account.
Grant your new service account the workflows.invoker role so that the account has permission to trigger your workflow:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com \ --role roles/workflows.invoker
Replace the following:
PROJECT_ID
: the ID of your Google Cloud project.SERVICE_ACCOUNT_NAME
: the name of the service account that you previously created.
Create a Cloud Scheduler job that triggers your workflow, using the service account you previously created to authenticate.
Note that if you are applying call logging, you must configure call logging through the Workflows page in the Google Cloud console. For details, refer to the following steps.
Console
To schedule a workflow, go to the Workflows page in the Google Cloud console:
On the Workflows page, select a workflow to go to its details page.
On the Workflow details page, click edit Edit.
On the Edit workflow page, select Add new trigger > Cloud Scheduler.
The Create a Scheduler job pane opens.
Define the schedule:
In the Name field, enter a name for your Cloud Scheduler job. It must be unique across the jobs in the same region.
In the Region list, select an appropriate region; for example, us-central1.
In the Frequency field, specify a time interval that you define in a unix-cron format. For example, to schedule your workflow to execute every 5 minutes, type
*/5 * * * *
.In the Timezone list, select the time zone that Cloud Scheduler should use to interpret the schedule you provide. You can search by country.
Click Continue.
Configure the execution:
In the Workflow's argument field, specify any runtime arguments to pass to your workflow before execution. Arguments must be in JSON format. For example:
. If your workflow doesn't use runtime arguments, accept the default of{"firstName":"Sherlock", "lastName":"Holmes"}
{}
, or leave the field blank.In the Workflow's call log level list, select the level of call logging that you want to apply during the execution of the workflow:
- Not specified: no logging level is specified. This is the default. An execution log level takes precedence over any workflow log level, unless the execution log level is not specified (the default); in that case, the workflow log level applies.
- Errors only: log all caught exceptions; or when a call is stopped due to an exception.
- All calls: log all calls to subworkflows or library functions and their results.
- No logs: no call logging.
In the Service account list, select the service account that you previously created.
Click Create.
Note that if you are updating an existing workflow, you do not need to redeploy the workflow.
The Cloud Scheduler job is now listed on the Triggers tab of the Workflow details page.
If you want to update or delete the job, you must edit the workflow:
- On the Workflow details page, click edit Edit.
- In the Triggers section, find the job you want to update or delete.
- Click edit Edit resource or delete Delete resource.
gcloud
Open a terminal and enter the following command:
gcloud scheduler jobs create http JOB_NAME \ --schedule="FREQUENCY" \ --uri="https://workflowexecutions.googleapis.com/v1/projects/PROJECT_ID/locations/REGION_NAME/workflows/WORKFLOW_NAME/executions" \ --message-body="{\"argument\": \"DOUBLE_ESCAPED_JSON_STRING\"}" \ --time-zone="TIME_ZONE" \ --oauth-service-account-email="SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com"
Replace the following:
JOB_NAME
: the name you are giving to your Cloud Scheduler job.FREQUENCY
: a time interval you define using a unix-cron format. For example, to schedule your workflow to execute every 5 minutes, type*/5 * * * *
.PROJECT_ID
: the ID of your Google Cloud project.REGION_NAME
: the region your Workflow is in, such asus-central1
.WORKFLOW_NAME
: the name of the workflow you want to schedule execution of.DOUBLE_ESCAPED_JSON_STRING
: a JSON encoding of any arguments you are passing. The double quotation marks inside the quoted string are escaped using backslashes (\). For example:--message-body="{\"argument\": \"{\\\"foo\\\": \\\"bar\\\"}\"}"
TIME_ZONE
: the time zone that Cloud Scheduler should use to interpret the schedule you provide. For example:America/New_York
.SERVICE_ACCOUNT_NAME
: the name of the service account that you previously created.
To list all jobs in a project:
gcloud scheduler jobs list
To delete a job:
gcloud scheduler jobs delete JOB_NAME
Your workflow now executes according to the frequency you have defined.
What's next
- Use IAM to control access
- Pass runtime arguments in an execution request
- Tutorial: Schedule Workflows with Cloud Scheduler