Skip to content

Commit

Permalink
Update deprecated utc calls
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholassaylor committed Nov 30, 2024
1 parent 9eaca95 commit 4c679a8
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
6 changes: 3 additions & 3 deletions WebHostLib/autolauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import logging
import multiprocessing
import typing
from datetime import timedelta, datetime
from datetime import timedelta, datetime, timezone
from threading import Event, Thread
from uuid import UUID

Expand Down Expand Up @@ -87,10 +87,10 @@ def keep_running():
with db_session:
rooms = select(
room for room in Room if
room.last_activity >= datetime.utcnow() - timedelta(days=3))
room.last_activity >= datetime.now(timezone.utc) - timedelta(days=3))
for room in rooms:
# we have to filter twice, as the per-room timeout can't currently be PonyORM transpiled.
if room.last_activity >= datetime.utcnow() - timedelta(seconds=room.timeout + 5):
if room.last_activity >= datetime.now(timezone.utc) - timedelta(seconds=room.timeout + 5):
hosters[room.id.int % len(hosters)].start_room(room.id)

except AlreadyRunningException:
Expand Down
4 changes: 2 additions & 2 deletions WebHostLib/customserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _save(self, exit_save: bool = False) -> bool:
room.multisave = pickle.dumps(self.get_save())
# saving only occurs on activity, so we can "abuse" this information to mark this as last_activity
if not exit_save: # we don't want to count a shutdown as activity, which would restart the server again
room.last_activity = datetime.datetime.utcnow()
room.last_activity = datetime.datetime.now(datetime.timezone.utc)
return True

def get_save(self) -> dict:
Expand Down Expand Up @@ -314,7 +314,7 @@ async def start_room(room_id):
with (db_session):
# ensure the Room does not spin up again on its own, minute of safety buffer
room = Room.get(id=room_id)
room.last_activity = datetime.datetime.utcnow() - \
room.last_activity = datetime.datetime.now(datetime.timezone.utc) - \
datetime.timedelta(minutes=1, seconds=room.timeout)
logging.info(f"Shutting down room {room_id} on {name}.")
finally:
Expand Down
6 changes: 3 additions & 3 deletions WebHostLib/landing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import timedelta, datetime
from datetime import timedelta, datetime, timezone

from flask import render_template
from pony.orm import count
Expand All @@ -10,6 +10,6 @@
@app.route('/', methods=['GET', 'POST'])
@cache.cached(timeout=300) # cache has to appear under app route for caching to work
def landing():
rooms = count(room for room in Room if room.creation_time >= datetime.utcnow() - timedelta(days=7))
seeds = count(seed for seed in Seed if seed.creation_time >= datetime.utcnow() - timedelta(days=7))
rooms = count(room for room in Room if room.creation_time >= datetime.now(timezone.utc) - timedelta(days=7))
seeds = count(seed for seed in Seed if seed.creation_time >= datetime.now(timezone.utc) - timedelta(days=7))
return render_template("landing.html", rooms=rooms, seeds=seeds)
2 changes: 1 addition & 1 deletion WebHostLib/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def host_room(room: UUID):
if room is None:
return abort(404)

now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
# indicate that the page should reload to get the assigned port
should_refresh = ((not room.last_port and now - room.creation_time < datetime.timedelta(seconds=3))
or room.last_activity < now - datetime.timedelta(seconds=room.timeout))
Expand Down
8 changes: 4 additions & 4 deletions WebHostLib/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime
from datetime import datetime, timezone
from uuid import UUID, uuid4
from pony.orm import Database, PrimaryKey, Required, Set, Optional, buffer, LongStr

Expand All @@ -20,8 +20,8 @@ class Slot(db.Entity):

class Room(db.Entity):
id = PrimaryKey(UUID, default=uuid4)
last_activity = Required(datetime, default=lambda: datetime.utcnow(), index=True)
creation_time = Required(datetime, default=lambda: datetime.utcnow(), index=True) # index used by landing page
last_activity = Required(datetime, default=lambda: datetime.now(timezone.utc), index=True)
creation_time = Required(datetime, default=lambda: datetime.now(timezone.utc), index=True) # index used by landing page
owner = Required(UUID, index=True)
commands = Set('Command')
seed = Required('Seed', index=True)
Expand All @@ -38,7 +38,7 @@ class Seed(db.Entity):
rooms = Set(Room)
multidata = Required(bytes, lazy=True)
owner = Required(UUID, index=True)
creation_time = Required(datetime, default=lambda: datetime.utcnow(), index=True) # index used by landing page
creation_time = Required(datetime, default=lambda: datetime.now(timezone.utc), index=True) # index used by landing page
slots = Set(Slot)
spoiler = Optional(LongStr, lazy=True)
meta = Required(LongStr, default=lambda: "{\"race\": false}") # additional meta information/tags
Expand Down
5 changes: 2 additions & 3 deletions WebHostLib/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@ def get_room_last_activity(self) -> Dict[TeamPlayer, datetime.timedelta]:
Does not include players who have no activity recorded.
"""
last_activity: Dict[TeamPlayer, datetime.timedelta] = {}
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
for (team, player), timestamp in self._multisave.get("client_activity_timers", []):
last_activity[team, player] = now - datetime.datetime.utcfromtimestamp(timestamp)
last_activity[team, player] = now - datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc)

return last_activity

Expand Down Expand Up @@ -307,7 +307,6 @@ def _process_if_request_valid(incoming_request: Request, room: Optional[Room]) -
if_modified = parsedate_to_datetime(if_modified_str)
if if_modified.tzinfo is None:
abort(400) # standard requires "GMT" timezone
# database may use datetime.utcnow(), which is timezone-naive. convert to timezone-aware.
last_activity = room.last_activity
if last_activity.tzinfo is None:
last_activity = room.last_activity.replace(tzinfo=datetime.timezone.utc)
Expand Down

0 comments on commit 4c679a8

Please sign in to comment.