add cronjob + test

This commit is contained in:
Frederik Jaeckel 2024-03-01 07:52:09 +01:00
parent 0fc0c4c8b9
commit 95f06e8e26
2 changed files with 72 additions and 28 deletions

View file

@ -3,7 +3,7 @@
# The COPYRIGHT file at the top level of this repository contains the
# full copyright notices and license terms.
from datetime import date
from datetime import date, timedelta
from dateutil.rrule import (
rrule, YEARLY, MONTHLY, WEEKLY, DAILY, MO, TU, WE, TH, FR, SA, SU)
from trytond.model import ModelSQL, ModelView, fields, Index, DeactivableMixin
@ -281,18 +281,25 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
return IrDate.today()
@classmethod
def update_next_occurence(cls, records):
def update_next_occurence(cls, records, query_date=None):
""" compute date of next execution, create/update nextrun-record,
delete nextrun-record if scheduled booking is disabled
Args:
records (list): scheduled-booking records
query_date (date): set date to compute next run,
defaults to 'today+1'
"""
pool = Pool()
IrDate = pool.get('ir.date')
NextRun = pool.get('cashbook.planner.nextrun')
context = Transaction().context
if not query_date:
query_date = context.get(
'nextrun_querydate',
IrDate.today() + timedelta(days=1))
to_create = []
to_write = []
to_delete = []
@ -304,9 +311,7 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
elif record.active:
# get next-run date
next_date = record._compute_dates_by_rrule(
query_date=context.get(
'nextrun_querydate', IrDate.today()),
count=1)
query_date=query_date, count=1)
if next_date:
next_date = next_date[0]
else:
@ -354,8 +359,28 @@ class ScheduledBooking(DeactivableMixin, ModelSQL, ModelView):
super(ScheduledBooking, cls).write(*args)
cls.update_next_occurence(records)
def run_booking(self):
""" do prepared booking
"""
print('-- booking:', self.rec_name)
@classmethod
def cronjob(cls):
pass
""" run planned booking for due jobs, re-schedule for next runs
"""
IrDate = Pool().get('ir.date')
context = Transaction().context
query_date = context.get('nextrun_crondate', IrDate.today())
records = cls.search([
('active', '=', True),
('nextrun.date', '<=', query_date)])
for record in records:
record.run_booking()
cls.update_next_occurence(
records,
query_date=query_date + timedelta(days=1))
# ens ScheduledBooking