1 # -*- encoding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
23 from mx import DateTime
28 from osv import fields,osv
33 'work_days': lambda interval: DateTime.RelativeDateTime(days=interval),
34 'days': lambda interval: DateTime.RelativeDateTime(days=interval),
35 'hours': lambda interval: DateTime.RelativeDateTime(hours=interval),
36 'weeks': lambda interval: DateTime.RelativeDateTime(days=7*interval),
37 'months': lambda interval: DateTime.RelativeDateTime(months=interval),
38 'minutes': lambda interval: DateTime.RelativeDateTime(minutes=interval),
41 class ir_cron(osv.osv, netsvc.Agent):
44 'name': fields.char('Name', size=60, required=True),
45 'user_id': fields.many2one('res.users', 'User', required=True),
46 'active': fields.boolean('Active'),
47 'interval_number': fields.integer('Interval Number'),
48 'interval_type': fields.selection( [('minutes', 'Minutes'),
49 ('hours', 'Hours'), ('work_days','Work Days'), ('days', 'Days'),('weeks', 'Weeks'), ('months', 'Months')], 'Interval Unit'),
50 'numbercall': fields.integer('Number of Calls', help='Number of time the function is called,\na negative number indicates that the function will always be called'),
51 'doall' : fields.boolean('Repeat Missed'),
52 'nextcall' : fields.datetime('Next Call Date', required=True),
53 'model': fields.char('Object', size=64),
54 'function': fields.char('Function', size=64),
55 'args': fields.text('Arguments'),
56 'priority': fields.integer('Priority', help='0=Very Urgent\n10=Not urgent')
60 'nextcall' : lambda *a: time.strftime('%Y-%m-%d %H:%M:%S'),
61 'priority' : lambda *a: 5,
62 'user_id' : lambda obj,cr,uid,context: uid,
63 'interval_number' : lambda *a: 1,
64 'interval_type' : lambda *a: 'months',
65 'numbercall' : lambda *a: 1,
66 'active' : lambda *a: 1,
67 'doall' : lambda *a: 1
70 def _callback(self, cr, uid, model, func, args):
71 args = (args or []) and eval(args)
72 m=self.pool.get(model)
73 if m and hasattr(m, func):
77 def _poolJobs(self, db_name, check=False):
79 #FIXME: multidb. Solution: a l'instanciation d'une nouvelle connection bd (ds pooler) fo que j'instancie
80 # un nouveau pooljob avec comme parametre la bd
82 cr = pooler.get_db(db_name).cursor()
87 cr.execute('select * from ir_cron where numbercall<>0 and active and nextcall<=now() order by priority')
88 for job in cr.dictfetchall():
89 nextcall = DateTime.strptime(job['nextcall'], '%Y-%m-%d %H:%M:%S')
90 numbercall = job['numbercall']
93 while nextcall<now and numbercall:
96 if not ok or job['doall']:
97 self._callback(cr, job['user_id'], job['model'], job['function'], job['args'])
99 nextcall += _intervalTypes[job['interval_type']](job['interval_number'])
103 addsql = ', active=False'
104 cr.execute("update ir_cron set nextcall=%s, numbercall=%s"+addsql+" where id=%s", (nextcall.strftime('%Y-%m-%d %H:%M:%S'), numbercall, job['id']))
109 # Can be improved to do at the min(min(nextcalls), time()+next_wait)
110 # But is this an improvement ?
113 self.setAlarm(self._poolJobs, int(time.time())+next_wait, [db_name])
116 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: