wiki:TracCronPlugin

Version 20 (modified by Thierry Bressure, 13 years ago) (diff)

--

Trac Cron Plugin

Notice: This plugin is unmaintained and available for adoption.

Description

This plugin add a scheduler inside Trac process that can handle any sort of task writing in python language. Main features are:

  • Task are plugin
  • Bundled out-of-the-box task
  • Scheduler are plugin
  • Bundled out-of-the-box scheduler
  • Listener mechanisme to be notified of task execution
  • Bundled out-of-the-box listener
  • Task execution history

The plugin provide an administration panel to help scheduling.

Administration panel of Trac Cron Plugin

Bugs/Feature Requests

Existing bugs and feature requests for TracCronPlugin are here.

If you have any issues, create a new ticket.

Download

Lastest release

You can use easy_install to download the latest egg into your python environment

easy_install TracCronPlugin

or download distributions files at pypi (egg, binary and source)

Latest source

Download the zipped source from [download:traccronplugin here].

Source

You can check out TracCronPlugin from here using Subversion, or browse the source with Trac.

Example

My company need to synchronize trac user with an external source of account. The job was made by an ETL but fine tuning of trac user was still done by a our modified version of Trac by clicking on a button on the admin panel that call a well designed API. So instead of manually clik on the button i searched a way to automate it. Unfortunately, i did not find any plugin for this and even if i could use a unix cron task or an windows AT command to plane the task, i thought it would be better done inside Trac process.

So if you have any function for entry point of a process (i mean trac related process) and you want to plane it as a job, Trac Cron Plugin is for you.

Simply create in a module (.py) a class that implement the ICronTask and put it inside plugins directory. Then you can either trick the trac.ini either comfortably use the dedicated admin panel.

How to write a task

You have to write python class that inherit of ICronTask which code is below:

class ICronTask(Interface):
    """
    Interface for component task
    """
    
    def wake_up(self, *args):
        """
        Call by the scheduler when the task need to be executed
        """
        raise NotImplementedError
    
    def getId(self):
        """
        Return the key to use in trac.ini to configure this task
        """
        raise NotImplementedError
    
    def getDescription(self):
        """
        Return the description of this task to be used in the admin panel.
        """
        raise NotImplementedError

Let's take a look at the heartbeat task packed with TracCronPlugin:

class HeartBeatTask(Component,ICronTask):
    """
    This is a simple task for testing purpose.
    It only write a trace in log a debug level
    """
    
    implements(ICronTask)
    
    def wake_up(self, *args):
        if len(args) > 0:
            for arg in args:
                self.env.log.debug("Heart beat: " + arg)
        else:
            self.env.log.debug("Heart beat: boom boom !!!")
    
    def getId(self):
        return "heart_beat"
    
    def getDescription(self):
        return self.__doc__

You need to implements the interface and to put the definition of the task inside the wake_up method

How to install the task

Since task are component you just have to put the class definition of your task in a python module in plugins directory. You can either create a .py file and put it in plugins directory or package the .py file into an eggs you'll leave in plugins directory.

TracCronPlugin will show up the task in it administration panel. Enjoy !

Configuration

The plugin can entirely be configured both witn trac.ini or administration panel. The section name of TracCronPlugin is traccron, here is a full sample

[traccron]
ticker_enabled = True
ticker_interval = 1
heart_beat.daily = 21h19, 21h20
heart_beat.daily.enabled = True
hear_beat_daily.arg = Good Morging
heart_beat.enabled = True
heart_beat.hourly = 17, 18
heart_beat.hourly.enabled = False
heart_beat.monthly = 15@21h15, 15@21h16
heart_beat.monthly.enabled = False
heart_beat.weekly = 4@21h28, 4@21h29
heart_beat.weekly.enabled = False

Global setting

ticker_enabled = True

This control the object called ticker which is the thread that launch tasks. If False, no ticket (no thread) is created so your Trac is merely like no cron is installed. This is the more global setting you can act on to enable or disable ALL task. Default value is True.

ticker_interval = 1

This key control the interval between each wake up of the ticker. The ticker thread periodically wake up to see if there is task to execute. Then the ticker thread go sleep for the amount of minutes specified by this key. You should not have modify this value except if you have system load issue. Default value is 1.

Task setting

For each task loaded by Trac Component manager, Trac Cron Plugin have those parameters. Let's look at hear beat task:

Enable/Disable a task

heart_beat.enabled = True

This is the second way to enable or disable a task. Since ticker_enabled is global and so all task will be affected, this key only affect one task. If False, wathever schedule the task have, no one will be trigged, so this is a way to disable a task while keep all the schedule in place for the time you will enable the task. Default is True.

Task scheduling

heart_beat.daily = 21h19, 21h20

This control the daily scheduler. As this name suggest, it trigger the task every day. You need to provide time you want the task to execute. You can give a comma separated list to trigger the task at multiple time everyday. Default is no value.

heart_beat.hourly = 17, 18

The goal of this scheduler is to trigger task every hours. You provode minute when you want the task to be executed. Accept comma sparated list of values. Default is no value.

heart_beat.monthly = 15@21h15, 15@21h16

This sceduler trigger task that need to be executed once a month. You provide the day in month and the hour when the task will be launched. The day is the index of the day starting at 1. Accept comma separated value. Default is no value

heart_beat.weekly = 4@21h28, 4@21h29

This sceduler trigger task that need to be executed once a week. You provide the day in week and the hour when the task will be launched. The day is the index of the day starting at 0 (Monday is 0). Accept comma separated value. Default is no value

Enable/Disable a schedule

Each schedule can individualy be enabled or disabled. This the configuration for daily scheduler:

heart_beat.daily.enabled = True

This enable or disable the trigger. If False, the scheduler will not trigger the task. Default is True.

Pass argument to a task

You can pass argument to the task in a per schedule basis. Here an example for daily schedule:

hear_beat_daily.arg = Good Morging

When the daily schedule will trigger the task, the value of the kay will be passed to the wake_up call. Parameters can be coma separated multiple value. Default is empty

Bundled Task

Beside the HearBeat task provided for testing purprose, TracCronPlugin come with following useful task.

sleeping_ticket

This task remind reporter about orphaned ticket and assiged user about sleeping ticket. Orphaned ticket is a ticket in new status since more than a given amount of day. An email notifcation is sent to the reporter in such a case. A sleeping ticket is a ticket assigned to an user, but the user either did not accepet it or did not touch the ticket(make comment for exemple) since more than a gievn amount of day. The assigned user is notified in such a case. The delay is an optional parameter associated with each schedule. Default value is 3 day.

Bundled Scheduler

Hourly scheduler

See task scheduling section.

Daily scheduler

See task scheduling section.

Weekly scheduler

See task scheduling section.

Monthly scheduler

See task scheduling section.

Bundled Listener

Email notification of task event

This listener notify by email about task execution. You can choose the number of event sent by email by setting the value of email_task_event.limit. When the listener has received at least the number of task execution,it will send the mail. Default is 1. The value of email_task_event.recipient must be filled otherwise no mail will be sent.

Here the configuration for this listener

email_task_event.enabled = True
email_task_event.limit = 1
email_task_event.recipient = admin

Here an sample of the email :

The above task has been run by Trac Cron Plugin

task[heart_beat]
started at 0 h 47
ended at 0 h 47
success

task[heart_beat]
started at 0 h 48
ended at 0 h 48
success

task[heart_beat]
started at 0 h 49
ended at 0 h 49
success

Recent Changes

17943 by rjollos on 2020-12-23 23:45:57
Drop latest code from PyPI
15468 by rjollos on 2016-04-14 00:01:45
Remove tag_svn_revision attribute

The attribute isn't supported in setuptools >= 10

12591 by t2y on 2013-02-04 08:50:52
added TracCronPlugin-0.2 version
(more)

Author/Contributors

Author: tbressure
Maintainer: tbressure
Contributors:

Attachments (2)

Download all attachments as: .zip