342
|
1 """
|
|
2 This module defines tasks and a runner for these tasks. Tasks can
|
|
3 have dependencies and it can be determined if they need to be run.
|
|
4 """
|
334
|
5
|
|
6 import logging
|
329
|
7
|
|
8 class TaskError(Exception):
|
|
9 pass
|
|
10
|
|
11
|
|
12 class Task:
|
|
13 """ Task that can run, and depend on other tasks """
|
|
14 def __init__(self, name):
|
|
15 self.name = name
|
|
16 self.completed = False
|
342
|
17 self.dependencies = set()
|
334
|
18 self.duration = 1
|
329
|
19
|
|
20 def run(self):
|
|
21 raise NotImplementedError("Implement this abstract method!")
|
|
22
|
|
23 def fire(self):
|
342
|
24 """ Wrapper around run that marks the task as done """
|
329
|
25 assert all(t.completed for t in self.dependencies)
|
|
26 self.run()
|
|
27 self.completed = True
|
|
28
|
342
|
29 def add_dependency(self, task):
|
|
30 """ Add another task as a dependency for this task """
|
|
31 if task is self:
|
|
32 raise TaskError('Can not add dependency on task itself!')
|
|
33 if self in task.down_stream_tasks:
|
|
34 raise TaskError('Can not introduce circular task')
|
|
35 self.dependencies.add(task)
|
|
36 return task
|
329
|
37
|
342
|
38 @property
|
|
39 def down_stream_tasks(self):
|
|
40 """ Return a set of all tasks that follow this task """
|
|
41 # TODO: is this upstream or downstream???
|
|
42 cdst = list(dep.down_stream_tasks for dep in self.dependencies)
|
|
43 cdst.append(self.dependencies)
|
|
44 return set.union(*cdst)
|
|
45
|
|
46 def __gt__(self, other):
|
|
47 return other in self.down_stream_tasks
|
329
|
48
|
332
|
49 def __repr__(self):
|
|
50 return 'Task "{}"'.format(self.name)
|
|
51
|
329
|
52
|
342
|
53 class EmptyTask(Task):
|
|
54 """ Basic task that does nothing """
|
|
55 def run(self):
|
|
56 pass
|
|
57
|
|
58
|
329
|
59 class TaskRunner:
|
332
|
60 """ Basic task runner that can run some tasks in sequence """
|
329
|
61 def __init__(self):
|
334
|
62 self.logger = logging.getLogger('taskrunner')
|
329
|
63 self.task_list = []
|
332
|
64
|
329
|
65 def add_task(self, task):
|
|
66 self.task_list.append(task)
|
|
67
|
334
|
68 @property
|
|
69 def total_duration(self):
|
|
70 return sum(t.duration for t in self.task_list)
|
|
71
|
329
|
72 def run_tasks(self):
|
342
|
73 # First sort tasks:
|
|
74 self.task_list.sort()
|
|
75
|
|
76 # Run tasks:
|
334
|
77 passed_time = 0.0
|
|
78 total_time = self.total_duration
|
329
|
79 try:
|
|
80 for t in self.task_list:
|
334
|
81 self.report_progress(passed_time / total_time, t.name)
|
329
|
82 t.fire()
|
334
|
83 passed_time += t.duration
|
329
|
84 except TaskError as e:
|
342
|
85 self.logger.error(str(e.msg))
|
329
|
86 return 1
|
334
|
87 self.report_progress(1, 'OK')
|
329
|
88 return 0
|
332
|
89
|
|
90 def display(self):
|
|
91 """ Display task how they would be run """
|
|
92 for task in self.task_list:
|
|
93 print(task)
|
334
|
94
|
|
95 def report_progress(self, percentage, text):
|
|
96 self.logger.info('[{:3.1%}] {}'.format(percentage, text))
|