-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathservice.go
More file actions
81 lines (68 loc) · 1.61 KB
/
Copy pathservice.go
File metadata and controls
81 lines (68 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"fmt"
"om/service"
log "github.com/sirupsen/logrus"
)
const (
serviceName = "oms"
serviceDisplayName = "OpenResty Manager Service"
serviceDescription = "One powerful manager for OpenResty"
)
// program represents the program that will be launched by as a service or a
// daemon.
type program struct{}
// Start implements service.Interface interface for *program.
func (p *program) Start(_ service.Service) (err error) {
go p.run()
return nil
}
func (p *program) run() {
OMRun()
}
// Stop implements service.Interface interface for *program.
func (p *program) Stop(_ service.Service) error {
log.Info("shutdown signal received")
OMStop()
return nil
}
// "start", "stop", "restart", "install", "uninstall"
func serviceHandler(action string) {
prg := &program{}
var s service.Service
var err error
svcConfig := &service.Config{
Name: serviceName,
DisplayName: serviceDisplayName,
Description: serviceDescription,
}
svcConfig.Dependencies = []string{"After=network.target syslog.target"}
options := make(service.KeyValue)
options["KillMod"] = "process"
svcConfig.Option = options
if s, err = service.New(prg, svcConfig); err != nil {
log.Fatal(err)
}
switch action {
case "run":
if err = s.Run(); err != nil {
log.Fatal(err)
}
case "status":
status, err := s.Status()
if err != nil {
log.Fatal(err)
}
switch status {
case service.StatusRunning:
fmt.Println("Running")
case service.StatusStopped:
fmt.Println("Stopped")
}
default:
if err = service.Control(s, action); err != nil {
log.Fatal(err)
}
fmt.Println("Success")
}
}