[[PageOutline(2-5,Contents,pullout)]] = Trac Cron Plugin = == Description == 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. [[Image(tcp_admin_panel.png, 640)]] == Bugs/Feature Requests == Existing bugs and feature requests for TracCronPlugin are [report:9?COMPONENT=TracCronPlugin here]. If you have any issues, create a [http://trac-hacks.org/newticket?component=TracCronPlugin&owner=tbressure new ticket]. == Download == Download the zipped source from [download:traccronplugin here]. == Source == You can check out TracCronPlugin from [http://trac-hacks.org/svn/traccronplugin here] using Subversion, or [source:traccronplugin 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 but comfortably use the dedicated admin panel. === How to write e task === You have to write python class that inherit of ICronTask which code is below: {{{ #!python class ICronTask(Interface): """ Interface for component task """ def wake_up(self): """ 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 cinfigure 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: {{{ #!python 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): 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 {{{ #!ini [traccron] ticker_enabled = True ticker_interval = 1 heart_beat.daily = 21h19, 21h20 heart_beat.daily.enabled = True 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 === {{{ #!ini 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 task. Default value is '''True'''. {{{ #!ini ticker_interval = 1 }}} This key control the interval between each wake up of the ticker. The ticket thread periodically wake up to see if there is task to execute. Then the ticket 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: {{{ #!ini 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'''. {{{ #!ini 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. {{{ #!ini heart_beat.daily.enabled = True }}} This enable or disable the daily trigger. If False, the scheduler will not trigger the task. Default is '''True'''. {{{ #!ini 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. {{{ #!ini heart_beat.hourly.enabled = False }}} This enable or disable the daily trigger. If False, the scheduler will not trigger the task. Default is '''True'''. {{{ #!ini 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 {{{ #!ini heart_beat.monthly.enabled = False }}} This enable or disable the daily trigger. If False, the scheduler will not trigger the task. Default is '''True'''. {{{ #!ini 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 {{{ #!ini heart_beat.weekly.enabled = False }}} This enable or disable the daily trigger. If False, the scheduler will not trigger the task. Default is '''True'''. == Recent Changes == [[ChangeLog(traccronplugin, 3)]] == Author/Contributors == '''Author:''' [wiki:tbressure] [[BR]] '''Maintainer:''' [wiki:tbressure] [[BR]] '''Contributors:'''