def get_tasks(self, state=Task.ANY_MASK): """ Returns a list of Task objects with the given state. @type state: integer @param state: A bitmask of states. @rtype: list[Task] @return: A list of tasks. """ return [t for t in Task.Iterator(self.task_tree, state)]
def is_completed(self): """ Returns True if the entire Workflow is completed, False otherwise. """ mask = Task.NOT_FINISHED_MASK iter = Task.Iterator(self.task_tree, mask) try: next = iter.next() except: # No waiting tasks found. return True return False
def complete_next(self, pick_up=True): """ Runs the next task. Returns True if completed, False otherwise. @type pick_up: boolean @param pick_up: When True, this method attempts to choose the next task not by searching beginning at the root, but by searching from the position at which the last call of complete_next() left off. @rtype: boolean @return: True if all tasks were completed, False otherwise. """ # Try to pick up where we left off. blacklist = [] if pick_up and self.last_task is not None: try: iter = Task.Iterator(self.last_task, Task.READY) next = iter.next() except: next = None self.last_task = None if next is not None: if next.complete(): self.last_task = next return True blacklist.append(next) # Walk through all waiting tasks. for task in Task.Iterator(self.task_tree, Task.READY): for blacklisted_task in blacklist: if task._is_descendant_of(blacklisted_task): continue if task.complete(): self.last_task = task return True blacklist.append(task) return False
def cancel(self, success=False): """ Cancels all open tasks in the workflow. @type success: boolean @param success: Whether the Workflow should be marked as successfully completed. """ self.success = success cancel = [] mask = Task.NOT_FINISHED_MASK for task in Task.Iterator(self.task_tree, mask): cancel.append(task) for task in cancel: task.cancel()
def _get_waiting_tasks(self): waiting = Task.Iterator(self.task_tree, Task.WAITING) return [w for w in waiting]