-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorm_model_task.go
349 lines (314 loc) · 13.5 KB
/
gorm_model_task.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package gojobs
import (
"context"
"errors"
"fmt"
"go.dtapp.net/gotime"
"go.opentelemetry.io/otel/codes"
"gorm.io/gorm"
"time"
)
// GormModelTask 任务
type GormModelTask struct {
ID uint `gorm:"primaryKey;comment:记录编号" json:"id,omitempty"` // 记录编号
Status string `gorm:"index;comment:状态码" json:"status,omitempty"` // 状态码
StatusDesc string `gorm:"comment:状态描述" json:"status_desc,omitempty"` // 状态描述
Params string `gorm:"comment:参数" json:"params,omitempty"` // 参数
Frequency int64 `gorm:"index;comment:频率(秒单位)" json:"frequency,omitempty"` // 频率(秒单位)
Spec string `gorm:"index;comment:cron表达式" json:"spec,omitempty"` // cron表达式
Number int64 `gorm:"comment:当前次数" json:"number,omitempty"` // 当前次数
MaxNumber int64 `gorm:"comment:最大次数" json:"max_number,omitempty"` // 最大次数
RunID string `gorm:"comment:执行编号" json:"run_id,omitempty"` // 执行编号
CustomID string `gorm:"index;comment:自定义编号" json:"custom_id,omitempty"` // 自定义编号
CustomSequence int64 `gorm:"comment:自定义顺序" json:"custom_sequence,omitempty"` // 自定义顺序
Type string `gorm:"index;comment:类型" json:"type,omitempty"` // 类型
TypeName string `gorm:"comment:类型名称" json:"type_name,omitempty"` // 类型名称
CreatedIP string `gorm:"default:0.0.0.0;comment:创建外网IP" json:"created_ip,omitempty"` // 创建外网IP
SpecifyIP string `gorm:"default:0.0.0.0;index;comment:指定外网IP" json:"specify_ip,omitempty"` // 指定外网IP
UpdatedIP string `gorm:"default:0.0.0.0;comment:更新外网IP" json:"updated_ip,omitempty"` // 更新外网IP
Result string `gorm:"comment:结果" json:"result,omitempty"` // 结果
NextRunTime time.Time `gorm:"comment:下次运行时间" json:"next_run_time,omitempty"` // 下次运行时间
CreatedAt time.Time `gorm:"autoCreateTime;comment:创建时间" json:"created_at,omitempty"` // 创建时间
UpdatedAt time.Time `gorm:"autoUpdateTime;comment:更新时间" json:"updated_at,omitempty"` // 更新时间
DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间" json:"deleted_at,omitempty"` // 删除时间
}
// TaskTakeId 编号查询任务
func (c *Client) TaskTakeId(ctx context.Context, tx *gorm.DB, id uint) (result GormModelTask) {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("id = ?", id).
Take(&result).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return result
}
if err != nil {
err = fmt.Errorf("编号查询任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return result
}
// TaskTake 自定义编号查询任务
func (c *Client) TaskTake(ctx context.Context, tx *gorm.DB, customId string) (result GormModelTask) {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("custom_id = ?", customId).
Take(&result).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return result
}
if err != nil {
err = fmt.Errorf("自定义编号查询任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return result
}
// 自定义编号加状态查询任务
func (c *Client) taskTake(ctx context.Context, tx *gorm.DB, customId, status string) (result GormModelTask) {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("custom_id = ?", customId).
Where("status = ?", status).
Take(&result).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return result
}
if err != nil {
err = fmt.Errorf("自定义编号加状态查询任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return result
}
// TaskTakeIn 查询单任务 - 任务运行
func (c *Client) TaskTakeIn(ctx context.Context, tx *gorm.DB, customId string) GormModelTask {
return c.taskTake(ctx, tx, customId, TASK_IN)
}
// TaskTakeSuccess 查询单任务 - 任务完成
func (c *Client) TaskTakeSuccess(ctx context.Context, tx *gorm.DB, customId string) GormModelTask {
return c.taskTake(ctx, tx, customId, TASK_SUCCESS)
}
// TaskTakeError 查询单任务 - 任务异常
func (c *Client) TaskTakeError(ctx context.Context, tx *gorm.DB, customId string) GormModelTask {
return c.taskTake(ctx, tx, customId, TASK_ERROR)
}
// TaskTakeTimeout 查询单任务 - 任务超时
func (c *Client) TaskTakeTimeout(ctx context.Context, tx *gorm.DB, customId string) GormModelTask {
return c.taskTake(ctx, tx, customId, TASK_TIMEOUT)
}
// TaskTakeWait 查询单任务 - 任务等待
func (c *Client) TaskTakeWait(ctx context.Context, tx *gorm.DB, customId string) GormModelTask {
return c.taskTake(ctx, tx, customId, TASK_WAIT)
}
// TaskTypeTake 查询单任务
func (c *Client) TaskTypeTake(ctx context.Context, tx *gorm.DB, customId, Type string) (result GormModelTask) {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("custom_id = ?", customId).
Where("type = ?", Type).
Take(&result).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return result
}
if err != nil {
err = fmt.Errorf("查询单任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return result
}
// 查询单任务
func (c *Client) taskTypeTake(ctx context.Context, tx *gorm.DB, customId, Type, status string) (result GormModelTask) {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("custom_id = ?", customId).Where("type = ?", Type).
Where("status = ?", status).
Take(&result).Error
if errors.Is(err, gorm.ErrRecordNotFound) {
return result
}
if err != nil {
err = fmt.Errorf("查询单任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return result
}
// TaskTypeTakeIn 查询单任务 - 任务运行
func (c *Client) TaskTypeTakeIn(ctx context.Context, tx *gorm.DB, customId, Type string) GormModelTask {
return c.taskTypeTake(ctx, tx, customId, Type, TASK_IN)
}
// TaskTypeTakeSuccess 查询单任务 - 任务完成
func (c *Client) TaskTypeTakeSuccess(ctx context.Context, tx *gorm.DB, customId, Type string) GormModelTask {
return c.taskTypeTake(ctx, tx, customId, Type, TASK_SUCCESS)
}
// TaskTypeTakeError 查询单任务 - 任务异常
func (c *Client) TaskTypeTakeError(ctx context.Context, tx *gorm.DB, customId, Type string) GormModelTask {
return c.taskTypeTake(ctx, tx, customId, Type, TASK_ERROR)
}
// TaskTypeTakeTimeout 查询单任务 - 任务超时
func (c *Client) TaskTypeTakeTimeout(ctx context.Context, tx *gorm.DB, customId, Type string) GormModelTask {
return c.taskTypeTake(ctx, tx, customId, Type, TASK_TIMEOUT)
}
// TaskTypeTakeWait 查询单任务 - 任务等待
func (c *Client) TaskTypeTakeWait(ctx context.Context, tx *gorm.DB, customId, Type string) GormModelTask {
return c.taskTypeTake(ctx, tx, customId, Type, TASK_WAIT)
}
// TaskFindAll 查询多任务
func (c *Client) TaskFindAll(ctx context.Context, tx *gorm.DB, frequency int64) (results []GormModelTask) {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("frequency = ?", frequency).
Order("id asc").
Find(&results).Error
if err != nil {
err = fmt.Errorf("查询多任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return results
}
// TaskFindAllType 查询多任务
func (c *Client) TaskFindAllType(ctx context.Context, tx *gorm.DB, Type string, frequency int64) (results []GormModelTask) {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("type = ?", Type).
Where("frequency = ?", frequency).
Order("id asc").
Find(&results).Error
if err != nil {
err = fmt.Errorf("查询多任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return results
}
// 查询多任务
func (c *Client) taskFindAll(ctx context.Context, tx *gorm.DB, frequency int64, status string) (results []GormModelTask) {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("frequency = ?", frequency).
Where("status = ?", status).
Order("id asc").
Find(&results).Error
if err != nil {
err = fmt.Errorf("查询多任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return results
}
// 查询多任务
func (c *Client) taskFindAllType(ctx context.Context, tx *gorm.DB, Type string, frequency int64, status string) (results []GormModelTask) {
if frequency == 0 {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("type = ?", Type).
Where("status = ?", status).
Order("id asc").
Find(&results).Error
if err != nil {
err = fmt.Errorf("查询多任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return results
}
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("type = ?", Type).
Where("frequency = ?", frequency).
Where("status = ?", status).
Order("id asc").
Find(&results).Error
if err != nil {
err = fmt.Errorf("查询多任务:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return results
}
// TaskFindAllIn 查询多任务 - 任务运行
func (c *Client) TaskFindAllIn(ctx context.Context, tx *gorm.DB, frequency int64) []GormModelTask {
return c.taskFindAll(ctx, tx, frequency, TASK_IN)
}
// TaskFindAllInType 查询多任务 - 任务运行
func (c *Client) TaskFindAllInType(ctx context.Context, tx *gorm.DB, Type string) []GormModelTask {
return c.taskFindAllType(ctx, tx, Type, 0, TASK_IN)
}
// TaskFindAllSuccess 查询多任务 - 任务完成
func (c *Client) TaskFindAllSuccess(ctx context.Context, tx *gorm.DB, frequency int64) []GormModelTask {
return c.taskFindAll(ctx, tx, frequency, TASK_SUCCESS)
}
// TaskFindAllSuccessType 查询多任务 - 任务完成
func (c *Client) TaskFindAllSuccessType(ctx context.Context, tx *gorm.DB, Type string) []GormModelTask {
return c.taskFindAllType(ctx, tx, Type, 0, TASK_SUCCESS)
}
// TaskFindAllError 查询多任务 - 任务异常
func (c *Client) TaskFindAllError(ctx context.Context, tx *gorm.DB, frequency int64) []GormModelTask {
return c.taskFindAll(ctx, tx, frequency, TASK_ERROR)
}
// TaskFindAllErrorType 查询多任务 - 任务异常
func (c *Client) TaskFindAllErrorType(ctx context.Context, tx *gorm.DB, Type string) []GormModelTask {
return c.taskFindAllType(ctx, tx, Type, 0, TASK_ERROR)
}
// TaskFindAllTimeout 查询多任务 - 任务超时
func (c *Client) TaskFindAllTimeout(ctx context.Context, tx *gorm.DB, frequency int64) []GormModelTask {
return c.taskFindAll(ctx, tx, frequency, TASK_TIMEOUT)
}
// TaskFindAllTimeoutType 查询多任务 - 任务超时
func (c *Client) TaskFindAllTimeoutType(ctx context.Context, tx *gorm.DB, Type string) []GormModelTask {
return c.taskFindAllType(ctx, tx, Type, 0, TASK_TIMEOUT)
}
// TaskFindAllWait 查询多任务 - 任务等待
func (c *Client) TaskFindAllWait(ctx context.Context, tx *gorm.DB, frequency int64) []GormModelTask {
return c.taskFindAll(ctx, tx, frequency, TASK_WAIT)
}
// TaskFindAllWaitType 查询多任务 - 任务等待
func (c *Client) TaskFindAllWaitType(ctx context.Context, tx *gorm.DB, Type string) []GormModelTask {
return c.taskFindAllType(ctx, tx, Type, 0, TASK_WAIT)
}
// StartTask 任务启动
func (c *Client) StartTask(ctx context.Context, tx *gorm.DB, id uint) error {
err := c.EditTask(ctx, tx, id).
Select("status", "status_desc").
Updates(GormModelTask{
Status: TASK_IN,
StatusDesc: "启动任务",
}).Error
if err != nil {
err = fmt.Errorf("任务启动失败:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return err
}
// StartTaskCustom 任务启动自定义
func (c *Client) StartTaskCustom(ctx context.Context, tx *gorm.DB, customId string, customSequence int64) error {
err := tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("custom_id = ?", customId).
Where("custom_sequence = ?", customSequence).
Where("status = ?", TASK_WAIT).
Select("status", "status_desc").
Updates(GormModelTask{
Status: TASK_IN,
StatusDesc: "启动任务",
}).Error
if err != nil {
err = fmt.Errorf("任务启动自定义失败:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return err
}
// EditTask 任务修改
func (c *Client) EditTask(ctx context.Context, tx *gorm.DB, id uint) *gorm.DB {
return tx.WithContext(ctx).Table(c.gormConfig.taskTableName).
Where("id = ?", id)
}
// UpdateFrequency 更新任务频率
func (c *Client) UpdateFrequency(ctx context.Context, tx *gorm.DB, id uint, frequency int64) error {
err := c.EditTask(ctx, tx, id).
Select("frequency", "next_run_time").
Updates(GormModelTask{
Frequency: frequency,
NextRunTime: gotime.Current().AfterSeconds(frequency).Time,
}).Error
if err != nil {
err = fmt.Errorf("更新任务频率失败:%v", err)
TraceRecordError(ctx, err)
TraceSetStatus(ctx, codes.Error, err.Error())
}
return err
}