Skip to content

Commit

Permalink
Merge pull request #3 from project-rig/fix-timezones
Browse files Browse the repository at this point in the history
Interpret incoming timestamps as UTC.
  • Loading branch information
mossblaser committed Jul 18, 2016
2 parents 24d33c7 + 4477e04 commit 7b94496
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
keywords="spinnaker allocation packing management supercomputer",

# Requirements
install_requires=["six>=1.8.0", "appdirs", "enum-compat"],
install_requires=["six>=1.8.0", "appdirs", "enum-compat", "pytz", "tzlocal"],

# Scripts
entry_points={
Expand Down
21 changes: 14 additions & 7 deletions spalloc/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@

logger = logging.getLogger(__name__)

# In Python 2, no default handler exists for software which doesn't configure
# its own logging so we must add one ourselves as per
# https://docs.python.org/3.1/library/logging.html#configuring-logging-for-a-library
logger.addHandler(logging.StreamHandler())


VERSION_RANGE_START = (0, 4, 0)
VERSION_RANGE_STOP = (2, 0, 0)
Expand Down Expand Up @@ -271,7 +276,7 @@ def __init__(self, *args, **kwargs):
# Snag the keepalive interval from the job
self._keepalive = job_state.keepalive

logger.info("Resumed job %d", self.id)
logger.info("Spalloc resumed job %d", self.id)
else:
# Get job creation arguments
job_args = args
Expand Down Expand Up @@ -303,7 +308,7 @@ def __init__(self, *args, **kwargs):
# Create the job (failing fast if can't communicate)
self.id = self._client.create_job(*job_args, **job_kwargs)

logger.info("Created job %d", self.id)
logger.info("Created spalloc job %d", self.id)

# Start keepalive thread now that everything is up
self._keepalive_thread.start()
Expand Down Expand Up @@ -353,11 +358,12 @@ def _reconnect(self):
try:
self._client.connect(self._timeout)
self._assert_compatible_version()
logger.info("Reconnected successfully.")
logger.info("Reconnected to spalloc server successfully.")
except (IOError, OSError) as e:
# Connect/version command failed... Leave the socket clearly
# broken so that we retry again
logger.warning("Reconnect attempt failed: %s", e)
logger.warning(
"Spalloc server is unreachable (%s), will keep trying...", e)
self._client.close()

def _keepalive_thread(self):
Expand Down Expand Up @@ -397,7 +403,7 @@ def destroy(self, reason=None):
try:
self._client.destroy_job(self.id, reason)
except (IOError, OSError, ProtocolTimeoutError) as e:
logger.warning("Could not destroy job: %s", e)
logger.warning("Could not destroy spalloc job: %s", e)

self.close()

Expand Down Expand Up @@ -689,15 +695,16 @@ def wait_until_ready(self, timeout=None):
# Now in the ready state!
return
elif cur_state == JobState.queued:
logger.info("Job has been queued by the server.")
logger.info("Job has been queued by the spalloc server.")
elif cur_state == JobState.power:
logger.info("Waiting for board power commands to complete.")
elif cur_state == JobState.destroyed:
# In a state which can never become ready
raise JobDestroyedError(self._get_state().reason)
elif cur_state == JobState.unknown:
# Server has forgotten what this job even was...
raise JobDestroyedError("Server no longer recognises job.")
raise JobDestroyedError(
"Spalloc server no longer recognises job.")

# Wait for a state change...
if finish_time is None:
Expand Down
9 changes: 7 additions & 2 deletions spalloc/scripts/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
import argparse
import datetime

from pytz import utc
from tzlocal import get_localzone

from collections import OrderedDict

from six import iteritems
Expand Down Expand Up @@ -122,8 +125,10 @@ def show_job_info(t, client, timeout, job_id):
info["Owner"] = job["owner"]
info["State"] = JobState(job["state"]).name
if job["start_time"] is not None:
info["Start time"] = datetime.datetime.fromtimestamp(
job["start_time"]).strftime('%d/%m/%Y %H:%M:%S')
utc_timestamp = datetime.datetime.fromtimestamp(
job["start_time"], utc)
local_timestamp = utc_timestamp.astimezone(get_localzone())
info["Start time"] = local_timestamp.strftime('%d/%m/%Y %H:%M:%S')
info["Keepalive"] = job["keepalive"]

args = job["args"]
Expand Down
9 changes: 7 additions & 2 deletions spalloc/scripts/ps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
import argparse
import datetime

from pytz import utc
from tzlocal import get_localzone

from spalloc import config
from spalloc import \
__version__, ProtocolClient, ProtocolTimeoutError, JobState
Expand Down Expand Up @@ -87,8 +90,10 @@ def render_job_list(t, jobs, machine=None, owner=None):
num_boards = ""

# Format start time
timestamp = datetime.datetime.fromtimestamp(
job["start_time"]).strftime('%d/%m/%Y %H:%M:%S')
utc_timestamp = datetime.datetime.fromtimestamp(
job["start_time"], utc)
local_timestamp = utc_timestamp.astimezone(get_localzone())
timestamp = local_timestamp.strftime('%d/%m/%Y %H:%M:%S')

if job["allocated_machine_name"] is not None:
machine_name = job["allocated_machine_name"]
Expand Down

0 comments on commit 7b94496

Please sign in to comment.