def _run(self, hosts, callback, queue_function, *args): hosts = to_hosts(hosts, default_domain=self.domain) self.total += len(hosts) callback = _prepare_connection(callback) task = Task(self.workqueue) for host in hosts: name = host.get_name() data = {"host": host} job_id = queue_function(callback, name, *args, data=data) if job_id is not None: task.add_job_id(job_id) if task.is_completed(): self._dbg(2, "No jobs enqueued.") return None self._dbg(2, "All jobs enqueued.") return task
def _run(self, hosts, callback, queue_function, *args): hosts = to_hosts(hosts, default_domain=self.domain) self.total += len(hosts) callback = _prepare_connection(callback) task = Task(self.workqueue) for host in hosts: name = host.get_name() data = {'host': host} job_id = queue_function(callback, name, *args, data=data) if job_id is not None: task.add_job_id(job_id) if task.is_completed(): self._dbg(2, 'No jobs enqueued.') return None self._dbg(2, 'All jobs enqueued.') return task
def testWait(self): task = Task(self.wq) self.assertEqual(task.is_completed(), True) job1 = Thread(1, object, 'foo1', None) job2 = Thread(2, object, 'foo2', None) task.add_job_id(job1.id) task.add_job_id(job2.id) self.assertEqual(task.is_completed(), False) self.wq.job_succeeded_event(job1) self.assertEqual(task.is_completed(), False) self.wq.job_succeeded_event(job2) self.assertEqual(task.is_completed(), True)
def enqueue(self, function, name = None, attempts = 1): """ Places the given function in the queue and calls it as soon as a thread is available. To pass additional arguments to the callback, use Python's functools.partial(). :type function: function :param function: The function to execute. :type name: string :param name: A name for the task. :type attempts: int :param attempts: The number of attempts on failure. :rtype: object :return: An object representing the task. """ self.total += 1 task = Task(self.workqueue) job_id = self.workqueue.enqueue(function, name, attempts) if job_id is not None: task.add_job_id(job_id) self._dbg(2, 'Function enqueued.') return task
def enqueue(self, function, name=None, attempts=1): """ Places the given function in the queue and calls it as soon as a thread is available. To pass additional arguments to the callback, use Python's functools.partial(). @type function: function @param function: The function to execute. @type name: string @param name: A name for the task. @type attempts: int @param attempts: The number of attempts on failure. @rtype: object @return: An object representing the task. """ self.total += 1 task = Task(self.workqueue) job_id = self.workqueue.enqueue(function, name, attempts) if job_id is not None: task.add_job_id(job_id) self._dbg(2, "Function enqueued.") return task
def testIsCompleted(self): task = Task(self.wq) task.add_job_id(123) task.wait() # Returns immediately because the id is not known.
def testConstructor(self): task = Task(self.wq)