In APScheduler, a job store is responsible for storing job information that the scheduler needs to execute tasks. You can add different types of job stores to persist jobs beyond the runtime of your application. The most commonly used job stores include SQLAlchemy for relational databases and MongoDB for document-oriented databases. Here’s how to add a job store using APScheduler:
Adding a Job Store
To add a job store to your scheduler, you typically use the add_jobstore method. Here’s a general outline of how to do it for different types of job stores:
1. Memory Job Store
The default job store which keeps jobs in memory. It doesn’t require explicit addition but can be useful in demonstrations or development environments.
from apscheduler.schedulers.background import BackgroundScheduler
scheduler = BackgroundScheduler()
# Add memory job store (default behavior but shown here for clarity)
scheduler.add_jobstore('memory', alias='default')
2. SQLAlchemy Job Store
Store jobs in a SQLAlchemy-compatible database such as SQLite, PostgreSQL, MySQL, etc.
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
scheduler = BackgroundScheduler()
# Using SQLAlchemyJobStore
scheduler.add_jobstore(SQLAlchemyJobStore(url='sqlite:///jobs.sqlite'), alias='default')
In the example above, the jobs are stored in an SQLite database named jobs.sqlite. You can replace the url with that of any supported database (e.g., PostgreSQL, MySQL).
3. MongoDB Job Store
Store jobs in a MongoDB database.
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.mongodb import MongoDBJobStore
scheduler = BackgroundScheduler()
# Using MongoDBJobStore
scheduler.add_jobstore(MongoDBJobStore(database='apscheduler', collection='jobs'), alias='default')
Ensure that you install the pymongo library to interact with MongoDB:
pip install pymongo
Using Django ORM Job Store
If you are integrating with Django, it's often convenient to use django_apscheduler, which provides a job store that uses the Django ORM:
from apscheduler.schedulers.background import BackgroundScheduler
from django_apscheduler.jobstores import DjangoJobStore
scheduler = BackgroundScheduler()
# Using DjangoJobStore
scheduler.add_jobstore(DjangoJobStore(), alias='default')
Starting the Scheduler
After configuring the job store, you generally start the scheduler:
def start_scheduler():
scheduler.start()
# Add a sample job
scheduler.add_job(
func=my_job_function,
trigger='interval',
seconds=60,
id='my_job'
)
def my_job_function():
print("Hello, world!")
Key Points
- Persistence: By using a persistent job store, you ensure that scheduled jobs survive restarts of your application.
- Aliases: It's common to use
alias for job stores when dealing with multiple stores.
- Database Access: Ensure that your application has the necessary permissions to access and modify the job store database.
Ensure you configure your job store according to your application's deployment environment (e.g., correct database credentials and connection strings). This way, APScheduler can effectively manage and persist your scheduled jobs.