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