src/pyams_scheduler/process.py
changeset 81 64b3a28afb23
parent 54 9f88cbe85980
equal deleted inserted replaced
80:0c69defba0af 81:64b3a28afb23
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
    13 __docformat__ = 'restructuredtext'
    13 __docformat__ = 'restructuredtext'
    14 
    14 
    15 
       
    16 # import standard library
       
    17 import logging
    15 import logging
    18 logger = logging.getLogger('PyAMS (scheduler)')
       
    19 
       
    20 from datetime import datetime
    16 from datetime import datetime
    21 from threading import Thread
    17 from threading import Thread
    22 
    18 
    23 # import interfaces
       
    24 from pyams_scheduler.interfaces import SCHEDULER_NAME
       
    25 from pyams_utils.interfaces import PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME
       
    26 
       
    27 # import packages
       
    28 from apscheduler.jobstores.memory import MemoryJobStore
    19 from apscheduler.jobstores.memory import MemoryJobStore
    29 from apscheduler.schedulers.background import BackgroundScheduler
    20 from apscheduler.schedulers.background import BackgroundScheduler
       
    21 from zope.interface import implementer
       
    22 
       
    23 from pyams_scheduler.interfaces import ISchedulerProcess, SCHEDULER_NAME, ITask
    30 from pyams_scheduler.task import ImmediateTaskTrigger
    24 from pyams_scheduler.task import ImmediateTaskTrigger
       
    25 from pyams_utils.interfaces import PYAMS_APPLICATION_DEFAULT_NAME, PYAMS_APPLICATION_SETTINGS_KEY
    31 from pyams_utils.zodb import ZODBConnection
    26 from pyams_utils.zodb import ZODBConnection
    32 from pyams_zmq.handler import ZMQMessageHandler
    27 from pyams_zmq.handler import ZMQMessageHandler
    33 from pyams_zmq.process import ZMQProcess
    28 from pyams_zmq.process import ZMQProcess
    34 
    29 
    35 
    30 
       
    31 logger = logging.getLogger('PyAMS (scheduler)')
       
    32 
       
    33 
    36 class BaseTaskThread(Thread):
    34 class BaseTaskThread(Thread):
    37 
    35 
    38     def __init__(self, process, settings):
    36     def __init__(self, process, settings):
    39         Thread.__init__(self)
    37         Thread.__init__(self)
    40         self.process = process
    38         self.process = process
    41         self.settings = settings
    39         if ITask.providedBy(settings):
       
    40             scheduler = settings.__parent__
       
    41             self.settings = {
       
    42                 'zodb_name': scheduler.zodb_name,
       
    43                 'task_name': settings.__name__,
       
    44                 'job_id': settings.internal_id
       
    45             }
       
    46         else:
       
    47             self.settings = settings
    42 
    48 
    43     def _get_connection(self):
    49     def _get_connection(self):
    44         zodb_name = self.settings.get('zodb_name')
    50         zodb_name = self.settings.get('zodb_name')
    45         return ZODBConnection(name=zodb_name)
    51         return ZODBConnection(name=zodb_name)
    46 
    52 
   201     """ØMQ scheduler messages handler"""
   207     """ØMQ scheduler messages handler"""
   202 
   208 
   203     handler = SchedulerHandler
   209     handler = SchedulerHandler
   204 
   210 
   205 
   211 
       
   212 @implementer(ISchedulerProcess)
   206 class SchedulerProcess(ZMQProcess):
   213 class SchedulerProcess(ZMQProcess):
   207     """ØMQ tasks scheduler process"""
   214     """ØMQ tasks scheduler process"""
   208 
   215 
   209     def __init__(self, zmq_address, handler, auth, clients, registry):
   216     def __init__(self, zmq_address, handler, auth, clients, registry):
   210         ZMQProcess.__init__(self, zmq_address, handler, auth, clients)
   217         ZMQProcess.__init__(self, zmq_address, handler, auth, clients)