Skip to main content

TaskScheduler

class automation.TaskScheduler(sync_frequency_minutes=15, force_create_task_name=None, force_create_task_project=None)​

A TaskScheduler launches Tasks (or calls functions) according to a set of cron-like scheduled jobs.

Notice: time-zone is always UTC.

Create a Task scheduler service.

  • Parameters

    • sync_frequency_minutes (float) – Sync the scheduler configuration every X minutes. Allows changing the schedule at runtime by editing the Task’s configuration object.

    • force_create_task_name (Optional[str]) – Name to force-create the Task Scheduler service under, even if a main Task.init() already exists.

    • force_create_task_project (Optional[str]) – Project to force-create the Task Scheduler service under, even if a main Task.init() already exists.


add_task​

add_task(schedule_task_id=None, schedule_function=None, queue=None, name=None, target_project=None, minute=None, hour=None, day=None, weekdays=None, month=None, year=None, limit_execution_time=None, single_instance=False, recurring=True, execute_immediately=False, reuse_task=False, task_parameters=None, task_overrides=None)

Create a cron job-like scheduling for a pre-existing Task. It is recommended to give the schedule entry a descriptive, unique name; if not provided, one is randomly generated.

When timespec parameters are specified exclusively, they define the time between task launches (see the year and weekdays exceptions). When multiple timespec parameters are specified, the parameter representing the longest duration defines the time between task launches, and the shorter timespec parameters define specific times.

Examples:

Launch every 15 minutes:

add_task(schedule_task_id='1235', queue='default', minute=15)

Launch every 1 hour:

add_task(schedule_task_id='1235', queue='default', hour=1)

Launch every 1 hour at hour:30 minutes (i.e. 1:30, 2:30 etc.):

add_task(schedule_task_id='1235', queue='default', hour=1, minute=30)

Launch every day at 22:30 (10:30 pm):

add_task(schedule_task_id='1235', queue='default', minute=30, hour=22, day=1)

Launch every other day at 7:30 (7:30 am):

add_task(schedule_task_id='1235', queue='default', minute=30, hour=7, day=2)

Launch every Saturday at 8:30am (notice day=0):

add_task(schedule_task_id='1235', queue='default', minute=30, hour=8, day=0, weekdays=['saturday'])

Launch every 2 hours on the weekends Saturday/Sunday (notice day is not passed):

add_task(schedule_task_id='1235', queue='default', hour=2, weekdays=['saturday', 'sunday'])

Launch once a month at the 5th of each month:

add_task(schedule_task_id='1235', queue='default', month=1, day=5)

Launch once a year on March 4th:

add_task(schedule_task_id='1235', queue='default', year=1, month=3, day=4)
  • Parameters

    • schedule_task_id (Union[str, Task, None]) – ID of Task to be cloned and scheduled for execution.

    • schedule_function (Optional[Callable]) – A function to call instead of providing a Task ID to schedule. The function is called from the scheduler context (i.e. runs on the same machine as the scheduler). Mutually exclusive with schedule_task_id.

    • queue (Optional[str]) – Name or ID of the queue to put the Task into (i.e. schedule it on).

    • name (Optional[str]) – Name or description for the cron Task (should be unique if provided, otherwise randomly generated).

    • target_project (Optional[str]) – The project to put the cloned scheduled Task in.

    • minute (Optional[int]) – Time (in minutes) between task launches. If specified together with hour, day, month, and / or year, it defines the minute of the hour.

    • hour (Optional[int]) – Time (in hours) between task launches. If specified together with day, month, and / or year, it defines the hour of day.

    • day (Optional[int]) – Time (in days) between task executions. If specified together with month and / or year, it defines the day of month.

    • weekdays (Optional[List[str]]) – Days of week to launch task (accepted inputs: ‘monday’, ‘tuesday’, ‘wednesday’, ‘thursday’, ‘friday’, ‘saturday’, ‘sunday’).

    • month (Optional[int]) – Time (in months) between task launches. If specified with year, it defines a specific month.

    • year (Optional[int]) – Specific year if value >= current year. Time (in years) between task launches if value <= 100.

    • limit_execution_time (Optional[float]) – Limit the execution time (in hours) of the specific job.

    • single_instance (bool) – If True, do not launch the Task job if the previous instance is still running (skip until the next scheduled time period) (default False).

    • recurring (bool) – If False, launch the Task only once instead of repeating (default True).

    • execute_immediately (bool) – If True, schedule the Task for immediate execution, then continue recurring based on the timing schedule arguments (default False).

    • reuse_task (bool) – If True, re-enqueue the same Task (i.e. do not clone it) every time (default False).

    • task_parameters (Optional[dict]) – Configuration parameters for the executed Task, for example: {'Args/batch': '12'}. Not available when reuse_task is True.

    • task_overrides (Optional[dict]) – Change the Task’s definition, for example: {'script.version_num': None, 'script.branch': 'main'}. Not available when reuse_task is True.

  • Return type

    bool

  • Returns

    True if the job was successfully added to the scheduling list.


get_scheduled_tasks​

get_scheduled_tasks()

Return the current set of scheduled jobs.

  • Return type

    List[ScheduleJob]

  • Returns

    A list of ScheduleJob instances.


remove_task​

remove_task(task_id)

Remove a Task ID from the scheduled task list.

  • Parameters

    task_id (Union[str, Task, Callable]) – Task or Task ID to be removed.

  • Return type

    bool

  • Returns

    True if the Task ID was found in the scheduled jobs list and removed.


start​

start()

Start the TaskScheduler loop.

Notice: this method does not return.

  • Return type

    None


start_remotely​

start_remotely(queue='services')

Serialize the current scheduler state and re-launch this scheduler as a Task, enqueued on the given remote queue.

Notice: this method does not return.

  • Parameters

    queue (str) – The queue to enqueue the scheduler service on (default 'services').

  • Return type

    None