-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.go
105 lines (86 loc) · 1.61 KB
/
service.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
package webdriver
import (
"errors"
"io"
"os"
"os/exec"
"runtime"
"syscall"
"time"
)
type CheckStatusFunc func() bool
type Service struct {
path string
args []string
cmd *exec.Cmd
}
func NewService(path string, args []string) *Service {
return &Service{
path: path,
args: args,
}
}
func (s *Service) Start() error {
if s.cmd != nil {
return errors.New("webdriver already running")
}
s.cmd = exec.Command(s.path, s.args...) // nolint gosec
stdout, err := s.cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := s.cmd.StderrPipe()
if err != nil {
return err
}
if err := s.cmd.Start(); err != nil {
return err
}
go func() {
_, _ = io.Copy(os.Stdout, stdout)
}()
go func() {
_, _ = io.Copy(os.Stderr, stderr)
}()
return nil
}
func (s *Service) Stop() error {
if s.cmd == nil {
return errors.New("webDriver not running")
}
if runtime.GOOS == "windows" {
if err := s.cmd.Process.Kill(); err != nil {
return err
}
} else {
if err := s.cmd.Process.Signal(syscall.SIGTERM); err != nil {
return err
}
}
return nil
}
func (s *Service) WaitForBoot(timeout time.Duration, fn CheckStatusFunc) error {
timeoutChan := time.After(timeout)
failedChan := make(chan struct{}, 1)
startedChan := make(chan struct{})
go func() {
up := fn()
for !up {
select {
case <-failedChan:
return
default:
time.Sleep(500 * time.Millisecond) // nolint gomnd
up = fn()
}
}
startedChan <- struct{}{}
}()
select {
case <-timeoutChan:
failedChan <- struct{}{}
return errors.New("failed to start before timeout")
case <-startedChan:
return nil
}
}