-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqueue.py
More file actions
108 lines (83 loc) · 2.84 KB
/
Copy pathqueue.py
File metadata and controls
108 lines (83 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""Queue view
This module is a Flask `Module` exposing the basic queue operations.
"""
import datetime
from flask import (Module, request, abort, json, jsonify,
make_response, current_app)
import httpqueue.model as model
PRIORITY_HEADER = 'X-httPQueue-Priority'
ID_HEADER = 'X-httPQueue-ID'
view = Module(__name__)
@view.route('/', methods=['GET'])
def list_queues():
"""Retrieve a JSON list of existing queues."""
return json.dumps(model.queue.list_queues())
@view.route('/<q_name>/', methods=['POST'])
def push_item(q_name):
"""Take in a JSON document and add it to the queue.
The request is also expected to have an X-httPQueue-Priority header.
"""
try:
priority = request.headers[PRIORITY_HEADER]
priority = datetime.datetime.strptime(priority, '%Y-%m-%dT%H:%M:%S.%f')
except KeyError:
current_app.logger.error('No priority header.')
abort(400)
except ValueError as ex:
current_app.logger.error(str(ex))
abort(400)
q = model.get_queue(q_name)
try:
task = json.loads(request.data)
except ValueError:
current_app.logger.error(
'Failed to parse JSON from request body: %s' % request.data)
abort(415)
q.push(priority, task)
return ''
@view.route('/<q_name>/', methods=['POP'])
def pop_item(q_name):
"""Remove and return the next item.
The item is moved to the pending queue until it has been acked or
its "pending lifetime" has been exceeded.
"""
q = model.get_queue(q_name)
q.restore_pending()
item = q.pop()
if item is None:
response = make_response()
response.status_code = 204
return response
response = make_response(jsonify(item['task']))
response.headers[ID_HEADER] = item['_id']
response.headers[PRIORITY_HEADER] = item['priority'].isoformat()
return response
@view.route('/<q_name>/id/<id>', methods=['ACK'])
def ack_item(q_name, id):
"""Notify the service that a previously popped item is trash.
"""
q = model.get_queue(q_name)
try:
q.ack(id)
except KeyError:
current_app.logger.error('Ack failed: no item with object id %s' % id)
abort(404)
except model.errors.InvalidId as ex:
current_app.logger.error(str(ex))
abort(404)
return ''
@view.route('/<q_name>/id/<id>', methods=['CANCEL'])
def cancel_item(q_name, id):
"""Notify the service that a previously queued item should be dropped.
If the document has already been picked up by a client a 404 is returned.
"""
q = model.get_queue(q_name)
try:
q.cancel(id)
except KeyError:
current_app.logger.error('Ack failed: no item with object id %s' % id)
abort(404)
except model.errors.InvalidId as ex:
current_app.logger.error(str(ex))
abort(404)
return ''