Ejemplo n.º 1
0
def restore_task_settings(restore_data):
    """ Restores task settings to the given values.  This restores the initial 
        task settings when the Task Manager is closed. 
        
        The restore_data argument must be a dict.  The keys correspond to task 
        names, and the values are tuples containing: 1. a dict mapping setting 
        names to their new values, and 2. a list of setting names to erase.  
        Every setting name should be found in one or the other, but not both.
        Here is a brief example (for brevity, not all setting names are shown)::
            
            {
                'task.task1': (
                    {
                        'ignore_result': True,
                    },
                    ['expires', acks_late],
                ),
                'task.task2': (
                    {
                        'exchange': 'MyExchange',
                        'routing_key': 'MyRoutingKey',
                    },
                    ['expires', 'rate_limit'],
                ),
            }
    """
    # only broadcast if there are workers
    if len(util.get_all_worker_names()):
        util.broadcast('restore_task_settings', 
                  arguments={'restore_data': restore_data})
Ejemplo n.º 2
0
 def _get_worker_count(self):
     try:
         n = self._worker_count
     except AttributeError:
         n = len(util.get_all_worker_names())
         self._worker_count = n
     return n
Ejemplo n.º 3
0
 def run_ready_policies(self):
     """ Look for Policies that may be due to be run, and try to run 
         them. 
     """
     start = datetime.datetime.now()
     modified_ids = []
     run_deltas = []
     
     # Only run policies if there are workers.
     if util.get_all_worker_names():
         self.logger.debug('Running ready policies...')
         try:
             for entry in self.registry:
                 was_run, next_run_delta = self.maybe_run_policy(entry)
                 if was_run:
                     entry.set_last_run_time(start)
                     modified_ids.append(entry.policy.id)
                 if next_run_delta:
                     run_deltas.append(next_run_delta)
         finally:
             self.logger.debug('Finished running ready policies.')
             now = datetime.datetime.now()
             for id in modified_ids:
                 self.registry.save(id, now)
     else:
         msg = 'Not running policies -- no workers are available.'
         self.logger.warn(msg)
         run_deltas.append(MIN_LOOP_SLEEP_TIME)
     
     delta = datetime.datetime.now() - start
     secs = delta.seconds + delta.microseconds/1000000.
     self.logger.info(
         'Ran {0} policies in {1} seconds'.format(len(modified_ids), secs))
     
     return min(run_deltas+[MAX_LOOP_SLEEP_TIME])
Ejemplo n.º 4
0
def get_all_task_settings():
    """ Like get_task_settings(), but it always returns the settings for all 
        tasks in all workers. 
    """
    # don't do anything if there are no workers
    if len(util.get_all_worker_names()) == 0:
        return {}
    settings = util.broadcast('get_task_settings', 
                         arguments={'tasknames': None, 
                         'setting_names': _setting_names}, 
                         reply=True)
    settings = util._merge_broadcast_result(settings)
    return util._condense_broadcast_result(settings) or {}
Ejemplo n.º 5
0
 def __init__(self, _broadcast, names=None):
     if not names:
         names = util.get_all_worker_names()
     super(WorkerApi, self).__init__(_broadcast, names)