/
process.go
1005 lines (893 loc) · 27.2 KB
/
process.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package process
import (
"fmt"
"io"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/ochinchina/filechangemonitor"
"github.com/ochinchina/supervisord/config"
"github.com/ochinchina/supervisord/events"
"github.com/ochinchina/supervisord/logger"
"github.com/ochinchina/supervisord/signals"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
)
// State the state of process
type State int
const (
// Stopped the stopped state
Stopped State = iota
// Starting the starting state
Starting = 10
// Running the running state
Running = 20
// Backoff the backoff state
Backoff = 30
// Stopping the stopping state
Stopping = 40
// Exited the Exited state
Exited = 100
// Fatal the Fatal state
Fatal = 200
// Unknown the unknown state
Unknown = 1000
)
var scheduler *cron.Cron = nil
func init() {
scheduler = cron.New(cron.WithSeconds())
scheduler.Start()
}
// String convert State to human-readable string
func (p State) String() string {
switch p {
case Stopped:
return "Stopped"
case Starting:
return "Starting"
case Running:
return "Running"
case Backoff:
return "Backoff"
case Stopping:
return "Stopping"
case Exited:
return "Exited"
case Fatal:
return "Fatal"
default:
return "Unknown"
}
}
// Process the program process management data
type Process struct {
supervisorID string
config *config.Entry
cmd *exec.Cmd
startTime time.Time
stopTime time.Time
state State
// true if process is starting
inStart bool
// true if the process is stopped by user
stopByUser bool
retryTimes *int32
lock sync.RWMutex
stdin io.WriteCloser
StdoutLog logger.Logger
StderrLog logger.Logger
}
// NewProcess creates new Process object
func NewProcess(supervisorID string, config *config.Entry) *Process {
proc := &Process{supervisorID: supervisorID,
config: config,
cmd: nil,
startTime: time.Unix(0, 0),
stopTime: time.Unix(0, 0),
state: Stopped,
inStart: false,
stopByUser: false,
retryTimes: new(int32)}
proc.config = config
proc.cmd = nil
proc.addToCron()
return proc
}
func (p *Process) GetConfig() *config.Entry {
return p.config
}
// add this process to crontab
func (p *Process) addToCron() {
s := p.config.GetString("cron", "")
if s != "" {
log.WithFields(log.Fields{"program": p.GetName()}).Info("try to create cron program with cron expression:", s)
scheduler.AddFunc(s, func() {
log.WithFields(log.Fields{"program": p.GetName()}).Info("start cron program")
if !p.isRunning() {
p.Start(false)
}
})
}
}
// Start process
// Args:
// wait - true, wait the program started or failed
func (p *Process) Start(wait bool) {
log.WithFields(log.Fields{"program": p.GetName()}).Info("try to start program")
p.lock.Lock()
if p.inStart {
log.WithFields(log.Fields{"program": p.GetName()}).Info("Don't start program again, program is already started")
p.lock.Unlock()
return
}
p.inStart = true
p.stopByUser = false
p.lock.Unlock()
var runCond *sync.Cond
if wait {
runCond = sync.NewCond(&sync.Mutex{})
runCond.L.Lock()
}
go func() {
for {
p.run(func() {
if wait {
runCond.L.Lock()
runCond.Signal()
runCond.L.Unlock()
}
})
// avoid print too many logs if fail to start program too quickly
if time.Now().Unix()-p.startTime.Unix() < 2 {
time.Sleep(5 * time.Second)
}
if p.stopByUser {
log.WithFields(log.Fields{"program": p.GetName()}).Info("Stopped by user, don't start it again")
break
}
if !p.isAutoRestart() {
log.WithFields(log.Fields{"program": p.GetName()}).Info("Don't start the stopped program because its autorestart flag is false")
break
}
}
p.lock.Lock()
p.inStart = false
p.lock.Unlock()
}()
if wait {
runCond.Wait()
runCond.L.Unlock()
}
}
// GetName returns name of program or event listener
func (p *Process) GetName() string {
if p.config.IsProgram() {
return p.config.GetProgramName()
} else if p.config.IsEventListener() {
return p.config.GetEventListenerName()
} else {
return ""
}
}
// GetGroup returns group the program belongs to
func (p *Process) GetGroup() string {
return p.config.Group
}
// GetDescription returns process status description
func (p *Process) GetDescription() string {
p.lock.RLock()
defer p.lock.RUnlock()
if p.state == Running {
seconds := int(time.Now().Sub(p.startTime).Seconds())
minutes := seconds / 60
hours := minutes / 60
days := hours / 24
if days > 0 {
return fmt.Sprintf("pid %d, uptime %d days, %d:%02d:%02d", p.cmd.Process.Pid, days, hours%24, minutes%60, seconds%60)
}
return fmt.Sprintf("pid %d, uptime %d:%02d:%02d", p.cmd.Process.Pid, hours%24, minutes%60, seconds%60)
} else if p.state != Stopped {
return p.stopTime.String()
}
return ""
}
// GetExitstatus returns exit status of the process if the program exit
func (p *Process) GetExitstatus() int {
p.lock.RLock()
defer p.lock.RUnlock()
if p.state == Exited || p.state == Backoff {
if p.cmd.ProcessState == nil {
return 0
}
status, ok := p.cmd.ProcessState.Sys().(syscall.WaitStatus)
if ok {
return status.ExitStatus()
}
}
return 0
}
// GetPid returns pid of running process or 0 it is not in running status
func (p *Process) GetPid() int {
p.lock.RLock()
defer p.lock.RUnlock()
if p.state == Stopped || p.state == Fatal || p.state == Unknown || p.state == Exited || p.state == Backoff {
return 0
}
return p.cmd.Process.Pid
}
// GetState returns process state
func (p *Process) GetState() State {
return p.state
}
// GetStartTime returns process start time
func (p *Process) GetStartTime() time.Time {
return p.startTime
}
// GetStopTime returns process stop time
func (p *Process) GetStopTime() time.Time {
switch p.state {
case Starting:
fallthrough
case Running:
fallthrough
case Stopping:
return time.Unix(0, 0)
default:
return p.stopTime
}
}
// GetStdoutLogfile returns program stdout log filename
func (p *Process) GetStdoutLogfile() string {
fileName := p.config.GetStringExpression("stdout_logfile", "/dev/null")
expandFile, err := PathExpand(fileName)
if err != nil {
return fileName
}
return expandFile
}
// GetStderrLogfile returns program stderr log filename
func (p *Process) GetStderrLogfile() string {
fileName := p.config.GetStringExpression("stderr_logfile", "/dev/null")
expandFile, err := PathExpand(fileName)
if err != nil {
return fileName
}
return expandFile
}
func (p *Process) getStartSeconds() int64 {
return int64(p.config.GetInt("startsecs", 1))
}
func (p *Process) getRestartPause() int {
return p.config.GetInt("restartpause", 0)
}
func (p *Process) getStartRetries() int32 {
return int32(p.config.GetInt("startretries", 3))
}
func (p *Process) isAutoStart() bool {
return p.config.GetString("autostart", "true") == "true"
}
// GetPriority returns program priority (as it set in config) with default value of 999
func (p *Process) GetPriority() int {
return p.config.GetInt("priority", 999)
}
func (p *Process) getNumberProcs() int {
return p.config.GetInt("numprocs", 1)
}
// SendProcessStdin sends data to process stdin
func (p *Process) SendProcessStdin(chars string) error {
if p.stdin != nil {
_, err := p.stdin.Write([]byte(chars))
return err
}
return fmt.Errorf("NO_FILE")
}
// check if the process should be
func (p *Process) isAutoRestart() bool {
autoRestart := p.config.GetString("autorestart", "unexpected")
if autoRestart == "false" {
return false
} else if autoRestart == "true" {
return true
} else {
p.lock.RLock()
defer p.lock.RUnlock()
if p.cmd != nil && p.cmd.ProcessState != nil {
exitCode, err := p.getExitCode()
// If unexpected, the process will be restarted when the program exits
// with an exit code that is not one of the exit codes associated with
// this process’ configuration (see exitcodes).
return err == nil && !p.inExitCodes(exitCode)
}
}
return false
}
func (p *Process) inExitCodes(exitCode int) bool {
for _, code := range p.getExitCodes() {
if code == exitCode {
return true
}
}
return false
}
func (p *Process) getExitCode() (int, error) {
if p.cmd.ProcessState == nil {
return -1, fmt.Errorf("no exit code")
}
if status, ok := p.cmd.ProcessState.Sys().(syscall.WaitStatus); ok {
return status.ExitStatus(), nil
}
return -1, fmt.Errorf("no exit code")
}
func (p *Process) getExitCodes() []int {
strExitCodes := strings.Split(p.config.GetString("exitcodes", "0,2"), ",")
result := make([]int, 0)
for _, val := range strExitCodes {
i, err := strconv.Atoi(val)
if err == nil {
result = append(result, i)
}
}
return result
}
// check if the process is running or not
//
func (p *Process) isRunning() bool {
if p.cmd != nil && p.cmd.Process != nil {
if runtime.GOOS == "windows" {
proc, err := os.FindProcess(p.cmd.Process.Pid)
return proc != nil && err == nil
}
return p.cmd.Process.Signal(syscall.Signal(0)) == nil
}
return false
}
// create Command object for the program
func (p *Process) createProgramCommand() error {
args, err := parseCommand(p.config.GetStringExpression("command", ""))
if err != nil {
return err
}
p.cmd, err = createCommand(args)
if err != nil {
return err
}
if p.setUser() != nil {
log.WithFields(log.Fields{"user": p.config.GetString("user", "")}).Error("fail to run as user")
return fmt.Errorf("fail to set user")
}
p.setProgramRestartChangeMonitor(args[0])
setDeathsig(p.cmd.SysProcAttr)
p.setEnv()
p.setDir()
p.setLog()
p.stdin, _ = p.cmd.StdinPipe()
return nil
}
func (p *Process) setProgramRestartChangeMonitor(programPath string) {
if p.config.GetBool("restart_when_binary_changed", false) {
absPath, err := filepath.Abs(programPath)
if err != nil {
absPath = programPath
}
AddProgramChangeMonitor(absPath, func(path string, mode filechangemonitor.FileChangeMode) {
log.WithFields(log.Fields{"program": p.GetName()}).Info("program is changed, restart it")
restart_cmd := p.config.GetString("restart_cmd_when_binary_changed", "")
s := p.config.GetString("restart_signal_when_binary_changed", "")
if len(restart_cmd) > 0 {
_, err := executeCommand(restart_cmd)
if err == nil {
log.WithFields(log.Fields{"program": p.GetName(), "command": restart_cmd}).Info("restart program with command successfully")
} else {
log.WithFields(log.Fields{"program": p.GetName(), "command": restart_cmd, "error": err}).Info("fail to restart program")
}
} else if len(s) > 0 {
p.sendSignals(strings.Fields(s), true)
} else {
p.Stop(true)
p.Start(true)
}
})
}
dirMonitor := p.config.GetString("restart_directory_monitor", "")
filePattern := p.config.GetString("restart_file_pattern", "*")
if dirMonitor != "" {
absDir, err := filepath.Abs(dirMonitor)
if err != nil {
absDir = dirMonitor
}
AddConfigChangeMonitor(absDir, filePattern, func(path string, mode filechangemonitor.FileChangeMode) {
log.WithFields(log.Fields{"program": p.GetName()}).Info("configure file for program is changed, restart it")
restart_cmd := p.config.GetString("restart_cmd_when_file_changed", "")
s := p.config.GetString("restart_signal_when_file_changed", "")
if len(restart_cmd) > 0 {
_, err := executeCommand(restart_cmd)
if err == nil {
log.WithFields(log.Fields{"program": p.GetName(), "command": restart_cmd}).Info("restart program with command successfully")
} else {
log.WithFields(log.Fields{"program": p.GetName(), "command": restart_cmd, "error": err}).Info("fail to restart program")
}
} else if len(s) > 0 {
p.sendSignals(strings.Fields(s), true)
} else {
p.Stop(true)
p.Start(true)
}
})
}
}
// wait for the started program exit
func (p *Process) waitForExit(startSecs int64) {
p.cmd.Wait()
if p.cmd.ProcessState != nil {
log.WithFields(log.Fields{"program": p.GetName()}).Infof("program stopped with status:%v", p.cmd.ProcessState)
} else {
log.WithFields(log.Fields{"program": p.GetName()}).Info("program stopped")
}
p.lock.Lock()
defer p.lock.Unlock()
p.stopTime = time.Now()
}
// fail to start the program
func (p *Process) failToStartProgram(reason string, finishCb func()) {
log.WithFields(log.Fields{"program": p.GetName()}).Errorf(reason)
p.changeStateTo(Fatal)
finishCb()
}
// monitor if the program is in running before endTime
//
func (p *Process) monitorProgramIsRunning(endTime time.Time, monitorExited *int32, programExited *int32) {
// if time is not expired
for time.Now().Before(endTime) && atomic.LoadInt32(programExited) == 0 {
time.Sleep(time.Duration(100) * time.Millisecond)
}
atomic.StoreInt32(monitorExited, 1)
p.lock.Lock()
defer p.lock.Unlock()
// if the program does not exit
if atomic.LoadInt32(programExited) == 0 && p.state == Starting {
log.WithFields(log.Fields{"program": p.GetName()}).Info("success to start program")
p.changeStateTo(Running)
}
}
func (p *Process) run(finishCb func()) {
p.lock.Lock()
defer p.lock.Unlock()
// check if the program is in running state
if p.isRunning() {
log.WithFields(log.Fields{"program": p.GetName()}).Info("Don't start program because it is running")
finishCb()
return
}
p.startTime = time.Now()
atomic.StoreInt32(p.retryTimes, 0)
startSecs := p.getStartSeconds()
restartPause := p.getRestartPause()
var once sync.Once
// finishCb can be only called one time
finishCbWrapper := func() {
once.Do(finishCb)
}
// process is not expired and not stoped by user
for !p.stopByUser {
if restartPause > 0 && atomic.LoadInt32(p.retryTimes) != 0 {
// pause
p.lock.Unlock()
log.WithFields(log.Fields{"program": p.GetName()}).Info("don't restart the program, start it after ", restartPause, " seconds")
time.Sleep(time.Duration(restartPause) * time.Second)
p.lock.Lock()
}
endTime := time.Now().Add(time.Duration(startSecs) * time.Second)
p.changeStateTo(Starting)
atomic.AddInt32(p.retryTimes, 1)
err := p.createProgramCommand()
if err != nil {
p.failToStartProgram("fail to create program", finishCbWrapper)
break
}
err = p.cmd.Start()
if err != nil {
if atomic.LoadInt32(p.retryTimes) >= p.getStartRetries() {
p.failToStartProgram(fmt.Sprintf("fail to start program with error:%v", err), finishCbWrapper)
break
} else {
log.WithFields(log.Fields{"program": p.GetName()}).Info("fail to start program with error:", err)
p.changeStateTo(Backoff)
continue
}
}
if p.StdoutLog != nil {
p.StdoutLog.SetPid(p.cmd.Process.Pid)
}
if p.StderrLog != nil {
p.StderrLog.SetPid(p.cmd.Process.Pid)
}
// logger.CompositeLogger is not `os.File`, so `cmd.Wait()` will wait for the logger to close
// if parent process passes its FD to child process, the logger will not close even when parent process exits
// we need to make sure the logger is closed when the process stops running
go func() {
// the sleep time must be less than `stopwaitsecs`, here I set half of `stopwaitsecs`
// otherwise the logger will not be closed before SIGKILL is sent
halfWaitsecs := time.Duration(p.config.GetInt("stopwaitsecs", 10)/2) * time.Second
for {
if !p.isRunning() {
break
}
time.Sleep(halfWaitsecs)
}
if p.StdoutLog != nil {
p.StdoutLog.Close()
}
if p.StderrLog != nil {
p.StderrLog.Close()
}
}()
monitorExited := int32(0)
programExited := int32(0)
// Set startsec to 0 to indicate that the program needn't stay
// running for any particular amount of time.
if startSecs <= 0 {
log.WithFields(log.Fields{"program": p.GetName()}).Info("success to start program")
p.changeStateTo(Running)
go finishCbWrapper()
} else {
go func() {
p.monitorProgramIsRunning(endTime, &monitorExited, &programExited)
finishCbWrapper()
}()
}
log.WithFields(log.Fields{"program": p.GetName()}).Debug("wait program exit")
p.lock.Unlock()
procExitC := make(chan struct{})
go func() {
p.waitForExit(startSecs)
close(procExitC)
}()
LOOP:
for {
select {
case <-procExitC:
break LOOP
default:
if !p.isRunning() {
break LOOP
}
}
time.Sleep(time.Duration(100) * time.Millisecond)
}
atomic.StoreInt32(&programExited, 1)
// wait for monitor thread exit
for atomic.LoadInt32(&monitorExited) == 0 {
time.Sleep(time.Duration(10) * time.Millisecond)
}
p.lock.Lock()
// if the program still in running after startSecs
if p.state == Running {
p.changeStateTo(Exited)
log.WithFields(log.Fields{"program": p.GetName()}).Info("program exited")
break
} else {
p.changeStateTo(Backoff)
}
// The number of serial failure attempts that supervisord will allow when attempting to
// start the program before giving up and putting the process into an Fatal state
// first start time is not the retry time
if atomic.LoadInt32(p.retryTimes) >= p.getStartRetries() {
p.failToStartProgram(fmt.Sprintf("fail to start program because retry times is greater than %d", p.getStartRetries()), finishCbWrapper)
break
}
}
}
func (p *Process) changeStateTo(procState State) {
if p.config.IsProgram() {
progName := p.config.GetProgramName()
groupName := p.config.GetGroupName()
if procState == Starting {
events.EmitEvent(events.CreateProcessStartingEvent(progName, groupName, p.state.String(), int(atomic.LoadInt32(p.retryTimes))))
} else if procState == Running {
events.EmitEvent(events.CreateProcessRunningEvent(progName, groupName, p.state.String(), p.cmd.Process.Pid))
} else if procState == Backoff {
events.EmitEvent(events.CreateProcessBackoffEvent(progName, groupName, p.state.String(), int(atomic.LoadInt32(p.retryTimes))))
} else if procState == Stopping {
events.EmitEvent(events.CreateProcessStoppingEvent(progName, groupName, p.state.String(), p.cmd.Process.Pid))
} else if procState == Exited {
exitCode, err := p.getExitCode()
expected := 0
if err == nil && p.inExitCodes(exitCode) {
expected = 1
}
events.EmitEvent(events.CreateProcessExitedEvent(progName, groupName, p.state.String(), expected, p.cmd.Process.Pid))
} else if procState == Fatal {
events.EmitEvent(events.CreateProcessFatalEvent(progName, groupName, p.state.String()))
} else if procState == Stopped {
events.EmitEvent(events.CreateProcessStoppedEvent(progName, groupName, p.state.String(), p.cmd.Process.Pid))
} else if procState == Unknown {
events.EmitEvent(events.CreateProcessUnknownEvent(progName, groupName, p.state.String()))
}
}
p.state = procState
}
// Signal sends signal to the process
//
// Args:
// sig - the signal to the process
// sigChildren - if true, sends the same signal to the process and its children
//
func (p *Process) Signal(sig os.Signal, sigChildren bool) error {
p.lock.RLock()
defer p.lock.RUnlock()
return p.sendSignal(sig, sigChildren)
}
func (p *Process) sendSignals(sigs []string, sigChildren bool) {
p.lock.RLock()
defer p.lock.RUnlock()
for _, strSig := range sigs {
sig, err := signals.ToSignal(strSig)
if err == nil {
p.sendSignal(sig, sigChildren)
} else {
log.WithFields(log.Fields{"program": p.GetName(), "signal": strSig}).Info("Invalid signal name")
}
}
}
// send signal to the process
//
// Args:
// sig - the signal to be sent
// sigChildren - if true, the signal also will be sent to children processes too
//
func (p *Process) sendSignal(sig os.Signal, sigChildren bool) error {
if p.cmd != nil && p.cmd.Process != nil {
log.WithFields(log.Fields{"program": p.GetName(), "signal": sig}).Info("Send signal to program")
err := signals.Kill(p.cmd.Process, sig, sigChildren)
return err
}
return fmt.Errorf("process is not started")
}
func (p *Process) setEnv() {
envFromFiles := p.config.GetEnvFromFiles("envFiles")
env := p.config.GetEnv("environment")
if len(env)+len(envFromFiles) != 0 {
p.cmd.Env = append(append(os.Environ(), envFromFiles...), env...)
} else {
p.cmd.Env = os.Environ()
}
}
func (p *Process) setDir() {
dir := p.config.GetStringExpression("directory", "")
if dir != "" {
p.cmd.Dir = dir
}
}
func (p *Process) setLog() {
if p.config.IsProgram() {
p.StdoutLog = p.createStdoutLogger()
captureBytes := p.config.GetBytes("stdout_capture_maxbytes", 0)
if captureBytes > 0 {
log.WithFields(log.Fields{"program": p.config.GetProgramName()}).Info("capture stdout process communication")
p.StdoutLog = logger.NewLogCaptureLogger(p.StdoutLog,
captureBytes,
"PROCESS_COMMUNICATION_STDOUT",
p.GetName(),
p.GetGroup())
}
p.cmd.Stdout = p.StdoutLog
if p.config.GetBool("redirect_stderr", false) {
p.StderrLog = p.StdoutLog
} else {
p.StderrLog = p.createStderrLogger()
}
captureBytes = p.config.GetBytes("stderr_capture_maxbytes", 0)
if captureBytes > 0 {
log.WithFields(log.Fields{"program": p.config.GetProgramName()}).Info("capture stderr process communication")
p.StderrLog = logger.NewLogCaptureLogger(p.StdoutLog,
captureBytes,
"PROCESS_COMMUNICATION_STDERR",
p.GetName(),
p.GetGroup())
}
p.cmd.Stderr = p.StderrLog
} else if p.config.IsEventListener() {
in, err := p.cmd.StdoutPipe()
if err != nil {
log.WithFields(log.Fields{"eventListener": p.config.GetEventListenerName()}).Error("fail to get stdin")
return
}
out, err := p.cmd.StdinPipe()
if err != nil {
log.WithFields(log.Fields{"eventListener": p.config.GetEventListenerName()}).Error("fail to get stdout")
return
}
events := strings.Split(p.config.GetString("events", ""), ",")
for i, event := range events {
events[i] = strings.TrimSpace(event)
}
p.cmd.Stderr = os.Stderr
p.registerEventListener(p.config.GetEventListenerName(),
events,
in,
out)
}
}
func (p *Process) createStdoutLogEventEmitter() logger.LogEventEmitter {
if p.config.GetBytes("stdout_capture_maxbytes", 0) <= 0 && p.config.GetBool("stdout_events_enabled", false) {
return logger.NewStdoutLogEventEmitter(p.config.GetProgramName(), p.config.GetGroupName(), func() int {
return p.GetPid()
})
}
return logger.NewNullLogEventEmitter()
}
func (p *Process) createStderrLogEventEmitter() logger.LogEventEmitter {
if p.config.GetBytes("stderr_capture_maxbytes", 0) <= 0 && p.config.GetBool("stderr_events_enabled", false) {
return logger.NewStdoutLogEventEmitter(p.config.GetProgramName(), p.config.GetGroupName(), func() int {
return p.GetPid()
})
}
return logger.NewNullLogEventEmitter()
}
func (p *Process) registerEventListener(eventListenerName string,
_events []string,
stdin io.Reader,
stdout io.Writer) {
eventListener := events.NewEventListener(eventListenerName,
p.supervisorID,
stdin,
stdout,
p.config.GetInt("buffer_size", 100))
events.RegisterEventListener(eventListenerName, _events, eventListener)
}
func (p *Process) unregisterEventListener(eventListenerName string) {
events.UnregisterEventListener(eventListenerName)
}
func (p *Process) createStdoutLogger() logger.Logger {
logFile := p.GetStdoutLogfile()
maxBytes := int64(p.config.GetBytes("stdout_logfile_maxbytes", 50*1024*1024))
backups := p.config.GetInt("stdout_logfile_backups", 10)
logEventEmitter := p.createStdoutLogEventEmitter()
props := make(map[string]string)
syslog_facility := p.config.GetString("syslog_facility", "")
syslog_tag := p.config.GetString("syslog_tag", "")
syslog_priority := p.config.GetString("syslog_stdout_priority", "")
if len(syslog_facility) > 0 {
props["syslog_facility"] = syslog_facility
}
if len(syslog_tag) > 0 {
props["syslog_tag"] = syslog_tag
}
if len(syslog_priority) > 0 {
props["syslog_priority"] = syslog_priority
}
return logger.NewLogger(p.GetName(), logFile, logger.NewNullLocker(), maxBytes, backups, props, logEventEmitter)
}
func (p *Process) createStderrLogger() logger.Logger {
logFile := p.GetStderrLogfile()
maxBytes := int64(p.config.GetBytes("stderr_logfile_maxbytes", 50*1024*1024))
backups := p.config.GetInt("stderr_logfile_backups", 10)
logEventEmitter := p.createStderrLogEventEmitter()
props := make(map[string]string)
syslog_facility := p.config.GetString("syslog_facility", "")
syslog_tag := p.config.GetString("syslog_tag", "")
syslog_priority := p.config.GetString("syslog_stderr_priority", "")
if len(syslog_facility) > 0 {
props["syslog_facility"] = syslog_facility
}
if len(syslog_tag) > 0 {
props["syslog_tag"] = syslog_tag
}
if len(syslog_priority) > 0 {
props["syslog_priority"] = syslog_priority
}
return logger.NewLogger(p.GetName(), logFile, logger.NewNullLocker(), maxBytes, backups, props, logEventEmitter)
}
func (p *Process) setUser() error {
userName := p.config.GetString("user", "")
if len(userName) == 0 {
return nil
}
// check if group is provided
pos := strings.Index(userName, ":")
groupName := ""
if pos != -1 {
groupName = userName[pos+1:]
userName = userName[0:pos]
}
u, err := user.Lookup(userName)
if err != nil {
return err
}
uid, err := strconv.ParseUint(u.Uid, 10, 32)
if err != nil {
return err
}
gid, err := strconv.ParseUint(u.Gid, 10, 32)
if err != nil && groupName == "" {
return err
}
if groupName != "" {
g, err := user.LookupGroup(groupName)
if err != nil {
return err
}
gid, err = strconv.ParseUint(g.Gid, 10, 32)
if err != nil {
return err
}
}
setUserID(p.cmd.SysProcAttr, uint32(uid), uint32(gid))
return nil
}
// Stop sends signal to process to make it quit
func (p *Process) Stop(wait bool) {
p.lock.Lock()
p.stopByUser = true
isRunning := p.isRunning()
p.lock.Unlock()
if !isRunning {
log.WithFields(log.Fields{"program": p.GetName()}).Info("program is not running")
return
}
log.WithFields(log.Fields{"program": p.GetName()}).Info("stop the program")
sigs := strings.Fields(p.config.GetString("stopsignal", "SIGTERM"))
waitsecs := time.Duration(p.config.GetInt("stopwaitsecs", 10)) * time.Second
killwaitsecs := time.Duration(p.config.GetInt("killwaitsecs", 2)) * time.Second
stopasgroup := p.config.GetBool("stopasgroup", false)
killasgroup := p.config.GetBool("killasgroup", stopasgroup)
if stopasgroup && !killasgroup {
log.WithFields(log.Fields{"program": p.GetName()}).Error("Cannot set stopasgroup=true and killasgroup=false")
}
var stopped int32 = 0
go func() {
for i := 0; i < len(sigs) && atomic.LoadInt32(&stopped) == 0; i++ {
// send signal to process
sig, err := signals.ToSignal(sigs[i])
if err != nil {
continue
}
log.WithFields(log.Fields{"program": p.GetName(), "signal": sigs[i]}).Info("send stop signal to program")
p.Signal(sig, stopasgroup)
endTime := time.Now().Add(waitsecs)
// wait at most "stopwaitsecs" seconds for one signal
for endTime.After(time.Now()) {
// if it already exits
if p.state != Starting && p.state != Running && p.state != Stopping {
atomic.StoreInt32(&stopped, 1)
break
}
time.Sleep(10 * time.Millisecond)
}
}
if atomic.LoadInt32(&stopped) == 0 {
log.WithFields(log.Fields{"program": p.GetName()}).Info("force to kill the program")
p.Signal(syscall.SIGKILL, killasgroup)
killEndTime := time.Now().Add(killwaitsecs)
for killEndTime.After(time.Now()) {
// if it exits
if p.state != Starting && p.state != Running && p.state != Stopping {
atomic.StoreInt32(&stopped, 1)
break
}
time.Sleep(10 * time.Millisecond)
}
atomic.StoreInt32(&stopped, 1)
}
}()
if wait {
for atomic.LoadInt32(&stopped) == 0 {
time.Sleep(1 * time.Second)
}
}
}
// GetStatus returns status of program as a string
func (p *Process) GetStatus() string {