-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sample Service.go
61 lines (53 loc) · 1.35 KB
/
Sample 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
package main
import (
"fmt"
"log"
"time"
"golang.org/x/sys/windows/svc"
)
type myService struct{}
func main() {
const svcname = "Client"
inService, err := svc.IsAnInteractiveSession()
if err != nil {
log.Fatalf("failed to determine if we are running in service: %v", err)
}
if inService {
runService(svcname)
return
}
}
func runService(name string) {
run := svc.Run
run(name, &myService{})
}
func (m *myService) 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}
log.Printf("my function")
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
select {
case c := <-r:
switch c.Cmd {
case svc.Interrogate:
changes <- c.CurrentStatus
time.Sleep(100 * time.Millisecond)
changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown:
output := fmt.Sprintf("-%d", c.Context)
log.Println(output)
break loop
case svc.Pause:
changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted}
case svc.Continue:
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
default:
fmt.Printf("unexpected control request #%d", c)
}
}
}
changes <- svc.Status{State: svc.StopPending}
return
}