-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
77 lines (64 loc) · 1.53 KB
/
util.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
package zerodown
import (
"fmt"
"os"
"strconv"
)
func IsParent() bool {
parent, err := strconv.Atoi(os.Getenv("PD_PARENT_PROCESS"))
if err == nil {
parentPID = parent
return false // The parent PID exists, this is a child process
}
return true
}
func IsChild() bool {
return !IsParent()
}
// Restart allows a child process to call for a restart. The parent process will
// start a new child and will shut down the current process
func Restart() (err error) {
if parentPID != 0 {
print("Sending %s signal to parent PID %d", ReloadSignals[0], parentPID)
process, err := os.FindProcess(parentPID)
if err != nil {
return fmt.Errorf("could not find parent process: %w", err)
}
if err = process.Signal(ReloadSignals[0]); err != nil {
return fmt.Errorf("could not signal parent process: %w", err)
}
} else {
panic("Restart should not be called on the parent process itself")
}
return nil
}
func print(str string, args ...any) {
if parentPID == 0 {
Logger.Printf("Zerodown-parent: "+str+"\n", args...)
} else {
Logger.Printf("Zerodown-child: "+str+"\n", args...)
}
}
func inArray[T comparable](needle T, haystack []T) bool {
for i := range haystack {
if haystack[i] == needle {
return true
}
}
return false
}
func combineSlices[T any](slices ...[]T) (result []T) {
var totLen = 0
for _, slice := range slices {
totLen += len(slice)
}
result = make([]T, totLen)
totLen = 0
for i1 := range slices {
for i2 := range slices[i1] {
result[totLen] = slices[i1][i2]
totLen++
}
}
return result
}