forked from cloudfoundry-attic/cf-mssql-broker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_windows.go
105 lines (88 loc) · 2.17 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
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
// +build windows
package main
import (
"flag"
"io"
"os"
"path"
"golang.org/x/sys/windows/svc"
)
type WindowsService struct {
writer io.Writer
}
func (ws *WindowsService) Execute(args []string, r <-chan svc.ChangeRequest, s chan<- svc.Status) (svcSpecificEC bool, exitCode uint32) {
s <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue}
go runMain(ws.writer)
loop:
for {
select {
case change := <-r:
switch change.Cmd {
case svc.Interrogate:
s <- change.CurrentStatus
case svc.Stop, svc.Shutdown:
{
break loop
}
case svc.Pause:
s <- svc.Status{State: svc.Paused, Accepts: svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue}
case svc.Continue:
s <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue}
default:
{
break loop
}
}
}
}
s <- svc.Status{State: svc.StopPending}
return
}
func main() {
var logDir = ""
flag.StringVar(&logDir, "logDir","", "The directory where the logs will be stored")
var serviceName = flag.String("serviceName", "mssql_broker", "The name of the service as installed in Windows SCM")
if !flag.Parsed() {
flag.Parse()
}
interactiveMode, err := svc.IsAnInteractiveSession()
if err != nil {
panic(err.Error())
}
if interactiveMode {
runMain(os.Stdout)
} else {
var err error
if logDir == "" {
//will default to %windir%\System32\
workingDir, err := os.Getwd()
if err != nil {
panic(err.Error())
}
logDir = path.Join(workingDir, "logs")
}
if _, err := os.Stat(logDir); os.IsNotExist(err) {
err := os.Mkdir(logDir, 0666)
if err != nil {
panic(err.Error())
}
}
logFilePath := path.Join(logDir, "mssql_broker.log")
logFile, err := os.OpenFile(logFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND|os.O_SYNC, 0660)
if err != nil {
panic(err.Error())
}
defer logFile.Close()
//setting stderr & stdout
os.Stdout = logFile
os.Stderr = logFile
fileWriter := NewWinFileWriter(logFile)
ws := WindowsService{
writer: fileWriter,
}
err = svc.Run(*serviceName, &ws)
if err != nil {
panic(err.Error())
}
}
}