This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
148 lines (128 loc) · 3.59 KB
/
service.go
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package main
import (
"bytes"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"sync"
"time"
"github.com/alexcoder04/friendly/v2/flinux"
"github.com/mitchellh/go-ps"
"gopkg.in/yaml.v3"
)
type Service struct {
Name string `yaml:"Name"`
Command string `yaml:"Command"`
Args []string `yaml:"Args"`
Interval uint `yaml:"Interval"`
KeepAlive bool `yaml:"KeepAlive"`
RetryNumber uint `yaml:"RetryNumber"`
When string `yaml:"When"`
Delay uint `yaml:"Delay"`
KillOld bool `yaml:"KillOld"`
}
func GetServices() []Service {
files, err := ioutil.ReadDir(*ConfigFolder)
if err != nil {
Logger.WithField("service", "_main").Fatalln("Cannot read contents of config dir")
}
services := []Service{}
for _, f := range files {
// ignore imcompatible extensions
extension := path.Ext(f.Name())
if extension != ".service" && extension != ".yml" && extension != ".yaml" {
continue
}
// read file
data, err := ioutil.ReadFile(path.Join(*ConfigFolder, f.Name()))
if err != nil {
Logger.WithField("service", "_main").Warnf("Cannot read %s, skipping\n", f.Name())
continue
}
// unmarshal data
s := Service{}
err = yaml.Unmarshal(data, &s)
if err != nil {
Logger.WithField("service", f.Name()).Warn("Cannot unmarshal, skipping")
continue
}
if s.When == "never" {
Logger.WithField("service", s.Name).Info("Disabled, skipping")
continue
}
if s.When != flinux.GetDisplayServer() && s.When != "always" {
Logger.WithField("service", s.Name).Info("Disabled on this display server, skipping")
continue
}
services = append(services, s)
Logger.WithField("service", s.Name).Info("Loaded")
}
return services
}
func GetPidByName(name string) int {
processes, err := ps.Processes()
if err != nil {
return 0
}
for _, p := range processes {
if p.Executable() == name {
return p.Pid()
}
}
return 0
}
func HandleService(s Service, wg *sync.WaitGroup) {
defer wg.Done()
if s.Delay > 0 {
Logger.WithField("service", s.Name).Infof("Delaying by %ds", s.Delay)
time.Sleep(time.Duration(s.Delay) * time.Second)
}
f, err := os.OpenFile(GetLogFileForService(s.Name), os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
Logger.WithField("service", s.Name).Warn("Failed to open log file, skipping")
return
}
if s.KillOld {
pid := GetPidByName(s.Command)
if pid != 0 {
proc, err := os.FindProcess(pid)
if err == nil {
proc.Kill()
}
}
}
for i := 0; true; i++ {
if i > int(s.RetryNumber) && s.Interval == 0 {
Logger.WithField("service", s.Name).Warn("Retry number exceeded, stopping")
return
}
var stdBuf bytes.Buffer
mw := io.MultiWriter(f, &stdBuf)
command := exec.Command(s.Command, s.Args...)
command.Stdout = mw
command.Stderr = mw
Logger.WithField("service", s.Name).Info("Starting")
err := command.Start()
if err != nil {
Logger.WithField("service", s.Name).Warn("Failed to start, retrying in %ds\n", s.RetryNumber)
time.Sleep(time.Duration(s.Interval) * time.Second)
}
err = command.Wait()
if err != nil {
Logger.WithField("service", s.Name).Warnf("Error waiting for command to finish (%s), restarting in %ds", err.Error(), s.RetryNumber)
time.Sleep(time.Duration(s.Interval) * time.Second)
}
if !flinux.GuiRunning() {
Logger.WithField("service", s.Name).Warn("Desktop seems to be shut down, stopping")
return
}
if !s.KeepAlive && s.Interval == 0 {
Logger.WithField("service", s.Name).Info("Finished")
return
}
Logger.WithField("service", s.Name).Infof("Restarting in %ds", s.Interval)
time.Sleep(time.Duration(s.Interval) * time.Second)
}
}