-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker.go
102 lines (88 loc) · 1.8 KB
/
worker.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
package main
import (
"bytes"
"errors"
"fmt"
"os/exec"
"path/filepath"
"strings"
)
type job struct {
fileName string
report guetzliReport
settings *settings
color colorFunc
logger *logger
errored bool
quit chan bool
done chan bool
}
func do(j *job) {
var (
outb bytes.Buffer
errb bytes.Buffer
)
args := []string{
"--quality",
fmt.Sprint(j.settings.quality),
"--memlimit",
fmt.Sprint(j.settings.memlimit),
}
if j.settings.nomemlimit {
args = append(args, "--nomemlimit")
}
if j.settings.logLevel == "debug" {
args = append(args, "--verbose")
}
outputFileName := j.fileName
if j.settings.source != j.settings.output {
outputFileName = strings.TrimSuffix(strings.TrimSuffix(outputFileName, filepath.Ext(outputFileName)), ".") + ".jpg"
}
args = append(args, j.settings.source+j.fileName)
args = append(args, j.settings.output+outputFileName)
j.fileName = outputFileName
cmd := exec.Command("guetzli", args...)
cmd.Stdout = &outb
cmd.Stderr = &errb
j.logger.log(logForJob(j)("start"))
err := cmd.Start()
if err != nil {
j.logger.log(errors.New(logForJob(j)(err.Error())))
j.errored = true
j.done <- false
close(j.done)
return
}
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
close(done)
}()
select {
case <-j.quit:
err := cmd.Process.Kill()
if err != nil {
j.logger.log(errors.New(logForJob(j)(err.Error())))
}
j.logger.log(logForJob(j)("cancelled"))
j.done <- false
close(j.done)
break
case err := <-done:
if err != nil {
j.errored = true
j.logger.log(errors.New(logForJob(j)(errb.String())))
j.done <- false
close(j.done)
} else {
doneMsg := outb.String()
if doneMsg == "" {
doneMsg = "done"
}
j.logger.log(logForJob(j)(doneMsg))
j.done <- true
close(j.done)
}
break
}
}