7
7
"infini.sh/framework/core/global"
8
8
"infini.sh/framework/core/task/chrono"
9
9
"infini.sh/framework/core/util"
10
- "infini.sh/framework/lib/goroutine"
11
10
"runtime"
12
11
"sync"
13
12
"time"
@@ -24,11 +23,8 @@ const (
24
23
Finished = "FINISHED"
25
24
)
26
25
27
- //use task.Run instead of goroutine
28
- var defaultGoRoutingGroup = goroutine .NewGroup (goroutine.Option {Name : "default" })
29
-
30
- func RunWithinGroup (tag string , f func (ctx context.Context ) error ) {
31
- defaultGoRoutingGroup .Go (f )
26
+ func RunWithinGroup (groupName string , f func (ctx context.Context ) error ) (taskID string ) {
27
+ return registerTransientTask (groupName ,"" ,f ,context .Background ())
32
28
}
33
29
34
30
func MustGetString (ctx context.Context , key string ) string {
@@ -42,12 +38,17 @@ func MustGetString(ctx context.Context, key string) string {
42
38
panic (errors .Errorf ("invalid key: %v" , key ))
43
39
}
44
40
45
- func RunWithContext (tag string , f func (ctx context.Context ) error , ctxInput context.Context ) error {
41
+ func RunWithContext (tag string , f func (ctx context.Context ) error , ctxInput context.Context ) (taskID string ) {
42
+ return registerTransientTask ("default" ,tag ,f ,ctxInput )
43
+ }
44
+
45
+ func registerTransientTask (group ,tag string , f func (ctx context.Context ) error , ctxInput context.Context ) (taskID string ) {
46
46
task := ScheduleTask {}
47
47
task .ID = util .GetUUID ()
48
+ task .Group = group
48
49
task .Description = tag
49
50
task .Type = Transient
50
- task .CreateTime = time .Now ()
51
+ task .CreateTime = time .Now ()
51
52
task .State = Pending
52
53
task .Ctx = ctxInput
53
54
Tasks .Store (task .ID , & task )
@@ -75,24 +76,25 @@ func RunWithContext(tag string, f func(ctx context.Context) error, ctxInput cont
75
76
Tasks .Delete (task .ID )
76
77
}()
77
78
78
- t := time .Now ()
79
+ t := time .Now ()
79
80
task .StartTime = & t
80
81
task .State = Running
81
82
err := func2 (ctxInput )
82
83
if err != nil {
83
84
log .Error (err )
84
85
}
85
86
}(f )
86
- return nil
87
+ return task . ID
87
88
}
88
89
89
90
type ScheduleTask struct {
90
91
ID string `config:"id" json:"id,omitempty"`
92
+ Group string `config:"group" json:"group,omitempty"`
91
93
Description string `config:"description" json:"description,omitempty"`
92
94
Type string `config:"type" json:"type,omitempty"`
93
95
Interval string `config:"interval" json:"interval,omitempty"`
94
96
Crontab string `config:"crontab" json:"crontab,omitempty"`
95
- CreateTime time.Time `config:"create_time" json:"create_time,omitempty"`
97
+ CreateTime time.Time `config:"create_time" json:"create_time,omitempty"`
96
98
StartTime * time.Time `config:"start_time" json:"start_time,omitempty"`
97
99
EndTime * time.Time `config:"end_time" json:"end_time,omitempty"`
98
100
@@ -106,12 +108,12 @@ const Interval = "interval"
106
108
const Crontab = "crontab"
107
109
const Transient = "transient"
108
110
109
- func RegisterScheduleTask (task ScheduleTask ) {
111
+ func RegisterScheduleTask (task ScheduleTask ) ( taskID string ) {
110
112
if task .ID == "" {
111
113
task .ID = util .GetUUID ()
112
114
}
113
- task .CreateTime = time .Now ()
114
- task .State = Pending
115
+ task .CreateTime = time .Now ()
116
+ task .State = Pending
115
117
if task .Type == "" && task .Interval != "" {
116
118
task .Type = Interval
117
119
} else if task .Type == "" && task .Crontab != "" {
@@ -143,6 +145,7 @@ func RegisterScheduleTask(task ScheduleTask) {
143
145
runTask (& task )
144
146
}
145
147
148
+ return task .ID
146
149
}
147
150
148
151
var quit = make (chan struct {})
@@ -213,7 +216,7 @@ func StopTask(id string) {
213
216
if ok {
214
217
item , ok := task .(* ScheduleTask )
215
218
if ok {
216
- if item != nil {
219
+ if item != nil {
217
220
switch item .Type {
218
221
case Interval :
219
222
if item .taskItem != nil {
0 commit comments