Browse code

Windows: Set service recovery options

Signed-off-by: John Howard <jhoward@microsoft.com>

John Howard authored on 2016/10/25 07:34:20
Showing 1 changed files
... ...
@@ -9,6 +9,8 @@ import (
9 9
 	"os/exec"
10 10
 	"path/filepath"
11 11
 	"syscall"
12
+	"time"
13
+	"unsafe"
12 14
 
13 15
 	"github.com/Sirupsen/logrus"
14 16
 	"github.com/spf13/pflag"
... ...
@@ -183,6 +185,40 @@ func registerService() error {
183 183
 		return err
184 184
 	}
185 185
 	defer s.Close()
186
+
187
+	// See http://stackoverflow.com/questions/35151052/how-do-i-configure-failure-actions-of-a-windows-service-written-in-go
188
+	const (
189
+		scActionNone       = 0
190
+		scActionRestart    = 1
191
+		scActionReboot     = 2
192
+		scActionRunCommand = 3
193
+
194
+		serviceConfigFailureActions = 2
195
+	)
196
+
197
+	type serviceFailureActions struct {
198
+		ResetPeriod  uint32
199
+		RebootMsg    *uint16
200
+		Command      *uint16
201
+		ActionsCount uint32
202
+		Actions      uintptr
203
+	}
204
+
205
+	type scAction struct {
206
+		Type  uint32
207
+		Delay uint32
208
+	}
209
+	t := []scAction{
210
+		{Type: scActionRestart, Delay: uint32(60 * time.Second / time.Millisecond)},
211
+		{Type: scActionRestart, Delay: uint32(60 * time.Second / time.Millisecond)},
212
+		{Type: scActionNone},
213
+	}
214
+	lpInfo := serviceFailureActions{ResetPeriod: uint32(24 * time.Hour / time.Second), ActionsCount: uint32(3), Actions: uintptr(unsafe.Pointer(&t[0]))}
215
+	err = windows.ChangeServiceConfig2(s.Handle, serviceConfigFailureActions, (*byte)(unsafe.Pointer(&lpInfo)))
216
+	if err != nil {
217
+		return err
218
+	}
219
+
186 220
 	err = eventlog.Install(*flServiceName, p, false, eventlog.Info|eventlog.Warning|eventlog.Error)
187 221
 	if err != nil {
188 222
 		return err