beatdrop.schedulers package

Submodules

beatdrop.schedulers.celery_redis_scheduler module

class beatdrop.schedulers.celery_redis_scheduler.Config

Bases: object

arbitrary_types_allowed = True
class beatdrop.schedulers.celery_redis_scheduler.CeleryRedisScheduler

Bases: RedisScheduler, CeleryScheduler

Hold schedule entries in Redis, and send to Celery task queues.

Uses Redis to store schedule entries and scheduler state. It is safe to run multiple CeleryRedisScheduler s simultaneously, as well as have many that are used as clients to read/write entries.

Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

  • lock_timeout (datetime.timedelta) – The time a scheduler does not refresh the scheduler lock before it is considered dead. Should be at least 3 times the max_interval.

  • redis_py_kwargs (Dict[str, Any]) – redis-py’s redis.Redis() key word arguments. Some of the client configuration items may be overwritten. https://redis-py.readthedocs.io/en/stable/connections.html#generic-client

  • celery_app (celery.Celery) – Celery app for sending tasks.

Example

from celery import Celery
from beatdrop import CeleryRedisScheduler

celery_app = Celery()
sched = CeleryRedisScheduler(
    max_interval=60,
    celery_app=celery_app,
    lock_timeout=180,
    redis_py_kwargs={
        "host": "my.redis.host",
        "port": 6379,
        "db": 0,
        "password": "mys3cr3t"
    }
)
# use the scheduler as a client
entry_list = sched.list()
for entry in entry_list:
    print(entry.key)

# or run it
sched.run()
celery_app: celery.Celery = FieldInfo(default=PydanticUndefined, extra={})
default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

This does not delete default entries.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.ScheduleEntryNotFound – The schedule entry could not be found.

list(page_size: int = 500) RedisScheduleEntryList

List schedule entries.

Parameters:

page_size (int, optional) – Redis suggested minimum page size, by default 500

Returns:

Iterator of all schedule entries. Automatically paginated redis results.

Return type:

RedisScheduleEntryList

lock_timeout: datetime.timedelta = FieldInfo(default=PydanticUndefined, extra={})
classmethod lock_timeout_3_times_interval(values: dict) dict
max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
redis_py_kwargs: Dict[str, Any] = FieldInfo(default=PydanticUndefined, extra={})
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

save(sched_entry: ScheduleEntry, read_only_attributes: bool = False) None

Save a new, or update an existing schedule entry in redis.

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to create or update.

  • read_only_attributes (bool, optional) – If true, read only attributes are also saved to the DB. Clients should almost always leave this false, by default False

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the Celery queue.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry to send to the Celery queue.

beatdrop.schedulers.celery_scheduler module

class beatdrop.schedulers.celery_scheduler.Config

Bases: object

arbitrary_types_allowed = True
class beatdrop.schedulers.celery_scheduler.CeleryScheduler

Bases: Scheduler

Implementation for sending celery tasks.

Combine as a second base class to be able to send tasks to Celery queues.

Example:

from beatdrop.schedulers import CeleryScheduler, SQLScheduler

class CelerySQLScheduler(SQLScheduler, CeleryScheduler):
    pass
Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

  • celery_app (celery.Celery) – Celery app for sending tasks.

celery_app: Celery = FieldInfo(default=PydanticUndefined, extra={})
default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

Raises:

beatdrop.exceptions.MethodNotImplementedErrordelete method not implemented.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.MethodNotImplementedErrorget method not implemented.

list() Iterator[ScheduleEntry]

List schedule entries.

Returns:

List of schedule entries.

Return type:

Iterator[ScheduleEntry]

Raises:

beatdrop.exceptions.MethodNotImplementedErrorlist method not implemented.

max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

Raises:

beatdrop.exceptions.MethodNotImplementedError – Must implement run method.

save(sched_entry: ScheduleEntry, client_read_only: bool = False) None

Save a new or update an existing schedule entry.

By default, read only attributes are not updated with save (suffixed with __).

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to add or update in scheduler.

  • client_read_only (bool) – Overwrite client read only fields? False will not overwrite client read only fields. True will. This should almost always be False, unless you are a scheduler, or you know what you’re doing.

Raises:

beatdrop.exceptions.MethodNotImplementedErrorsave method not implemented.

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the Celery queue.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry to send to the Celery queue.

beatdrop.schedulers.celery_sql_scheduler module

class beatdrop.schedulers.celery_sql_scheduler.Config

Bases: object

arbitrary_types_allowed = True
class beatdrop.schedulers.celery_sql_scheduler.CelerySQLScheduler

Bases: SQLScheduler, CeleryScheduler

Hold schedule entries in an SQL database, and send tasks to Celery queues.

Uses an SQL database to store schedule entries and scheduler state. It is safe to run multiple CelerySQLScheduler s simultaneously, as well as have many that are purely used as clients to read/write entries.

NOTE - You must also install the DB driver specified in the URL for create_engine_kwargs.

NOTE - Before running the scheduler for the first time the DB tables must be created using create_tables().

Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

  • lock_timeout (datetime.timedelta) – The time a scheduler does not refresh the scheduler lock before it is considered dead. Should be at least 3 times the max_interval.

  • create_engine_kwargs (dict) – Keyword arguments to pass to sqlalchemy.create_engine. See SQLAlchemy docs for more info. https://docs.sqlalchemy.org/en/14/core/engines.html#sqlalchemy.create_engine

  • celery_app (celery.Celery) – Celery app for sending tasks.

Example

from celery import Celery
from beatdrop import CelerySQLScheduler

celery_app = Celery()
sched = CelerySQLScheduler(
    max_interval=60,
    celery_app=celery_app,
    lock_timeout=180,
    create_engine_kwargs={
        "url": "sqlite:///my_sqlite.db"
    }
)
# use the scheduler as a client
entry_list = sched.list()
for entry in entry_list:
    print(entry.key)

# or run it
sched.run()
celery_app: celery.Celery = FieldInfo(default=PydanticUndefined, extra={})
create_engine_kwargs: Dict[str, Any] = FieldInfo(default=PydanticUndefined, extra={})
create_tables() None

Create DB tables for the schedule entries.

default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

This does not delete default entries.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.ScheduleEntryNotFound – The schedule entry could not be found.

list(page_size: int = 500) SQLScheduleEntryList

List schedule entries.

Parameters:

page_size (int, optional) – DB page size, by default 500

Returns:

Iterator of all schedule entries. Automatically paginated DB results.

Return type:

SQLScheduleEntryList

lock_timeout: datetime.timedelta = FieldInfo(default=PydanticUndefined, extra={})
classmethod lock_timeout_3_times_interval(values: dict) dict
max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

save(sched_entry: ScheduleEntry, read_only_attributes: bool = False) None

Save a new, or update an existing schedule entry in the DB.

If read_only_attributes is set to False, sched_entry’s read only attributes will be set to what’s in the DB.

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to create or update.

  • read_only_attributes (bool, optional) – If true, read only attributes are also saved to the DB. Clients should almost always leave this false, by default False

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the Celery queue.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry to send to the Celery queue.

classmethod url_in_kwargs(v: dict) dict

beatdrop.schedulers.mem_scheduler module

class beatdrop.schedulers.mem_scheduler.MemScheduler

Bases: Scheduler

Static in memory scheduler.

This scheduler does not implement the send method. This must be implemented before it can actually send tasks to the specified backend.

Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

Raises:

beatdrop.exceptions.MethodNotImplementedErrordelete method not implemented.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.MethodNotImplementedErrorget method not implemented.

list() ScheduleEntry

List schedule entries.

Returns:

List of schedule entries.

Return type:

Iterator[ScheduleEntry]

Raises:

beatdrop.exceptions.MethodNotImplementedErrorlist method not implemented.

max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

save(sched_entry: ScheduleEntry, client_read_only: bool = False) None

Save a new or update an existing schedule entry.

By default, read only attributes are not updated with save (suffixed with __).

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to add or update in scheduler.

  • client_read_only (bool) – Overwrite client read only fields? False will not overwrite client read only fields. True will. This should almost always be False, unless you are a scheduler, or you know what you’re doing.

Raises:

beatdrop.exceptions.MethodNotImplementedErrorsave method not implemented.

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the task backend.

This should be used by the run method when a schedule is due. Subclasses can override this for common schedulers without changing the specifics of how a schedule runs. Send it to a queue, to celery etc.

NOTE: for the reasons above the send method should not perform any actions against the state of the scheduler or schedule entries.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry that will be sent to the task backend.

Raises:

beatdrop.exceptions.MethodNotImplementedError – Must implement send method.

beatdrop.schedulers.redis_scheduler module

class beatdrop.schedulers.redis_scheduler.RedisScheduleEntryList

Bases: object

Iterator for RedisScheduler entries.

Parameters:
  • page_size (int) – Redis suggested minimum page size.

  • default_sched_entries (List[ScheduleEntry]) – Default schedule entries that will be iterated over first.

  • redis_conn (Redis) – Redis connection object.

  • hash_key (str) – Redis key of the hash that stores schedule entries.

  • entry_type_registry (EntryTypeRegistry) – Entry type registry for deserializing JSON models from redis.

__init__(page_size: int, default_sched_entries: List[ScheduleEntry], redis_conn: Redis, hash_key: str, entry_type_registry: EntryTypeRegistry)
class beatdrop.schedulers.redis_scheduler.RedisScheduler

Bases: SingletonLockScheduler

Hold schedule entries in a Redis.

Uses Redis to store schedule entries and scheduler state. It is safe to run multiple RedisScheduler s simultaneously, as well as have many that are used as clients to read/write entries.

This scheduler does not implement the send method. This must be implemented before it can actually send tasks to the specified backend.

Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

  • lock_timeout (datetime.timedelta) – The time a scheduler does not refresh the scheduler lock before it is considered dead. Should be at least 3 times the max_interval.

  • redis_py_kwargs (Dict[str, Any]) – redis-py’s redis.Redis() key word arguments. Some of the client configuration items may be overwritten. https://redis-py.readthedocs.io/en/stable/connections.html#generic-client

default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

This does not delete default entries.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.ScheduleEntryNotFound – The schedule entry could not be found.

list(page_size: int = 500) RedisScheduleEntryList

List schedule entries.

Parameters:

page_size (int, optional) – Redis suggested minimum page size, by default 500

Returns:

Iterator of all schedule entries. Automatically paginated redis results.

Return type:

RedisScheduleEntryList

lock_timeout: datetime.timedelta = FieldInfo(default=PydanticUndefined, extra={})
classmethod lock_timeout_3_times_interval(values: dict) dict
max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
redis_py_kwargs: Dict[str, Any] = FieldInfo(default=PydanticUndefined, extra={})
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

save(sched_entry: ScheduleEntry, read_only_attributes: bool = False) None

Save a new, or update an existing schedule entry in redis.

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to create or update.

  • read_only_attributes (bool, optional) – If true, read only attributes are also saved to the DB. Clients should almost always leave this false, by default False

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the task backend.

This should be used by the run method when a schedule is due. Subclasses can override this for common schedulers without changing the specifics of how a schedule runs. Send it to a queue, to celery etc.

NOTE: for the reasons above the send method should not perform any actions against the state of the scheduler or schedule entries.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry that will be sent to the task backend.

Raises:

beatdrop.exceptions.MethodNotImplementedError – Must implement send method.

beatdrop.schedulers.rq_redis_scheduler module

class beatdrop.schedulers.rq_redis_scheduler.Config

Bases: object

arbitrary_types_allowed = True
class beatdrop.schedulers.rq_redis_scheduler.RQRedisScheduler

Bases: RedisScheduler, RQScheduler

Hold schedule entries in Redis, and send to RQ (Redis Queue) task queues.

Uses Redis to store schedule entries and scheduler state. It is safe to run multiple RQRedisScheduler s simultaneously, as well as have many that are used as clients to read/write entries.

Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

  • lock_timeout (datetime.timedelta) – The time a scheduler does not refresh the scheduler lock before it is considered dead. Should be at least 3 times the max_interval.

  • redis_py_kwargs (Dict[str, Any]) – redis-py’s redis.Redis() key word arguments. Some of the client configuration items may be overwritten. https://redis-py.readthedocs.io/en/stable/connections.html#generic-client

  • rq_queue (rq.Queue) – RQ Queue to send tasks to.

Example

from rq import Queue
from beatdrop import RQRedisScheduler

rq_queue = Queue()
sched = RQRedisScheduler(
    max_interval=60,
    rq_queue=rq_queue,
    lock_timeout=180,
    redis_py_kwargs={
        "host": "my.redis.host",
        "port": 6379,
        "db": 0,
        "password": "mys3cr3t"
    }
)
# use the scheduler as a client
entry_list = sched.list()
for entry in entry_list:
    print(entry.key)

# or run it
sched.run()
default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

This does not delete default entries.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.ScheduleEntryNotFound – The schedule entry could not be found.

list(page_size: int = 500) RedisScheduleEntryList

List schedule entries.

Parameters:

page_size (int, optional) – Redis suggested minimum page size, by default 500

Returns:

Iterator of all schedule entries. Automatically paginated redis results.

Return type:

RedisScheduleEntryList

lock_timeout: datetime.timedelta = FieldInfo(default=PydanticUndefined, extra={})
classmethod lock_timeout_3_times_interval(values: dict) dict
max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
redis_py_kwargs: Dict[str, Any] = FieldInfo(default=PydanticUndefined, extra={})
rq_queue: rq.Queue = FieldInfo(default=PydanticUndefined, extra={})
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

save(sched_entry: ScheduleEntry, read_only_attributes: bool = False) None

Save a new, or update an existing schedule entry in redis.

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to create or update.

  • read_only_attributes (bool, optional) – If true, read only attributes are also saved to the DB. Clients should almost always leave this false, by default False

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the RQ queue.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry to send to the RQ queue.

beatdrop.schedulers.rq_scheduler module

class beatdrop.schedulers.rq_scheduler.Config

Bases: object

arbitrary_types_allowed = True
class beatdrop.schedulers.rq_scheduler.RQScheduler

Bases: Scheduler

Implementation for sending RQ (Redis Queue) tasks.

Combine as a second base class to be able to send tasks to RQ queues.

Example:

from beatdrop.schedulers import RQScheduler, SQLScheduler

class RQSQLScheduler(SQLScheduler, RQScheduler):
    pass
Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

  • rq_queue (rq.Queue) – RQ Queue to send tasks to.

default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

Raises:

beatdrop.exceptions.MethodNotImplementedErrordelete method not implemented.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.MethodNotImplementedErrorget method not implemented.

list() Iterator[ScheduleEntry]

List schedule entries.

Returns:

List of schedule entries.

Return type:

Iterator[ScheduleEntry]

Raises:

beatdrop.exceptions.MethodNotImplementedErrorlist method not implemented.

max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
rq_queue: Queue = FieldInfo(default=PydanticUndefined, extra={})
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

Raises:

beatdrop.exceptions.MethodNotImplementedError – Must implement run method.

save(sched_entry: ScheduleEntry, client_read_only: bool = False) None

Save a new or update an existing schedule entry.

By default, read only attributes are not updated with save (suffixed with __).

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to add or update in scheduler.

  • client_read_only (bool) – Overwrite client read only fields? False will not overwrite client read only fields. True will. This should almost always be False, unless you are a scheduler, or you know what you’re doing.

Raises:

beatdrop.exceptions.MethodNotImplementedErrorsave method not implemented.

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the RQ queue.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry to send to the RQ queue.

beatdrop.schedulers.rq_sql_scheduler module

class beatdrop.schedulers.rq_sql_scheduler.Config

Bases: object

arbitrary_types_allowed = True
class beatdrop.schedulers.rq_sql_scheduler.RQSQLScheduler

Bases: SQLScheduler, RQScheduler

Hold schedule entries in an SQL database, and send to RQ (Redis Queue) task queues.

Uses an SQL database to store schedule entries and scheduler state. It is safe to run multiple RQSQLScheduler s simultaneously, as well as have many that are purely used as clients to read/write entries.

NOTE - You must also install the DB driver specified in the URL for create_engine_kwargs.

NOTE - Before running the scheduler for the first time the DB tables must be created using create_tables().

Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

  • lock_timeout (datetime.timedelta) – The time a scheduler does not refresh the scheduler lock before it is considered dead. Should be at least 3 times the max_interval.

  • create_engine_kwargs (dict) – Keyword arguments to pass to sqlalchemy.create_engine. See SQLAlchemy docs for more info. https://docs.sqlalchemy.org/en/14/core/engines.html#sqlalchemy.create_engine

  • rq_queue (rq.Queue) – RQ Queue to send tasks to.

Example

from rq import Queue
from beatdrop import RQSQLScheduler

rq_queue = Queue()
sched = RQSQLScheduler(
    max_interval=60,
    rq_queue=rq_queue,
    lock_timeout=180,
    create_engine_kwargs={
        "url": "sqlite:///my_sqlite.db"
    }
)
# use the scheduler as a client
entry_list = sched.list()
for entry in entry_list:
    print(entry.key)

# or run it
sched.run()
create_engine_kwargs: Dict[str, Any] = FieldInfo(default=PydanticUndefined, extra={})
create_tables() None

Create DB tables for the schedule entries.

default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

This does not delete default entries.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.ScheduleEntryNotFound – The schedule entry could not be found.

list(page_size: int = 500) SQLScheduleEntryList

List schedule entries.

Parameters:

page_size (int, optional) – DB page size, by default 500

Returns:

Iterator of all schedule entries. Automatically paginated DB results.

Return type:

SQLScheduleEntryList

lock_timeout: datetime.timedelta = FieldInfo(default=PydanticUndefined, extra={})
classmethod lock_timeout_3_times_interval(values: dict) dict
max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
rq_queue: rq.Queue = FieldInfo(default=PydanticUndefined, extra={})
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

save(sched_entry: ScheduleEntry, read_only_attributes: bool = False) None

Save a new, or update an existing schedule entry in the DB.

If read_only_attributes is set to False, sched_entry’s read only attributes will be set to what’s in the DB.

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to create or update.

  • read_only_attributes (bool, optional) – If true, read only attributes are also saved to the DB. Clients should almost always leave this false, by default False

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the RQ queue.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry to send to the RQ queue.

classmethod url_in_kwargs(v: dict) dict

beatdrop.schedulers.scheduler module

class beatdrop.schedulers.scheduler.Scheduler

Bases: object

Base Scheduler class.

All runnable schedulers must implement these methods:

  • run - Run the scheduler.

  • send - Send a schedule entry to the task system.

All schedulers should implement these methods :

  • list - List schedule entries.

  • get - Get a schedule entry.

  • save - Save a new or update an existing schedule entry.

  • delete - Delete a schedule entry.

See the docs on the methods for more information.

Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

Raises:

beatdrop.exceptions.MethodNotImplementedErrordelete method not implemented.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.MethodNotImplementedErrorget method not implemented.

list() Iterator[ScheduleEntry]

List schedule entries.

Returns:

List of schedule entries.

Return type:

Iterator[ScheduleEntry]

Raises:

beatdrop.exceptions.MethodNotImplementedErrorlist method not implemented.

max_interval: timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

Raises:

beatdrop.exceptions.MethodNotImplementedError – Must implement run method.

save(sched_entry: ScheduleEntry, client_read_only: bool = False) None

Save a new or update an existing schedule entry.

By default, read only attributes are not updated with save (suffixed with __).

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to add or update in scheduler.

  • client_read_only (bool) – Overwrite client read only fields? False will not overwrite client read only fields. True will. This should almost always be False, unless you are a scheduler, or you know what you’re doing.

Raises:

beatdrop.exceptions.MethodNotImplementedErrorsave method not implemented.

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the task backend.

This should be used by the run method when a schedule is due. Subclasses can override this for common schedulers without changing the specifics of how a schedule runs. Send it to a queue, to celery etc.

NOTE: for the reasons above the send method should not perform any actions against the state of the scheduler or schedule entries.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry that will be sent to the task backend.

Raises:

beatdrop.exceptions.MethodNotImplementedError – Must implement send method.

beatdrop.schedulers.singleton_lock_scheduler module

class beatdrop.schedulers.singleton_lock_scheduler.SingletonLockScheduler

Bases: Scheduler

Base singleton lock scheduler class.

This base class is used by scheduler where more that one can be started in parallel with run, but only one should be checking entries and sending them. Uses a lock based approach to stop/tell other schedulers from running and sending tasks.

Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

  • lock_timeout (datetime.timedelta) – The time a scheduler does not refresh the scheduler lock before it is considered dead. Should be at least 3 times the max_interval.

default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

Raises:

beatdrop.exceptions.MethodNotImplementedErrordelete method not implemented.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.MethodNotImplementedErrorget method not implemented.

list() Iterator[ScheduleEntry]

List schedule entries.

Returns:

List of schedule entries.

Return type:

Iterator[ScheduleEntry]

Raises:

beatdrop.exceptions.MethodNotImplementedErrorlist method not implemented.

lock_timeout: timedelta = FieldInfo(default=PydanticUndefined, extra={})
classmethod lock_timeout_3_times_interval(values: dict) dict
max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

Raises:

beatdrop.exceptions.MethodNotImplementedError – Must implement run method.

save(sched_entry: ScheduleEntry, client_read_only: bool = False) None

Save a new or update an existing schedule entry.

By default, read only attributes are not updated with save (suffixed with __).

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to add or update in scheduler.

  • client_read_only (bool) – Overwrite client read only fields? False will not overwrite client read only fields. True will. This should almost always be False, unless you are a scheduler, or you know what you’re doing.

Raises:

beatdrop.exceptions.MethodNotImplementedErrorsave method not implemented.

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the task backend.

This should be used by the run method when a schedule is due. Subclasses can override this for common schedulers without changing the specifics of how a schedule runs. Send it to a queue, to celery etc.

NOTE: for the reasons above the send method should not perform any actions against the state of the scheduler or schedule entries.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry that will be sent to the task backend.

Raises:

beatdrop.exceptions.MethodNotImplementedError – Must implement send method.

beatdrop.schedulers.sql_scheduler module

class beatdrop.schedulers.sql_scheduler.SQLScheduleEntry

Bases: Base

Table to hold schedule entries in an SQL DB.

  • key_ holds the scheduler entry key.

  • json_ holds the serialized JSON for the scheduler entry.

__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

json_
key_
key_id
class beatdrop.schedulers.sql_scheduler.SQLScheduleEntryList

Bases: object

Iterator for SQLSchedule entries.

Parameters:
  • page_size (int) – Page size for DB pagination

  • default_sched_entries (List[ScheduleEntry]) – Default schedule entries that will be iterated over first.

  • session_maker (sessionmaker) – SQLAlchemy Session maker to query DB.

  • entry_type_registry (EntryTypeRegistry) – Entry type registry for deserializing JSON models from the DB.

__init__(page_size: int, default_sched_entries: List[ScheduleEntry], session_maker: sessionmaker, entry_type_registry: EntryTypeRegistry)
class beatdrop.schedulers.sql_scheduler.SQLSchedulerLock

Bases: Base

Scheduler lock table.

In order to strive for only one scheduler actively sending tasks, there is a separate table that will help to manage this. This table only has one column last_refreshed_at which is a datetime in UTC.

This table should only ever have 0 or 1 row.

The scheduler lock is managed by using db FOR UPDATE queries and the datetime value of last_refreshed_at.

__init__(**kwargs)

A simple constructor that allows initialization from kwargs.

Sets attributes on the constructed instance using the names and values in kwargs.

Only keys that are present as attributes of the instance’s class are allowed. These could be, for example, any mapped columns or relationships.

last_refreshed_at
class beatdrop.schedulers.sql_scheduler.SQLScheduler

Bases: SingletonLockScheduler

Hold schedule entries in an SQL database.

Uses an SQL database to store schedule entries and scheduler state. It is safe to run multiple SQLScheduler s simultaneously, as well as have many that are purely used as clients to read/write entries.

This scheduler does not implement the send method. This must be implemented before it can actually send tasks to the specified backend.

Parameters:
  • max_interval (datetime.timedelta) – The maximum interval that the scheduler should sleep before waking up to check for due tasks.

  • sched_entry_types (Tuple[Type[ScheduleEntry]], default : (CrontabEntry, CrontabTZEntry, EventEntry, IntervalEntry)) – A list of valid schedule entry types for this scheduler. These are only stored in the scheduler, not externally.

  • default_sched_entries (List[ScheduleEntry], default : []) – Default list of schedule entries. In general these entries are not held in non-volatile storage so any metadata they hold will be lost if the scheduler fails. These entries are static. The keys cannot be overwritten or deleted.

  • lock_timeout (datetime.timedelta) – The time a scheduler does not refresh the scheduler lock before it is considered dead. Should be at least 3 times the max_interval.

  • create_engine_kwargs (dict) – Keyword arguments to pass to sqlalchemy.create_engine. See SQLAlchemy docs for more info. https://docs.sqlalchemy.org/en/14/core/engines.html#sqlalchemy.create_engine

create_engine_kwargs: Dict[str, Any] = FieldInfo(default=PydanticUndefined, extra={})
create_tables() None

Create DB tables for the schedule entries.

default_sched_entries: List[ScheduleEntry] | None = FieldInfo(default=[], extra={})
delete(sched_entry: ScheduleEntry) None

Delete a schedule entry from the scheduler.

This does not delete default entries.

Parameters:

sched_entry (ScheduleEntry) – Scheduler entry to delete from the scheduler.

get(key: str) ScheduleEntry

Retrieve a schedule entry by its key.

Parameters:

key (str) – The schedule entry key.

Returns:

The schedule entry with the matching key.

Return type:

ScheduleEntry

Raises:

beatdrop.exceptions.ScheduleEntryNotFound – The schedule entry could not be found.

list(page_size: int = 500) SQLScheduleEntryList

List schedule entries.

Parameters:

page_size (int, optional) – DB page size, by default 500

Returns:

Iterator of all schedule entries. Automatically paginated DB results.

Return type:

SQLScheduleEntryList

lock_timeout: datetime.timedelta = FieldInfo(default=PydanticUndefined, extra={})
classmethod lock_timeout_3_times_interval(values: dict) dict
max_interval: datetime.timedelta
classmethod max_interval_gte_one(max_interval: timedelta) timedelta
run(max_iterations: int = None) None

Run the scheduler.

Parameters:

max_iterations (int) –

default : None

The maximum number of iterations to run the scheduler. None is unlimited.

save(sched_entry: ScheduleEntry, read_only_attributes: bool = False) None

Save a new, or update an existing schedule entry in the DB.

If read_only_attributes is set to False, sched_entry’s read only attributes will be set to what’s in the DB.

Parameters:
  • sched_entry (ScheduleEntry) – Schedule entry to create or update.

  • read_only_attributes (bool, optional) – If true, read only attributes are also saved to the DB. Clients should almost always leave this false, by default False

sched_entry_types: Tuple[Type[ScheduleEntry]] = FieldInfo(default=(<class 'beatdrop.entries.crontab_entry.CrontabEntry'>, <class 'beatdrop.entries.crontab_tz_entry.CrontabTZEntry'>, <class 'beatdrop.entries.event_entry.EventEntry'>, <class 'beatdrop.entries.interval_entry.IntervalEntry'>), extra={})
send(sched_entry: ScheduleEntry) None

Send a schedule entry to the task backend.

This should be used by the run method when a schedule is due. Subclasses can override this for common schedulers without changing the specifics of how a schedule runs. Send it to a queue, to celery etc.

NOTE: for the reasons above the send method should not perform any actions against the state of the scheduler or schedule entries.

Parameters:

sched_entry (ScheduleEntry) – Schedule entry that will be sent to the task backend.

Raises:

beatdrop.exceptions.MethodNotImplementedError – Must implement send method.

classmethod url_in_kwargs(v: dict) dict