-
Notifications
You must be signed in to change notification settings - Fork 13
/
script.go
236 lines (179 loc) · 4.96 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package hitrix
import (
"fmt"
"log"
"os"
"strings"
"time"
"github.com/latolukasz/beeorm"
"github.com/ryanuber/columnize"
"github.com/coretrix/hitrix/pkg/entity"
"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)
if !isInterval && !isInfinity {
log.Println("Failed script - "+s.Description(), " - it must implement either Interval or Infinity interface")
}
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() {
log.Println("starting orm background consumer")
asyncConsumer := beeorm.NewBackgroundConsumer(ormService)
for {
if asyncConsumer.Digest(appService.GlobalContext) {
log.Println("orm background consumer exited successfully")
break
}
log.Println("orm background consumer count not obtain lock, sleeping for 30 seconds")
time.Sleep(time.Second * 30)
}
})
}
func (processor *BackgroundProcessor) RunAsyncRequestLoggerCleaner() {
ormService := service.DI().OrmEngine()
ormConfig := service.DI().OrmConfig()
entities := ormConfig.GetEntities()
if _, ok := entities["entity.RequestLoggerEntity"]; !ok {
panic("you should register RequestLoggerEntity")
}
GoroutineWithRestart(func() {
log.Println("starting request logger cleaner")
for {
removeAllOldRequestLoggerRows(ormService)
log.Println("sleeping request logger cleaner")
time.Sleep(time.Minute * 30)
}
})
}
func removeAllOldRequestLoggerRows(ormService *beeorm.Engine) {
pager := beeorm.NewPager(1, 1000)
for {
query := beeorm.NewRedisSearchQuery()
query.FilterDateLess("CreatedAt", service.DI().Clock().Now().AddDate(0, -1, 0))
var requestLoggerEntities []*entity.RequestLoggerEntity
ormService.RedisSearch(&requestLoggerEntities, query, pager)
flusher := ormService.NewFlusher()
for _, requestLoggerEntity := range requestLoggerEntities {
flusher.Delete(requestLoggerEntity)
}
flusher.Flush()
log.Printf("%d rows was removed", len(requestLoggerEntities))
if len(requestLoggerEntities) < pager.PageSize {
break
}
pager.IncrementPage()
}
}