API

class periodtask.TaskList(*args)[source]

Defines the tasks to run and starts the sceduler. Pass in Task instances to schedule.

start()[source]

Start The scheduler. This will block until SIGTERM or SIGINT received.

class periodtask.Task(name, command, periods='', run_on_start=False, mail_success=None, mail_failure=None, mail_skipped=None, mail_delayed=None, send_mail_func=None, wait_timeout=10, max_lines=50, stop_signal=<Signals.SIGTERM: 15>, policy=0, template_dir=[], stdout_logger=<logging.Logger object>, stdout_level=20, stderr_logger=<logging.Logger object>, stderr_level=20, cwd=None, email_limitation=True, failure_email_limitation=None)[source]

Represents a task to schedule.

Parameters:
  • name (str) – The name of the task, will apear in logs and emails.
  • command (tuple) – See args param of the Popen constructor.
  • periods (list/str) – A cron expression (str) or a list of them. See Cron format reference for more information. By default (when set to an empyt string) this will be equivalent to 0 */5 * * * * UTC.
  • run_on_start (bool) – Indicates weather the task should run when the scheduler starts no matter what was given in periods. Useful for manually testing the task.
  • mail_success (func/bool) –

    If set to a truthy value an email will be sent after the task run successfully. If this is a function, this function will be used to send out the email (if send_mail_func does not override it). The signature of the function is

    def send_mail(subject, message, html_message=None)
    
  • mail_failure (func/bool) – Controls emails sent when the task fails. Otherwise it is the same as mail_success.
  • mail_skipped (func/bool) – Controls emails sent when the task is skipped due to the defined policy.
  • mail_deleyed (func/bool) – Controls emails sent when the task is delayed due to the defined policy.
  • send_mail_func (func) – If set, this must be a function. This function will be used to send emails, no matter what was set in mail_… params.
  • wait_timeout (number) – After sending stop_signal to the task process, we wait this many seconds for the process to stop. If the timeout expires, we kill the process.
  • max_lines (int/tuple) –

    STDOUT and STDERR are collected from the task process. To avoid haevy memory usage we only store this many lines in memory. More precisely STDOUT head and tail, STDERR head and tail are list of lines. This parameter controls the maximum length of these lists.

    examples:

    parameter stdout stderr
    value head tail head tail
    2 2 2 2 2
    (2, 3) 2 2 3 3
    (10, (2,3)) 10 10 2 3
    ((1, 2), (3, 4)) 1 2 3 4
  • stop_signal (int) – This signal will be sent to the task process when we want to stop it gracefully.
  • policy (int) –

    Available values are periodtask.SKIP, periodtask.DELAY and periodtask.RUN.

    SKIP
    If a process is (still) running and the task is scheduled, this new process will be skipped. If requested, an email will be sent.
    DELAY
    If a process is (still) running and the task is scheduled, this new process will be delayed and will run immediatelly when the actual process terminates. If requested, an email will be sent.
    RUN
    Tasks will always run when scheduled.
  • template_dir (list/str) – Directories to look for email templates in.
  • stdout_logger (logging.logger) – The logger to use for the STDOUT of the task process.
  • stdout_level (int) – The STDOUT of the task process will be logged to this level.
  • stderr_logger (logging.logger) – The logger to use for the STDERR of the task process.
  • stderr_level (int) – The STDERR of the task process will be logged to this level.
  • cwd (str) –

    The task process will run with cwd as the working directory. See the Popen constructor.

  • email_limitation (bool) – If True, only one skip or delay email will be sent. When the blocking process terminates, an extra email will be sent using the mail_skipped or mail_delayed functions.
  • failure_email_limitation (bool) – To disable consecutive failure emails this param should be set to True. If left None, the value of this param is derived from email_limitation.