| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package notify |
| 16 |
|
| 17 |
import ( |
| 18 |
"log" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"golang.org/x/sys/windows/svc" |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
var globalStatus chan<- svc.Status |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
func SetGlobalStatus(status chan<- svc.Status) { |
| 32 |
globalStatus = status |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func Ready() error { |
| 38 |
if globalStatus != nil { |
| 39 |
globalStatus <- svc.Status{ |
| 40 |
State: svc.Running, |
| 41 |
Accepts: svc.AcceptStop | svc.AcceptShutdown, |
| 42 |
} |
| 43 |
} |
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
func Reloading() error { |
| 50 |
if globalStatus != nil { |
| 51 |
globalStatus <- svc.Status{State: svc.StartPending} |
| 52 |
} |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
func Stopping() error { |
| 59 |
if globalStatus != nil { |
| 60 |
globalStatus <- svc.Status{State: svc.StopPending} |
| 61 |
} |
| 62 |
return nil |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
func Status(name string) error { |
| 69 |
if globalStatus == nil { |
| 70 |
return nil |
| 71 |
} |
| 72 |
|
| 73 |
var state svc.State |
| 74 |
var accepts svc.Accepted |
| 75 |
accepts = 0 |
| 76 |
|
| 77 |
switch strings.ToLower(name) { |
| 78 |
case "stopped": |
| 79 |
state = svc.Stopped |
| 80 |
case "start_pending": |
| 81 |
state = svc.StartPending |
| 82 |
case "stop_pending": |
| 83 |
state = svc.StopPending |
| 84 |
case "running": |
| 85 |
state = svc.Running |
| 86 |
accepts = svc.AcceptStop | svc.AcceptShutdown |
| 87 |
case "continue_pending": |
| 88 |
state = svc.ContinuePending |
| 89 |
case "pause_pending": |
| 90 |
state = svc.PausePending |
| 91 |
case "paused": |
| 92 |
state = svc.Paused |
| 93 |
accepts = svc.AcceptStop | svc.AcceptShutdown | svc.AcceptPauseAndContinue |
| 94 |
default: |
| 95 |
log.Printf("unknown state: %s", name) |
| 96 |
return nil |
| 97 |
} |
| 98 |
|
| 99 |
globalStatus <- svc.Status{State: state, Accepts: accepts} |
| 100 |
return nil |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
func Error(err error, code int) error { |
| 106 |
if globalStatus != nil { |
| 107 |
globalStatus <- svc.Status{ |
| 108 |
State: svc.StopPending, |
| 109 |
ServiceSpecificExitCode: uint32(code), |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return nil |
| 114 |
} |