changeset 76eb7d9901f8 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=76eb7d9901f8
description:
Remove global variables in cron startup
It is better to follow the same startup design than other service.
issue8200
review259131002
diffstat:
bin/trytond-cron | 5 +----
trytond/cron.py | 51 ++++++++++++++++++++++-----------------------------
trytond/ir/cron.py | 3 +++
3 files changed, 26 insertions(+), 33 deletions(-)
diffs (109 lines):
diff -r d1851fde50bc -r 76eb7d9901f8 bin/trytond-cron
--- a/bin/trytond-cron Fri Mar 22 12:16:36 2019 +0100
+++ b/bin/trytond-cron Fri Mar 22 12:17:50 2019 +0100
@@ -3,7 +3,6 @@
# this repository contains the full copyright notices and license terms.
import sys
import os
-import time
DIR = os.path.abspath(os.path.normpath(os.path.join(__file__,
'..', '..', 'trytond')))
@@ -22,6 +21,4 @@
import trytond.cron as cron
with commandline.pidfile(options):
- while True:
- cron.run(options)
- time.sleep(60)
+ cron.run(options)
diff -r d1851fde50bc -r 76eb7d9901f8 trytond/cron.py
--- a/trytond/cron.py Fri Mar 22 12:16:36 2019 +0100
+++ b/trytond/cron.py Fri Mar 22 12:17:50 2019 +0100
@@ -1,6 +1,7 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import threading
+import time
import logging
from trytond.pool import Pool
@@ -11,33 +12,25 @@
def run(options):
- database_list = Pool.database_list()
- for dbname in options.database_names:
- thread = _threads.get(dbname)
- if thread and thread.is_alive():
- logger.info('skip "%s" as previous cron still running', dbname)
- continue
- pool = Pool(dbname)
- if dbname not in database_list:
- with Transaction().start(dbname, 0, readonly=True):
- pool.init()
- if not pool.lock.acquire(0):
- logger.warning('can not acquire lock on "%s"', dbname)
- continue
- try:
- try:
- Cron = pool.get('ir.cron')
- except KeyError:
- logger.error(
- 'missing "ir.cron" on "%s"', dbname, exc_info=True)
+ for db_name in options.database_names:
+ pool = Pool(db_name)
+ with Transaction().start(db_name, 0, readonly=True):
+ pool.init()
+
+ threads = {}
+ while True:
+ for db_name in options.database_names:
+ thread = threads.get(db_name)
+ if thread and thread.is_alive():
+ logger.info(
+ 'skip "%s" as previous cron still running', db_name)
continue
- finally:
- pool.lock.release()
- thread = threading.Thread(
- target=Cron.run,
- args=(dbname,), kwargs={})
- logger.info('start thread for "%s"', dbname)
- thread.start()
- _threads[dbname] = thread
-_threads = {}
-Pool.start()
+ pool = Pool(db_name)
+ Cron = pool.get('ir.cron')
+ thread = threading.Thread(
+ target=Cron.run,
+ args=(db_name,), kwargs={})
+ logger.info('start thread for "%s"', db_name)
+ thread.start()
+ threads[db_name] = thread
+ time.sleep(60)
diff -r d1851fde50bc -r 76eb7d9901f8 trytond/ir/cron.py
--- a/trytond/ir/cron.py Fri Mar 22 12:16:36 2019 +0100
+++ b/trytond/ir/cron.py Fri Mar 22 12:17:50 2019 +0100
@@ -151,6 +151,7 @@
@classmethod
def run(cls, db_name):
+ logger.info('cron started for "%s"', db_name)
now = datetime.datetime.now()
with Transaction().start(db_name, 0) as transaction:
transaction.database.lock(transaction.connection, cls._table)
@@ -160,6 +161,7 @@
])
for cron in crons:
+ logger.info("Run cron %s", cron.id)
try:
next_call = cron.next_call
number_calls = cron.number_calls
@@ -188,3 +190,4 @@
while transaction.tasks:
task_id = transaction.tasks.pop()
run_task(db_name, task_id)
+ logger.info('cron finished for "%s"', db_name)