This quickstart walks you through performing the following basic operations using Cloud Scheduler:
- Create a Cloud Scheduler job.
- Set a recurring schedule for a job.
- Specify a Pub/Sub topic as the job target.
- Run a job.
- Verify success.
Cloud Scheduler has a free tier, so running this quickstart should not result in any costs. For more information, see Pricing.
Before you begin
Use the following instructions to prepare for this quickstart. You may have already done some of these steps.Install and configure the gcloud
command line tool:
Update all components:
gcloud components update
Create a project with an App Engine app.
Or you can use an existing project, if you have one.
Open a terminal on the machine where you installed the Cloud SDK and create a Cloud project:
gcloud projects create [PROJECT_ID]
where
PROJECT_ID
is the ID for the project you want to create. Project IDs must start with a lowercase letter and can have lowercase ASCII letters, digits or hyphens. They must be between 6 and 30 characters.Configure
gcloud
to use the project you have chosen:gcloud config set project [PROJECT-ID]
where
PROJECT_ID
is the ID you set previously.Now create the app:
gcloud app create [--region=REGION]
where [REGION] is the location in which the app runs. Note that two locations, called
europe-west
andus-central
in App Engine commands, are called, respectively,europe-west1
andus-central1
in Cloud Scheduler commands.You can check the region of an existing app with the command:
gcloud app describe
The region is listed by
locationId
. Your location, for example, might be shown aslocationId: us-central
.Once you set a zone for the App Engine app, you cannot change it.
Use the Google Cloud Console to enable features:
Use gcloud
to set up Pub/Sub:
Set up a Pub/Sub topic to use as a target for your cron job:
gcloud pubsub topics create cron-topic
This command creates a topic called 'cron-topic'. Make a note of the name because you use this later in the quickstart.
Create a Pub/Sub subscription; you need this to view the results of your job.
gcloud pubsub subscriptions create cron-sub --topic cron-topic
Create a job
Visit the Cloud Scheduler page in the console:
Click the Create job button.
Give your job a name and optionally add a description.
Specify the frequency for your job, using the unix-cron format.
* * * * *
See Configuring Cron Job Schedules for more information. Developers familiar with App Engine Cron format can also use that.
Select your timezone.
In the Target field, select Pub/Sub topic from the dropdown menu and enter the topic you created earlier (
cron-topic
).Add a Payload string to be sent to your Pub/Sub target.
Click Create.
You now have a job that sends a message to your Pub/Sub topic every minute. Let's run the job you just created.
Run your job
In the Cloud Scheduler console page opened with your project, click the Run now button.
The first job created in a project can take a few minutes to run the first time it is invoked, because of required configuration. So you might need to wait a bit.
View the results under the Result column.
Congratulations! You have just viewed the results in the console of running a cron job that sends a message to Pub/Sub. Next, you'll learn how to verify that Pub/Sub actually got the message.
Verify the results in Pub/Sub
To verify that your Pub/Sub topic is receiving messages from your job:
Invoke the following command:
gcloud pubsub subscriptions pull cron-sub --limit 5
View the results. You should see output that looks roughly like the following
+-------------+----------------+------------+-----------------------------+ | DATA | MESSAGE_ID | ATTRIBUTES | ACK_ID | +-------------+----------------+------------+-----------------------------+ | hello cron! | 56344662920621 | | EkwnGERJUytDCypYEU4EISE-... | +-------------+----------------+------------+-----------------------------+ | hello cron! | 56340561244339 | | EkwnGERJUytDCypYEU4EISE-... | +-------------+----------------+------------+-----------------------------+ | hello cron! | 56340388879975 | | EkwnGERJUytDCypYEU4EISE-... | +-------------+----------------+------------+-----------------------------+ | hello cron! | 56337745052761 | | EkwnGERJUytDCypYEU4EISE... | +-------------+----------------+------------+-----------------------------+ | hello cron! | 56337890896134 | | EkwnGERJUytDCypYEU4EISE-... | +-------------+----------------+------------+-----------------------------+
Clean up
To avoid incurring charges to your Google Cloud account for the resources used in this quickstart, follow these steps.
Delete the Cloud Scheduler job
Go to the Cloud Scheduler page in the Cloud Console.
Click the checkbox next to your job.
Click the Delete button at the top of the page and confirm your delete.
Delete the Pub/Sub topic
Go to the Cloud Pub/Sub page in the Cloud Console.
Click the checkbox next to your topic.
Click Delete at the top of the page and confirm your delete.
Delete the project
If you created a project just for this quickstart.
- In the Cloud Console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
What's next
- Learn more about Configuring Cron Job Schedules
- Learn how to view Cloud Scheduler logs