Skip to main content

Cron-like task scheduler with overlap prevention and interval support

Project description

philiprehberger-task-scheduler

Tests PyPI version Last updated

Cron-like task scheduler with overlap prevention and interval support.

Installation

pip install philiprehberger-task-scheduler

Usage

Cron Scheduling

from philiprehberger_task_scheduler import Scheduler

scheduler = Scheduler()

@scheduler.cron("*/5 * * * *")  # every 5 minutes
def check_health():
    ping_server()

@scheduler.cron("0 9 * * 1-5", overlap=False)  # weekdays at 9am
def daily_report():
    generate_and_send_report()

scheduler.start()  # blocks

Interval Scheduling

@scheduler.interval(seconds=30)
def poll_queue():
    process_messages()

@scheduler.interval(minutes=5, overlap=False)
def sync_data():
    pull_latest_data()

One-Shot Tasks

@scheduler.once(delay=10)  # run once after 10 seconds
def startup_task():
    warm_cache()

Background Mode

scheduler.start(background=True)
# ... your app continues running ...
scheduler.stop()

Programmatic API

scheduler.add("my-job", fn=my_function, cron="0 * * * *")
scheduler.add("poller", fn=poll, interval_seconds=60)
scheduler.remove("my-job")

Next Run Preview

for name, next_time in scheduler.next_runs():
    print(f"{name}: next at {next_time}")

Cron Syntax

Standard 5-field cron expressions:

┌───────────── minute (0-59)
│ ┌───────────── hour (0-23)
│ │ ┌───────────── day of month (1-31)
│ │ │ ┌───────────── month (1-12)
│ │ │ │ ┌───────────── day of week (0-6, Mon-Sun)
│ │ │ │ │
* * * * *

Supports: *, ranges (1-5), lists (1,3,5), steps (*/5).

API

Function / Class Description
Scheduler Cron-like task scheduler with cron(), interval(), once() decorators and add(), remove(), start(), stop() methods
Job A scheduled job with name, function, schedule config, and next_run property

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_task_scheduler-0.1.9.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file philiprehberger_task_scheduler-0.1.9.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_task_scheduler-0.1.9.tar.gz
Algorithm Hash digest
SHA256 8fa959f99532fd0dbc071fa512f0e8701917ddd3760363e921fdc4f7bd198b4b
MD5 092c71b9a4985b542bf1c60536951bcb
BLAKE2b-256 da075ae55ad4e3ab7c646ced47e59bca649d2f1bf2d2d717204f97c58ca49124

See more details on using hashes here.

File details

Details for the file philiprehberger_task_scheduler-0.1.9-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_task_scheduler-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0e2c65cff32810e8d0cd2a147a568974ef2d8159978d5c58c926490751d17c38
MD5 b2da27ade4ed3e2bd154f83188f7cc9d
BLAKE2b-256 3c15d6caa7d13384af6f07f1b5ea60d6e01bf0c26c4f9349155f57e4772ccefb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page