Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A simple complete SHARQ worker which does a Dequeue request to fetch a job and Finish request to mark it as successfully complete. Requires Python Requests (http://docs.python-requests.org/en/latest/)
import time
import json
import requests
while True:
# dequeue the job from the queue of type `sms`
try:
response = requests.get('http://localhost:8080/dequeue/sms/')
if response.status_code == 200:
# successful dequeue.
r = json.loads(response.text)
print r['payload'] # process the payload here.
queue_id = r['queue_id']
job_id = r['job_id']
# mark the job as completed successfully by
# sending a finish request.
requests.post(
'http://localhost:8080/finish/sms/%s/%s/' % (queue_id, job_id))
elif response.status_code == 404:
# no job found (either queue is empty or none
# of the jobs are ready yet).
time.sleep(1) # wait for a second before retrying if needed.
except Exception as e:
print "Something went wrong!"
time.sleep(5) # retry after 5 seconds.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment