-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
process.go
146 lines (116 loc) · 3.4 KB
/
process.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
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
package keygen
import (
"context"
"time"
"github.com/keygen-sh/jsonapi-go"
)
type ProcessStatusCode string
const (
ProcessStatusCodeAlive ProcessStatusCode = "ALIVE"
ProcessStatusCodeDead ProcessStatusCode = "DEAD"
)
type process struct {
ID string `json:"-"`
Type string `json:"-"`
Pid string `json:"pid"`
MachineID string `json:"-"`
}
// GetID implements the jsonapi.MarshalResourceIdentifier interface.
func (p process) GetID() string {
return p.ID
}
// GetType implements the jsonapi.MarshalResourceIdentifier interface.
func (p process) GetType() string {
return "processes"
}
// GetData implements the jsonapi.MarshalData interface.
func (p process) GetData() interface{} {
return p
}
// GetRelationships implements jsonapi.MarshalRelationships interface.
func (p process) GetRelationships() map[string]interface{} {
relationships := make(map[string]interface{})
relationships["machine"] = jsonapi.ResourceObjectIdentifier{
Type: "machines",
ID: p.MachineID,
}
return relationships
}
// Process represents a Keygen process object.
type Process struct {
ID string `json:"-"`
Type string `json:"-"`
Pid string `json:"pid"`
Status ProcessStatusCode `json:"status"`
Interval int `json:"interval"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Metadata map[string]interface{} `json:"metadata"`
MachineID string `json:"-"`
}
// GetID implements the jsonapi.MarshalResourceIdentifier interface.
func (p Process) GetID() string {
return p.ID
}
// GetType implements the jsonapi.MarshalResourceIdentifier interface.
func (p Process) GetType() string {
return "processes"
}
// GetData implements the jsonapi.MarshalData interface.
func (p Process) GetData() interface{} {
// Transform public process to private process to only send a subset of attrs
return process{
Pid: p.Pid,
MachineID: p.MachineID,
}
}
// SetID implements the jsonapi.UnmarshalResourceIdentifier interface.
func (p *Process) SetID(id string) error {
p.ID = id
return nil
}
// SetType implements the jsonapi.UnmarshalResourceIdentifier interface.
func (p *Process) SetType(t string) error {
p.Type = t
return nil
}
// SetData implements the jsonapi.UnmarshalData interface.
func (p *Process) SetData(to func(target interface{}) error) error {
return to(p)
}
// Processes represents an array of process objects.
type Processes []Process
// SetData implements the jsonapi.UnmarshalData interface.
func (p *Processes) SetData(to func(target interface{}) error) error {
return to(p)
}
// Kill deletes the current Process. An error will be returned if the process
// deletion fails.
func (p *Process) Kill(ctx context.Context) error {
client := NewClient()
if _, err := client.Delete(ctx, "processes/"+p.ID, nil, nil); err != nil {
return err
}
return nil
}
func (p *Process) monitor(ctx context.Context) error {
if err := p.ping(ctx); err != nil {
return err
}
go func() {
t := (time.Duration(p.Interval) * time.Second) - (30 * time.Second)
for range time.Tick(t) {
if err := p.ping(ctx); err != nil {
panic(err)
}
}
}()
return nil
}
func (p *Process) ping(ctx context.Context) error {
client := NewClient()
if _, err := client.Post(ctx, "processes/"+p.ID+"/actions/ping", nil, p); err != nil {
return err
}
return nil
}