If you’re looking to run code regularly, like a cron job, you can use Google Cloud Platform’s Cloud Scheduler to automatically run serverless Cloud Functions 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 using cron syntax. However, it only replaces the scheduling component of cron—Cloud Scheduler isn’t a compute service, and it can only send HTTP requests or send pub/sub messages, so on its own it’s fairly limited in scope.
Setting Up a Scheduled Function
Head over to the Cloud Functions console to set up a function. You don’t need to make a new function, as you could just edit an existing one to run off of Pub/Sub messages.
Give it a name, choose how much RAM to allocate to it, and switch it over to “Cloud Pub/Sub” as the trigger.
You’ll need to select or create a Pub/Sub topic for the function to subscribe to.
Give it a name, and click “Create Topic.”
Once that’s done, you can upload your code, either by pasting it inline or uploading a zip, or by linking a Cloud Source repository.
Head over to the Cloud Scheduler console to create the cron job.
minute hour day month weekday
Set the target to Pub/Sub, and enter in the topic name you created for the function.
The function should now run automatically, but you can click “Run Now” from Cloud Scheduler to test it out. You can also view the logs for previous executions from this panel.
If the function isn’t executing properly, check the logs of the Cloud Function to make sure it’s responding to requests at all. If it’s getting requests, it’s probably an error with your code, and if it isn’t, it’s likely an error or mispelling with the Pub/Sub topic.