Skip to content
Snippets Groups Projects
Commit 75948217 authored by Sybren A. Stüvel's avatar Sybren A. Stüvel
Browse files

Server: .../depsgrahp: format Last-Modified header in ISO-8601 format

This format allows sub-second precision, which is what we need for reliable
queries.
parent 6eb0ed68
No related branches found
No related tags found
No related merge requests found
...@@ -261,6 +261,7 @@ def get_depsgraph(manager_id, request_json): ...@@ -261,6 +261,7 @@ def get_depsgraph(manager_id, request_json):
task_query['status'] = {'$in': DEPSGRAPH_CLEAN_SLATE_TASK_STATUSES} task_query['status'] = {'$in': DEPSGRAPH_CLEAN_SLATE_TASK_STATUSES}
else: else:
# Not clean slate, just give all updated tasks assigned to this manager. # Not clean slate, just give all updated tasks assigned to this manager.
log.debug('Modified-since header: %s', modified_since)
modified_since = dateutil.parser.parse(modified_since) modified_since = dateutil.parser.parse(modified_since)
task_query['_updated'] = {'$gt': modified_since} task_query['_updated'] = {'$gt': modified_since}
task_query['status'] = {'$in': DEPSGRAPH_MODIFIED_SINCE_TASK_STATUSES} task_query['status'] = {'$in': DEPSGRAPH_MODIFIED_SINCE_TASK_STATUSES}
...@@ -296,36 +297,11 @@ def get_depsgraph(manager_id, request_json): ...@@ -296,36 +297,11 @@ def get_depsgraph(manager_id, request_json):
if depsgraph: if depsgraph:
last_modification = max(task['_updated'] for task in depsgraph) last_modification = max(task['_updated'] for task in depsgraph)
log.debug('Last modification was %s', last_modification) log.debug('Last modification was %s', last_modification)
resp.headers['Last-Modified'] = format_http_date(last_modification) # We need a format that can handle sub-second precision.
resp.headers['Last-Modified'] = last_modification.isoformat()
resp.headers['X-Last-Modified-Format'] = 'ISO-8601'
return resp return resp
def format_http_date(last_modification):
"""Format the given timestamp into RFC 1123 format.
:param last_modification: datetime in UTC timezone
:type last_modification: datetime.datetime
"""
import time
import email.utils
# This incorrectly represents 'stamp' in local time, instead of the UTC we get
# from the database.
stamp = time.mktime(last_modification.timetuple())
# Subtract the UTC to local time offset
timediff = time.mktime(time.gmtime(0))
stamp -= timediff
http_date = email.utils.formatdate(
timeval=stamp,
localtime=False,
usegmt=True
) # --> Wed, 22 Oct 2008 10:55:46 GMT
return http_date
def setup_app(app): def setup_app(app):
app.register_api_blueprint(api_blueprint, url_prefix='/flamenco/managers') app.register_api_blueprint(api_blueprint, url_prefix='/flamenco/managers')
...@@ -95,7 +95,7 @@ class DepsgraphTest(AbstractFlamencoTest): ...@@ -95,7 +95,7 @@ class DepsgraphTest(AbstractFlamencoTest):
last_modified = parse(resp.headers['Last-Modified']) last_modified = parse(resp.headers['Last-Modified'])
with self.app.test_request_context(): with self.app.test_request_context():
task0 = self.flamenco.db('tasks').find_one({'_id': self.task_ids[0]}) task0 = self.flamenco.db('tasks').find_one({'_id': self.task_ids[0]})
self.assert_equal_to_second(task0['_updated'], last_modified) self.assertEqual(task0['_updated'], last_modified)
# The tasks in the database, as well as the response, should be set to claimed-by-manager # The tasks in the database, as well as the response, should be set to claimed-by-manager
with self.app.test_request_context(): with self.app.test_request_context():
...@@ -164,15 +164,9 @@ class DepsgraphTest(AbstractFlamencoTest): ...@@ -164,15 +164,9 @@ class DepsgraphTest(AbstractFlamencoTest):
task0 = self.flamenco.db('tasks').find_one({'_id': self.task_ids[0]}) task0 = self.flamenco.db('tasks').find_one({'_id': self.task_ids[0]})
task2 = self.flamenco.db('tasks').find_one({'_id': self.task_ids[2]}) task2 = self.flamenco.db('tasks').find_one({'_id': self.task_ids[2]})
# They should be equal to second precision # They should be equal to second precision
self.assert_equal_to_second(task2['_updated'], last_modified) self.assertEqual(task2['_updated'], last_modified)
self.assertEqual(task0['status'], u'claimed-by-manager') self.assertEqual(task0['status'], u'claimed-by-manager')
self.assertEqual(task2['status'], u'claimed-by-manager') self.assertEqual(task2['status'], u'claimed-by-manager')
self.assertEqual(2 * [u'claimed-by-manager'], self.assertEqual(2 * [u'claimed-by-manager'],
[task['status'] for task in depsgraph]) [task['status'] for task in depsgraph])
def assert_equal_to_second(self, actual, expected):
import datetime
diff = datetime.timedelta(microseconds=actual.microsecond)
self.assertEqual(actual - diff, expected)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment