summaryrefslogtreecommitdiff
path: root/examples/separate_worker/sync_worker.py
blob: 4329d02907a26cbf42e37383b05aed12f838b4d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Example demonstrating a scheduler that only runs jobs but does not process schedules.
You need to be running both this and the scheduler script simultaneously in order for
the scheduled task to be run.

Requires the "postgresql" and "redis" services to be running.
To install prerequisites: pip install sqlalchemy psycopg2 redis
To run: python sync_worker.py

When run together with sync_scheduler.py, it should print a line on the
console on a one-second interval.
"""

from __future__ import annotations

import logging

from sqlalchemy.future import create_engine

from apscheduler.datastores.sqlalchemy import SQLAlchemyDataStore
from apscheduler.eventbrokers.redis import RedisEventBroker
from apscheduler.schedulers.sync import Scheduler

logging.basicConfig(level=logging.INFO)
engine = create_engine("postgresql+psycopg2://postgres:secret@localhost/testdb")
data_store = SQLAlchemyDataStore(engine)
event_broker = RedisEventBroker.from_url("redis://localhost")

# Uncomment the next two lines to use the MQTT event broker instead
# from apscheduler.eventbrokers.mqtt import MQTTEventBroker
# event_broker = MQTTEventBroker()

with Scheduler(data_store, event_broker, process_schedules=False) as scheduler:
    scheduler.run_until_stopped()