This repository was archived by the owner on Nov 2, 2021. It is now read-only.
forked from elkeschaper/Gc3pie_error_prone_workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_workflow.py
executable file
·215 lines (169 loc) · 5.99 KB
/
test_workflow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#! /usr/bin/env python
GLOBAL_OUTPUT_DIR = '/tmp'
import os
import os.path
import sys
## Interface to Gc3libs
import gc3libs
from gc3libs import Application, Run, Task
from gc3libs.cmdline import SessionBasedScript, _Script
from gc3libs.workflow import SequentialTaskCollection, ParallelTaskCollection
import gc3libs.utils
import gc3libs.debug
class StopOnError(object):
"""
Mix-in class to make a `SequentialTaskCollection`:class: turn to STOPPED
state as soon as one of the tasks fail.
"""
def next(self, done):
if done == len(self.tasks) - 1:
self.execution.returncode = self.tasks[done].execution.returncode
return Run.State.TERMINATED
else:
rc = self.tasks[done].execution.exitcode
if rc != 0:
return Run.State.STOPPED
else:
return Run.State.RUNNING
############################# Basic Applications/Tasks ###################################
class A(Application):
@gc3libs.debug.trace
def __init__(self, k, outdir, **kwargs):
gc3libs.log.info("Creating A for producing %d output files", k)
self.k = k
self.outdir = outdir
gc3libs.Application.__init__(
self,
arguments = [
"./a2b.py",
"-o", (outdir + '/A'),
"-n", self.k,
],
inputs = ['a2b.py'],
outputs = [],
#join = True,
stdout = "stdout.log",
stderr = "stderr.log",
**kwargs
)
def terminated(self):
# check that files `A.1`, `A.2`, etc. have been produced
for j in range(self.k):
if not os.path.isfile(self.outdir + ("/A.%d" % j)):
self.execution.returncode = 1
return
self.execution.returncode = 0
class ApplicationWithOneOutputFile(Application):
def terminated(self):
if not os.path.isfile(self.outfile):
self.execution.exitcode = 1
class B(ApplicationWithOneOutputFile):
def __init__(self, infile, outfile, **kwargs):
self.infile = infile
self.outfile = outfile
gc3libs.Application.__init__(
self,
arguments = [
"./b2c.py",
"-i", infile,
"-o", outfile,
],
inputs = ['b2c.py'],
outputs = [],
#join = True,
stdout = "stdout.log",
stderr = "stderr.log",
**kwargs
)
class C(ApplicationWithOneOutputFile):
def __init__(self, infile, outfile, **kwargs):
self.infile = infile
self.outfile = outfile
gc3libs.Application.__init__(
self,
arguments = [
"./c2d.py",
"-i", infile,
"-o", outfile,
],
inputs = ['c2d.py'],
outputs = [],
#join = True,
stdout = "stdout.log",
stderr = "stderr.log",
**kwargs
)
class D(ApplicationWithOneOutputFile):
def __init__(self, input_dir, outfile, **kwargs):
self.input_dir = input_dir
self.outfile = outfile
gc3libs.Application.__init__(
self,
arguments = [
"./d.py", input_dir,
"-o", outfile,
],
inputs = ['d.py'],
outputs = [],
#join = True,
stdout = "stdout.log",
stderr = "stderr.log",
**kwargs
)
######################## Support Classes / Workflow elements #############################
class MainSequentialFlow(StopOnError, SequentialTaskCollection):
def __init__(self, k, **kwargs):
self.k = k
gc3libs.log.info("\t Calling MainSequentialFlow.__init({})".format(k))
SequentialTaskCollection.__init__(self, [
A(k, GLOBAL_OUTPUT_DIR, **kwargs),
MainParallelFlow(k ,GLOBAL_OUTPUT_DIR, **kwargs),
D(GLOBAL_OUTPUT_DIR, GLOBAL_OUTPUT_DIR + '/D.txt', **kwargs),
], **kwargs)
def terminated(self):
gc3libs.log.info("\t MainSequentialFlow.terminated [%s]" % self.execution.returncode)
class MainParallelFlow(ParallelTaskCollection):
@gc3libs.debug.trace
def __init__(self, k, output_dir_of_a, **kwargs):
self.k = k
self.output_dir_of_a = output_dir_of_a
# use our knowledge of what A does (violates DRY, though)
self.tasks = [
InnerSequentialFlow(j, output_dir_of_a, **kwargs)
for j in range(k)
]
ParallelTaskCollection.__init__(self, self.tasks, **kwargs)
def terminated(self):
gc3libs.log.info("\t\tMainParallelFlow.terminated")
class InnerSequentialFlow(StopOnError, SequentialTaskCollection):
@gc3libs.debug.trace
def __init__(self, j, output_dir_of_a, **kwargs):
# XXX: hard-code paths!
output_of_a = ("%s/A.%d" % (output_dir_of_a, j))
output_of_b = ("%s/B.%d" % (GLOBAL_OUTPUT_DIR, j))
output_of_c = ("%s/C.%d" % (GLOBAL_OUTPUT_DIR, j))
initial_tasks = [
B(output_of_a, output_of_b, **kwargs),
C(output_of_b, output_of_c, **kwargs),
]
SequentialTaskCollection.__init__(self, initial_tasks, **kwargs)
def terminated(self):
gc3libs.log.info("\t\t\t\tInnerSequentialFlow.terminated [%d]" % self.execution.returncode)
############################# Main Session Creator (?) ###################################
class TestWorkflow(SessionBasedScript):
"""
Test pipeline
"""
def __init__(self):
SessionBasedScript.__init__(
self,
version = '0.0.2',
)
def setup_options(self):
self.add_param("-k", type=int, default=7, metavar="NUM", help="Number of parallel strands.")
def new_tasks(self, kwargs):
yield test_workflow.MainSequentialFlow(self.params.k, **kwargs)
# run script
if __name__ == '__main__':
import test_workflow
TestWorkflow().run()