Debian Codegraph @

biomaj/process/processfactory.py

raw
1 import threading
2 import logging
3 import os
4 from biomaj.process.metaprocess import MetaProcess
5
6
7 class ProcessFactory:
8 '''
9 Manage process execution
10 '''
11
12 NB_THREAD = 2
13
14 def __init__(self, bank, redis_client=None, redis_prefix=None):
15 self.bank = bank
16 self.threads_tasks = []
17 if self.bank.session:
18 self.meta_data = self.bank.session.get('per_process_metadata')
19 else:
20 self.meta_data = {}
21 self.redis_client = redis_client
22 self.redis_prefix = redis_prefix
23
24 def run(self, simulate=False):
25 '''
26 Run processes
27
28 :param simulate: does not execute process
29 :type simulate: bool
30 :return: status of execution - bool
31 '''
32 pass
33
34 def run_threads(self, simulate=False):
35 '''
36 Start meta threads
37
38 :param simulate: do not execute processes
39 :type simulate: bool
40 :return: tuple global execution status and status per meta process
41 '''
42 logging.debug('Start meta threads')
43 os.chdir(self.bank.config.get('process.dir'))
44 threads = []
45 running_th = []
46 for thread_tasks in self.threads_tasks:
47 meta_thread = MetaProcess(self.bank, thread_tasks, self.meta_status, self.meta_data, simulate)
48 meta_thread._lock = ProcessFactory._LOCK
49 meta_thread.workflow = self.workflow
50 meta_thread.start()
51 threads.append(meta_thread)
52 running_th.append(meta_thread)
53 # Wait for the end of the threads
54 kill_received = False
55 while len(running_th) > 0:
56 try:
57 # Join all threads using a timeout so it doesn't block
58 # Filter out threads which have been joined or are None
59
60 # Check for cancel request
61 if self.redis_client and self.redis_client.get(self.redis_prefix + ':' + self.bank.name + ':action:cancel'):
62 logging.warn('Cancel requested, stopping process update')
63 self.redis_client.delete(self.redis_prefix + ':' + self.bank.name + ':session:' + self.session)
64 kill_received = True
65 for t in running_th:
66 t.kill_received = True
67 running_th = [t.join(1000) for t in running_th if t is not None and t.is_alive()]
68 except KeyboardInterrupt:
69 logging.warn("Ctrl-c received! Sending kill to threads...")
70 logging.warn("Running tasks will continue and process will stop.")
71 kill_received = True
72 for t in running_th:
73 t.kill_received = True
74
75 for meta_thread in threads:
76 meta_thread.join()
77 global_meta_status = {}
78 global_status = True
79
80 for meta_thread in threads:
81 for meta in meta_thread.meta_status:
82 global_meta_status[meta] = meta_thread.meta_status[meta]
83 if not meta_thread.global_status:
84 global_status = False
85
86 if kill_received:
87 global_status = False
88
89 logging.debug('Meta threads are over')
90 return (global_status, global_meta_status)
91
92 def fill_tasks_in_threads(self, metas):
93 '''
94 Dispatch meta processes in available threads
95 '''
96 self.threads_tasks = []
97 for i in range(0, ProcessFactory.NB_THREAD):
98 # Fill array of meta process in future threads
99 self.threads_tasks.append([])
100 thread_id = 0
101 for meta in metas:
102 meta_process = meta.strip()
103 if thread_id == ProcessFactory.NB_THREAD:
104 thread_id = 0
105 self.threads_tasks[thread_id].append(meta_process)
106 thread_id += 1
107
108
109 class PreProcessFactory(ProcessFactory):
110 '''
111 Manage preprocesses
112 '''
113
114 def __init__(self, bank, metas=None, redis_client=None, redis_prefix=None):
115 '''
116 Creates a preprocess factory
117
118 :param bank: Bank
119 :type bank: :class:`biomaj.bank.Bank`
120 :param metas: initial status of meta processes
121 :type metas: dict
122 '''
123 ProcessFactory.__init__(self, bank, redis_client, redis_prefix)
124 self.meta_status = None
125 if metas is not None:
126 self.meta_status = metas
127 self.workflow = 'preprocess'
128
129 def run(self, simulate=False):
130 '''
131 Run processes
132
133 :param simulate: does not execute process
134 :type simulate: bool
135 :return: status of execution - bool
136 '''
137 logging.info('PROC:PRE')
138 if self.bank.config.get('db.pre.process') is None:
139 metas = []
140 else:
141 metas = self.bank.config.get('db.pre.process').split(',')
142 self.fill_tasks_in_threads(metas)
143 (status, self.meta_status) = self.run_threads(simulate)
144 return status
145
146
147 class RemoveProcessFactory(ProcessFactory):
148 '''
149 Manage remove processes
150 '''
151
152 def __init__(self, bank, metas=None, redis_client=None, redis_prefix=None):
153 '''
154 Creates a remove process factory
155
156 :param bank: Bank
157 :type bank: :class:`biomaj.bank.Bank`
158 :param metas: initial status of meta processes
159 :type metas: dict
160 '''
161 ProcessFactory.__init__(self, bank, redis_client, redis_prefix)
162 self.meta_status = None
163 if metas is not None:
164 self.meta_status = metas
165 self.workflow = 'removeprocess'
166
167 def run(self, simulate=False):
168 '''
169 Run processes
170
171 :param simulate: does not execute process
172 :type simulate: bool
173 :return: status of execution - bool
174 '''
175 logging.info('PROC:REMOVE')
176 if self.bank.config.get('db.remove.process') is None:
177 metas = []
178 else:
179 metas = self.bank.config.get('db.remove.process').split(',')
180 self.fill_tasks_in_threads(metas)
181 (status, self.meta_status) = self.run_threads(simulate)
182 return status
183
184
185 class PostProcessFactory(ProcessFactory):
186 '''
187 Manage postprocesses
188
189 self.blocks: dict of meta processes status
190 Each meta process status is a dict of process status
191 '''
192
193 def __init__(self, bank, blocks=None, redis_client=None, redis_prefix=None):
194 '''
195 Creates a postprocess factory
196
197 :param bank: Bank
198 :type bank: :class:`biomaj.bank.Bank`
199 :param blocks: initial status of block processes
200 :type blocks: dict
201 '''
202 ProcessFactory.__init__(self, bank, redis_client, redis_prefix)
203 self.blocks = {}
204 if blocks is not None:
205 self.blocks = blocks
206 self.workflow = 'postprocess'
207
208 def run(self, simulate=False):
209 '''
210 Run processes
211
212 :param simulate: does not execute process
213 :type simulate: bool
214 :return: status of execution - bool
215 '''
216 logging.info('PROC:POST:BLOCK')
217 blocks = self.bank.config.get('BLOCKS')
218 if blocks is None or blocks == '':
219 process_blocks = []
220 else:
221 process_blocks = blocks.split(',')
222 metas = []
223 self.meta_status = None
224 global_status = True
225 for process_block in process_blocks:
226 if not global_status:
227 continue
228 logging.info('PROC:POST:BLOCK:' + process_block)
229 if process_block in self.blocks:
230 self.meta_status = self.blocks[process_block]
231 # run each block
232 metas = self.bank.config.get(process_block.strip() + '.db.post.process').split(',')
233 self.fill_tasks_in_threads(metas)
234 (status, self.blocks[process_block]) = self.run_threads(simulate)
235 if not status:
236 global_status = False
237 return global_status
238
239
240 ProcessFactory._LOCK = threading.Lock()