Skip to content

Commit

Permalink
Merge pull request #45 from IT-CEREBRUM/fix-greg-task-replace
Browse files Browse the repository at this point in the history
Fix an issue where new greg tasks gets an old nbf
  • Loading branch information
fredrikhl authored and GitHub Enterprise committed Jan 30, 2023
2 parents 10ec59d + 510b9fe commit d3cd157
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
5 changes: 4 additions & 1 deletion Cerebrum/modules/greg/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from Cerebrum.modules.tasks import queue_handler
from Cerebrum.modules.tasks import task_models
from Cerebrum.utils import date as date_utils

from .datasource import parse_message

Expand Down Expand Up @@ -80,7 +81,7 @@ def get_tasks(event):
:rtype: generator
:returns:
zero or more :py:class:`Cerebrum.modules.task.task_models.Task`
zero or more :class:`Cerebrum.modules.task.task_models.Task`
objects
"""
try:
Expand Down Expand Up @@ -108,5 +109,7 @@ def get_tasks(event):
rk=event.method.routing_key,
ct=event.method.consumer_tag,
),
# Explicit defaults, to ensure we replace old values
attempts=0,
nbf=date_utils.now(),
)
23 changes: 14 additions & 9 deletions Cerebrum/modules/no/dfo/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from Cerebrum.modules.tasks import queue_handler
from Cerebrum.modules.tasks import task_models
from Cerebrum.modules.tasks import task_queue
from Cerebrum.utils.date import now
from Cerebrum.utils import date as date_utils
from .datasource import parse_message

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -64,6 +64,7 @@ def handle_task(self, db, task):
@classmethod
def create_manual_task(cls, reference, sub=None, nbf=None):
""" Create a manual task. """
now = date_utils.now()
payload = task_models.Payload(
fmt='dfo-event',
version=1,
Expand All @@ -72,9 +73,9 @@ def create_manual_task(cls, reference, sub=None, nbf=None):
queue=cls.queue,
sub=cls.manual_sub if sub is None else sub,
key=reference,
nbf=nbf,
nbf=nbf or now,
attempts=0,
reason='manual: on={when}'.format(when=now()),
reason='manual: on={when}'.format(when=now),
payload=payload,
)

Expand Down Expand Up @@ -108,18 +109,20 @@ def handle_task(self, db, task):
task_db.push_task(task_models.merge_tasks(task, old_task))

@classmethod
def create_manual_task(cls, reference):
def create_manual_task(cls, reference, sub=None, nbf=None):
""" Create a manual task. """
now = date_utils.now()
payload = task_models.Payload(
fmt='dfo-event',
version=1,
data={'id': reference, 'uri': 'dfo:stillinger'})
return task_models.Task(
queue=cls.queue,
sub=cls.manual_sub,
sub=sub or cls.manual_sub,
key=reference,
attempts=0,
reason='manual: on={when}'.format(when=now()),
nbf=nbf or now,
reason='manual: on={when}'.format(when=now),
payload=payload,
)

Expand All @@ -142,7 +145,9 @@ def get_tasks(event):
logger.error('Invalid event: %s', repr(event), exc_info=True)
return

is_delayed = fields['nbf'] and fields['nbf'] > now()
now = date_utils.now()

is_delayed = fields['nbf'] and fields['nbf'] > now
sub = None

if fields['uri'] == 'dfo:ansatte':
Expand All @@ -163,12 +168,12 @@ def get_tasks(event):
queue=queue,
sub=sub,
key=fields['id'],
nbf=fields['nbf'],
nbf=fields['nbf'] or now,
reason='event: ex={ex} rk={rk} tag={ct} on={when}'.format(
ex=event.method.exchange,
rk=event.method.routing_key,
ct=event.method.consumer_tag,
when=str(now()),
when=str(now),
),
attempts=0,
# NOTE: Rememeber to bump the version if *any* changes are made to this
Expand Down

0 comments on commit d3cd157

Please sign in to comment.