Skip to content

Commit

Permalink
fix(tasks): resolve incorrect UTC timezone usage for task completion …
Browse files Browse the repository at this point in the history
…dates in list view (#1786)
  • Loading branch information
laurent-laporte-pro authored Nov 13, 2023
1 parent 20a49c7 commit 1f23998
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
3 changes: 2 additions & 1 deletion antarest/core/tasks/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
5 changes: 3 additions & 2 deletions antarest/launcher/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
import shutil
from datetime import datetime, timedelta, timezone
from datetime import datetime, timedelta
from http import HTTPStatus
from pathlib import Path
from typing import Dict, List, Optional, cast
Expand Down Expand Up @@ -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(
Expand Down
6 changes: 4 additions & 2 deletions antarest/matrixstore/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import tempfile
import zipfile
from abc import ABC, abstractmethod
from datetime import datetime, timezone
from datetime import datetime
from pathlib import Path
from typing import List, Optional, Sequence, Tuple, Union

Expand Down Expand Up @@ -154,11 +154,13 @@ def create(self, data: Union[List[List[MatrixData]], npt.NDArray[np.float64]]) -
matrix_id = self.matrix_content_repository.save(data)
shape = data.shape if isinstance(data, np.ndarray) else (len(data), len(data[0]) if data else 0)
with db():
# Do not use the `timezone.utc` timezone to preserve a naive datetime.
created_at = datetime.utcnow()
matrix = Matrix(
id=matrix_id,
width=shape[1],
height=shape[0],
created_at=datetime.now(timezone.utc),
created_at=created_at,
)
self.repo.save(matrix)
return matrix_id
Expand Down
29 changes: 29 additions & 0 deletions tests/core/tasks/test_task_job_service.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1f23998

Please sign in to comment.