7

Google Cloud Functions Tutorial : Using the Cloud Scheduler to trigger your func...

 3 years ago
source link: https://rominirani.com/google-cloud-functions-tutorial-using-the-cloud-scheduler-to-trigger-your-functions-756160a95c43
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Google Cloud Functions Tutorial : Using the Cloud Scheduler to trigger your functions

This is part of a Google Cloud Functions Tutorial Series. Check out the series for all the articles.

By now, you will be familiar with creating Cloud Functions (foreground and background) and associating the appropriate trigger i.e. HTTP Trigger, PubSub Trigger and others. We usually find that we have a need to trigger the Cloud Function at regular intervals.

Consider the following Cloud Functions that you might have written:

  1. A function that checks a Stock Price every 10 minutes and sends you an email.
  2. A function that runs once a day at 3:00 AM to begin the process of taking a backup.

You could even have complex scheduling like run a Cloud Function every Monday, Wednesday and Friday at 10:00 AM.

In all the above situations, you have the following requirements:

  1. A Service that will trigger your Cloud Function.
  2. A Service that can accept simple to complex scheduling intervals. For e.g every NN minutes, every NN hours, every week on Friday at 10:00 PM and more. In other words, support a CRON like expression that we know from Unix world.

Cloud Scheduler

The Cloud Scheduler now available in Beta helps us out in the above requirements.

As per the official documentation, Cloud Scheduler is an enterprise grade job scheduler that will help you automate your jobs across various Google Services in a standard fashion and comes with retry mechanisms that you can configure. It can also trigger your jobs in a variety of ways and currently supports invoking a HTTP endpoint, send a message to Cloud Pub/Sub and trigger an internal App Engine URL.

In our example here, we will be looking at triggering a sample Cloud Function every minute. Let’s go.

Before we begin, a few assumptions:

  1. You have an existing Google Cloud Platform project with Cloud Functions API enabled.
  2. You have a sample Cloud Function ready with a HTTP Trigger associated with it. Let us assume that this Cloud Function is called function-1. Note down the HTTPs Trigger URL of the Cloud Function.
  3. You need to enable the Cloud Scheduler API for your Google Cloud Platform project. From GCP console, visit APIs and search for Cloud Scheduler API as shown below:
Image for post
Image for post

Click on Cloud Scheduler API and click on Enable:

Image for post
Image for post

Once enabled, you might see a message that says that you will need to have credentials in order to use the API. That is fine for now, since we plan to do everything via the GCP console and Cloud Functions deployed inside the same GCP Project. The API credentials would be required in case you were making any requests to this API from external Apps.

This completes our initial setup.

Using the Cloud Scheduler

We have completed the initial setup and have our Cloud Function function-1 ready. All we need to do now is to create a job in the Cloud Scheduler. All the details like the schedule, what to trigger and other parameters will be configured at the time of Job creation itself and are associated with the Job.

From the GCP Console main menu, visit the Cloud Scheduler page:

Image for post
Image for post

Click on the Cloud Scheduler option. Since we do not have any jobs yet, we will see the message as shown below:

Image for post
Image for post

Click on Create job. This will prompt you to select a region in which you would like to initialise the Cloud Scheduler. Select the appropriate region. You should see a message as shown below:

Image for post
Image for post

Once it is initialised, you will be led to a form to create a new job and you can provide the details as shown below:

Image for post
Image for post

The key things to note in the Job details form are:

  • Name : Give a unique name to your job.
  • Frequency : Provide the cron expression. Since we want to trigger the function every minute, I use : * * * * *
  • Timezone : You can select a specific timezone in which to execute the function. The Cloud Scheduler will use that.
  • Target : As mentioned earlier, the Cloud Scheduler currently supports App Engine, HTTP and Pub/Sub. We will select HTTP i.e. ask the Cloud Scheduler to invoke a HTTP endpoint that we will specify next. This HTTP endpoint will be that of our HTTP Trigger based Cloud Function.
  • URL : Provide the HTTPs endpoint of the Cloud Function i.e. function-1.
  • HTTP method : This applies to HTTP invocations. You can choose from one of the HTTP methods like POST, GET, etc. In our case, we will use the POST method.

Click on Create button. This will create the Job and you will see it listed in the list of jobs. You can either wait for the execution to happen (look at the Result column) since it is every 1 minute or if you want to invoke it directly click on Run now.

Once it has executed, you will see the status getting updated as shown below (only the specific columns are shown):

Image for post
Image for post

Click on View button to see the logs. Here is the sample log statemens from one run of the job:

Image for post
Image for post

Note that we could also have a Pub/Sub Trigger configured Cloud Function get triggered by the Scheduler by using the same steps given above to create a job. Except that in the target, you will select Pub/Sub and provide the Pub/Sub Topic name. At the scheduler trigger time, the Cloud Scheduler will publish a message to the topic with the message body that you specify.

This completes our note on triggering a Cloud Function using the just released Cloud Scheduler service.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK