Cron Jobs
Jetpack's cron module allows developers to run functions on regular schedule. This can be useful for running periodic tasks or recurring tasks, or for setting a function to run during periods of lower utilization.
Cron Jobs can be scheduled to run on a set time interval (e.g., every 10 minutes) or at a set time (E.g., every Monday at 7am).
Functions are marked as cron jobs using the cron
module of the Jetpack SDK. On deploy, Jetpack will create the necessary configuration, and schedule the function to run as a Kubernetes Cron Job.
Quickstart
The fastest way to create a new cronjob project is with jetpack create
:
jetpack create my-cronjob --template cronjob --name my-cronjob
Answer the remaining questions as follows:
- What is the name of this application? my-cronjob
- Is your application a web server? No
- Would you like to use Jetpack's trial cluster, or your own kubernetes cluster? Jetpack provided cluster
This will create the following project and files in the my-cronjob
directory:
my-cronjob
├── Dockerfile
├── app
│ └── main.py
├── jetconfig.yaml
└── requirements.txt
Inspecting the main.py
file, we can see that a Cron function has been automatically stubbed out for us:
from jetpack import cron
@cron.repeat(cron.every(10).minutes)
async def cron_job_function():
### Code for your cronjob goes here
print("I run every 10 minutes")
Let's test our cronjob using jetpack dev --exec
. This will let us check how the cronjob will run in our cluster by executing it immediately upon deploy, instead of waiting for the crontab to trigger.
In order to use --exec we need to pass the fully qualified name of our function:
jetpack dev my-cronjob --exec main.cron_job_function
Once the app finished deploying, our function will execute, and the logs will stream to our CLI.
App deployed to namespace "john-lago-jetpack-io"
+ my-cronjob-app-sdk-exec-2-wj9r6 › app
+ jetpack-runtime-665bd99474-lgdtm › jetpack-runtime
my-cronjob-app-sdk-exec-2-wj9r6 app /usr/local/bin/jetpack-sdk
my-cronjob-app-sdk-exec-2-wj9r6 app jetpack SDK was detected. Attempting to execute...
my-cronjob-app-sdk-exec-2-wj9r6 app I run every 10 minutes
We can now deploy the CronJob using jetpack up
, and it will execute in our cluster every 10 minutes:
jetpack up my-cronjob
Once we're finished, we can delete the cronjob by running jetpack down
jetpack down my-cronjob
Defining Cron Jobs with the Jetpack SDK
Installing the Jetpack SDK
The Jetpack SDK is available on PyPi. To use the latest version of the SDK, add jetpack-io
to your requirements.txt file:
#requirements.txt
...
jetpack-io
You can pin a specific version as follows:
#requirements.txt
...
jetpack-io>=0.51
The Jetpack SDK is under active development and new releases may have breaking changes. We recommend using the latest version as much as possible
Defining Cron Jobs with decorators
Once we have our project set up, we can take a look at the code in our cron job folder. In main.py
, we define a cron_job
function that we would like to run every 10 minutes:
...
9
10 async def cron_job():
11 payload = '{"text":"I\\'m a cronjob that runs every 10 minutes"}'
12 time.sleep(20)
13 requests.post(url, data=payload, headers=headers)
14
Normally, we would have to define a Job and CronJob in Kubernetes to schedule this function, but with the Jetpack SDK, we can do this with a simple annotation.
First, we'll import cron module from the Jetpack SDK:
1 from jetpack import cron
#...
Now we can annotate the function so that Jetpack knows to run it on a Cron schedule:
...
9 @cron.repeat(cron.every(10).minutes)
10 async def cron_job():
...
Note that the
async
keyword is required in the function definition. Jetpack will return aTypeError
if you attempt to decorate and launch a non-async function.
This annotation tells Jetpack to schedule the function as a Kubernetes CronJob to run every 10 minutes. If we wanted the job to run every hour, we could decorate the function with:
...
9 @cron.repeat(cron.every().hour)
10 def cron_job():
...
Defining Cron Jobs to run at a specific time
If we wanted the function to run at a precise time every day (say, 10 AM UTC), we would add the following decorator:
...
9 @cron.repeat(cron.every().day.at("10:00")) #Note that time is 24h, UTC
10 def cron_job():
...
Note that all time strings must be in 24-hour format (hh:mm), and use the UTC time zone.
More details on scheduling can be found in the SDK Reference.
Defining Cron Jobs via function calls
Cron jobs can also be defined through the cron
module directly. For example, we can also schedule the cron_job
function to run every 10 minutes by writing:
...
14
15 cron.repeat(cron.every(10).minutes)(cron_job)
16
...
Deploying Cron Jobs
Once the function has been decorated, you can deploy it using either jetpack dev
or jetpack up
. This will automatically create a Docker image to run your function, as well as the Kubernetes config to create your CronJob, and deploy it to your namespace. You can view the CronJobs deployed to your namespace by running:
kubectl get cronjobs.batch