-
Notifications
You must be signed in to change notification settings - Fork 13
/
script.go
191 lines (161 loc) · 3.52 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
185
186
187
188
189
190
191
package hitrix
import (
"context"
"fmt"
"log"
"os"
"runtime/debug"
"strings"
"time"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/service"
"github.com/ryanuber/columnize"
)
type Script interface {
Description() string
Run(ctx context.Context, exit Exit)
Unique() bool
}
type BackgroundProcessor struct {
Server *Hitrix
}
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 (processor *BackgroundProcessor) 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 := processor.runScript(script)
if isInfinity {
select {}
}
if !isInterval {
processor.Server.done <- true
break
}
//TODO
if !valid {
log.Print("Error in last run.")
}
time.Sleep(interval.Interval())
}
}()
processor.Server.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 (processor *BackgroundProcessor) 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
}
}()
app := service.DI().App()
script.Run(app.GlobalContext, &exit{s: processor.Server})
return valid
}()
}
func (processor *BackgroundProcessor) RunAsyncOrmConsumer() {
ormService, has := service.DI().OrmEngine()
if !has {
panic("Orm is not registered")
}
go func() {
asyncConsumer := beeorm.NewBackgroundConsumer(ormService)
for {
if asyncConsumer.Digest() {
break
}
time.Sleep(time.Second * 30)
}
}()
}