diff --git a/packages/flamenco/flamenco/managers/api.py b/packages/flamenco/flamenco/managers/api.py index 1644d0b4f539625b55822a7a91d035d32d678338..b5090f194139dcf5d31194e5a82d977f88a8c70a 100644 --- a/packages/flamenco/flamenco/managers/api.py +++ b/packages/flamenco/flamenco/managers/api.py @@ -261,6 +261,7 @@ def get_depsgraph(manager_id, request_json): task_query['status'] = {'$in': DEPSGRAPH_CLEAN_SLATE_TASK_STATUSES} else: # 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) task_query['_updated'] = {'$gt': modified_since} task_query['status'] = {'$in': DEPSGRAPH_MODIFIED_SINCE_TASK_STATUSES} @@ -296,36 +297,11 @@ def get_depsgraph(manager_id, request_json): if depsgraph: last_modification = max(task['_updated'] for task in depsgraph) 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 -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): app.register_api_blueprint(api_blueprint, url_prefix='/flamenco/managers') diff --git a/packages/flamenco/tests/test_depsgraph.py b/packages/flamenco/tests/test_depsgraph.py index 7a08db3ee34b78c571fbe279ac1f8289c00e8ba2..d277df7c90f3373758ca30bbb58654c2469d7a93 100644 --- a/packages/flamenco/tests/test_depsgraph.py +++ b/packages/flamenco/tests/test_depsgraph.py @@ -95,7 +95,7 @@ class DepsgraphTest(AbstractFlamencoTest): last_modified = parse(resp.headers['Last-Modified']) with self.app.test_request_context(): 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 with self.app.test_request_context(): @@ -164,15 +164,9 @@ class DepsgraphTest(AbstractFlamencoTest): task0 = self.flamenco.db('tasks').find_one({'_id': self.task_ids[0]}) task2 = self.flamenco.db('tasks').find_one({'_id': self.task_ids[2]}) # 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(task2['status'], u'claimed-by-manager') self.assertEqual(2 * [u'claimed-by-manager'], [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)