From a2978974a49f9743b4c928173abee08de2427e2b Mon Sep 17 00:00:00 2001 From: Jonathan Heathcote Date: Mon, 18 Jul 2016 10:56:31 +0100 Subject: [PATCH 1/3] Use UTC for timestamps. --- setup.py | 2 +- spalloc_server/controller.py | 18 ++++++++++++++---- spalloc_server/server.py | 6 +++--- tests/test_controller.py | 11 +++++++++-- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index 1d17677..d529378 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ keywords="spinnaker allocation packing management supercomputer", # Requirements - install_requires=["rig", "six", "enum-compat", "inotify_simple"], + install_requires=["rig", "six", "enum-compat", "inotify_simple", "pytz"], # Scripts entry_points={ diff --git a/spalloc_server/controller.py b/spalloc_server/controller.py index dec63e6..57964e9 100644 --- a/spalloc_server/controller.py +++ b/spalloc_server/controller.py @@ -14,6 +14,10 @@ import time +from datetime import datetime, timedelta + +from pytz import utc + from rig.geometry import spinn5_chip_coord from spalloc_server.coordinates import \ @@ -1049,7 +1053,7 @@ class JobStateTuple(namedtuple("JobStateTuple", If the job has been destroyed, this may be a string describing the reason the job was terminated. start_time : float or None - The Unix time at which the job was created. + The Unix time (UTC) at which the job was created. """ # Python 3.4 Workaround: https://bugs.python.org/issue24931 @@ -1094,7 +1098,7 @@ class JobTuple(namedtuple("JobTuple", owner : str The string giving the name of the Job's owner. start_time : float - The time the job was created (Unix time) + The time the job was created (Unix time, UTC) keepalive : float or None The maximum time allowed between queries for this job before it is automatically destroyed (or None if the job can remain allocated @@ -1158,7 +1162,7 @@ class _Job(object): owner : str The job's owner. start_time : float - The time the job was created (Unix time) + The time the job was created (Unix time, UTC) keepalive : float or None The maximum time allowed between queries for this job before it is automatically destroyed (or None if the job can remain allocated @@ -1218,7 +1222,13 @@ def __init__(self, id, owner, self.id = id self.owner = owner - self.start_time = start_time if start_time is not None else time.time() + + if start_time is not None: + self.start_time = start_time + else: + now = datetime.now(utc) + epoch = datetime(1970, 1, 1, tzinfo=utc) + self.start_time = (now - epoch) / timedelta(seconds=1) # If None, never kill this job due to inactivity. Otherwise, stop the # job if the time exceeds this value. It is the allocator's diff --git a/spalloc_server/server.py b/spalloc_server/server.py index b06f6b6..e0813d7 100644 --- a/spalloc_server/server.py +++ b/spalloc_server/server.py @@ -626,8 +626,8 @@ def get_job_state(self, client, job_id): If the job has been destroyed, this may be a string describing the reason the job was terminated. start_time : float or None - For queued and allocated jobs, gives the Unix time at which the - job was created (or None otherwise). + For queued and allocated jobs, gives the Unix time (UTC) at + which the job was created (or None otherwise). """ out = self._controller.get_job_state(job_id)._asdict() out["state"] = int(out["state"]) @@ -849,7 +849,7 @@ def list_jobs(self, client): "owner" is the string giving the name of the Job's owner. - "start_time" is the time the job was created. + "start_time" is the time the job was created (Unix time, UTC). "keepalive" is the maximum time allowed between queries for this job before it is automatically destroyed (or None if the job can diff --git a/tests/test_controller.py b/tests/test_controller.py index dd8a65c..7f94ad4 100644 --- a/tests/test_controller.py +++ b/tests/test_controller.py @@ -6,6 +6,10 @@ import time +from datetime import datetime, timedelta + +from pytz import utc + from collections import OrderedDict from rig.links import Links @@ -641,8 +645,11 @@ def test_list_jobs(conn, m): assert jobs[0].owner == "me" assert jobs[1].owner == "you" - assert time.time() - 1.0 <= jobs[0].start_time <= time.time() - assert time.time() - 1.0 <= jobs[1].start_time <= time.time() + now = datetime.now(utc) + epoch = datetime(1970, 1, 1, tzinfo=utc) + unixtime_now = (now - epoch) / timedelta(seconds=1) + assert unixtime_now - 1.0 <= jobs[0].start_time <= unixtime_now + assert unixtime_now - 1.0 <= jobs[1].start_time <= unixtime_now assert jobs[0].keepalive == 60.0 assert jobs[1].keepalive is None From 8c401b37f6e65bff4c2fe98308b165c34acfe03f Mon Sep 17 00:00:00 2001 From: Jonathan Heathcote Date: Mon, 18 Jul 2016 12:41:58 +0100 Subject: [PATCH 2/3] Fix Python 2.7 compatibility. --- spalloc_server/controller.py | 4 ++-- tests/test_controller.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spalloc_server/controller.py b/spalloc_server/controller.py index 57964e9..69f9043 100644 --- a/spalloc_server/controller.py +++ b/spalloc_server/controller.py @@ -14,7 +14,7 @@ import time -from datetime import datetime, timedelta +from datetime import datetime from pytz import utc @@ -1228,7 +1228,7 @@ def __init__(self, id, owner, else: now = datetime.now(utc) epoch = datetime(1970, 1, 1, tzinfo=utc) - self.start_time = (now - epoch) / timedelta(seconds=1) + self.start_time = (now - epoch).total_seconds() # If None, never kill this job due to inactivity. Otherwise, stop the # job if the time exceeds this value. It is the allocator's diff --git a/tests/test_controller.py b/tests/test_controller.py index 7f94ad4..8827b6b 100644 --- a/tests/test_controller.py +++ b/tests/test_controller.py @@ -6,7 +6,7 @@ import time -from datetime import datetime, timedelta +from datetime import datetime from pytz import utc @@ -647,7 +647,7 @@ def test_list_jobs(conn, m): now = datetime.now(utc) epoch = datetime(1970, 1, 1, tzinfo=utc) - unixtime_now = (now - epoch) / timedelta(seconds=1) + unixtime_now = (now - epoch).total_seconds() assert unixtime_now - 1.0 <= jobs[0].start_time <= unixtime_now assert unixtime_now - 1.0 <= jobs[1].start_time <= unixtime_now From 438b187efaaedefcbca8880e7cd7698028d522ae Mon Sep 17 00:00:00 2001 From: Jonathan Heathcote Date: Mon, 18 Jul 2016 12:50:16 +0100 Subject: [PATCH 3/3] Flag manual start_time constructor argument as no cover This argument is never used but is just present for completeness (as to not confuse future readers...) --- spalloc_server/controller.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spalloc_server/controller.py b/spalloc_server/controller.py index 69f9043..ae23beb 100644 --- a/spalloc_server/controller.py +++ b/spalloc_server/controller.py @@ -1223,8 +1223,8 @@ def __init__(self, id, owner, self.owner = owner - if start_time is not None: - self.start_time = start_time + if start_time is not None: # pragma: no branch + self.start_time = start_time # pragma: no cover else: now = datetime.now(utc) epoch = datetime(1970, 1, 1, tzinfo=utc)