Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Use crash-loop.ps1 to restart rancher-agent windows service
Browse files Browse the repository at this point in the history
Add a crash-loop.ps1 to bring up the crashed service. So in
rancher agent, it needs to set it failure action to run the
crash-loop.ps1
  • Loading branch information
Yuxing authored and Denise committed Feb 12, 2018
1 parent 589ac50 commit b7c32bb
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions register/service_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import (

"bytes"
"fmt"
"os/exec"
"path/filepath"
"syscall"

"github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/debug"
"golang.org/x/sys/windows/svc/eventlog"
"golang.org/x/sys/windows/svc/mgr"
"os/exec"
"path/filepath"
"syscall"
)

// credits for https://github.com/docker/docker/blob/5c1826ec4de381df9f739ce0de28e37d4f734d47/cmd/dockerd/service_windows.go
Expand All @@ -26,6 +27,7 @@ const (
logFile = "C:/ProgramData/rancher/agent.log"
rancherPanicFile = "C:/ProgramData/rancher/panic.log"
homeDir = "C:/ProgramData/rancher"
restartCommand = `"powershell.exe" -File "c:\program files\rancher\crash-loop.ps1" agent`
// These should match the values in event_messages.mc.
eventInfo = 1
eventWarn = 1
Expand Down Expand Up @@ -196,11 +198,17 @@ func registerService(url string) error {
Delay uint32
}
t := []scAction{
{Type: scActionRestart, Delay: uint32(60 * time.Second / time.Millisecond)},
{Type: scActionRestart, Delay: uint32(60 * time.Second / time.Millisecond)},
{Type: scActionNone},
{Type: scActionRunCommand, Delay: uint32(15 * time.Second / time.Millisecond)},
{Type: scActionRunCommand, Delay: uint32(15 * time.Second / time.Millisecond)},
{Type: scActionRunCommand, Delay: uint32(15 * time.Second / time.Millisecond)},
}
cmd, _ := syscall.UTF16PtrFromString(restartCommand)
lpInfo := serviceFailureActions{
ResetPeriod: uint32(24 * time.Hour / time.Second),
ActionsCount: uint32(3),
Actions: uintptr(unsafe.Pointer(&t[0])),
Command: cmd,
}
lpInfo := serviceFailureActions{ResetPeriod: uint32(24 * time.Hour / time.Second), ActionsCount: uint32(3), Actions: uintptr(unsafe.Pointer(&t[0]))}
err = windows.ChangeServiceConfig2(s.Handle, serviceConfigFailureActions, (*byte)(unsafe.Pointer(&lpInfo)))
if err != nil {
return err
Expand Down

0 comments on commit b7c32bb

Please sign in to comment.