This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/workers/webhookworker.py

51 lines
1.5 KiB
Python
Raw Normal View History

2013-11-15 21:45:02 +00:00
import logging
import argparse
import requests
import json
2013-11-15 21:45:02 +00:00
from data.queue import webhook_queue
from workers.worker import Worker
root_logger = logging.getLogger('')
root_logger.setLevel(logging.DEBUG)
FORMAT = '%(asctime)-15s - %(levelname)s - %(pathname)s - %(funcName)s - %(message)s'
formatter = logging.Formatter(FORMAT)
logger = logging.getLogger(__name__)
class WebhookWorker(Worker):
def process_queue_item(self, job_details):
url = job_details['url']
payload = job_details['payload']
headers = {'Content-type': 'application/json'}
2013-11-15 21:45:02 +00:00
try:
resp = requests.post(url, data=json.dumps(payload), headers=headers)
2013-11-15 21:45:02 +00:00
if resp.status_code/100 != 2:
logger.error('%s response for webhook to url: %s' % (resp.status_code,
url))
return False
except requests.exceptions.RequestException as ex:
logger.exception('Webhook was unable to be sent: %s' % ex.message)
return False
return True
parser = argparse.ArgumentParser(description='Worker daemon to send webhooks')
parser.add_argument('--log', help='Specify the log file for the worker as a daemon.')
2013-11-15 21:45:02 +00:00
args = parser.parse_args()
if args.log is not None:
2013-11-15 21:45:02 +00:00
handler = logging.FileHandler(args.log)
else:
handler = logging.StreamHandler()
handler.setFormatter(formatter)
root_logger.addHandler(handler)
worker = WebhookWorker(webhook_queue, poll_period_seconds=15,
reservation_seconds=3600)
worker.start()