-
Notifications
You must be signed in to change notification settings - Fork 20
/
qor_job.go
284 lines (231 loc) · 5.87 KB
/
qor_job.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package worker
import (
"database/sql/driver"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/jinzhu/gorm"
"github.com/qor/admin"
"github.com/qor/audited"
"github.com/qor/serializable_meta"
)
// QorJobInterface is a interface, defined methods that needs for a qor job
type QorJobInterface interface {
GetJobID() string
GetJobName() string
GetStatus() string
SetStatus(string) error
GetJob() *Job
SetJob(*Job)
GetProgress() uint
SetProgress(uint) error
GetProgressText() string
SetProgressText(string) error
GetLogs() []string
AddLog(string) error
GetResultsTable() ResultsTable
AddResultsRow(...TableCell) error
StartReferesh()
StopReferesh()
GetArgument() interface{}
serializable_meta.SerializableMetaInterface
}
// ResultsTable is a struct, including importing/exporting results
type ResultsTable struct {
Name string `json:"-"` // only used for generate string column in database
TableCells [][]TableCell
}
// Scan used to scan value from database into itself
func (resultsTable *ResultsTable) Scan(data interface{}) error {
switch values := data.(type) {
case []byte:
return json.Unmarshal(values, resultsTable)
case string:
return resultsTable.Scan([]byte(values))
default:
return errors.New("unsupported data type for Qor Job error table")
}
}
// Value used to read value from itself and save it into databae
func (resultsTable ResultsTable) Value() (driver.Value, error) {
result, err := json.Marshal(resultsTable)
return string(result), err
}
// TableCell including Value, Error for a data cell
type TableCell struct {
Value string
Error string
}
// QorJob predefined qor job struct, which will be used for Worker, if it doesn't include a job resource
type QorJob struct {
gorm.Model
Status string `sql:"default:'new'"`
Progress uint
ProgressText string
Log string `sql:"size:65532"`
ResultsTable ResultsTable `sql:"size:65532"`
mutex sync.Mutex `sql:"-"`
stopReferesh bool `sql:"-"`
inReferesh bool `sql:"-"`
// Add `valid:"-"`` to make the QorJob work well with qor/validations
// When the qor/validations auto exec the validate struct callback we get error
// runtime: goroutine stack exceeds 1000000000-byte limit
// fatal error: stack overflow
Job *Job `sql:"-" valid:"-"`
audited.AuditedModel
serializable_meta.SerializableMeta
}
// GetJobID get job's ID from a qor job
func (job *QorJob) GetJobID() string {
return fmt.Sprint(job.ID)
}
// GetJobName get job's name from a qor job
func (job *QorJob) GetJobName() string {
return job.Kind
}
// GetStatus get job's status from a qor job
func (job *QorJob) GetStatus() string {
return job.Status
}
// SetStatus set job's status to a qor job instance
func (job *QorJob) SetStatus(status string) error {
job.mutex.Lock()
defer job.mutex.Unlock()
job.Status = status
if status == JobStatusDone {
job.Progress = 100
}
if job.shouldCallSave() {
return job.callSave()
}
return nil
}
func (job *QorJob) shouldCallSave() bool {
return !job.inReferesh || job.stopReferesh
}
func (job *QorJob) StartReferesh() {
job.mutex.Lock()
defer job.mutex.Unlock()
if !job.inReferesh {
job.inReferesh = true
job.stopReferesh = false
go func() {
job.referesh()
}()
}
}
func (job *QorJob) StopReferesh() {
job.mutex.Lock()
defer job.mutex.Unlock()
err := job.callSave()
if err != nil {
log.Println(err)
}
job.stopReferesh = true
}
func (job *QorJob) referesh() {
job.mutex.Lock()
defer job.mutex.Unlock()
err := job.callSave()
if err != nil {
log.Println(err)
}
if job.stopReferesh {
job.inReferesh = false
job.stopReferesh = false
} else {
time.AfterFunc(5*time.Second, job.referesh)
}
}
func (job *QorJob) callSave() error {
worker := job.GetJob().Worker
context := worker.Admin.NewContext(nil, nil).Context
return worker.JobResource.CallSave(job, context)
}
// SetJob set `Job` for a qor job instance
func (job *QorJob) SetJob(j *Job) {
job.Kind = j.Name
job.Job = j
}
// GetJob get predefined job for a qor job instance
func (job *QorJob) GetJob() *Job {
if job.Job != nil {
return job.Job
}
return nil
}
// GetArgument get job's argument
func (job *QorJob) GetArgument() interface{} {
return job.GetSerializableArgument(job)
}
// GetSerializableArgumentResource get job's argument's resource
func (job *QorJob) GetSerializableArgumentResource() *admin.Resource {
if j := job.GetJob(); j != nil {
return j.Resource
}
return nil
}
// GetProgress get qor job's progress
func (job *QorJob) GetProgress() uint {
return job.Progress
}
// SetProgress set qor job's progress
func (job *QorJob) SetProgress(progress uint) error {
job.mutex.Lock()
defer job.mutex.Unlock()
if progress > 100 {
progress = 100
}
job.Progress = progress
if job.shouldCallSave() {
return job.callSave()
}
return nil
}
// GetProgressText get qor job's progress text
func (job *QorJob) GetProgressText() string {
return job.ProgressText
}
// SetProgressText set qor job's progress text
func (job *QorJob) SetProgressText(str string) error {
job.mutex.Lock()
defer job.mutex.Unlock()
job.ProgressText = str
if job.shouldCallSave() {
return job.callSave()
}
return nil
}
// GetLogs get qor job's logs
func (job *QorJob) GetLogs() []string {
return strings.Split(job.Log, "\n")
}
// AddLog add a log to qor job
func (job *QorJob) AddLog(log string) error {
job.mutex.Lock()
defer job.mutex.Unlock()
fmt.Println(log)
job.Log += "\n" + log
if job.shouldCallSave() {
return job.callSave()
}
return nil
}
// GetResultsTable get the job's process logs
func (job *QorJob) GetResultsTable() ResultsTable {
return job.ResultsTable
}
// AddResultsRow add a row of process results to a job
func (job *QorJob) AddResultsRow(cells ...TableCell) error {
job.mutex.Lock()
defer job.mutex.Unlock()
job.ResultsTable.TableCells = append(job.ResultsTable.TableCells, cells)
if job.shouldCallSave() {
return job.callSave()
}
return nil
}