让 Action Scheduler 能够在几秒钟内完成 运行?
Make Action Scheduler to be able to run in seconds?
我想将 Action Scheduler 设置为每 30 秒 运行,但它只有 1 分钟作为其间隔的最低可能选择。
我怎样才能 customize/change 它可以 运行 秒?
这就是我最终要做的,我需要测试几天才能确认它是否完成工作。
继承ir.cron
模型添加seconds
选择。
将 seconds
添加到 _intervalTypes
以使其与 seconds
选择兼容。
将原来的_process_job
复制到这里替换,这样修改后的_intervalTypes
和seconds
属性就存在了[=19] =]
安装了这个自定义模块。
这是代码。
from odoo import models, fields, api
from datetime import datetime
from dateutil.relativedelta import relativedelta
import pytz
_intervalTypes = {
'days': lambda interval: relativedelta(days=interval),
'hours': lambda interval: relativedelta(hours=interval),
'weeks': lambda interval: relativedelta(days=7*interval),
'months': lambda interval: relativedelta(months=interval),
'minutes': lambda interval: relativedelta(minutes=interval),
'seconds': lambda interval: relativedelta(seconds=interval),
}
class IrCronInherit(models.Model):
_inherit = 'ir.cron'
interval_type = fields.Selection([
('seconds', 'Seconds'),
('minutes', 'Minutes'),
('hours', 'Hours'),
('days', 'Days'),
('weeks', 'Weeks'),
('months', 'Months')], string='Interval Unit', default='months')
@classmethod
def _process_job(cls, job_cr, job, cron_cr):
""" Run a given job taking care of the repetition.
:param job_cr: cursor to use to execute the job, safe to commit/rollback
:param job: job to be run (as a dictionary).
:param cron_cr: cursor holding lock on the cron job row, to use to update the next exec date,
must not be committed/rolled back!
"""
with api.Environment.manage():
try:
cron = api.Environment(job_cr, job['user_id'], {
'lastcall': fields.Datetime.from_string(job['lastcall'])
})[cls._name]
# Use the user's timezone to compare and compute datetimes,
# otherwise unexpected results may appear. For instance, adding
# 1 month in UTC to July 1st at midnight in GMT+2 gives July 30
# instead of August 1st!
now = fields.Datetime.context_timestamp(cron, datetime.now())
nextcall = fields.Datetime.context_timestamp(cron, fields.Datetime.from_string(job['nextcall']))
numbercall = job['numbercall']
ok = False
while nextcall < now and numbercall:
if numbercall > 0:
numbercall -= 1
if not ok or job['doall']:
cron._callback(job['cron_name'], job['ir_actions_server_id'], job['id'])
if numbercall:
nextcall += _intervalTypes[job['interval_type']](job['interval_number'])
ok = True
addsql = ''
if not numbercall:
addsql = ', active=False'
cron_cr.execute("UPDATE ir_cron SET nextcall=%s, numbercall=%s, lastcall=%s"+addsql+" WHERE id=%s",(
fields.Datetime.to_string(nextcall.astimezone(pytz.UTC)),
numbercall,
fields.Datetime.to_string(now.astimezone(pytz.UTC)),
job['id']
))
cron.flush()
cron.invalidate_cache()
finally:
job_cr.commit()
cron_cr.commit()
现在,当我实际将它设置为 30 秒时,它运行不到一分钟,但不是一直运行在 30 秒,但它现在可以。
我想将 Action Scheduler 设置为每 30 秒 运行,但它只有 1 分钟作为其间隔的最低可能选择。
我怎样才能 customize/change 它可以 运行 秒?
这就是我最终要做的,我需要测试几天才能确认它是否完成工作。
继承
ir.cron
模型添加seconds
选择。将
seconds
添加到_intervalTypes
以使其与seconds
选择兼容。将原来的
_process_job
复制到这里替换,这样修改后的_intervalTypes
和seconds
属性就存在了[=19] =]安装了这个自定义模块。
这是代码。
from odoo import models, fields, api
from datetime import datetime
from dateutil.relativedelta import relativedelta
import pytz
_intervalTypes = {
'days': lambda interval: relativedelta(days=interval),
'hours': lambda interval: relativedelta(hours=interval),
'weeks': lambda interval: relativedelta(days=7*interval),
'months': lambda interval: relativedelta(months=interval),
'minutes': lambda interval: relativedelta(minutes=interval),
'seconds': lambda interval: relativedelta(seconds=interval),
}
class IrCronInherit(models.Model):
_inherit = 'ir.cron'
interval_type = fields.Selection([
('seconds', 'Seconds'),
('minutes', 'Minutes'),
('hours', 'Hours'),
('days', 'Days'),
('weeks', 'Weeks'),
('months', 'Months')], string='Interval Unit', default='months')
@classmethod
def _process_job(cls, job_cr, job, cron_cr):
""" Run a given job taking care of the repetition.
:param job_cr: cursor to use to execute the job, safe to commit/rollback
:param job: job to be run (as a dictionary).
:param cron_cr: cursor holding lock on the cron job row, to use to update the next exec date,
must not be committed/rolled back!
"""
with api.Environment.manage():
try:
cron = api.Environment(job_cr, job['user_id'], {
'lastcall': fields.Datetime.from_string(job['lastcall'])
})[cls._name]
# Use the user's timezone to compare and compute datetimes,
# otherwise unexpected results may appear. For instance, adding
# 1 month in UTC to July 1st at midnight in GMT+2 gives July 30
# instead of August 1st!
now = fields.Datetime.context_timestamp(cron, datetime.now())
nextcall = fields.Datetime.context_timestamp(cron, fields.Datetime.from_string(job['nextcall']))
numbercall = job['numbercall']
ok = False
while nextcall < now and numbercall:
if numbercall > 0:
numbercall -= 1
if not ok or job['doall']:
cron._callback(job['cron_name'], job['ir_actions_server_id'], job['id'])
if numbercall:
nextcall += _intervalTypes[job['interval_type']](job['interval_number'])
ok = True
addsql = ''
if not numbercall:
addsql = ', active=False'
cron_cr.execute("UPDATE ir_cron SET nextcall=%s, numbercall=%s, lastcall=%s"+addsql+" WHERE id=%s",(
fields.Datetime.to_string(nextcall.astimezone(pytz.UTC)),
numbercall,
fields.Datetime.to_string(now.astimezone(pytz.UTC)),
job['id']
))
cron.flush()
cron.invalidate_cache()
finally:
job_cr.commit()
cron_cr.commit()
现在,当我实际将它设置为 30 秒时,它运行不到一分钟,但不是一直运行在 30 秒,但它现在可以。