-
Notifications
You must be signed in to change notification settings - Fork 13
/
script.go
156 lines (132 loc) · 3.1 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
package hitrix
import (
"fmt"
"log"
"os"
"runtime/debug"
"strings"
"time"
"github.com/coretrix/hitrix/service/component/app"
"github.com/latolukasz/beeorm"
"github.com/coretrix/hitrix/service"
"github.com/ryanuber/columnize"
)
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 {
valid := processor.runScript(s)
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).(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 + "\n" + string(debug.Stack()))
valid = false
}
}()
app := service.DI().App()
log.Println("Run script - " + s.Description())
s.Run(app.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)
}
})
}