This content originally appeared on Level Up Coding - Medium and was authored by Dev INTJ Code
Programmatically Manage and Execute Custom Cron Based Jobs
Introduction
In this tutorial, we’ll be creating our very own custom task scheduler. For a better context, let’s suppose the need to schedule and execute some tasks — it might be sending notifications, archiving, and whatnot; however, for simplicity — let’s limit the scope of this article to managing and executing custom cron-based jobs.
So, without further ado — let’s leverage the APScheduler — a python library that can handle the above scenarios.
Project Setup
While obviously, we can do this within Flask, Django, or any other python framework — for the sake of brevity, let’s do this with only the necessary library (APScheduler).
First, let’s install the APScheduler:
pip install apscheduler
Next, let’s have a quick look at the entire implementation — then let’s go through all this one thing at a time.
Deep Dive
Starting from the imports, we have a Scheduler and a Trigger:
To overcommunicate, a Scheduler manages the jobs; and a Trigger is to initiate the execution of each job. While we are using a BackgroundScheduler here — it’s certainly possible to use other types like BlockingScheduler; same with the trigger.
Before anything else, let’s begin by creating a scheduler with a UTC timezone. Then, let’s add one job that schedules the other jobs (interval-based every 5 seconds). Notice that we defined a next_run_time — that is, to run the job immediately at first.
Now, let’s proceed with the related functions inside the scheduling of jobs:
First, let’s check the retrieval of jobs — which for sure is possible to implement in several ways — such as retrieving it from a database, HTTP request, events, etc. For a simple demonstration, we are retrieving it from a local JSON file in our project:
Next, let’s see how to add a job — here, we used the scheduler’s add_job method. It’s worth pointing out that we explicitly set the timezone for the CronTrigger of this job — because, for cron-based jobs, it defaults to the system’s timezone (even though the scheduler itself has a timezone).
Quite similarly, for updating a job — we’ll also use the scheduler’s object to remove and add the updated one back:
Lastly, for executing a job — we created a simple function that we previously passed to the add_job method:
And there we go — pretty straightforward, right?
As usual, the entire source code is always available on GitHub.
Level Up Coding
Thanks for being a part of our community! More content in the Level Up Coding publication.
Follow: Twitter, LinkedIn, Newsletter
Level Up is transforming tech recruiting 👉 Join our talent collective
APScheduler: Dynamic Task Scheduling Library in Python was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Dev INTJ Code
Dev INTJ Code | Sciencx (2022-07-13T01:42:59+00:00) APScheduler: Dynamic Task Scheduling Library in Python. Retrieved from https://www.scien.cx/2022/07/13/apscheduler-dynamic-task-scheduling-library-in-python/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.