-
Notifications
You must be signed in to change notification settings - Fork 13
/
script.go
164 lines (137 loc) · 3.39 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
package hitrix
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/latolukasz/beeorm"
"github.com/ryanuber/columnize"
"github.com/coretrix/hitrix/service"
"github.com/coretrix/hitrix/service/component/app"
)
type BackgroundProcessor struct {
Server *Hitrix
}
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)
}
func (processor *BackgroundProcessor) RunScript(s app.IScript) {
options, isOptional := s.(app.Optional)
if isOptional {
if !options.Active() {
log.Print("IScript not active. Exiting.")
return
}
}
interval, isInterval := s.(app.Interval)
_, isInfinity := s.(app.Infinity)
go func() {
for {
log.Println("Run script - " + s.Description())
valid := processor.runScript(s)
if valid {
log.Println("Successfully executed script - " + s.Description())
} else {
log.Println("Failed script - " + s.Description())
time.Sleep(time.Second * 10)
continue
}
if isInfinity {
log.Println("Infinity - " + s.Description())
select {}
}
if !isInterval {
log.Println("Finished - " + s.Description())
processor.Server.done <- true
break
}
log.Println("Sleep for " + fmt.Sprint(interval.Interval()) + " seconds - " + s.Description())
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).(app.IScript)
options := make([]string, 0)
interval, is := def.(app.Interval)
if is {
options = append(options, "interval")
duration := "every " + interval.Interval().String()
_, is := def.(app.IntervalOptional)
if is {
duration += " with condition"
}
options = append(options, duration)
}
if def.Unique() {
options = append(options, "unique")
}
optional, is := def.(app.Optional)
if is {
options = append(options, "optional")
if optional.Active() {
options = append(options, "active")
} else {
options = append(options, "inactive")
}
}
intermediate, is := def.(app.Intermediate)
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(s app.IScript) 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)
}
service.DI().ErrorLogger().LogError(message)
valid = false
}
}()
appService := service.DI().App()
s.Run(appService.GlobalContext, &exit{s: processor.Server})
return valid
}()
}
func (processor *BackgroundProcessor) RunAsyncOrmConsumer() {
ormService := service.DI().OrmEngine()
appService := service.DI().App()
GoroutineWithRestart(func() {
asyncConsumer := beeorm.NewBackgroundConsumer(ormService)
for {
if asyncConsumer.Digest(appService.GlobalContext) {
break
}
time.Sleep(time.Second * 30)
}
})
}