-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtask_job.go
61 lines (49 loc) · 1.59 KB
/
task_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
package gotasks
import (
"log"
"time"
"github.com/google/uuid"
)
type ArgsMap map[string]interface{}
// StructToArgsMap Convert struct to ArgsMap, e.g. am := StructToArgsMap(yourStruct)
func StructToArgsMap(v interface{}) ArgsMap {
v_bytes, err := json.Marshal(v)
if err != nil {
log.Panicf("failed to convert %+v to ArgsMap: %s", v, err)
}
argsMap := ArgsMap{}
err = json.Unmarshal(v_bytes, &argsMap)
if err != nil {
log.Panicf("failed to convert %+v to ArgsMap: %s", v, err)
}
return argsMap
}
// MapToArgsMap Convert golang map to ArgsMap, e.g. am := MapToArgsMap(yourStruct)
func MapToArgsMap(v interface{}) ArgsMap {
return StructToArgsMap(v)
}
// ArgsMapToStruct Convert ArgsMap to struct, e.g. err := ArgsMapToStruct(am, &yourStruct)
func ArgsMapToStruct(am ArgsMap, s interface{}) error {
v_bytes, err := json.Marshal(am)
if err != nil {
return err
}
return json.Unmarshal(v_bytes, s)
}
type Task struct {
ID string `json:"task_id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
QueueName string `json:"queue_name"`
JobName string `json:"job_name"`
ArgsMap ArgsMap `json:"args_map"`
CurrentHandlerIndex int `json:"current_handler_index"`
OriginalArgsMap ArgsMap `json:"original_args_map"`
ResultLog string `json:"result_log"`
}
func NewTask(queueName, jobName string, argsMap ArgsMap) *Task {
u, _ := uuid.NewUUID()
now := time.Now()
task := &Task{u.String(), now, now, queueName, jobName, argsMap, 0, argsMap, ""}
return task
}