Python is often called a glue language because it’s a powerful tool for easily combining multiple different systems. Part of this is because Python has great client library support, which lets you interact with lots of APIs, products, and services from the comfort of Python. But it doesn’t just end with the cloud. Python is useful for combining just about anything with anything else.
A Cron Job is a Linux command used for scheduling tasks to run on the server. They are most commonly used in automation.In this article we will look into the steps of Scheduling a cron job in python on Google cloud storage.
As an example we will be writing a python script that gets the top stories from the Hacker News API, then iterate over every story. If any of their titles match serverless, it’ll send you an email with the links. We’ll use the request library to make an HTTP request to the URL for the API, which happens to be hosted on Firebase, a Google Cloud product.
The first step would be writing a Python script,
Then if any of the stories match, we’ll use some custom code to send an email formatted with all of the “serverless” stories. We can put this all in a single Python function as shown below:
We can then deploy this app to Cloud Functions using the below commands:
$ gcloud functions deploy check_serverless_stories \ --runtime python38 --trigger-http
After deploying, we get a public URL for our function. So whenever we visit this URL it will email us any new articles on the front page of the Hackernews.com.
And the last step is running it on a schedule. For this, we use Cloud Scheduler to create a new job, which runs once a day.
Cron jobs are scheduled at recurring intervals specified using the Unix cron format. You can define the schedule so that your job runs multiple times a day, or runs on specific days and months.
We’ll schedule our job to run once a day at midnight. We’ll use the gcloud scheduler jobs create command to create a job which calls our function on the schedule as shown below:
$ gcloud scheduler jobs create http email_job \ --schedule = "0 0 * * *" \ --uri = https://YOUR-REGION-project.cloudfunctions.net/check_serverless_stories
We can use gcloud scheduler jobs list to confirm that our job was created using the below command:
$ gcloud scheduler jobs list
And we can use gcloud scheduler jobs run to run it once regardless of the schedule as below:
$ gcloud scheduler jobs run email_job
This gets you any serverless articles delivered right to ypur inbox every day without you having to do a thing.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.