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 ""
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 ""
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 ""
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