2014-07-18 02:51:58 +00:00
|
|
|
import logging
|
|
|
|
import argparse
|
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
|
|
|
|
from app import notification_queue
|
|
|
|
from workers.worker import Worker
|
|
|
|
|
|
|
|
from endpoints.notificationmethod import NotificationMethod, InvalidNotificationMethodException
|
|
|
|
from endpoints.notificationevent import NotificationEvent, InvalidNotificationEventException
|
2014-09-22 16:52:57 +00:00
|
|
|
from workers.worker import JobException
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
from data import model
|
|
|
|
|
|
|
|
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 NotificationWorker(Worker):
|
|
|
|
def process_queue_item(self, job_details):
|
2014-07-31 17:30:54 +00:00
|
|
|
notification_uuid = job_details['notification_uuid'];
|
|
|
|
repo_namespace = job_details['repository_namespace']
|
|
|
|
repo_name = job_details['repository_name']
|
|
|
|
|
|
|
|
notification = model.get_repo_notification(repo_namespace, repo_name, notification_uuid)
|
2014-07-18 02:51:58 +00:00
|
|
|
if not notification:
|
|
|
|
# Probably deleted.
|
2014-09-22 16:52:57 +00:00
|
|
|
return
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
event_name = notification.event.name
|
|
|
|
method_name = notification.method.name
|
|
|
|
|
|
|
|
try:
|
|
|
|
event_handler = NotificationEvent.get_event(event_name)
|
|
|
|
method_handler = NotificationMethod.get_method(method_name)
|
|
|
|
except InvalidNotificationMethodException as ex:
|
|
|
|
logger.exception('Cannot find notification method: %s' % ex.message)
|
2014-09-22 16:52:57 +00:00
|
|
|
raise JobException('Cannot find notification method: %s' % ex.message)
|
2014-07-18 02:51:58 +00:00
|
|
|
except InvalidNotificationEventException as ex:
|
2014-09-22 16:52:57 +00:00
|
|
|
logger.exception('Cannot find notification event: %s' % ex.message)
|
|
|
|
raise JobException('Cannot find notification event: %s' % ex.message)
|
|
|
|
|
|
|
|
method_handler.perform(notification, event_handler, job_details)
|
2014-07-18 02:51:58 +00:00
|
|
|
|
|
|
|
|
2014-09-22 16:52:57 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
logging.config.fileConfig('conf/logging.conf', disable_existing_loggers=False)
|
2014-07-18 02:51:58 +00:00
|
|
|
|
2014-09-22 16:52:57 +00:00
|
|
|
worker = NotificationWorker(notification_queue, poll_period_seconds=10, reservation_seconds=30,
|
|
|
|
retry_after_seconds=30)
|
|
|
|
worker.start()
|