Skip to content

Commit

Permalink
Replace deprecated datetime.datetime.utcnow()
Browse files Browse the repository at this point in the history
The function is deprecated in python 3.12, see
https://docs.python.org/3.12/library/datetime.html?highlight=utcnow#datetime.datetime.now

"Because naive datetime objects are treated
by many datetime methods as local times,
it is preferred to use aware datetimes to represent times in UTC.
As such, the recommended way to create an object representing
the current time in UTC is by calling `datetime.now(timezone.utc)`."

Raise worker version to 213 (also server side).
  • Loading branch information
ppigazzini committed Aug 27, 2023
1 parent de5eb8e commit 4f8f721
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 86 deletions.
2 changes: 1 addition & 1 deletion server/fishtest/actiondb.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def insert_action(self, **action):
action["run_id"] = str(action["run_id"])
ret = validate(schema, action, "action", strict=True)
if ret == "":
action["time"] = datetime.utcnow().replace(tzinfo=timezone.utc).timestamp()
action["time"] = datetime.now(timezone.utc).timestamp()
self.actions.insert_one(action)
else:
raise Exception("Validation failed with error '{}'".format(ret))
12 changes: 6 additions & 6 deletions server/fishtest/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64
import copy
from datetime import datetime
from datetime import datetime, timezone

from fishtest.stats.stat_util import SPRT_elo
from fishtest.util import optional_key, union, validate, worker_name
Expand All @@ -25,7 +25,7 @@
on how frequently the main instance flushes its run cache.
"""

WORKER_VERSION = 212
WORKER_VERSION = 213


def validate_request(request):
Expand Down Expand Up @@ -126,7 +126,7 @@ def handle_error(self, error, exception=HTTPBadRequest):
raise exception(self.add_time({"error": error}))

def validate_username_password(self, api):
self.__t0 = datetime.utcnow()
self.__t0 = datetime.now(timezone.utc)
self.__api = api
# is the request valid json?
try:
Expand Down Expand Up @@ -191,7 +191,7 @@ def validate_request(self, api):
self.__task = task

def add_time(self, result):
result["duration"] = (datetime.utcnow() - self.__t0).total_seconds()
result["duration"] = (datetime.now(timezone.utc) - self.__t0).total_seconds()
return result

def get_username(self):
Expand Down Expand Up @@ -349,7 +349,7 @@ def get_elo(self):

@view_config(route_name="api_calc_elo")
def calc_elo(self):
self.__t0 = datetime.utcnow()
self.__t0 = datetime.now(timezone.utc)
self.__api = "/api/calc_elo"

W = self.request.params.get("W")
Expand Down Expand Up @@ -553,7 +553,7 @@ def beat(self):
self.validate_request("/api/beat")
run = self.run()
task = self.task()
task["last_updated"] = datetime.utcnow()
task["last_updated"] = datetime.now(timezone.utc)
self.request.rundb.buffer(run, False)
return self.add_time({})

Expand Down
42 changes: 28 additions & 14 deletions server/fishtest/rundb.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import threading
import time
import zlib
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

import fishtest.stats.stat_util
from bson.binary import Binary
Expand All @@ -35,7 +35,7 @@

DEBUG = False

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

last_rundb = None

Expand Down Expand Up @@ -140,7 +140,9 @@ def new_run(
adjudication=True,
):
if start_time is None:
start_time = datetime.utcnow()
start_time = datetime.now(timezone.utc)
else:
start_time = start_time.replace(tzinfo=timezone.utc)

run_args = {
"base_tag": base_tag,
Expand Down Expand Up @@ -421,15 +423,18 @@ def flush_buffers(self):
self.start_timer()

def scavenge(self, run):
if datetime.utcnow() < boot_time + timedelta(seconds=300):
if datetime.now(timezone.utc) < boot_time + timedelta(seconds=300):
return False
# print("scavenge ", run["_id"])
dead_task = False
old = datetime.utcnow() - timedelta(minutes=6)
old = datetime.now(timezone.utc) - timedelta(minutes=6)
task_id = -1
for task in run["tasks"]:
task_id += 1
if task["active"] and task["last_updated"] < old:
if (
task["active"]
and task["last_updated"].replace(tzinfo=timezone.utc) < old
):
task["active"] = False
dead_task = True
print(
Expand Down Expand Up @@ -468,7 +473,9 @@ def get_machines(self):
machines = (
task["worker_info"]
| {
"last_updated": task.get("last_updated", None),
"last_updated": task["last_updated"].replace(tzinfo=timezone.utc)
if task.get("last_updated")
else None,
"run": run,
"task_id": task_id,
}
Expand Down Expand Up @@ -779,11 +786,13 @@ def priority(run): # lower is better
# name is already connected.

my_name = "-".join(worker_name(worker_info).split("-")[0:3])
now = datetime.utcnow()
now = datetime.now(timezone.utc)
for task in active_tasks:
task_name = "-".join(worker_name(task["worker_info"]).split("-")[0:3])
if my_name == task_name:
last_update = (now - task["last_updated"]).seconds
last_update = (
now - task["last_updated"].replace(tzinfo=timezone.utc)
).seconds
# 120 = period of heartbeat in worker.
if last_update <= 120:
error = 'Request_task: There is already a worker running with name "{}" which sent an update {} seconds ago'.format(
Expand Down Expand Up @@ -931,7 +940,7 @@ def priority(run): # lower is better
"num_games": task_size,
"active": True,
"worker_info": worker_info,
"last_updated": datetime.utcnow(),
"last_updated": datetime.now(timezone.utc),
"start": opening_offset,
"stats": {
"wins": 0,
Expand Down Expand Up @@ -986,7 +995,9 @@ def active_run_lock(self, id):
if self.purge_count > 100000:
old = time.time() - 10000
self.active_runs = dict(
(k, v) for k, v in self.active_runs.items() if v["time"] >= old
(k, v)
for k, v in self.active_runs.items()
if v["time"].replace(tzinfo=timezone.utc) >= old
)
self.purge_count = 0
if id in self.active_runs:
Expand Down Expand Up @@ -1074,7 +1085,7 @@ def update_task(self, worker_info, run_id, task_id, stats, spsa):
def sync_update_task(self, worker_info, run_id, task_id, stats, spsa):
run = self.get_run(run_id)
task = run["tasks"][task_id]
update_time = datetime.utcnow()
update_time = datetime.now(timezone.utc)

error = ""

Expand Down Expand Up @@ -1356,8 +1367,11 @@ def approve_run(self, run_id, approver):
def purge_run(self, run, p=0.001, res=7.0, iters=1):
# Only purge finished runs
assert run["finished"]
now = datetime.utcnow()
if "start_time" not in run or (now - run["start_time"]).days > 30:
now = datetime.now(timezone.utc)
if (
"start_time" not in run
or (now - run["start_time"].replace(tzinfo=timezone.utc)).days > 30
):
return "Run too old to be purged"
# Do not revive failed runs
if run.get("failed", False):
Expand Down
4 changes: 2 additions & 2 deletions server/fishtest/userdb.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import sys
import threading
import time
from datetime import datetime
from datetime import datetime, timezone

from pymongo import ASCENDING

Expand Down Expand Up @@ -85,7 +85,7 @@ def create_user(self, username, password, email):
{
"username": username,
"password": password,
"registration_time": datetime.utcnow(),
"registration_time": datetime.now(timezone.utc),
"blocked": True,
"email": email,
"groups": [],
Expand Down
7 changes: 4 additions & 3 deletions server/fishtest/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import math
import re
import smtplib
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from email.mime.text import MIMEText
from functools import cache

Expand Down Expand Up @@ -425,8 +425,9 @@ def post_in_fishcooking_results(run):


def diff_date(date):
if date != datetime.min:
diff = datetime.utcnow() - date
utc_date = date.replace(tzinfo=timezone.utc)
if utc_date != datetime.min.replace(tzinfo=timezone.utc):
diff = datetime.now(timezone.utc) - utc_date
else:
diff = timedelta.max
return diff
Expand Down
19 changes: 12 additions & 7 deletions server/fishtest/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import copy
import datetime
import hashlib
import html
import os
import re
import threading
import time
from datetime import datetime, timezone

import fishtest.stats.stat_util
import requests
Expand Down Expand Up @@ -543,7 +543,7 @@ def get_master_info(url):
if idx == 0:
message = message_lines[0].strip()
date_str = commit["commit"]["committer"]["date"]
date = datetime.datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ")
date = datetime.strptime(date_str, "%Y-%m-%dT%H:%M:%SZ")
if bench:
return {
"bench": bench.group(2),
Expand Down Expand Up @@ -868,8 +868,8 @@ def update_nets(request, run):
if not net:
return
if "first_test" not in net:
net["first_test"] = {"id": run_id, "date": datetime.datetime.utcnow()}
net["last_test"] = {"id": run_id, "date": datetime.datetime.utcnow()}
net["first_test"] = {"id": run_id, "date": datetime.now(timezone.utc)}
net["last_test"] = {"id": run_id, "date": datetime.now(timezone.utc)}
request.rundb.update_nn(net)


Expand Down Expand Up @@ -969,8 +969,11 @@ def tests_modify(request):
run = request.rundb.get_run(request.POST["run"])
before = del_tasks(run)

now = datetime.datetime.utcnow()
if "start_time" not in run or (now - run["start_time"]).days > 30:
now = datetime.now(timezone.utc)
if (
"start_time" not in run
or (now - run["start_time"].replace(tzinfo=timezone.utc)).days > 30
):
request.session.flash("Run too old to be modified", "error")
return HTTPFound(location=request.route_url("tests"))

Expand Down Expand Up @@ -1325,7 +1328,9 @@ def tests_view(request):
if task["active"]:
active += 1
cores += task["worker_info"]["concurrency"]
last_updated = task.get("last_updated", datetime.datetime.min)
last_updated = task.get("last_updated", datetime.min).replace(
tzinfo=timezone.utc
)
task["last_updated"] = last_updated

chi2 = get_chi2(run["tasks"])
Expand Down
4 changes: 2 additions & 2 deletions server/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import base64
import datetime
import sys
import unittest
import zlib
from datetime import datetime, timezone

from fishtest.api import WORKER_VERSION, ApiView
from pyramid.httpexceptions import HTTPUnauthorized
Expand All @@ -26,7 +26,7 @@ def new_run(self, add_tasks=0):
"",
username="travis",
tests_repo="travis",
start_time=datetime.datetime.utcnow(),
start_time=datetime.now(timezone.utc),
)
run = self.rundb.get_run(run_id)
run["approved"] = True
Expand Down
6 changes: 3 additions & 3 deletions server/tests/test_rundb.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import sys
import unittest
from datetime import datetime, timezone

import util
from fishtest.api import WORKER_VERSION
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_10_create_run(self):
"",
username="travis",
tests_repo="travis",
start_time=datetime.datetime.utcnow(),
start_time=datetime.now(timezone.utc),
)
run = self.rundb.get_run(run_id_stc)
run["finished"] = True
Expand Down Expand Up @@ -92,7 +92,7 @@ def test_10_create_run(self):
"",
username="travis",
tests_repo="travis",
start_time=datetime.datetime.utcnow(),
start_time=datetime.now(timezone.utc),
)
print(" ")
print(run_id)
Expand Down
4 changes: 2 additions & 2 deletions server/tests/test_users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import datetime
import unittest
from datetime import datetime, timezone

import util
from fishtest.views import login, signup
Expand Down Expand Up @@ -91,7 +91,7 @@ def setUp(self):
"",
username="travis",
tests_repo="travis",
start_time=datetime.datetime.utcnow(),
start_time=datetime.now(timezone.utc),
)
self.rundb.userdb.user_cache.insert_one(
{"username": "JoeUser", "cpu_hours": 12345}
Expand Down
12 changes: 5 additions & 7 deletions server/utils/convert_actions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import datetime
from datetime import datetime, timezone

import pymongo
from bson.objectid import ObjectId
Expand All @@ -10,19 +10,17 @@
actions = actions_collection.find({}).sort("_id", 1)
count = 0
print("Starting conversion...")
t0 = datetime.datetime.utcnow()
t0 = datetime.now(timezone.utc)
for action in actions:
count += 1
action_id = action["_id"]
if "time" in action and isinstance(action["time"], datetime.datetime):
action["time"] = (
action["time"].replace(tzinfo=datetime.timezone.utc).timestamp()
)
if "time" in action and isinstance(action["time"], datetime):
action["time"] = action["time"].replace(tzinfo=timezone.utc).timestamp()
if "run_id" in action and isinstance(action["run_id"], ObjectId):
action["run_id"] = str(action["run_id"])
actions_collection.replace_one({"_id": action_id}, action)
print("Actions converted: {}.".format(count), end="\r")
t1 = datetime.datetime.utcnow()
t1 = datetime.now(timezone.utc)
duration = (t1 - t0).total_seconds()
time_per_run = duration / count
print("")
Expand Down
Loading

0 comments on commit 4f8f721

Please sign in to comment.