-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_windows.go
78 lines (70 loc) · 1.8 KB
/
main_windows.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
// +build windows
package main
import (
"os/exec"
"golang.org/x/sys/windows/svc"
"time"
"fmt"
"regexp"
"strings"
)
type WindowsService struct {
running func()
}
func splitCommands(command string) []string {
r := regexp.MustCompile("'.+'|\".+\"|\\S+")
m := r.FindAllString(command, -1)
for i, item := range m {
if strings.HasPrefix(item, `"`) {
item = item[1 : len(item)-1]
}
if strings.HasSuffix(item, `"`) {
item = item[:len(item)-1]
}
m[i] = item
}
return m
}
func getStartCommands(command string) *exec.Cmd {
command = "/c " + command
args := splitCommands(command)
fmt.Println("args:", args)
return exec.Command("cmd", args...)
}
func (m *WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue
changes <- svc.Status{State: svc.StartPending}
fasttick := time.Tick(500 * time.Millisecond)
slowtick := time.Tick(2 * time.Second)
tick := fasttick
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
go m.running()
loop:
for {
select {
case <-tick:
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
// Testing deadlock from https://code.google.com/p/winsvc/issues/detail?id=4
time.Sleep(100 * time.Millisecond)
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
break loop
case svc.Pause:
changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted}
tick = slowtick
case svc.Continue:
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
tick = fasttick
default:
}
}
}
changes <- svc.Status{State: svc.StopPending}
return
}
func runningInService(running func()) {
svc.Run("WebFS", &WindowsService{running})
}