قالب وردپرس درنا توس
Home / Tips and Tricks / How to run GCP cloud functions regularly with Cloud Scheduler – CloudSavvy IT

How to run GCP cloud functions regularly with Cloud Scheduler – CloudSavvy IT

    Google Cloud Platforms Cloud Scheduler.

If you want to run code regularly, like a cron job, you can use the Google Cloud Platforms Cloud Scheduler to automatically run serverless cloud features at fixed time intervals, without using any actual servers.

What is Cloud Scheduler?

Cloud Scheduler advertises itself as “Managed Cron as a Service.”

; It allows you to schedule tasks with cron syntax. But it only replaces the scheduling component in cron – Cloud Scheduler is not a computer service, and it can only send HTTP requests or send pub / sub messages, so on its own it is quite limited in scope.

One of the most useful applications for the scheduler is to trigger cloud functions at regular intervals. Cloud functions run code without servers with different runtimes such as JavaScript with Node, Python or Java. You can set Cloud features to trigger from Pub / Sub messages, which Cloud Scheduler can send, so you end up with a system where you can run serverless code regularly with cron syntax.

If the code you need to run needs to be done on a specific server, the best option is still to just use local cron to run all the scripts you need. But if you can script it with JavaScript / Python and do not care about the function’s execution environment, Cloud Functions will handle it well without you having to set up a server.

Set a scheduled function

Switch to the cloud function console to set a function. You do not need to create a new function, as you can only edit an existing function to delete pub / sub messages.

Give it a name, choose how much RAM to allocate to it and switch to “Cloud Pub / Sub” as the trigger.

select pub sub

You must select or create a Pub / Sub topic for the feature you want to subscribe to.

create new topic

Give it a name and click “Create Topic.”

give the subject a name

Once this is done, you can upload your code, either by pasting it inline or uploading a zip or by linking a cloud source archive.

upload code

Switch to the Cloud Scheduler console to create the cron job.

create a new cron job

Give it a name and set the frequency with cron syntax. You can read our guide to cron or use this online tool to help you with scheduling. The general syntax is:

minute hour day month weekday

Set the target on Pub / Sub and enter the subject name you created for the function.

fill in job settings

The feature should now run automatically, but you can click “Run Now” from the Cloud Scheduler to test it. You can also view the logs of previous executions from this panel.

Jump now

If the function does not run properly, check the logs for the cloud function to make sure it responds to requests at all. If it receives requests, it’s probably a bug in your code, and if it’s not, it’s probably a bug or spelling in the Pub / Sub topic.

Source link