-
Notifications
You must be signed in to change notification settings - Fork 26
/
main_windows.go
64 lines (55 loc) · 1.38 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
package main
import (
"sync"
"github.com/ferama/rospo/cmd"
"github.com/judwhite/go-svc"
wsvc "golang.org/x/sys/windows/svc"
)
// rospo implements svc.Service
type rospo struct {
wg sync.WaitGroup
quit chan bool
}
// Init initialize the rospo service
func (r *rospo) Init(env svc.Environment) error {
return nil
}
// Start starts the rospo windows service
func (r *rospo) Start() error {
// The Start method must not block, or Windows may assume your service failed
// to start. Launch a Goroutine here to do something interesting/blocking.
r.wg.Add(1)
go func() {
go cmd.Execute()
<-r.quit
r.wg.Done()
}()
return nil
}
// Stop shutdown the windows service
func (r *rospo) Stop() error {
// The Stop method is invoked by stopping the Windows service, or by pressing Ctrl+C on the console.
// This method may block, but it's a good idea to finish quickly or your process may be killed by
// Windows during a shutdown/reboot. As a general rule you shouldn't rely on graceful shutdown.
close(r.quit)
r.wg.Wait()
return nil
}
func main() {
isWindowsService, err := wsvc.IsWindowsService()
if err != nil {
panic(err)
}
if isWindowsService {
prg := &rospo{
quit: make(chan bool),
}
// Call svc.Run to start rospo service
if err := svc.Run(prg); err != nil {
panic(err)
}
} else {
// run as usuale if we are not running as windows service
cmd.Execute()
}
}