Overview
Cloud Scheduler lets you set up scheduled units of work to be executed at defined times or regular intervals. These work units are commonly known as cron jobs. Typical use cases might include sending out a report email on a daily basis, updating cached data every 10 minutes, or updating summary information once an hour. You can automate everything, including retries in case of failure to reduce manual intervention.
Each cron job created using Cloud Scheduler is sent to a target, where the work for the task is accomplished. The target must be one of the following types:
Publicly available HTTP/S endpoints
Pub/Sub topics
App Engine HTTP/S applications
In this lab you will learn how to:
Create a Cloud Scheduler job.
Set a recurring schedule for a job.
Specify a Cloud Pub/Sub topic as the job target.
Run a job.
Verify success.
Task 1. Enable Cloud Scheduler API
- Click on APIs & services > Library:
In the search bar, type in "scheduler", then click on the Cloud Scheduler API tile.
Click Enable.
Task 2. Set up Cloud Pub/Sub
- Create a Pub/Sub topic to use as a target for your cron job:
gcloud pubsub topics create cron-topic
Copied!content_copy
This command creates a topic called cron-topic
.
Make a note of the name, you will use it later.
Create a Cloud Pub/Sub subscription:
gcloud pubsub subscriptions create cron-sub --topic cron-topic
Copied!content_copy
You need this to view the results of your job.
Click Check my progress to verify the objective.
Set up Cloud Pub/Sub
Check my progress
Task 3. Create a job
- Visit the Cloud Scheduler page in the console - you can use the Navigation menu or the search bar:
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 for "every minute":
* * * * *
Copied!content_copy
Select your Timezone. Click Continue.
In the Target type field, select Pub/Sub topic from the dropdown menu.
Under Select a Cloud Pub/Sub topic dropdown select the topic you created earlier (
cron-topic
).Add a Message body string to be sent to your Cloud Pub/Sub target:
- Click Create.
You now have a job that sends a message to your Cloud Pub/Sub topic every minute. Wait a minute or 2 for the job to get succeeded.
Task 4. Verify the results in Cloud Pub/Sub
- To verify that your Cloud Pub/Sub topic is receiving messages from your job, invoke the following command:
gcloud pubsub subscriptions pull cron-sub --limit 5
Copied!content_copy
- View the results.
You should see output that looks similar to the following:
- If you don't see 5 responses, run the command again until you do.
Task 5. Test your knowledge
Test your knowledge about Google Cloud Platform by answering this question:
You can trigger an App Engine app, send a message via Cloud Pub/Sub, or hit an arbitrary HTTP endpoint running on Compute Engine, Google Kubernetes Engine, or on-premises with your Cloud Scheduler job.TrueFalse
Answer of Lab
cloud services enable cloudscheduler.googleapis.com
gcloud pubsub topics create cron-topic
gcloud pubsub subscriptions create cron-sub --topic cron-topic