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