/
task.go
521 lines (465 loc) · 15.9 KB
/
task.go
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package scipipe
import (
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
// Task represents a single static shell command, or go function, to be
// executed, and are scheduled and managed by a corresponding Process
type Task struct {
Name string
Command string
CustomExecute func(*Task)
InIPs map[string]*FileIP
OutIPs map[string]*FileIP
Params map[string]string
Tags map[string]string
Done chan int
cores int
workflow *Workflow
Process *Process
portInfos map[string]*PortInfo
subStreamIPs map[string][]*FileIP
}
// ------------------------------------------------------------------------
// Factory method(s)
// ------------------------------------------------------------------------
// NewTask instantiates and initializes a new Task
func NewTask(workflow *Workflow, process *Process, name string, cmdPat string, inIPs map[string]*FileIP, outPathFuncs map[string]func(*Task) string, portInfos map[string]*PortInfo, params map[string]string, tags map[string]string, prepend string, customExecute func(*Task), cores int) *Task {
t := &Task{
Name: name,
InIPs: inIPs,
OutIPs: make(map[string]*FileIP),
Params: params,
Tags: tags,
Command: "",
CustomExecute: customExecute,
Done: make(chan int),
cores: cores,
workflow: workflow,
Process: process,
portInfos: portInfos,
subStreamIPs: make(map[string][]*FileIP),
}
// Collect substream IPs
for ptName, ptInfo := range portInfos {
if ptInfo.join && ptInfo.joinSep != "" {
// Merge multiple input paths from a substream on the IP, into one string
ips := []*FileIP{}
for ip := range inIPs[ptName].SubStream.Chan {
ips = append(ips, ip)
}
t.subStreamIPs[ptName] = ips
}
}
// Create Out-IPs
for oname, outPathFunc := range outPathFuncs {
oip, err := NewFileIP(outPathFunc(t))
if err != nil {
process.Failf("Could not create out IP %s: %v", oname, err)
}
if ptInfo, ok := portInfos[oname]; ok {
if ptInfo.doStream {
oip.doStream = true
}
}
t.OutIPs[oname] = oip
}
t.Command = t.formatCommand(cmdPat, portInfos, inIPs, t.subStreamIPs, t.OutIPs, params, tags, prepend)
return t
}
const (
parentDirPlaceHolder = "__parent__"
)
// formatCommand is a helper function for NewTask, that formats a shell command
// based on concrete file paths and parameter values
func (t *Task) formatCommand(cmd string, portInfos map[string]*PortInfo, inIPs map[string]*FileIP, subStreamIPs map[string][]*FileIP, outIPs map[string]*FileIP, params map[string]string, tags map[string]string, prepend string) string {
r := getShellCommandPlaceHolderRegex()
placeHolderMatches := r.FindAllStringSubmatch(cmd, -1)
type placeHolderInfo struct {
match string
portName string
modifiers []string
}
placeHolderInfos := make([]*placeHolderInfo, 0)
for _, match := range placeHolderMatches {
restMatch := match[2]
parts := strings.Split(restMatch, "|")
portName := parts[0]
modifiers := parts[1:]
placeHolderInfos = append(placeHolderInfos,
&placeHolderInfo{
portName: portName,
match: match[0],
modifiers: modifiers,
})
}
for _, placeHolder := range placeHolderInfos {
portName := placeHolder.portName
portInfo := portInfos[portName]
var replacement string
switch portInfo.portType {
case "o":
if outIPs[portName] == nil {
t.Failf("Missing outpath for outport (%s) for command (%s)", portName, cmd)
}
replacement = outIPs[portName].TempPath()
replacement = applyPathModifiers(replacement, placeHolder.modifiers)
replacement = replaceParentDirsWithPlaceholder(replacement)
case "os":
if outIPs[portName] == nil {
t.Failf("Missing outpath for outport (%s) for command (%s)", portName, cmd)
}
replacement = outIPs[portName].FifoPath()
replacement = applyPathModifiers(replacement, placeHolder.modifiers)
if !strInSlice("basename", placeHolder.modifiers) {
replacement = prependParentDirPath(replacement)
}
case "i":
if inIPs[portName] == nil {
t.Failf("Missing in-IP for inport (%s) for command (%s)", portName, cmd)
}
if portInfo.join && portInfo.joinSep != "" {
// Merge multiple input paths from a substream on the IP, into one string
paths := []string{}
for _, ip := range subStreamIPs[portName] {
path := ip.Path()
path = applyPathModifiers(path, placeHolder.modifiers)
path = prependParentDirPath(path)
paths = append(paths, path)
}
replacement = strings.Join(paths, portInfo.joinSep)
} else {
if inIPs[portName].Path() == "" {
t.Failf("Missing inpath for inport (%s), and no substream, for command (%s)", portName, cmd)
}
if inIPs[portName].doStream {
replacement = inIPs[portName].FifoPath()
} else {
replacement = inIPs[portName].Path()
}
replacement = applyPathModifiers(replacement, placeHolder.modifiers)
if !strInSlice("basename", placeHolder.modifiers) {
replacement = prependParentDirPath(replacement)
}
}
case "p":
if params[portName] == "" {
t.Failf("Missing param value for param (%s) for command (%s)", portName, cmd)
} else {
replacement = params[portName]
replacement = applyPathModifiers(replacement, placeHolder.modifiers)
}
case "t":
if tags[portName] == "" {
t.Failf("Missing tag value for tag (%s) for command (%s)", portName, cmd)
} else {
replacement = tags[portName]
replacement = applyPathModifiers(replacement, placeHolder.modifiers)
}
default:
t.Failf("Replace failed for port (%s) for command (%s)", portName, cmd)
}
cmd = strings.Replace(cmd, placeHolder.match, replacement, -1)
}
// Add prepend string to the command
if prepend != "" {
cmd = fmt.Sprintf("%s %s", prepend, cmd)
}
return cmd
}
// ------------------------------------------------------------------------
// Main API methods: Accessor methods
// ------------------------------------------------------------------------
// InIP returns an IP for the in-port with name portName
func (t *Task) InIP(portName string) *FileIP {
if t.InIPs[portName] == nil {
t.Failf("No such in-portname (%s)", portName)
}
return t.InIPs[portName]
}
// InPath returns the path name of an input file for the task
func (t *Task) InPath(portName string) string {
return t.InIP(portName).Path()
}
// OutIP returns an IP for the in-port with name portName
func (t *Task) OutIP(portName string) *FileIP {
if ip, ok := t.OutIPs[portName]; ok {
return ip
}
t.Failf("No such out-portname (%s)", portName)
return nil
}
// OutPath returns the path name of an input file for the task
func (t *Task) OutPath(portName string) string {
return t.OutIP(portName).Path()
}
// Param returns the value of a param, for the task
func (t *Task) Param(portName string) string {
if param, ok := t.Params[portName]; ok {
return param
}
t.Failf("No such param port (%s)", portName)
return "invalid"
}
// Tag returns the value of a param, for the task
func (t *Task) Tag(tagName string) string {
if tag, ok := t.Tags[tagName]; ok {
return tag
}
t.Failf("No such tag (%s)", tagName)
return "invalid"
}
// ------------------------------------------------------------------------
// Execute the task
// ------------------------------------------------------------------------
// Execute executes the task (the shell command or go function in CustomExecute)
func (t *Task) Execute() {
defer close(t.Done)
// Do some sanity checks
if t.tempDirsExist() {
t.Failf("Existing temp folders found, so existing. Clean up temporary folders (starting with %s) before restarting the workflow!", tempDirPrefix)
}
if t.anyOutputsExist() {
t.Done <- 1
return
}
// Execute task
t.workflow.IncConcurrentTasks(t.cores) // Will block if max concurrent tasks is reached
err := t.createDirs() // Create output directories needed for any outputs
if err != nil {
t.Failf("Could not create directories: %v", err)
}
startTime := time.Now()
if t.CustomExecute != nil {
outputsStr := ""
for oipName, oip := range t.OutIPs {
outputsStr += " " + oipName + ": " + oip.Path()
}
t.Auditf("Executing: Custom Go function with outputs: %s", outputsStr)
t.CustomExecute(t)
t.Auditf("Finished: Custom Go function with outputs: %s", outputsStr)
} else {
t.Auditf("Executing: %s", t.Command)
t.executeCommand(t.Command)
t.Auditf("Finished: %s", t.Command)
}
finishTime := time.Now()
t.writeAuditLogs(startTime, finishTime)
t.ensureAllOutputsExist()
finErr := t.finalizePaths()
if finErr != nil {
t.Fail(finErr)
}
t.workflow.DecConcurrentTasks(t.cores)
t.Done <- 1
}
// ------------------------------------------------------------------------
// Helper methods for the Execute method
// ------------------------------------------------------------------------
// anyTempFileExists checks if any temporary workflow files exist and if so, returns true
func (t *Task) tempDirsExist() bool {
if _, err := os.Stat(t.TempDir()); os.IsNotExist(err) {
return false
}
return true
}
// anyOutputsExist if any output file IP, or temporary file IPs, exist
func (t *Task) anyOutputsExist() (anyFileExists bool) {
anyFileExists = false
for _, oip := range t.OutIPs {
if !oip.doStream {
opath := oip.Path()
if _, err := os.Stat(opath); err == nil {
t.Auditf("Output file already exists, so skipping: %s", opath)
anyFileExists = true
}
}
}
return
}
// createDirs creates directories for out-IPs of the task
func (t *Task) createDirs() error {
err := os.MkdirAll(t.TempDir(), 0777)
if err != nil {
t.Failf("Could not create tempdir %s: %v", t.TempDir(), err)
}
for _, oip := range t.OutIPs {
oipDir := oip.TempDir() // This will create all out dirs, including the temp dir
if oip.doStream { // Temp dirs are not created for fifo files
oipDir = filepath.Dir(oip.FifoPath())
} else {
oipDir = t.TempDir() + "/" + oipDir
}
err := os.MkdirAll(oipDir, 0777)
if err != nil {
return errors.New(fmt.Sprintf("Could not create directory %s: %v", oipDir, err))
}
}
return nil
}
// executeCommand executes the shell command cmd via bash
func (t *Task) executeCommand(cmd string) {
// cd into the task's tempdir, execute the command, and cd back
out, err := exec.Command("bash", "-c", "cd "+t.TempDir()+" && "+cmd+" && cd ..").CombinedOutput()
if err != nil {
t.Failf("Command failed!\nCommand:\n%s\n\nOutput:\n%s\nOriginal error:%s", cmd, string(out), err)
}
}
func (t *Task) writeAuditLogs(startTime time.Time, finishTime time.Time) {
// Append audit info for the task to all its output IPs
auditInfo := NewAuditInfo()
auditInfo.Command = t.Command
auditInfo.ProcessName = t.Process.Name()
auditInfo.Params = t.Params
auditInfo.StartTime = startTime
auditInfo.FinishTime = finishTime
auditInfo.ExecTimeNS = finishTime.Sub(startTime)
// Set the audit infos from incoming IPs into the "Upstream" map
for inpName, iip := range t.InIPs {
if t.portInfos[inpName].join {
for _, subIP := range t.subStreamIPs[inpName] {
auditInfo.Upstream[subIP.Path()] = subIP.AuditInfo()
}
continue
}
auditInfo.Upstream[iip.Path()] = iip.AuditInfo()
}
// Add output paths generated for this task
for oipName, oip := range t.OutIPs {
auditInfo.OutFiles[oipName] = oip.Path()
}
// Add the current audit info to output ips and write them to file
for _, oip := range t.OutIPs {
oip.SetAuditInfo(auditInfo)
for _, iip := range t.InIPs {
oip.AddTags(iip.Tags())
}
oip.WriteAuditLogToFile()
}
}
func (t *Task) ensureAllOutputsExist() {
for _, ip := range t.OutIPs {
filePath := filepath.Join(t.TempDir(), ip.TempPath())
if _, err := os.Stat(filePath); os.IsNotExist(err) && !ip.doStream {
t.Failf("Missing output temp-file (%s) for ip with path (%s)", filePath, ip.Path())
}
}
}
func (t *Task) finalizePaths() error {
outIPs := []*FileIP{}
for _, ip := range t.OutIPs {
outIPs = append(outIPs, ip)
}
return FinalizePaths(t.TempDir(), outIPs...)
}
func (t *Task) Auditf(msg string, parts ...interface{}) {
t.Audit(fmt.Sprintf(msg+"\n", parts...))
}
func (t *Task) Audit(msg string) {
Audit.Printf("[Task:%s] %s", t.Process.Name(), msg)
}
func (t *Task) Failf(msg string, parts ...interface{}) {
t.Fail(fmt.Sprintf(msg+"\n", parts...))
}
func (t *Task) Fail(msg interface{}) {
Failf("[Task:%s] %s", t.Process.Name(), msg)
}
// FinalizePaths renames temporary output files/directories to their proper paths.
// It is called both from Task, and from Process that implement cutom execution
// schedule.
func FinalizePaths(tempExecDir string, ips ...*FileIP) error {
for _, oip := range ips {
// Move paths for ports, to final destinations
if !oip.doStream {
tempPath := tempExecDir + "/" + oip.TempPath()
finPath := oip.Path()
Debug.Println("Moving OutIP path: ", tempPath, " -> ", finPath)
renameErr := os.Rename(tempPath, finPath)
if renameErr != nil {
return errors.New(fmt.Sprintf("Could not rename out-IP file %s to %s: %s", tempPath, finPath, renameErr))
}
}
}
// For remaining paths in temporary execution dir, just move out of it
err := filepath.Walk(tempExecDir, func(tempPath string, fileInfo os.FileInfo, err error) error {
if !fileInfo.IsDir() {
finPath := strings.Replace(tempPath, tempExecDir+"/", "", 1)
finPath = strings.Replace(finPath, FSRootPlaceHolder+"/", "/", 1)
finPath = replacePlaceholdersWithParentDirs(finPath)
finPathDir := filepath.Dir(finPath)
if _, err := os.Stat(finPathDir); os.IsNotExist(err) {
errMkdir := os.MkdirAll(finPathDir, 0777)
if errMkdir != nil {
Error.Printf("Failed to create directory for final path: %s\n", finPathDir)
}
}
Debug.Println("Moving remaining file path: ", tempPath, " -> ", finPath)
renameErr := os.Rename(tempPath, finPath)
if renameErr != nil {
return errors.New(fmt.Sprintf("Could not rename remaining file %s to %s: %s", tempPath, finPath, renameErr))
}
}
return err
})
if err != nil {
Error.Printf("Failed walking temporary execution directory %s: %v\n", tempExecDir, err)
}
// Remove temporary execution dir (but not for absolute paths, or current dir)
if tempExecDir != "" && tempExecDir != "." && tempExecDir[0] != '/' {
remErr := os.RemoveAll(tempExecDir)
if remErr != nil {
return errors.New(fmt.Sprintf("Could not remove temp dir: %s: %s", tempExecDir, remErr))
}
}
return nil
}
var tempDirPrefix = "_scipipe_tmp"
// TempDir returns a string that is unique to a task, suitable for use
// in file paths. It is built up by merging all input filenames and parameter
// values that a task takes as input, joined with dots.
func (t *Task) TempDir() string {
pathPrefix := tempDirPrefix + "." + sanitizePathFragment(t.Name)
hashPcs := []string{t.Name}
for _, ipName := range sortedFileIPMapKeys(t.InIPs) {
hashPcs = append(hashPcs, splitAllPaths(t.InIP(ipName).Path())...)
}
for _, subIPName := range sortedFileIPSliceMapKeys(t.subStreamIPs) {
for _, subIPs := range t.subStreamIPs[subIPName] {
hashPcs = append(hashPcs, splitAllPaths(subIPs.Path())...)
}
}
for _, paramName := range sortedStringMapKeys(t.Params) {
hashPcs = append(hashPcs, paramName+"_"+t.Param(paramName))
}
for _, tagName := range sortedStringMapKeys(t.Tags) {
hashPcs = append(hashPcs, tagName+"_"+t.Tag(tagName))
}
// If resulting name is longer than 255
if len(pathPrefix) > (255 - 40 - 1) {
hashPcs = append(hashPcs, pathPrefix)
pathPrefix = tempDirPrefix
}
sha1sum := sha1.Sum([]byte(strings.Join(hashPcs, "")))
pathSegment := pathPrefix + "." + hex.EncodeToString(sha1sum[:])
return pathSegment
}
func prependParentDirPath(path string) string {
if path[0] == '/' {
return path
}
// For relative paths, add ".." to get out of current dir
return "../" + path
}
func replaceParentDirsWithPlaceholder(pathSegment string) string {
return strings.ReplaceAll(pathSegment, "../", parentDirPlaceHolder)
}
func replacePlaceholdersWithParentDirs(pathSegment string) string {
return strings.ReplaceAll(pathSegment, parentDirPlaceHolder, "../")
}