# Path: hedwig/core/service.py
# class ServiceManager(object):
#     def __init__(self, worker_cls, num_workers=2):
#         if not (inspect.isclass(worker_cls) and issubclass(worker_cls, multiprocessing.Process)):
#             raise ValueError("Worker class must extend multiprocessing.Process")
#         self.workers = []
#         self.num_workers = num_workers
#         self.worker_cls = worker_cls
#         self.running = False
#         signal.signal(signal.SIGINT, self.stop)
#         signal.signal(signal.SIGTERM, self.stop)
# 
#     def start(self, *args, **kwargs):
#         self.running = True
#         LOGGER.info("Starting: Hedwig Worker Service...")
#         try:
#             while self.running:
#                 if len(self.workers) < self.num_workers:
#                     worker = self.worker_cls(*args, **kwargs)
#                     worker.start()
#                     self.workers.append(worker)
#                 else:
#                     for worker in self.workers:
#                         if not worker.is_alive():
#                             worker.shutdown()
#                             self.workers.remove(worker)
#                             worker.terminate()
#                     time.sleep(5)
# 
#         except KeyboardInterrupt:
#             self.stop()
#         except Exception as e:
#             LOGGER.exception(str(e))
#             traceback.print_exc(file=sys.stdout)
#             self.stop()
# 
#     def stop(self, signum=None, frame=None):
#         self.running = False
#         LOGGER.info("Stopping: Hedwig Worker Service...")
#         for worker in self.workers:
#             LOGGER.info("Stopping Worker: " + worker.name)
#             worker.shutdown()
#             worker.terminate()
#         sys.exit(0)
# 
#     def is_running(self):
#         return self.running
# 
# Path: hedwig/core/worker.py
# class HedwigWorker(multiprocessing.Process):
#     def __init__(self, settings, *args, **kwargs):
#         self.hedwig_consumer = Consumer(settings)
#         super(HedwigWorker, self).__init__(*args, **kwargs)
# 
#     def run(self):
#         LOGGER.info("Hedwig consumer: starting")
#         self.hedwig_consumer.consume()
#         LOGGER.info("hedwig consumer: stopped")
# 
#     def shutdown(self):
#         LOGGER.info("Hedwig consumer: shutting down")
#         self.hedwig_consumer.shutdown()
#         LOGGER.info("Hedwig consumer: shutdown complete")
# 
# Path: tests/settings.py
# USER_SETTINGS = {
#     'VHOST': 'hedwig_server',
#     'USERNAME': 'hedwig_test',
#     'PASSWORD': 'hedwig_pass',
#     'CONSUMER': {
#         'RAISE_EXCEPTION': True,
#         'QUEUES': {
#             'everything_printer': {
#                 'BINDINGS': ['#'],
#                 'CALLBACK': 'tests.callbacks.printer',
#                 'DURABLE': False,
#                 'AUTO_DELETE': True,
#                 'NO_ACK': True
#             },
#             # 'accounts_catch_all': {
#             #     'BINDINGS': ['accounts.#'],
#             #     'CALLBACK': 'tests.callbacks.accounts_printer',
#             #     'DURABLE': False,
#             #     'AUTO_DELETE': True,
#             #     'NO_ACK': True
#             # },
#             # 'message_create': {
#             #     'BINDINGS': ['message.serializer.create.*'],
#             #     'CALLBACK': 'tests.callbacks.message_printer',
#             #     'DURABLE': False,
#             #     'AUTO_DELETE': True,
#             #     'NO_ACK': True
#             # }
#         }
#     }
# }


from hedwig.core.service import ServiceManager
from hedwig.core.worker import HedwigWorker
from .settings import hedwig_settings
import sys


def main(argv):
    msg_service = ServiceManager(HedwigWorker, num_workers=1)
    msg_service.start(settings=hedwig_settings)



# Path: hedwig/core/service.py
# class ServiceManager(object):
#     def __init__(self, worker_cls, num_workers=2):
#         if not (inspect.isclass(worker_cls) and issubclass(worker_cls, multiprocessing.Process)):
#             raise ValueError("Worker class must extend multiprocessing.Process")
#         self.workers = []
#         self.num_workers = num_workers
#         self.worker_cls = worker_cls
#         self.running = False
#         signal.signal(signal.SIGINT, self.stop)
#         signal.signal(signal.SIGTERM, self.stop)
# 
#     def start(self, *args, **kwargs):
#         self.running = True
#         LOGGER.info("Starting: Hedwig Worker Service...")
#         try:
#             while self.running:
#                 if len(self.workers) < self.num_workers:
#                     worker = self.worker_cls(*args, **kwargs)
#                     worker.start()
#                     self.workers.append(worker)
#                 else:
#                     for worker in self.workers:
#                         if not worker.is_alive():
#                             worker.shutdown()
#                             self.workers.remove(worker)
#                             worker.terminate()
#                     time.sleep(5)
# 
#         except KeyboardInterrupt:
#             self.stop()
#         except Exception as e:
#             LOGGER.exception(str(e))
#             traceback.print_exc(file=sys.stdout)
#             self.stop()
# 
#     def stop(self, signum=None, frame=None):
#         self.running = False
#         LOGGER.info("Stopping: Hedwig Worker Service...")
#         for worker in self.workers:
#             LOGGER.info("Stopping Worker: " + worker.name)
#             worker.shutdown()
#             worker.terminate()
#         sys.exit(0)
# 
#     def is_running(self):
#         return self.running
# 
# Path: hedwig/core/worker.py
# class HedwigWorker(multiprocessing.Process):
#     def __init__(self, settings, *args, **kwargs):
#         self.hedwig_consumer = Consumer(settings)
#         super(HedwigWorker, self).__init__(*args, **kwargs)
# 
#     def run(self):
#         LOGGER.info("Hedwig consumer: starting")
#         self.hedwig_consumer.consume()
#         LOGGER.info("hedwig consumer: stopped")
# 
#     def shutdown(self):
#         LOGGER.info("Hedwig consumer: shutting down")
#         self.hedwig_consumer.shutdown()
#         LOGGER.info("Hedwig consumer: shutdown complete")
# 
# Path: tests/settings.py
# USER_SETTINGS = {
#     'VHOST': 'hedwig_server',
#     'USERNAME': 'hedwig_test',
#     'PASSWORD': 'hedwig_pass',
#     'CONSUMER': {
#         'RAISE_EXCEPTION': True,
#         'QUEUES': {
#             'everything_printer': {
#                 'BINDINGS': ['#'],
#                 'CALLBACK': 'tests.callbacks.printer',
#                 'DURABLE': False,
#                 'AUTO_DELETE': True,
#                 'NO_ACK': True
#             },
#             # 'accounts_catch_all': {
#             #     'BINDINGS': ['accounts.#'],
#             #     'CALLBACK': 'tests.callbacks.accounts_printer',
#             #     'DURABLE': False,
#             #     'AUTO_DELETE': True,
#             #     'NO_ACK': True
#             # },
#             # 'message_create': {
#             #     'BINDINGS': ['message.serializer.create.*'],
#             #     'CALLBACK': 'tests.callbacks.message_printer',
#             #     'DURABLE': False,
#             #     'AUTO_DELETE': True,
#             #     'NO_ACK': True
#             # }
#         }
#     }
# }


# Path: tests/run.py
from hedwig.core.service import ServiceManager
from hedwig.core.worker import HedwigWorker
from .settings import hedwig_settings
import sys



def main(argv):
    msg_service = ServiceManager(HedwigWorker, num_workers=1)
    msg_service.start(settings=hedwig_settings)



if __name__ == "__main__":