-
-
Save sandeepraju/3da0ad035aa9bf5504b1 to your computer and use it in GitHub Desktop.
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/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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