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.
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.
You must select or create a Pub / Sub topic for the feature you want to subscribe to.
Give it a name and click “Create Topic.”
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.
Switch to the Cloud Scheduler console to create the 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.
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.
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.