https://github.com/glef1x/apscheduler-di
🧑🏻🔬 A lightweight and easy-to-use Dependency Injection plugin for APScheduler
https://github.com/glef1x/apscheduler-di
apscheduler asyncio dependency-injection scheduler scheduler-library
Last synced: 10 months ago
JSON representation
🧑🏻🔬 A lightweight and easy-to-use Dependency Injection plugin for APScheduler
- Host: GitHub
- URL: https://github.com/glef1x/apscheduler-di
- Owner: GLEF1X
- Created: 2021-11-10T09:32:06.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-12-09T02:00:55.000Z (over 1 year ago)
- Last Synced: 2025-05-07T17:14:56.756Z (over 1 year ago)
- Topics: apscheduler, asyncio, dependency-injection, scheduler, scheduler-library
- Language: Python
- Homepage:
- Size: 158 KB
- Stars: 18
- Watchers: 2
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Implementation of dependency injection for `apscheduler`
[](https://pypi.org/project/apscheduler-di/)[](https://codecov.io/gh/GLEF1X/apscheduler-di)[](https://pepy.tech/project/apscheduler-di)
### Motivation:
* `apscheduler-di` addresses the issue of `apscheduler` not natively supporting Dependency Injection, making it challenging for developers to pass complex objects to jobs without encountering issues.
## Features:
* Supports type hints ([PEP 561](https://www.python.org/dev/peps/pep-0561/))
* Extend `apscheduler` and provide handy aliases for events(such as `on_startup`, `on_shutdown` and
etc)
* Offers a way to implement the [Dependency Inversion](https://en.wikipedia.org/wiki/Dependency_inversion_principle) principle from SOLID design principles.
"Under the hood" `apscheduler-di`
implements [Decorator](https://en.wikipedia.org/wiki/Decorator_pattern) pattern and wraps up the
work of native `BaseScheduler` using [rodi](https://github.com/Neoteroi/rodi) lib
### Quick example:
```python
import os
from typing import Dict
from apscheduler.jobstores.redis import RedisJobStore
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler_di import ContextSchedulerDecorator
# pip install redis
job_stores: Dict[str, RedisJobStore] = {
"default": RedisJobStore(
jobs_key="dispatched_trips_jobs", run_times_key="dispatched_trips_running"
)
}
class Tack:
def tack(self):
print("Tack!")
def tick(tack: Tack, some_argument: int):
print(tack)
def main():
scheduler = ContextSchedulerDecorator(BlockingScheduler(jobstores=job_stores))
scheduler.ctx.add_instance(Tack(), Tack)
scheduler.add_executor('processpool')
scheduler.add_job(tick, 'interval', seconds=3, kwargs={"some_argument": 5})
print('Press Ctrl+{0} to exit'.format('Break' if os.name == 'nt' else 'C'))
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
pass
if __name__ == '__main__':
main()
```