-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmonitor.go
66 lines (58 loc) · 2.11 KB
/
monitor.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
package main
import (
"log"
"strconv"
"strings"
"time"
)
func startProcessIfNotRunning() {
logInfo("Title = %s", processTitle)
processID = findByTitle(processTitle)
if processID == 0 {
processID, err := startCommand(processCommand)
if err != nil {
log.Fatal("❌ Error starting process:", err)
}
logInfo("Process started with PID %s", strconv.Itoa(int(processID)))
} else {
logInfo("ℹ️ Found already running process with PID %d. Not starting a new process", processID)
}
lastRequestTime = time.Now()
}
func monitorAndRestartProcess() {
for {
time.Sleep(1 * time.Minute)
logDebug("lastRequestTime = " + lastRequestTime.String())
if time.Since(lastRequestTime) > restartThreshold {
logInfo("❗ No requests were received for more than %s. Restarting process...", restartThreshold.String())
err1 := kill(processID)
if err1 != nil {
if strings.TrimSpace(processTitle) != "" {
// lookup existing running process, maybe Pid got messed up due to manual user restart of the original process
pId := findByTitle(processTitle)
if pId > 0 && pId != processID {
// the process has a different process id than expected
err2 := kill(pId)
if err2 != nil {
logInfo("❌ Could neither kill the originally spawned pid %d nor pid %d found by title", processID, pId)
log.Printf(" PID %d error: %s\n", processID, err1)
log.Printf(" PID %d error: %s\n", pId, err2)
}
} else {
logInfo("❌ Could not kill the originally spawned process and did not find existing one by title")
log.Printf(" PID: %d, Error: %s\n", processID, err1)
}
} else {
logInfo("❌ Could not kill the originally spawned process %d", processID)
}
}
// Wait 10 seconds
time.Sleep(10 * time.Second)
// Start process
startProcessIfNotRunning()
lastRequestTime = time.Now()
} else if time.Since(lastRequestTime) > restartThreshold-1*time.Minute {
logInfo("⚠️ WARNING: No requests were received for more than %s. Restarting process in one minute if still no request happened...", (restartThreshold - 1*time.Minute).String())
}
}
}