forked from ochinchina/supervisord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
121 lines (111 loc) · 2.84 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"unicode"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
)
type Options struct {
Configuration string `short:"c" long:"configuration" description:"the configuration file" default:"supervisord.conf"`
Daemon bool `short:"d" long:"daemon" description:"run as daemon"`
EnvFile string `long:"env-file" description:"the environment file"`
}
func init() {
log.SetOutput(os.Stdout)
if runtime.GOOS == "windows" {
log.SetFormatter(&log.TextFormatter{DisableColors: true, FullTimestamp: true})
} else {
log.SetFormatter(&log.TextFormatter{DisableColors: false, FullTimestamp: true})
}
log.SetLevel(log.DebugLevel)
}
func initSignals(s *Supervisor) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-sigs
log.WithFields(log.Fields{"signal": sig}).Info("receive a signal to stop all process & exit")
s.procMgr.StopAllProcesses()
os.Exit(-1)
}()
}
var options Options
var parser = flags.NewParser(&options, flags.Default & ^flags.PrintErrors)
func LoadEnvFile() {
if len(options.EnvFile) <= 0 {
return
}
//try to open the environment file
f, err := os.Open(options.EnvFile)
if err != nil {
log.WithFields(log.Fields{"file": options.EnvFile}).Error("Fail to open environment file")
return
}
defer f.Close()
reader := bufio.NewReader(f)
for {
//for each line
line, err := reader.ReadString('\n')
if err != nil {
break
}
//if line starts with '#', it is a comment line, ignore it
line = strings.TrimSpace(line)
if len(line) > 0 && line[0] == '#' {
continue
}
//if environment variable is exported with "export"
if strings.HasPrefix(line, "export") && len(line) > len("export") && unicode.IsSpace(rune(line[len("export")])) {
line = strings.TrimSpace(line[len("export"):])
}
//split the environment variable with "="
pos := strings.Index(line, "=")
if pos != -1 {
k := strings.TrimSpace(line[0:pos])
v := strings.TrimSpace(line[pos+1:])
//if key and value are not empty, put it into the environment
if len(k) > 0 && len(v) > 0 {
os.Setenv(k, v)
}
}
}
}
func RunServer() {
// infinite loop for handling Restart ('reload' command)
LoadEnvFile()
for true {
s := NewSupervisor(options.Configuration)
initSignals(s)
if sErr, _, _, _ := s.Reload(); sErr != nil {
panic(sErr)
}
s.WaitForExit()
}
}
func main() {
if _, err := parser.Parse(); err != nil {
flagsErr, ok := err.(*flags.Error)
if ok {
switch flagsErr.Type {
case flags.ErrHelp:
fmt.Fprintln(os.Stdout, err)
os.Exit(0)
case flags.ErrCommandRequired:
if options.Daemon {
Deamonize(RunServer)
} else {
RunServer()
}
default:
fmt.Fprintf(os.Stderr, "error when parsing command: %s\n", err)
os.Exit(1)
}
}
}
}