Skip to content

Commit

Permalink
Remove deprecated utcnow calls (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
JBorrow authored Nov 11, 2024
1 parent c9ef1bc commit 8845745
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 24 deletions.
7 changes: 5 additions & 2 deletions librarian_background/create_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def on_call(self):
return self.core(session=session)

def core(self, session: Session):
core_begin = datetime.datetime.utcnow()
core_begin = datetime.datetime.now(datetime.timezone.utc)

try:
store_from = self.get_store(self.clone_from, session)
Expand Down Expand Up @@ -145,7 +145,10 @@ def core(self, session: Session):
for instance in instances:
# First, check if we have gone over time:
if (
(datetime.datetime.utcnow() - core_begin > self.soft_timeout)
(
datetime.datetime.now(datetime.timezone.utc) - core_begin
> self.soft_timeout
)
if self.soft_timeout
else False
):
Expand Down
7 changes: 5 additions & 2 deletions librarian_background/recieve_clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def core(self, session: Session):
Checks for incoming transfers and processes them.
"""

core_begin = datetime.datetime.utcnow()
core_begin = datetime.datetime.now(datetime.timezone.utc)

# Find incoming transfers that are STAGED
ongoing_transfers: list[IncomingTransfer] = (
Expand All @@ -68,7 +68,10 @@ def core(self, session: Session):

for transfer in ongoing_transfers:
if (
(datetime.datetime.utcnow() - core_begin > self.soft_timeout)
(
datetime.datetime.now(datetime.timezone.utc) - core_begin
> self.soft_timeout
)
if self.soft_timeout
else False
):
Expand Down
6 changes: 3 additions & 3 deletions librarian_server/orm/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
that need to be remedied by an outside entity.
"""

from datetime import datetime
from datetime import datetime, timezone
from typing import Optional

from sqlalchemy.orm import Session
Expand Down Expand Up @@ -61,7 +61,7 @@ def new_error(
severity=severity,
category=category,
message=message,
raised_time=datetime.utcnow(),
raised_time=datetime.now(timezone.utc),
cleared_time=None,
cleared=False,
caller=caller,
Expand All @@ -78,5 +78,5 @@ def clear(self, session: Session) -> None:
"""

self.cleared = True
self.cleared_time = datetime.utcnow()
self.cleared_time = datetime.now(timezone.utc)
session.commit()
4 changes: 2 additions & 2 deletions librarian_server/orm/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
be a path, e.g. abcd/efgh/ijkl.txt).
"""

from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path

from sqlalchemy.orm import Session
Expand Down Expand Up @@ -108,7 +108,7 @@ def new_file(

return File(
name=str(filename),
create_time=datetime.utcnow(),
create_time=datetime.now(timezone.utc),
size=size,
checksum=checksum,
uploader=uploader,
Expand Down
6 changes: 3 additions & 3 deletions librarian_server/orm/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
what files have instances on remote librarians that we are aware about.
"""

from datetime import datetime
from datetime import datetime, timezone
from pathlib import Path

from sqlalchemy.orm import Session
Expand Down Expand Up @@ -82,7 +82,7 @@ def new_instance(
file=file,
store=store,
deletion_policy=deletion_policy,
created_time=datetime.utcnow(),
created_time=datetime.now(timezone.utc),
available=True,
)

Expand Down Expand Up @@ -175,6 +175,6 @@ def new_instance(
store_id=store_id,
librarian_id=librarian.id,
librarian=librarian,
copy_time=datetime.utcnow(),
copy_time=datetime.now(timezone.utc),
sender=server_settings.name,
)
8 changes: 4 additions & 4 deletions librarian_server/orm/librarian.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
to.
"""

from datetime import datetime
from datetime import datetime, timezone

from pydantic import ValidationError

Expand Down Expand Up @@ -86,8 +86,8 @@ def new_librarian(
url=url,
port=port,
authenticator=encrypt_string(authenticator),
last_seen=datetime.utcnow(),
last_heard=datetime.utcnow(),
last_seen=datetime.now(timezone.utc),
last_heard=datetime.now(timezone.utc),
)

if not check_connection:
Expand All @@ -107,7 +107,7 @@ def new_librarian(
"is returning an invalid response to ping."
)

librarian.last_seen = datetime.utcnow()
librarian.last_seen = datetime.now(timezone.utc)

return librarian

Expand Down
8 changes: 4 additions & 4 deletions librarian_server/orm/transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def new_transfer(
source=source,
transfer_size=transfer_size,
transfer_checksum=transfer_checksum,
start_time=datetime.datetime.utcnow(),
start_time=datetime.datetime.now(datetime.timezone.utc),
)

def fail_transfer(self, session: "Session", commit: bool = True):
Expand Down Expand Up @@ -240,7 +240,7 @@ def new_transfer(
transfer_checksum=file.checksum,
file_name=file.name,
instance_id=instance.id,
start_time=datetime.datetime.utcnow(),
start_time=datetime.datetime.now(datetime.timezone.utc),
)

def fail_transfer(self, session: "Session", commit: bool = True):
Expand Down Expand Up @@ -413,7 +413,7 @@ def new_transfer(
source_store_id=source_store_id,
destination_store_id=destination_store_id,
source_instance_id=source_instance_id,
start_time=datetime.datetime.utcnow(),
start_time=datetime.datetime.now(datetime.timezone.utc),
)

def fail_transfer(self, session: "Session"):
Expand All @@ -422,7 +422,7 @@ def fail_transfer(self, session: "Session"):
"""

self.status = TransferStatus.FAILED
self.end_time = datetime.datetime.utcnow()
self.end_time = datetime.datetime.now(datetime.timezone.utc)

session.commit()

Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def test_server_with_many_files_and_errors(test_server, test_orm):

if x % 2:
error.cleared = True
error.cleared_time = datetime.datetime.utcnow()
error.cleared_time = datetime.datetime.now(datetime.timezone.utc)

session.add(error)
session.commit()
Expand Down
2 changes: 1 addition & 1 deletion tests/server_unit_test/test_post_to_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, severity, category, message, id):
self.category = category
self.severity = severity
self.id = id
self.raised_time = datetime.datetime.utcnow()
self.raised_time = datetime.datetime.now(datetime.timezone.utc)
self.caller = (
inspect.stack()[1].filename
+ ":"
Expand Down
4 changes: 2 additions & 2 deletions tests/server_unit_test/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def test_search_by_filename(test_server_with_many_files_and_errors, test_client)
def test_search_by_created_time(test_server_with_many_files_and_errors, test_client):
request = FileSearchRequest(
create_time_window=(
datetime.datetime.utcnow() - datetime.timedelta(days=1),
datetime.datetime.utcnow(),
datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=1),
datetime.datetime.now(datetime.timezone.utc),
)
)

Expand Down

0 comments on commit 8845745

Please sign in to comment.