[FIX] correct stripping of arguments when _reexec() OpenERP
[odoo/odoo.git] / openerp / service / cron.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2011 OpenERP SA (<http://www.openerp.com>)
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 """ Cron jobs scheduling
23
24 Cron jobs are defined in the ir_cron table/model. This module deals with all
25 cron jobs, for all databases of a single OpenERP server instance.
26
27 """
28
29 import logging
30 import threading
31 import time
32 from datetime import datetime
33
34 import openerp
35
36 _logger = logging.getLogger(__name__)
37
38 SLEEP_INTERVAL = 60 # 1 min
39
40 def cron_runner(number):
41     while True:
42         time.sleep(SLEEP_INTERVAL + number) # Steve Reich timing style
43         registries = openerp.modules.registry.RegistryManager.registries
44         _logger.debug('cron%d polling for jobs', number)
45         for db_name, registry in registries.items():
46             while True and registry.ready:
47                 # acquired = openerp.addons.base.ir.ir_cron.ir_cron._acquire_job(db_name)
48                 # TODO why isnt openerp.addons.base defined ?
49                 import sys
50                 base = sys.modules['addons.base']
51                 acquired = base.ir.ir_cron.ir_cron._acquire_job(db_name)
52                 if not acquired:
53                     break
54
55 def start_service():
56     """ Start the above runner function in a daemon thread.
57
58     The thread is a typical daemon thread: it will never quit and must be
59     terminated when the main process exits - with no consequence (the processing
60     threads it spawns are not marked daemon).
61
62     """
63     
64     # Force call to strptime just before starting the cron thread
65     # to prevent time.strptime AttributeError within the thread.
66     # See: http://bugs.python.org/issue7980
67     datetime.strptime('2012-01-01', '%Y-%m-%d')
68
69     for i in range(openerp.tools.config['max_cron_threads']):
70         def target():
71             cron_runner(i)
72         t = threading.Thread(target=target, name="openerp.service.cron.cron%d" % i)
73         t.setDaemon(True)
74         t.start()
75         _logger.debug("cron%d started!" % i)
76
77 def stop_service():
78     pass
79
80 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: