在从这里获得一些指导之后,再通读现有Go WindowsService界面的源代码,我提出了自己的答案,我将在下面尝试记录。
对于类型参考使用Windows DLL的工作时,MSDN文档在这里。
我的代码如下所示:
import ( "unsafe" "golang.org/x/sys/windows")const ( SC_ACTION_NONE= 0 SC_ACTION_RESTART = 1 SC_ACTION_REBOOT = 2 SC_ACTION_RUN_COMMAND = 3 SERVICE_CONFIG_FAILURE_ACTIONS = 2)type SERVICE_FAILURE_ACTIONS struct { ResetPeriod uint32 RebootMsg *uint16 Command *uint16 ActionsCount uint32 Actions uintptr}type SC_ACTION struct { Type uint32 Delayuint32}func setServiceFailureActions(handle windows.Handle) error { t := []SC_ACTION{ { Type: SC_ACTION_RESTART, Delay: uint32(1000) }, { Type: SC_ACTION_RESTART, Delay: uint32(10000) }, { Type: SC_ACTION_RESTART, Delay: uint32(60000) }, } m := SERVICE_FAILURE_ACTIONS{ ResetPeriod: uint32(60), ActionsCount: uint32(3), Actions: uintptr(unsafe.Pointer(&t[0])) } return windows.ChangeServiceConfig2(handle, SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&m)))}
在我的基本示例中,您需要传递服务句柄,然后将失败 *** 作设置为硬编码默认值:
- 1秒后第一次重新启动。
- 10秒后重新启动第二次。
- 重新启动第三次以及60秒后的任何后续时间。
- 60秒后重置故障计数器。
我刚刚测试过,看来工作正常。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)