|Version 12 (modified by tbressure, 5 years ago) (diff)|
Trac Cron Plugin
This plugin add a scheduler inside Trac process that can handle any sort of task writing in python language. The plugin provide an administration panel to help scheduling.
If you have any issues, create a new ticket.
Download the zipped source from here.
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 !
[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
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.
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.
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