Skip to content

Commit

Permalink
fix(models): Don't log job execution timestamps until after job has b…
Browse files Browse the repository at this point in the history
…een submitted.
  • Loading branch information
jcass77 committed Jul 20, 2020
1 parent cc56c5a commit 63fcb88
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions django_apscheduler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ def atomic_update_or_create(
with lock:
# Convert all datetimes to internal Django format before doing calculations and persisting in the database.
run_time = get_django_internal_datetime(run_time)

finished = get_django_internal_datetime(timezone.now())
duration = (finished - run_time).total_seconds()
finished = finished.timestamp()

try:
with transaction.atomic():
job_execution = DjangoJobExecution.objects.select_for_update(
Expand All @@ -163,10 +168,6 @@ def atomic_update_or_create(
# See https://github.com/pytransitions/transitions
return job_execution

finished = get_django_internal_datetime(timezone.now())
duration = (finished - run_time).total_seconds()
finished = finished.timestamp()

job_execution.finished = finished
job_execution.duration = duration
job_execution.status = status
Expand All @@ -181,10 +182,17 @@ def atomic_update_or_create(

except DjangoJobExecution.DoesNotExist:
# Execution was not created by a 'submit' previously - do so now
if status == DjangoJobExecution.SENT:
# Don't log durations until after job has been submitted for execution
finished = None
duration = None

job_execution = DjangoJobExecution.objects.create(
job_id=job_id,
run_time=run_time,
status=status,
duration=duration,
finished=finished,
exception=exception,
traceback=traceback,
)
Expand Down

0 comments on commit 63fcb88

Please sign in to comment.