forked from WorldObservationLog/wrapper-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstance.go
More file actions
93 lines (83 loc) · 2.18 KB
/
Copy pathinstance.go
File metadata and controls
93 lines (83 loc) · 2.18 KB
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
package main
import (
"encoding/json"
"os"
"os/exec"
"time"
)
const wrapperTerminateGrace = 5 * time.Second
var Instances []*WrapperInstance
type WrapperInstance struct {
Id string `json:"id"`
Account string `json:"account"`
Region string `json:"region"`
DecryptPort int `json:"-"`
M3U8Port int `json:"-"`
NoRestart bool `json:"-"`
Cmd *exec.Cmd `json:"-"`
Done chan struct{} `json:"-"`
// proc carries the running process's supervision state: start time, the
// tail of its output, and whether the manager asked it to stop. It is a
// pointer and unexported because this struct is copied by value out of
// data/instances.json and serialised back into it; nothing here may hold a
// lock or be persisted. Nil for instances that have no process behind them.
proc *wrapperProc
}
func SaveInstances() {
instances, err := json.Marshal(Instances)
if err != nil {
panic(err)
}
err = os.WriteFile("data/instances.json", instances, 0777)
if err != nil {
panic(err)
}
}
func LoadInstance() []WrapperInstance {
if _, err := os.Stat("data/instances.json"); os.IsNotExist(err) {
return make([]WrapperInstance, 0)
}
var instances []WrapperInstance
content, err := os.ReadFile("data/instances.json")
if err != nil {
panic(err)
}
err = json.Unmarshal(content, &instances)
if err != nil {
panic(err)
}
return instances
}
func InsertInstance(instance *WrapperInstance) {
for _, existing := range Instances {
if existing.Id == instance.Id {
return
}
}
Instances = append(Instances, instance)
}
func RemoveInstance(instance *WrapperInstance) {
for i, existing := range Instances {
if existing.Id == instance.Id {
Instances = append(Instances[:i], Instances[i+1:]...)
return
}
}
}
func GetInstance(id string) *WrapperInstance {
for _, instance := range Instances {
if instance.Id == id {
return instance
}
}
return &WrapperInstance{}
}
func GetInstancesByAccount(account string) []*WrapperInstance {
var matches []*WrapperInstance
for _, instance := range Instances {
if instance.Account == account {
matches = append(matches, instance)
}
}
return matches
}