PyCron — Python Based Job Scheduler

Ankur Katiyar
3 min readFeb 20, 2025

--

Building a Job Scheduler: A FastAPI-Powered Task Automation System

Task automation and job scheduling are fundamental components of modern software systems. In this article, I’ll guide you through building a robust Job Scheduler using FastAPI and APScheduler that handles dependent tasks through an intuitive web interface.

What We’re Building

Our Job Scheduler is a web-based application that allows you to:

  • Create and schedule jobs using cron syntax
  • Manage job dependencies
  • Monitor job execution through a dashboard
  • Execute jobs on-demand
  • Track comprehensive logs
ankur-katiyar/PyCron at react

Technical Architecture

The application is built using:

  • FastAPI: A modern, fast web framework for building APIs
  • APScheduler: Advanced Python Scheduler for job management
  • SQLAlchemy: SQL toolkit and ORM
  • Passlib + Bcrypt: For secure authentication
  • Jinja2: Template engine for the frontend

Source Code can be cloned from —

ankur-katiyar/PyCron: Job Scheduler is a robust web-based application developed using FastAPI and APScheduler

Project Structure

job-scheduler/
├── app/
│ ├── api.py
│ ├── models.py
│ └── main.py
├── k8s/
│ └── deployment.yaml
├── Dockerfile
├── requirements.txt
└── README.md

Getting Started

Prerequisites

  • Python 3.7+
  • Git
  • Basic understanding of cron syntax

Step 1: Clone the Repository

git clone https://github.com/yourusername/job-scheduler.git
cd job-scheduler

Successful repository clone

Step 2: Set Up Virtual Environment

For Windows:

python -m venv venv
venv\Scripts\activate

For Unix/MacOS:

python -m venv venv
source venv/bin/activate

Step 3: Install Dependencies

pip install -r requirements.txt

Contents of requirements.txt:

anyio==4.8.0
APScheduler==3.9.1
bcrypt==4.2.1
click==8.1.8
colorama==0.4.6
fastapi==0.95.1
greenlet==3.1.1
h11==0.14.0
idna==3.10
itsdangerous==2.2.0
Jinja2==3.1.2
MarkupSafe==3.0.2
passlib==1.7.4
pip==24.3.1
pydantic==1.10.21
python-multipart==0.0.20
pytz==2025.1
setuptools==75.8.0
six==1.17.0
sniffio==1.3.1
SQLAlchemy==1.4.48
starlette==0.26.1
typing_extensions==4.12.2
tzdata==2025.1
tzlocal==5.3
uvicorn==0.22.0

Step 4: Start the Application

uvicorn app.api:app --host 0.0.0.0 --port 8000 --reload

Core Features Deep Dive

1. Job Creation

Jobs are defined with:

  • A unique name
  • Cron schedule
  • Command to execute
  • Optional dependencies

Sample job configuration:

{
"name": "data_processing",
"schedule": "*/15 * * * *", # Every 15 minutes
"command": "python process_data.py",
"dependencies": [1, 2] # Must wait for jobs 1 and 2
}

2. Kubernetes Deployment

For production environments, we provide Kubernetes manifests in k8s/deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: job-scheduler
spec:
replicas: 1
selector:
matchLabels:
app: job-scheduler
template:
spec:
containers:
- name: job-scheduler
image: job-scheduler:latest
ports:
- containerPort: 8000

Docker Support

The application includes a Dockerfile for containerization:

FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app/ .CMD ["uvicorn", "api:app", "--host", "0.0.0.0", "--port", "8000"]

Security Features

The application implements several security measures:

1. Password Hashing

pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")

2. Session Management

app.add_middleware(SessionMiddleware, 
secret_key=os.environ.get("SECRET_KEY"))

Contributing

We welcome contributions! Visit our GitHub repository:

github.com/yourusername/job-scheduler

Resources

Job Scheduler running with multiple scheduled tasks

This project is open source and available under the MIT license. Feel free to use it, modify it, and contribute back to the community!

Tags: Python, FastAPI, DevOps, Automation, Programming, WebDevelopment, OpenSource

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ankur Katiyar
Ankur Katiyar

No responses yet

Write a response