From a3f0f5e2fd2811033e672dec0948c02f69635c95 Mon Sep 17 00:00:00 2001 From: Laurent LAPORTE Date: Thu, 26 Oct 2023 19:14:51 +0200 Subject: [PATCH] fix(task-view): do not use the `timezone.utc` timezone to preserve a naive datetime --- antarest/core/tasks/service.py | 3 ++- antarest/launcher/service.py | 3 ++- tests/core/tasks/test_task_job_service.py | 29 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/core/tasks/test_task_job_service.py diff --git a/antarest/core/tasks/service.py b/antarest/core/tasks/service.py index 227b3fbf71..bfb4c7230a 100644 --- a/antarest/core/tasks/service.py +++ b/antarest/core/tasks/service.py @@ -412,5 +412,6 @@ def _update_task_status( task.result_status = result task.result = command_result if status.is_final(): - task.completion_date = datetime.datetime.now(datetime.timezone.utc) + # Do not use the `timezone.utc` timezone to preserve a naive datetime. + task.completion_date = datetime.datetime.utcnow() self.repo.save(task) diff --git a/antarest/launcher/service.py b/antarest/launcher/service.py index 7312181dea..7e3b462008 100644 --- a/antarest/launcher/service.py +++ b/antarest/launcher/service.py @@ -166,7 +166,8 @@ def update( job_result.output_id = output_id final_status = status in [JobStatus.SUCCESS, JobStatus.FAILED] if final_status: - job_result.completion_date = datetime.now(timezone.utc) + # Do not use the `timezone.utc` timezone to preserve a naive datetime. + job_result.completion_date = datetime.utcnow() self.job_result_repository.save(job_result) self.event_bus.push( Event( diff --git a/tests/core/tasks/test_task_job_service.py b/tests/core/tasks/test_task_job_service.py new file mode 100644 index 0000000000..40a463675a --- /dev/null +++ b/tests/core/tasks/test_task_job_service.py @@ -0,0 +1,29 @@ +import datetime + +from sqlalchemy.orm import Session # type: ignore + +from antarest.core.tasks.model import TaskJob + + +def test_database_date_utc(db_session: Session) -> None: + now = datetime.datetime.utcnow() + later = now + datetime.timedelta(seconds=1) + + with db_session: + task_job = TaskJob(name="foo") + db_session.add(task_job) + db_session.commit() + + with db_session: + task_job = db_session.query(TaskJob).filter(TaskJob.name == "foo").one() + assert now <= task_job.creation_date <= later + assert task_job.completion_date is None + + with db_session: + task_job = db_session.query(TaskJob).filter(TaskJob.name == "foo").one() + task_job.completion_date = datetime.datetime.utcnow() + db_session.commit() + + with db_session: + task_job = db_session.query(TaskJob).filter(TaskJob.name == "foo").one() + assert now <= task_job.creation_date <= task_job.completion_date <= later