A lightweight Python task scheduler and processor using PostgreSQL.
Project description
Python Simple Tasks
A lightweight task scheduling and processing system for Python, designed to be simple, efficient, and robust. Python Simple Tasks supports both one-time and recurring tasks, leveraging PostgreSQL for task management and offering Elastic Beanstalk integration for production environments.
Features
🎯 Core Features
- Dynamic Tasks: Queue tasks dynamically at runtime with full argument support.
- Recurring Tasks: Schedule recurring tasks directly in
settings.pywith support for intervals (e.g., daily, weekly). - Task Status Tracking: Monitor tasks with statuses like
pending,success, orfailure. - PostgreSQL Integration: Reliable, scalable backend for task management.
- Elastic Beanstalk Support: Automatic cron job configuration for production environments.
- CLI Integration: Manage tasks with a straightforward command-line interface:
- Process tasks once.
- Run tasks in watch mode for continuous processing.
- Set up database tables and Elastic Beanstalk configurations.
⚙️ Production-Ready
- Database-Driven Management: Tasks are stored in a PostgreSQL database with columns for scheduling, status, output, and more.
- Elastic Beanstalk Integration: A cron job is set up to process tasks in production environments.
🧑💻 Developer-Friendly
- Local Debugging: Watch mode simulates task processing locally for easy debugging.
- Idempotent Operations: Safe CLI commands for database and configuration setup to avoid redundant operations.
Setup
1. Install the Package
Install python-simple-tasks using pip:
pip install python-simple-tasks
2. Set Up Your PostgreSQL Database
Ensure PostgreSQL is running and create a database for the tasks system (e.g., python_simple_tasks):
createdb python_simple_tasks
3. Configure Your Application
Add a settings.py file in your project root with the following database configuration:
DATABASES = {
"default": {
"NAME": "python_simple_tasks",
"USER": "postgres",
"PASSWORD": "postgres",
"HOST": "localhost",
"PORT": 5432,
}
}
# Define recurring tasks here
TASK_SCHEDULER_TASKS = [
{
"name": "send_weekly_report",
"interval": {"days": 7}, # Run every 7 days
"run_time": "09:00", # UTC time
"function": lambda: send_report(email="example@example.com", subject="Weekly Update"),
},
{
"name": "cleanup_temp_files",
"interval": {"days": 1}, # Run daily
"run_time": "02:00", # UTC time
"function": lambda: cleanup(folder="/tmp", dry_run=False),
},
]
4. Set Up the Database Tables
Run the CLI command to create the necessary database tables:
pst --setup-tables
Usage
Run Tasks Once
Process all due tasks one time:
pst
Watch Mode
Continuously process tasks in watch mode:
pst --watch
Customize the interval between task processing cycles (default is 10 seconds):
pst --watch --interval 5
Elastic Beanstalk Configuration
Generate Elastic Beanstalk settings for production environments:
pst --setup-eb
Optionally overwrite existing settings:
pst --setup-eb --overwrite
Examples
Dynamically Queue a One-Time Task
You can dynamically queue a one-time task at runtime:
from datetime import datetime, timedelta
from python_simple_tasks.scheduler import queue_task
queue_task(
name="send_custom_email",
scheduled_time=datetime.now(tz=timezone.utc) + timedelta(minutes=10),
function=lambda: send_report(email="user@example.com", subject="Custom Report"),
)
Define Recurring Tasks
Recurring tasks can be defined in settings.py:
TASK_SCHEDULER_TASKS = [
{
"name": "daily_cleanup",
"interval": {"days": 1},
"run_time": "00:00", # Midnight UTC
"function": lambda: cleanup(folder="/tmp", dry_run=False),
}
]
Inspect the Task Table
The tasks table tracks task status, timestamps, and results:
| id | name | scheduled_time | completed | status | output | created_at |
|---|---|---|---|---|---|---|
| 1 | send_weekly_report |
2025-01-30 09:00:00 |
TRUE |
success |
"Weekly report sent successfully!" | 2025-01-23 08:00:00 |
| 2 | cleanup_temp_files |
2025-01-31 02:00:00 |
FALSE |
failure |
"Error: Folder not found" | 2025-01-30 08:00:00 |
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_simple_tasks-1.0.0.tar.gz.
File metadata
- Download URL: python_simple_tasks-1.0.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b898af128edb6dd4bef442c50c5046fdfc53bb4076eace65bd9f57216f972377
|
|
| MD5 |
7cb5d2bb23cf3f020be5201a5ef3aeec
|
|
| BLAKE2b-256 |
9ee808396c9fee482c94835a5e8389e0dd8102ee6ea880cdcffc8aaf636dcaaa
|
File details
Details for the file python_simple_tasks-1.0.0-py3-none-any.whl.
File metadata
- Download URL: python_simple_tasks-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8888f2bd8efb2281339d3d70771b8e5ae031d3f036f8dc4ad969ee4ce4e4a0a4
|
|
| MD5 |
950cba2595a2cfc16aa3225e0cfce4b9
|
|
| BLAKE2b-256 |
3f6bc3dfe148d64ec75bdac5ba38408ca352b0f4da7cecd28c5b4c8b094fb067
|