wasp_general.task.scheduler package¶
Submodules¶
wasp_general.task.scheduler.proto module¶
-
class
wasp_general.task.scheduler.proto.WRunningRecordRegistryProto[source]¶ Bases:
objectThis class describes a registry of running tasks. It executes a scheduler record (
WScheduleRecord), creates and store the related records (WScheduleRecord), and watches that these tasks are running
-
class
wasp_general.task.scheduler.proto.WScheduleRecord(task, policy=None, task_group_id=None, on_drop=None, on_wait=None)[source]¶ Bases:
objectThis class specifies how
WScheduleTaskshould run. It should be treated as scheduler record, that may not have execution time.WScheduleRecordhas a policy, that describes what scheduler should do if it can not run this task at the specified moment. This policy is a recommendation for a scheduler and a scheduler can omit it if (for example) a scheduler queue is full. In any case, if this task is dropped (skipped) or postponed (moved to a queue of waiting tasks) correlated callback is called. “on_drop” callback is called for skipped tasks (it invokes viaWScheduleRecord.task_dropped()method) and “on_wait” for postponed tasks (viaWScheduleRecord.task_postponed()method)note: It is important that tasks with the same id (task_group_id) have the same postpone policy. If they do not have the same policy, then exception may be raised. No pre-checks are made to resolve this, because of unpredictable logic of different tasks from different sources
-
class
PostponePolicy[source]¶ Bases:
enum.EnumSpecifies what should be with this task if a scheduler won’t be able to run it (like if the scheduler limit of running tasks is reached).
-
drop= 2¶
-
postpone_first= 3¶
-
postpone_last= 4¶
-
wait= 1¶
-
-
task_dropped()[source]¶ Call a “on_drop” callback. This method is executed by a scheduler when it skip this task
Returns: None
-
class
-
class
wasp_general.task.scheduler.proto.WScheduleTask(thread_join_timeout=None)[source]¶ Bases:
wasp_general.task.thread.WThreadTaskClass represent task that may run by a scheduler Every schedule task must be able:
- to be stopped at any time
- to return its status (running or stopped)
- to notify when task end (thread event)
note: derived classes must implement
WThreadTask.thread_started()andWThreadTask.thread_stopped()methods in order to be instantiableEach task instance has “unique” identifier
-
class
wasp_general.task.scheduler.proto.WSchedulerServiceProto[source]¶ Bases:
objectRepresent a scheduler. A core of wasp_general.task.scheduler module
-
update(task_source=None)[source]¶ Update task sources information about next start. Update information for the specified source or for all of them
Parameters: task_source – if it is specified - then update information for this source only This method implementation must be thread-safe as different threads (different task source, different registries) may modify scheduler internal state. :return:
-
-
class
wasp_general.task.scheduler.proto.WTaskSourceProto[source]¶ Bases:
objectPrototype for scheduler record generator.
WSchedulerServiceProtodoesn’t have scheduler as set of records. Instead, a service uses this class as scheduler records holder and checks if it is time to execute them.-
has_records()[source]¶ Return records that should be run at the moment.
Returns: tuple of WScheduleRecord (tuple with one element at least) or None
-
next_start()[source]¶ Return datetime when the next task should be executed.
Returns: datetime in UTC timezone
-
wasp_general.task.scheduler.scheduler module¶
-
class
wasp_general.task.scheduler.scheduler.WPostponedRecordRegistry(maximum_records=None)[source]¶ Bases:
objectRegistry for postponed records.
-
class
wasp_general.task.scheduler.scheduler.WRunningRecordRegistry(watchdog_cls=None, thread_name_suffix=None)[source]¶ Bases:
wasp_general.thread.WCriticalResource,wasp_general.task.scheduler.proto.WRunningRecordRegistryProto,wasp_general.task.thread.WPollingThreadTaskRegistry of started scheduled records. Has
WRunningRecordRegistry.cleanup_event()event that is set when any of running scheduled task stopped. This event starts process of internal clean up (descriptors that were created for the record - will be removed)-
exec(record)[source]¶ Start the given schedule record (no time checks are made by this method, task will be started as is)
Parameters: record – schedule record to start Returns: None
-
running_records()[source]¶ Return schedule records that are running at the moment
Returns: tuple of WScheduleRecord
-
-
class
wasp_general.task.scheduler.scheduler.WSchedulerService(maximum_running_records=None, running_record_registry=None, maximum_postponed_records=None, postponed_record_registry=None, thread_name_suffix=None)[source]¶ Bases:
wasp_general.thread.WCriticalResource,wasp_general.task.scheduler.proto.WSchedulerServiceProto,wasp_general.task.thread.WPollingThreadTaskMain scheduler service. This class unites different registries to present entire scheduler
-
add_task_source(task_source)[source]¶ Add tasks source
Parameters: task_source – task source to add Returns: None
-
maximum_postponed_records()[source]¶ Return number of tasks that are able to be postponed
Returns: int or None (for no limit)
-
maximum_running_records()[source]¶ Return number of tasks that are able to run simultaneously
Returns: int
-
records_status()[source]¶ Return number of running and postponed tasks
Returns: tuple of two ints (first - running tasks, second - postponed tasks)
-
running_records()[source]¶ Return scheduled tasks that are running at the moment
Returns: tuple of WScheduleRecord
-
-
class
wasp_general.task.scheduler.scheduler.WSchedulerWatchdog(record, registry)[source]¶ Bases:
wasp_general.thread.WCriticalResource,wasp_general.task.thread.WPollingThreadTaskClass that is looking for execution process of scheduled task. Each scheduled task has its own watchdog. Watchdog that will un-register stopped task from registry of running tasks
-
classmethod
create(record, registry)[source]¶ Core method for watchdog creation. Derived classes may redefine this method in order to change watchdog creation process
Parameters: - record – schedule record that is ready to be executed
- registry – registry that is created this watchdog and registry that must be notified of scheduled task stopping
Returns:
-
classmethod
-
class
wasp_general.task.scheduler.scheduler.WTaskSourceRegistry[source]¶ Bases:
objectRegistry of tasks sources. It works as a dynamic queue - every task source notify this registry when next task should be started. And this registry fetches those tasks that is about to start. Registry is able to return schedule records from different sources at one time.
-
check()[source]¶ Check if there are records that are ready to start and return them if there are any
Returns: tuple of WScheduleRecord or None (if there are no tasks to start)
-
wasp_general.task.scheduler.task_source module¶
-
class
wasp_general.task.scheduler.task_source.WCronLocalTZSchedule(start_datetime=None, minute=None, hour=None, day_of_month=None, day_of_week=None, month=None)[source]¶ Bases:
wasp_general.task.scheduler.task_source.WCronSchedule
-
class
wasp_general.task.scheduler.task_source.WCronSchedule(start_datetime=None, minute=None, hour=None, day_of_month=None, day_of_week=None, month=None)[source]¶ Bases:
object
-
class
wasp_general.task.scheduler.task_source.WCronScheduleRecord(cron_schedule, task, policy=None, task_group_id=None, on_drop=None, on_wait=None, omit_skipped=True)[source]¶
-
class
wasp_general.task.scheduler.task_source.WCronTaskSource(scheduler_service)[source]¶ Bases:
wasp_general.task.scheduler.task_source.WBasicTaskSource,wasp_general.thread.WCriticalResource
-
class
wasp_general.task.scheduler.task_source.WCronUTCSchedule(start_datetime=None, minute=None, hour=None, day_of_month=None, day_of_week=None, month=None)[source]¶ Bases:
wasp_general.task.scheduler.task_source.WCronSchedule