Skip to content

Instantly share code, notes, and snippets.

@robspychala
Created April 26, 2011 16:32
Show Gist options
  • Save robspychala/942611 to your computer and use it in GitHub Desktop.
Save robspychala/942611 to your computer and use it in GitHub Desktop.
Simple example of a redis based webhook implementation. (gist has external dependancies, but they should be easy to remove)
#!/usr/bin/env python2.6
import sys, urllib2, urllib, time, threading, signal
sys.path.append('support/lib')
import settings
from settings import logging
from lib import taskqueue
import redis
r = redis.Redis(host='localhost', port=6379, db=0)
run_thread = True
# Callback called when you run `supervisorctl stop'
def sigterm_handler(signum, frame):
logging.info("stopping webhook")
global run_thread
run_thread = False
sys.exit(0)
def listen_for_messages(queue_name):
logging.info("listening to %s", queue_name)
global run_thread
while run_thread:
ret = r.blpop(queue_name, 10)
if ret:
q, body = ret[0], ret[1]
else:
continue
task= None
try:
task = taskqueue.Task.decode_body(body)
except:
logging.error("Badly formatted body %s" % str(body))
continue
url = task.url
if not task.url.startswith("http"):
url = "%s%s" % (settings.taskqueue_server, task.url)
try:
req = urllib2.Request(url, urllib.urlencode(task.params), {"X-Requested-With": "XMLHttpRequest"})
response = urllib2.urlopen(req)
resp_content = response.read()
logging.info("Processed %s %s" % (task.url, str(task.params)))
except Exception, e:
logging.error("Exception in callback", exc_info=True)
time.sleep(2)
taskqueue.Queue(queue_name).add(task)
logging.info("Re-queued %s %s" % (task.url, str(task.params)))
logging.info("stop listening to %s", queue_name)
if __name__ == "__main__":
for queue_name in [settings.emailin_queue, settings.emailout_queue, settings.task_queue]:
t = threading.Thread(target=listen_for_messages, args=[queue_name])
t.start()
signal.signal(signal.SIGTERM, sigterm_handler)
while True:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment