Skip to main content

Cron-like task scheduler with overlap prevention and interval support

Project description

philiprehberger-task-scheduler

Tests PyPI version License

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

Install

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).

Development

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

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.4.tar.gz (5.1 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.4.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_task_scheduler-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a1fe40fa80e3bff4d018fc72527e5e0c0e082c64179c2f16c3c9d9a12265b044
MD5 6e3a780ca985af7119a72c38111d5cd4
BLAKE2b-256 baff0a2352983051f83e103ef30543f2f88f683c0b2b27516c68f771148181fe

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_task_scheduler-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1a04b51e5ebe516fa9cbccc46f8e6c4c70b6fc8ba83f299d3fbe193399d86675
MD5 fb293661f1db7cbcdd0b0c3d90e8669d
BLAKE2b-256 9484701e9730c9f24a6bd6449beaaa14b5ed8e5558ec4fdebce54d7fc6d1136b

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