# Path: src/mercury/common/task_managers/base/worker.py
# class Worker(object):
#     """Worker executing tasks"""
#     def __init__(self, task_handler, maximum_requests, maximum_age,
#                  handler_args, handler_kwargs):
#         """Create a new Worker to execute tasks.
# 
#         :param task_handler: Task handler to fetch and execute tasks.
#         :param maximum_requests: Maximum number of tasks this worker can
#             execute.
#         :param maximum_age: Maximum age of the worker.
#         """
#         self.maximum_requests = maximum_requests
#         self.maximum_age = maximum_age
#         self.birth = None
#         self.kill_signal = None
#         self.handled_tasks = 0
# 
#         self.task = task_handler(*handler_args, **handler_kwargs)
# 
#     def start(self):
#         """Start the worker.
# 
#         The worker fetches and executes tasks as long as its maximum number
#         of tasks is not reached.
#         """
#         self.birth = time.time()
#         while self.handled_tasks < self.maximum_requests:
#             if self.kill_signal:
#                 log.info(
#                     'Shutting down... I served my masters well: %s tasks ,'
#                     'lived %s seconds' % (self.handled_tasks,
#                                           time.time() - self.birth))
#                 break
#             task = self.task.fetch()
#             if not task:
#                 continue
#             self.task.execute()
#             self.handled_tasks += 1


import logging
import signal
import threading
import time
from .worker import Worker
log = logging.getLogger(__name__)


class Manager(object):
    """Class managing workers"""
    def __init__(self,
                 task_handler,
                 number_of_workers=10,
                 maximum_requests_per_thread=5000,
                 maximum_age=3600,
                 handler_args=None,
                 handler_kwargs=None):
        """Create a new Manager.

        :param task_handler: The Task object that will execute tasks.
        :param number_of_workers: Maximum number of workers for this manager.
        :param maximum_requests_per_thread: Maximum number of tasks per worker.
        :param maximum_age: Maximum age of workers.
        """
        self.number_of_workers = number_of_workers
        self.max_requests = maximum_requests_per_thread
        self.max_age = maximum_age
        self.task_handler = task_handler
        self.handler_args = handler_args or ()
        self.handler_kwargs = handler_kwargs or {}
        self.workers = []

    def spawn(self):
        """Add and start a new worker to handle tasks."""
        worker_dict = {}

# Path: src/mercury/common/task_managers/base/worker.py
# class Worker(object):
#     """Worker executing tasks"""
#     def __init__(self, task_handler, maximum_requests, maximum_age,
#                  handler_args, handler_kwargs):
#         """Create a new Worker to execute tasks.
# 
#         :param task_handler: Task handler to fetch and execute tasks.
#         :param maximum_requests: Maximum number of tasks this worker can
#             execute.
#         :param maximum_age: Maximum age of the worker.
#         """
#         self.maximum_requests = maximum_requests
#         self.maximum_age = maximum_age
#         self.birth = None
#         self.kill_signal = None
#         self.handled_tasks = 0
# 
#         self.task = task_handler(*handler_args, **handler_kwargs)
# 
#     def start(self):
#         """Start the worker.
# 
#         The worker fetches and executes tasks as long as its maximum number
#         of tasks is not reached.
#         """
#         self.birth = time.time()
#         while self.handled_tasks < self.maximum_requests:
#             if self.kill_signal:
#                 log.info(
#                     'Shutting down... I served my masters well: %s tasks ,'
#                     'lived %s seconds' % (self.handled_tasks,
#                                           time.time() - self.birth))
#                 break
#             task = self.task.fetch()
#             if not task:
#                 continue
#             self.task.execute()
#             self.handled_tasks += 1


# Path: src/mercury/common/task_managers/base/manager.py
import logging
import signal
import threading
import time
from .worker import Worker

log = logging.getLogger(__name__)


class Manager(object):
    """Class managing workers"""
    def __init__(self,
                 task_handler,
                 number_of_workers=10,
                 maximum_requests_per_thread=5000,
                 maximum_age=3600,
                 handler_args=None,
                 handler_kwargs=None):
        """Create a new Manager.

        :param task_handler: The Task object that will execute tasks.
        :param number_of_workers: Maximum number of workers for this manager.
        :param maximum_requests_per_thread: Maximum number of tasks per worker.
        :param maximum_age: Maximum age of workers.
        """
        self.number_of_workers = number_of_workers
        self.max_requests = maximum_requests_per_thread
        self.max_age = maximum_age
        self.task_handler = task_handler
        self.handler_args = handler_args or ()
        self.handler_kwargs = handler_kwargs or {}
        self.workers = []

    def spawn(self):
        """Add and start a new worker to handle tasks."""
        worker_dict = {}

        w = Worker(self.task_handler, self.max_requests, self.max_age,