-
Notifications
You must be signed in to change notification settings - Fork 685
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
the ability to set the Recovery options #49
Comments
Right now, manually. I would be open to a patch that used the Config.Option map to set a windows specific recovery mode. |
there's |
I never had the need for that, but @bogdanteleaga implemented something similar here https://github.com/juju/juju/blob/master/service/windows/service_windows.go#L396 Alex |
thanks @alexbrainman for pointing me at this. I had the time to try and figure out and copy over some code from so what I did is that I extracted the code into one individual Go main package to ease the process of debugging it package main
import (
"fmt"
"unsafe"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc/mgr"
)
// this service should get its recovery options set to restart
var SERVICE_NAME = "insert service name here"
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms681988(v=vs.85).aspx
const (
SERVICE_CONFIG_FAILURE_ACTIONS = 2
SERVICE_CONFIG_FAILURE_ACTIONS_FLAG = 4
)
const (
SC_ACTION_NONE = iota
SC_ACTION_RESTART
SC_ACTION_REBOOT
SC_ACTION_RUN_COMMAND
)
type serviceAction struct {
actionType uint16
delay uint32
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms685939(v=vs.85).aspx
type serviceFailureActions struct {
dwResetPeriod uint32
lpRebootMsg *uint16
lpCommand *uint16
cActions uint32
scAction *serviceAction
}
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms685937(v=vs.85).aspx
type serviceFailureActionsFlag struct {
failureActionsOnNonCrashFailures bool
}
func main() {
m, err := mgr.Connect()
if err != nil {
panic(err)
}
defer m.Disconnect()
s, err := m.OpenService(SERVICE_NAME)
if err != nil {
panic(err)
}
defer s.Close()
action := serviceAction{
actionType: SC_ACTION_RESTART,
delay: 5000,
}
failActions := serviceFailureActions{
dwResetPeriod: 5,
lpRebootMsg: nil,
lpCommand: nil,
cActions: 1,
scAction: &action,
}
err = windows.ChangeServiceConfig2(s.Handle, SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&failActions)))
if err != nil {
panic(err)
}
flag := serviceFailureActionsFlag{
failureActionsOnNonCrashFailures: true,
}
err = windows.ChangeServiceConfig2(s.Handle, SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, (*byte)(unsafe.Pointer(&flag)))
if err != nil { // it will hit this error: The parameter is incorrect
panic(err)
}
}
perhaps @gabriel-samfira can help us with this. |
So juju is still using go1.2.1. I tried it out and it seems that using 1.2.1 the code works, but you have to use gabriel's forks that actually compile on 1.2.1. The 2nd call does seem to panic on 1.5, which is peculiar and problematic at the same time since juju wants to migrate to 1.5. I will have to try a new go environment though, the one I'm using now is all sorts of messed up and might give odd results, but it does confirm what you found so far. EDIT: I can confirm this doesn't work with neither a development go version of 1.5 I had installed nor go 1.5.2 |
switch from bool to int in serviceFailureActionsFlag and give that a shot. |
Yup converting it to int works in go 1.5 |
@bogdanteleaga @gabriel-samfira your help is much appreciated, it works for me now with |
that is great! Glad we could help :). |
Windows BOOL type occupies 4 bytes. But Go int is int64 on windows/amd64, isn't it? Better use uint32 (or int32) where Windows BOOL is used. Alex |
@alexbrainman good point. they both works after testing. @kardianos how do you propose migrating such code into |
Check this out #33 (comment) for some ideas on how to implement this. |
…yscall-for-go15 service/windows: comply syscall structure with go1.5 As seen [here](kardianos/service#49 (comment)), it seems like using booleans in go1.5 causes a problem that can be easily solved by using int32. (Review request: http://reviews.vapour.ws/r/3383/)
…aults Update server_url defaults for Graylog 2.1.x
For anyone wondering, it was implemented via these options: https://github.com/kardianos/service/blob/master/service.go#L197-L206 |
I believe the current service package doesn't not offer the ability to set the recovery options for windows, such as on first failure, second failure, subsequent failure, to restart the service, or restart the computer.
mgr.Config type
what's the best way to set my service to restart on failure ?
The text was updated successfully, but these errors were encountered: