-
Notifications
You must be signed in to change notification settings - Fork 14
/
script.go
184 lines (159 loc) · 3.46 KB
/
script.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
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
package hitrix
import (
"context"
"fmt"
"log"
"os"
"runtime/debug"
"strings"
"time"
"github.com/coretrix/hitrix/service"
"github.com/ryanuber/columnize"
)
type Script interface {
Description() string
Run(ctx context.Context, exit Exit)
Unique() bool
}
type Exit interface {
Valid()
Error()
Custom(exitCode int)
}
type exit struct {
s *Hitrix
}
func (e *exit) Custom(exitCode int) {
e.s.exit <- exitCode
}
func (e *exit) Valid() {
e.Custom(0)
}
func (e *exit) Error() {
e.Custom(1)
}
type ScriptInfinity interface {
Infinity() bool
}
type ScriptInterval interface {
Interval() time.Duration
}
type ScriptIntervalOptional interface {
IntervalActive() bool
}
type ScriptIntermediate interface {
IsIntermediate() bool
}
type ScriptOptional interface {
Active() bool
}
func (h *Hitrix) RunScript(script Script) {
options, isOptional := script.(ScriptOptional)
if isOptional {
if !options.Active() {
log.Print("Script not active. Exiting.")
return
}
}
interval, isInterval := script.(ScriptInterval)
_, isInfinity := script.(ScriptInfinity)
go func() {
for {
valid := h.runScript(script)
if isInfinity {
select {}
}
if !isInterval {
h.done <- true
break
}
//TODO
if !valid {
log.Print("Error in last run.")
}
time.Sleep(interval.Interval())
}
}()
h.await()
}
func listScrips() {
scripts := service.DI().App().Scripts
if len(scripts) > 0 {
output := []string{
"NAME | OPTIONS | DESCRIPTION ",
}
for _, defCode := range scripts {
def := service.GetServiceRequired(defCode).(Script)
options := make([]string, 0)
interval, is := def.(ScriptInterval)
if is {
options = append(options, "interval")
duration := "every " + interval.Interval().String()
_, is := def.(ScriptIntervalOptional)
if is {
duration += " with condition"
}
options = append(options, duration)
}
if def.Unique() {
options = append(options, "unique")
}
optional, is := def.(ScriptOptional)
if is {
options = append(options, "optional")
if optional.Active() {
options = append(options, "active")
} else {
options = append(options, "inactive")
}
}
intermediate, is := def.(ScriptIntermediate)
if is && intermediate.IsIntermediate() {
options = append(options, "intermediate")
}
output = append(output, strings.Join([]string{defCode, strings.Join(options, ","), def.Description()}, " | "))
}
_, _ = os.Stdout.WriteString(columnize.SimpleFormat(output) + "\n")
}
}
func (h *Hitrix) runDynamicScrips(ctx context.Context, code string) {
scripts := service.DI().App().Scripts
if len(scripts) == 0 {
panic(fmt.Sprintf("unknown script %s", code))
}
for _, defCode := range scripts {
if defCode == code {
def, has := service.GetServiceOptional(defCode)
if !has {
panic(fmt.Sprintf("unknown script %s", code))
}
defScript := def.(Script)
defScript.Run(ctx, &exit{s: h})
return
}
}
panic(fmt.Sprintf("unknown script %s", code))
}
func (h *Hitrix) runScript(script Script) bool {
return func() bool {
valid := true
defer func() {
if err := recover(); err != nil {
var message string
asErr, is := err.(error)
if is {
message = asErr.Error()
} else {
message = fmt.Sprint(err)
}
errorLogger, has := service.DI().ErrorLogger()
if has {
errorLogger.LogError(message + "\n" + string(debug.Stack()))
}
valid = false
}
}()
script.Run(h.ctx, &exit{s: h})
return valid
}()
}