-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: nabil salah <[email protected]>
- Loading branch information
1 parent
90034a3
commit 9b436bc
Showing
7 changed files
with
156 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package foreman | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/shirou/gopsutil/process" | ||
) | ||
|
||
// signal initialize signal handling in foreman | ||
func (foreman *Foreman) signal () { | ||
sigs := []os.Signal {syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGCHLD, syscall.SIGHUP} | ||
signal.Notify(foreman.signalsChannel, sigs...) | ||
go foreman.receiveSignals(foreman.signalsChannel) | ||
} | ||
|
||
// receiveSignals receive signals form sigChannel and calls a | ||
// proper signal handler. | ||
func (foreman *Foreman) receiveSignals(sigChannel <-chan os.Signal) { | ||
for sig := range sigChannel { | ||
switch sig { | ||
case syscall.SIGCHLD: | ||
foreman.sigchldHandler() | ||
default: | ||
foreman.killServicesAndExit() | ||
} | ||
} | ||
} | ||
|
||
// sigintHandler handles SIGINT signals | ||
func (foreman *Foreman) killServicesAndExit() { | ||
for _, service := range foreman.services { | ||
fmt.Println(service.pid) | ||
syscall.Kill(service.pid, syscall.SIGINT) | ||
Check failure on line 37 in pkg/signals.go GitHub Actions / Check
|
||
} | ||
os.Exit(0) | ||
} | ||
|
||
|
||
// sigchldHandler handles SIGCHLD signals | ||
func (foreman *Foreman) sigchldHandler() { | ||
for _, service := range foreman.services { | ||
childProcess, _ := process.NewProcess(int32(service.pid)) | ||
childStatus, _ := childProcess.Status() | ||
if childStatus == "Z" { | ||
service.info.status = "inactive" | ||
p, _ := os.FindProcess(service.pid) | ||
p.Wait() | ||
Check failure on line 51 in pkg/signals.go GitHub Actions / Check
|
||
fmt.Printf("[%d] %s process terminated [%v]\n", service.pid, service.name, time.Now()) | ||
if !service.info.runOnce { | ||
fmt.Printf("[%d] %s process restarted [%v]\n", service.pid, service.name, time.Now()) | ||
foreman.restartService(service.name) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// restartService restarts service by sending service to servicesToRunChannel | ||
// to be run by a worker thread. | ||
func (foreman *Foreman) restartService(service string) { | ||
foreman.servicesToRunChannel <- service | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,5 +8,3 @@ app1: | |
app2: | ||
cmd: ping -c 10 yahoo.com | ||
run_once: true | ||
deps: | ||
- app1 |