-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathapp_api.go
More file actions
221 lines (219 loc) · 6.05 KB
/
app_api.go
File metadata and controls
221 lines (219 loc) · 6.05 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package patrol
import (
"time"
)
func (self *App) apiRequest(
request *API_Request,
) bool {
now := time.Now()
// CAS / Non-Ping Attributes:
//
// we have to update our CAS BEFORE we update `lastseen, started, pid` and other ping values
// these and other similar variables will modify our CAS!!!
cas_valid := true
if request.CAS > 0 &&
request.CAS != self.o.GetCAS() {
// CAS does not match!
cas_valid = false
}
if cas_valid {
// keyvalue
if request.KeyValueReplace {
// replace
self.o.ReplaceKeyValue(request.KeyValue)
} else {
// merge
if len(request.KeyValue) > 0 {
self.o.SetKeyValue(request.KeyValue)
}
}
// toggle
if request.Toggle > 0 {
self.toggle(request.Toggle)
}
}
// Non-CAS / Ping Attributes:
//
// we need to compare our PID to our previous PID
// if our PID does not match, we need to close our previous App and create a new App
// if PID exists we're assumed to be in a Ping, so we can call triggers
// PID is the only attribute that is required to be sent with a Ping
// the reason for this is that in the future this is the only actions that WILL NOT require a correct CAS!!!
if request.PID > 0 {
// request PID exists
// PID requires a ping, so all ping triggers are safe
if self.o.GetPID() > 0 {
// App PID exists
if request.PID != self.o.GetPID() {
// App PID does not match
// this is a new App
// close previous App
self.close()
// start a new App
self.instance_id = uuidMust(uuidV4())
self.o.SetStarted(now)
// set PID
self.o.SetPID(request.PID)
// call trigger
if self.config.TriggerStartedPinged != nil {
// we're going to unlock and call our trigger
self.o.Unlock()
self.config.TriggerStartedPinged(self)
self.o.Lock()
}
} else {
// PID matches
// set lastseen
self.o.SetLastSeen(now)
// call trigger
if self.config.TriggerPinged != nil {
// we're going to unlock and call our trigger
self.o.Unlock()
self.config.TriggerPinged(self)
self.o.Lock()
}
}
} else {
// App PID does not exist
// set PID
self.o.SetPID(request.PID)
if self.o.GetStarted().IsZero() {
// this is a new App
self.instance_id = uuidMust(uuidV4())
self.o.SetStarted(now)
// call trigger
if self.config.TriggerStartedPinged != nil {
// we're going to unlock and call our trigger
self.o.Unlock()
self.config.TriggerStartedPinged(self)
self.o.Lock()
}
} else {
// app was previously started
// set lastseen
self.o.SetLastSeen(now)
// call trigger
if self.config.TriggerPinged != nil {
// we're going to unlock and call our trigger
self.o.Unlock()
self.config.TriggerPinged(self)
self.o.Lock()
}
}
}
} else {
// request PID doesn't exist
if request.Ping {
// set lastseen
self.o.SetLastSeen(now)
// call trigger
if self.config.TriggerPinged != nil {
// we're going to unlock and call our trigger
self.o.Unlock()
self.config.TriggerPinged(self)
self.o.Lock()
}
}
}
return cas_valid
}
func (self *App) toggle(
toggle uint8,
) {
if toggle == API_TOGGLE_STATE_ENABLE {
self.o.SetDisabled(false)
} else if toggle == API_TOGGLE_STATE_DISABLE {
self.o.SetDisabled(true)
self.o.SetRestart(false)
self.o.SetRunOnce(false)
self.o.SetRunOnceConsumed(false)
} else if toggle == API_TOGGLE_STATE_RESTART {
self.o.SetDisabled(false)
self.o.SetRestart(true)
self.o.SetRunOnce(false)
self.o.SetRunOnceConsumed(false)
} else if toggle == API_TOGGLE_STATE_RUNONCE_ENABLE {
self.o.SetRunOnce(true)
if !self.o.GetStarted().IsZero() {
// we're already running, we must consume run_once
self.o.SetRunOnceConsumed(true)
}
} else if toggle == API_TOGGLE_STATE_RUNONCE_DISABLE {
self.o.SetRunOnce(false)
self.o.SetRunOnceConsumed(false)
} else if toggle == API_TOGGLE_STATE_ENABLE_RUNONCE_ENABLE {
self.o.SetDisabled(false)
self.o.SetRunOnce(true)
if !self.o.GetStarted().IsZero() {
// we're already running, we must consume run_once
self.o.SetRunOnceConsumed(true)
}
} else if toggle == API_TOGGLE_STATE_ENABLE_RUNONCE_DISABLE {
self.o.SetDisabled(false)
self.o.SetRunOnce(false)
self.o.SetRunOnceConsumed(false)
}
}
func (self *App) Snapshot() *API_Response {
self.o.RLock()
defer self.o.RUnlock()
return self.apiResponse(api_endpoint_snapshot)
}
func (self *App) apiResponse(
endpoint uint8,
) *API_Response {
result := &API_Response{
InstanceID: self.instance_id,
Name: self.config.Name,
PID: self.o.GetPID(),
Disabled: self.o.IsDisabled(),
Restart: self.o.IsRestart(),
RunOnce: self.o.IsRunOnce(),
Secret: self.config.Secret != "",
CAS: self.o.GetCAS(),
}
if endpoint != api_endpoint_status {
// we don't need these values for individual status objects
result.ID = self.id
result.Group = "app"
// we need to read lock patrol
self.patrol.mu.RLock()
result.Shutdown = self.patrol.shutdown
self.patrol.mu.RUnlock()
}
if endpoint != api_endpoint_udp {
// we don't want either of these for UDP, it's too much data
//
// we're not going to include history in our snapshot, it's too much data to dereference
// if history is needed it should be taken additionally after snapshot
if endpoint != api_endpoint_snapshot {
result.History = self.getHistory()
}
result.KeyValue = self.o.GetKeyValue()
}
if !self.o.GetStarted().IsZero() {
result.Started = &Timestamp{
Time: self.o.GetStarted(),
TimestampFormat: self.patrol.config.Timestamp,
}
}
if self.o.GetLastSeen().IsZero() {
if self.config.KeepAlive == APP_KEEPALIVE_PID_PATROL {
// if our app was running lastseen should exist
if !self.o.GetStarted().IsZero() {
// we should set lastseen to now
// we're responsible for this service to always be running
result.LastSeen = &Timestamp{
Time: time.Now(),
TimestampFormat: self.patrol.config.Timestamp,
}
}
}
} else {
result.LastSeen = &Timestamp{
Time: self.o.GetLastSeen(),
TimestampFormat: self.patrol.config.Timestamp,
}
}
return result
}