Skip to content

Commit

Permalink
Merge pull request #8 from matrixbotio/print-stopped-by-wrapper
Browse files Browse the repository at this point in the history
print "process is stopped by wrapper" and successfully exit if process is stopped by wrapper
  • Loading branch information
KaMeHb-UA authored Jan 30, 2022
2 parents 278cc1a + 43650f8 commit 444e8aa
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ func healthcheck() {
client := http.Client{
Timeout: 5 * time.Second,
}
if isProcessStoppedByWrapper() {
println("process is stopped by wrapper")
return
}
print("localhost GET 8080 /health ")
start := time.Now().UnixMilli()
resp, err := client.Get("http://localhost:8080/health")
Expand Down
18 changes: 18 additions & 0 deletions src/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"io"
"os"
"strings"
Expand All @@ -13,6 +14,7 @@ import (
)

var timeFormat = "2006-01-02 15:04:05"
var stoppedFile = "/wrapper-process-stopped"

func appendToLimitedArr(arr []string, str string, count int) []string {
if len(arr) > count - 1 {
Expand Down Expand Up @@ -86,12 +88,26 @@ func sendSig(sig os.Signal){
cmd.Process.Signal(sig)
}

func notifyExternalProcessesStopped(){
os.OpenFile(stoppedFile, os.O_RDONLY | os.O_CREATE, 0666)
}

func notifyExternalProcessesStarted(){
os.Remove(stoppedFile)
}

func isProcessStoppedByWrapper() bool {
_, err := os.Stat(stoppedFile)
return errors.Is(err, os.ErrNotExist)
}

func stopProcess(){
if !running {
return
}
paused = true
running = false
notifyExternalProcessesStopped()
sendSig(syscall.SIGSTOP)
}

Expand All @@ -106,11 +122,13 @@ func resumeProcess(){

func termProcess(){
resumeProcess()
notifyExternalProcessesStopped()
sendSig(syscall.SIGTERM)
}

func killProcess(){
resumeProcess()
notifyExternalProcessesStopped()
sendSig(syscall.SIGKILL)
}

Expand Down
1 change: 1 addition & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func run() {
}
state = "Запущен"
running = true
notifyExternalProcessesStarted()
startTime = time.Now().Format(timeFormat)
files := readStdoutStderr(stdout, stderr)
waitErr := cmd.Wait()
Expand Down

0 comments on commit 444e8aa

Please sign in to comment.