Cron-like task scheduler with overlap prevention and interval support.
Project description
philiprehberger-task-scheduler
Cron-like task scheduler with overlap prevention and interval support.
Installation
pip install philiprehberger-task-scheduler
Usage
from philiprehberger_task_scheduler import Scheduler
scheduler = Scheduler()
@scheduler.cron("*/5 * * * *") # every 5 minutes
def check_health():
ping_server()
scheduler.start() # blocks
Interval Scheduling
from philiprehberger_task_scheduler import Scheduler
scheduler = Scheduler()
@scheduler.interval(seconds=30)
def poll_queue():
process_messages()
@scheduler.interval(minutes=5, overlap=False)
def sync_data():
pull_latest_data()
One-Shot Tasks
from philiprehberger_task_scheduler import Scheduler
scheduler = Scheduler()
@scheduler.once(delay=10) # run once after 10 seconds
def startup_task():
warm_cache()
Background Mode
from philiprehberger_task_scheduler import Scheduler
scheduler = Scheduler()
scheduler.start(background=True)
# ... your app continues running ...
scheduler.stop()
Programmatic API
from philiprehberger_task_scheduler import Scheduler
scheduler = Scheduler()
scheduler.add("my-job", fn=my_function, cron="0 * * * *")
scheduler.add("poller", fn=poll, interval_seconds=60)
scheduler.remove("my-job")
Execution History
from philiprehberger_task_scheduler import Scheduler
scheduler = Scheduler(history_limit=50)
scheduler.add("job", fn=my_function, interval_seconds=60)
scheduler.start(background=True)
# Get all history (newest first)
for record in scheduler.history:
print(f"{record.job_name}: {record.status.value} in {record.duration_seconds:.2f}s")
# Get history for a specific job
for record in scheduler.get_job_history("job"):
if record.error:
print(f"Error: {record.error}")
Next Run Preview
from philiprehberger_task_scheduler import Scheduler
scheduler = Scheduler()
scheduler.add("job", fn=my_function, cron="0 * * * *")
for name, next_time in scheduler.next_runs():
print(f"{name}: next at {next_time}")
Task Dependencies
from philiprehberger_task_scheduler import Scheduler
scheduler = Scheduler()
@scheduler.interval(seconds=60, name="fetch-data")
def fetch_data():
download_latest()
@scheduler.interval(seconds=60, name="process-data", depends_on="fetch-data")
def process_data():
transform_and_store()
Graceful Shutdown
from philiprehberger_task_scheduler import Scheduler
scheduler = Scheduler()
scheduler.start(background=True)
# Wait for running tasks to finish before stopping
scheduler.stop(wait=True)
# Or set a timeout (seconds)
scheduler.stop(wait=True, timeout=10)
Missed Job Handling
from philiprehberger_task_scheduler import Scheduler, MissedJobPolicy
scheduler = Scheduler()
@scheduler.cron("0 * * * *", missed_policy=MissedJobPolicy.RUN_ONCE)
def hourly_sync():
sync_data()
@scheduler.cron("*/5 * * * *", missed_policy=MissedJobPolicy.RUN_ALL)
def critical_check():
check_systems()
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(history_limit) |
Task scheduler with cron, interval, and one-shot scheduling |
Scheduler.cron(expression, overlap, name, depends_on, missed_policy) |
Decorator to schedule with a cron expression |
Scheduler.interval(seconds, minutes, hours, overlap, name, depends_on, missed_policy) |
Decorator to schedule at a fixed interval |
Scheduler.once(delay, name) |
Decorator to schedule a one-shot task |
Scheduler.add(name, fn, cron, interval_seconds, overlap, depends_on, missed_policy) |
Programmatically add a job |
Scheduler.remove(name) |
Remove a job by name |
Scheduler.start(background) |
Start the scheduler (blocks unless background=True) |
Scheduler.stop(wait, timeout) |
Stop the scheduler with optional graceful shutdown |
Scheduler.next_runs() |
Get the next run time for each job |
Scheduler.history |
Execution history (newest first) |
Scheduler.get_job_history(name) |
Execution history for a specific job |
Job |
A scheduled job with name, function, schedule config, and next_run property |
ExecutionRecord |
Record of a job execution with status, duration, and error |
ExecutionStatus |
Enum: SUCCESS, FAILED |
MissedJobPolicy |
Enum: SKIP, RUN_ONCE, RUN_ALL |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
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 philiprehberger_task_scheduler-0.2.0.tar.gz.
File metadata
- Download URL: philiprehberger_task_scheduler-0.2.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0571ac4fddef7d43522326e3457f0ef27ff21d1e34bbfed5b11506cc09583f8e
|
|
| MD5 |
b00dea099bf1d92e984c7316a9aa4afc
|
|
| BLAKE2b-256 |
a10a998638377d779ccec7e9692e20bb424953129107d6aacc60916e76c40bd5
|
File details
Details for the file philiprehberger_task_scheduler-0.2.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_task_scheduler-0.2.0-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4c6ecfd4af0ffc6631d2a258d92988e3393504593b049d3e47b214ebd776db2
|
|
| MD5 |
4007d3751308a4f7451bee580b34a2ab
|
|
| BLAKE2b-256 |
484339ee09866f0fbb73a75f20460a8752eb2b683cd8ba9c804c517f123b82b4
|