Beispiel #1
0
 def run(self,args,kwargs):
     taskinfo = {
         "task_id":self.task_id,
         "task_name":self.task_name,
         "args":self.args,
         "kwargs":self.kwargs,
         "queue":self.queue
         }
     TaskQueue.enqueue(taskinfo)
Beispiel #2
0
 def __init__(self, server="localhost:6379", password=None):
     """
     >>> from pyres.scheduler import Scheduler
     >>> scheduler = Scheduler('localhost:6379')
     """
     self._shutdown = False
     self.resq = TaskQueue()
Beispiel #3
0
class Scheduler(object):

    def __init__(self, server="localhost:6379", password=None):
        """
        >>> from pyres.scheduler import Scheduler
        >>> scheduler = Scheduler('localhost:6379')
        """
        self._shutdown = False
        self.resq = TaskQueue()

    def register_signal_handlers(self):
        logger.info('registering signals')
        signal.signal(signal.SIGTERM, self.schedule_shutdown)
        signal.signal(signal.SIGINT, self.schedule_shutdown)
        signal.signal(signal.SIGQUIT, self.schedule_shutdown)

    def schedule_shutdown(self, signal, frame):
        logger.info('shutting down started')
        self._shutdown = True

    def __call__(self):
        _setproctitle("Starting")
        logger.info('starting up')
        self.register_signal_handlers()
        #self.load_schedule()
        logger.info('looking for delayed items')
        while True:
            if self._shutdown:
                break
            self.handle_delayed_items()
            _setproctitle("Waiting")
            logger.debug('sleeping')
            time.sleep(5)
        logger.info('shutting down complete')

    def next_timestamp(self):
        while True:
            timestamp = self.resq.next_delayed_timestamp()
            if timestamp:
                yield timestamp
            else:
                break

    def next_item(self, timestamp):
        while True:
            item = self.resq.next_item_for_timestamp(timestamp)
            if item:
                yield item
            else:
                break

    def handle_delayed_items(self):
        for timestamp in self.next_timestamp():
            _setproctitle('Handling timestamp %s' % timestamp)
            logger.info('handling timestamp: %s' % timestamp)
            for item in self.next_item(timestamp):
                logger.debug('queueing item %s' % item)
                klass = item['class']
                queue = item['queue']
                args = item['args']
                kwargs = {}
                if 'first_attempt' in item:
                    kwargs['first_attempt'] = item['first_attempt']
                #self.resq.enqueue_from_string(klass, queue, *args, **kwargs)

    @classmethod
    def run(cls, server, password=None):
        sched = cls(server=server, password=password)
        sched()
Beispiel #4
0
 def next_task(self, interval=0):
     taskinfo = TaskQueue.dequeue(self.queue)
     task = None
     if taskinfo:
         task = self.build_task(taskinfo)
     return task