Merge pull request #880 from samuelcolvin/millisecs-in-timestamps

add milliseconds into timestamps
main
Selwin Ong 7 years ago committed by GitHub
commit 47ee65eb84

@ -157,16 +157,19 @@ def utcnow():
return datetime.datetime.utcnow() return datetime.datetime.utcnow()
_TIMESTAMP_FORMAT = '%Y-%m-%dT%H:%M:%S.%fZ'
def utcformat(dt): def utcformat(dt):
return dt.strftime(as_text('%Y-%m-%dT%H:%M:%SZ')) return dt.strftime(as_text(_TIMESTAMP_FORMAT))
def utcparse(string): def utcparse(string):
try: try:
return datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%SZ') return datetime.datetime.strptime(string, _TIMESTAMP_FORMAT)
except ValueError: except ValueError:
# This catches RQ < 0.4 datetime format # This catches any jobs remain with old datetime format
return datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%S.%f+00:00') return datetime.datetime.strptime(string, '%Y-%m-%dT%H:%M:%SZ')
def first(iterable, default=None, key=None): def first(iterable, default=None, key=None):

@ -1,9 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from datetime import timedelta
def strip_microseconds(date):
return date - timedelta(microseconds=date.microsecond)

@ -14,7 +14,6 @@ else:
import queue as queue import queue as queue
from tests import fixtures, RQTestCase from tests import fixtures, RQTestCase
from tests.helpers import strip_microseconds
from rq.compat import PY2 from rq.compat import PY2
from rq.exceptions import NoSuchJobError, UnpickleError from rq.exceptions import NoSuchJobError, UnpickleError
@ -170,7 +169,7 @@ class TestJob(RQTestCase):
self.testconn.hset('rq:job:some_id', 'data', self.testconn.hset('rq:job:some_id', 'data',
"(S'tests.fixtures.some_calculation'\nN(I3\nI4\nt(dp1\nS'z'\nI2\nstp2\n.") "(S'tests.fixtures.some_calculation'\nN(I3\nI4\nt(dp1\nS'z'\nI2\nstp2\n.")
self.testconn.hset('rq:job:some_id', 'created_at', self.testconn.hset('rq:job:some_id', 'created_at',
'2012-02-07T22:13:24Z') '2012-02-07T22:13:24.123456Z')
# Fetch returns a job # Fetch returns a job
job = Job.fetch('some_id') job = Job.fetch('some_id')
@ -179,7 +178,7 @@ class TestJob(RQTestCase):
self.assertIsNone(job.instance) self.assertIsNone(job.instance)
self.assertEqual(job.args, (3, 4)) self.assertEqual(job.args, (3, 4))
self.assertEqual(job.kwargs, dict(z=2)) self.assertEqual(job.kwargs, dict(z=2))
self.assertEqual(job.created_at, datetime(2012, 2, 7, 22, 13, 24)) self.assertEqual(job.created_at, datetime(2012, 2, 7, 22, 13, 24, 123456))
def test_persistence_of_empty_jobs(self): # noqa def test_persistence_of_empty_jobs(self): # noqa
"""Storing empty jobs.""" """Storing empty jobs."""
@ -192,11 +191,8 @@ class TestJob(RQTestCase):
job = Job.create(func=fixtures.some_calculation, args=(3, 4), kwargs=dict(z=2)) job = Job.create(func=fixtures.some_calculation, args=(3, 4), kwargs=dict(z=2))
job.save() job.save()
expected_date = strip_microseconds(job.created_at)
stored_date = self.testconn.hget(job.key, 'created_at').decode('utf-8') stored_date = self.testconn.hget(job.key, 'created_at').decode('utf-8')
self.assertEqual( self.assertEqual(stored_date, utcformat(job.created_at))
stored_date,
utcformat(expected_date))
# ... and no other keys are stored # ... and no other keys are stored
self.assertEqual( self.assertEqual(

@ -51,11 +51,13 @@ class TestUtils(RQTestCase):
def test_utcparse(self): def test_utcparse(self):
"""Ensure function utcparse works correctly""" """Ensure function utcparse works correctly"""
utc_formated_time = '2017-08-31T10:14:02Z' utc_formated_time = '2017-08-31T10:14:02.123456Z'
utc_compat_formated_time = '2017-08-31T10:20:56.226733+00:00' self.assertEqual(datetime.datetime(2017, 8, 31, 10, 14, 2, 123456), utcparse(utc_formated_time))
def test_utcparse_legacy(self):
"""Ensure function utcparse works correctly"""
utc_formated_time = '2017-08-31T10:14:02Z'
self.assertEqual(datetime.datetime(2017, 8, 31, 10, 14, 2), utcparse(utc_formated_time)) self.assertEqual(datetime.datetime(2017, 8, 31, 10, 14, 2), utcparse(utc_formated_time))
self.assertEqual(datetime.datetime(2017, 8, 31, 10, 20, 56, 226733), utcparse(utc_compat_formated_time))
def test_backend_class(self): def test_backend_class(self):
"""Ensure function backend_class works correctly""" """Ensure function backend_class works correctly"""

@ -22,7 +22,6 @@ from tests.fixtures import (create_file, create_file_after_timeout,
div_by_zero, do_nothing, say_hello, say_pid, div_by_zero, do_nothing, say_hello, say_pid,
run_dummy_heroku_worker, access_self, run_dummy_heroku_worker, access_self,
modify_self, modify_self_and_error) modify_self, modify_self_and_error)
from tests.helpers import strip_microseconds
from rq import (get_failed_queue, Queue, SimpleWorker, Worker, from rq import (get_failed_queue, Queue, SimpleWorker, Worker,
get_current_connection) get_current_connection)
@ -212,7 +211,7 @@ class TestWorker(RQTestCase):
self.assertEqual(q.count, 1) self.assertEqual(q.count, 1)
# keep for later # keep for later
enqueued_at_date = strip_microseconds(job.enqueued_at) enqueued_at_date = str(job.enqueued_at)
w = Worker([q]) w = Worker([q])
w.work(burst=True) # should silently pass w.work(burst=True) # should silently pass
@ -228,7 +227,7 @@ class TestWorker(RQTestCase):
# Should be the original enqueued_at date, not the date of enqueueing # Should be the original enqueued_at date, not the date of enqueueing
# to the failed queue # to the failed queue
self.assertEqual(job.enqueued_at, enqueued_at_date) self.assertEqual(str(job.enqueued_at), enqueued_at_date)
self.assertIsNotNone(job.exc_info) # should contain exc_info self.assertIsNotNone(job.exc_info) # should contain exc_info
def test_custom_exc_handling(self): def test_custom_exc_handling(self):

Loading…
Cancel
Save