forked from DataDog/datadog-agent
/
service.go
109 lines (97 loc) · 3.17 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
106
107
108
109
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build windows
// +build windows
package service
import (
"fmt"
"time"
"github.com/StackVista/stackstate-agent/cmd/agent/app"
"github.com/StackVista/stackstate-agent/cmd/agent/common"
"github.com/StackVista/stackstate-agent/cmd/agent/common/signals"
"github.com/StackVista/stackstate-agent/pkg/config"
"github.com/StackVista/stackstate-agent/pkg/util/log"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/eventlog"
)
var elog debug.Log
type agentWindowsService struct{}
// Execute sets up the configuration and runs the Agent as a Windows service
func (m *agentWindowsService) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPreShutdown
changes <- svc.Status{State: svc.StartPending}
if err := common.CheckAndUpgradeConfig(); err != nil {
elog.Warning(0x80000002, err.Error())
// continue running with what we have.
}
if err := app.StartAgent(); err != nil {
log.Errorf("Failed to start agent %v", err)
elog.Error(0xc000000B, err.Error())
errno = 1 // indicates non-successful return from handler.
changes <- svc.Status{State: svc.Stopped}
return
}
elog.Info(0x40000003, config.ServiceName)
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
loop:
for {
select {
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:
log.Info("Received stop message from service control manager")
elog.Info(0x4000000c, config.ServiceName)
break loop
case svc.PreShutdown:
log.Info("Received pre shutdown message from service control manager")
elog.Info(0x40000010, config.ServiceName)
break loop
case svc.Shutdown:
log.Info("Received shutdown message from service control manager")
elog.Info(0x4000000d, config.ServiceName)
break loop
default:
log.Warnf("unexpected control request #%d", c)
elog.Error(0xc0000009, fmt.Sprint(c.Cmd))
}
case <-signals.Stopper:
elog.Info(0x4000000a, config.ServiceName)
break loop
}
}
elog.Info(0x4000000d, config.ServiceName)
log.Infof("Initiating service shutdown")
changes <- svc.Status{State: svc.StopPending}
app.StopAgent()
changes <- svc.Status{State: svc.Stopped}
return
}
// RunService runs the Agent as a Windows service
func RunService(isDebug bool) {
var err error
if isDebug {
elog = debug.New(config.ServiceName)
} else {
elog, err = eventlog.Open(config.ServiceName)
if err != nil {
return
}
}
defer elog.Close()
elog.Info(0x40000007, config.ServiceName)
run := svc.Run
err = run(config.ServiceName, &agentWindowsService{})
if err != nil {
elog.Error(0xc0000008, err.Error())
return
}
elog.Info(0x40000004, config.ServiceName)
}