-
Notifications
You must be signed in to change notification settings - Fork 46
/
background.go
110 lines (95 loc) · 2.68 KB
/
background.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
// Copyright (c) 2018 Dean Jackson <[email protected]>
// MIT Licence - http://opensource.org/licenses/MIT
package aw
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"github.com/deanishe/awgo/util"
)
// ErrJobExists is the error returned by RunInBackground if a job with
// the given name is already running.
type ErrJobExists struct {
Name string // Name of the job
Pid int // PID of the running job
}
// Error implements error interface.
func (err ErrJobExists) Error() string {
return fmt.Sprintf(`job "%s" already running with PID %d`, err.Name, err.Pid)
}
// Is returns true if target is of type ErrJobExists.
func (err ErrJobExists) Is(target error) bool {
_, ok := target.(ErrJobExists)
return ok
}
// IsJobExists returns true if error is of type or wraps ErrJobExists.
func IsJobExists(err error) bool {
return errors.Is(err, ErrJobExists{})
}
// RunInBackground executes cmd in the background. It returns an
// ErrJobExists error if a job of the same name is already running.
func (wf *Workflow) RunInBackground(jobName string, cmd *exec.Cmd) error {
if wf.IsRunning(jobName) {
pid, _ := wf.getPid(jobName)
return ErrJobExists{jobName, pid}
}
if cmd.SysProcAttr == nil {
cmd.SysProcAttr = &syscall.SysProcAttr{}
}
// Prevent process from being killed when parent is
cmd.SysProcAttr.Setpgid = true
if err := cmd.Start(); err != nil {
return fmt.Errorf("execute command %v: %w", cmd, err)
}
return wf.savePid(jobName, cmd.Process.Pid)
}
// Kill stops a background job.
func (wf *Workflow) Kill(jobName string) error {
pid, err := wf.getPid(jobName)
if err != nil {
return err
}
p := wf.pidFile(jobName)
err = syscall.Kill(pid, syscall.SIGTERM)
os.Remove(p)
return err
}
// IsRunning returns true if a job with name jobName is currently running.
func (wf *Workflow) IsRunning(jobName string) bool {
pid, err := wf.getPid(jobName)
if err != nil {
return false
}
if err = syscall.Kill(pid, 0); err != nil {
// Delete stale PID file
os.Remove(wf.pidFile(jobName))
return false
}
return true
}
// Save PID to a job-specific file.
func (wf *Workflow) savePid(jobName string, pid int) error {
return ioutil.WriteFile(wf.pidFile(jobName), []byte(strconv.Itoa(pid)), 0600)
}
// Return PID for job.
func (wf *Workflow) getPid(jobName string) (int, error) {
data, err := ioutil.ReadFile(wf.pidFile(jobName))
if err != nil {
return 0, err
}
pid, err := strconv.Atoi(string(data))
if err != nil {
return 0, err
}
return pid, nil
}
// Path to PID file for job.
func (wf *Workflow) pidFile(jobName string) string {
dir := util.MustExist(filepath.Join(wf.awCacheDir(), "jobs"))
return filepath.Join(dir, jobName+".pid")
}