Skip to content

Commit 6affbe4

Browse files
committed
refactor: refactoring tasks
1 parent 6811c5e commit 6affbe4

File tree

7 files changed

+25
-573
lines changed

7 files changed

+25
-573
lines changed

NOTICE

+1-6
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,4 @@ distributed under the License is distributed on an "AS IS" BASIS,
200200
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201201
See the License for the specific language governing permissions and
202202
limitations under the License.
203-
*/
204-
205-
// Unless explicitly stated otherwise all files in this repository are licensed
206-
// under the MIT License.
207-
// This product includes software developed at Guance Cloud (https://www.guance.com/).
208-
// Copyright 2021-present Guance, Inc.
203+
*/

app.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -218,18 +218,17 @@ func (app *App) initEnvironment(customFunc func()) {
218218
if err != nil {
219219
panic(err)
220220
}
221+
221222
config.RegisterOption("keystore", ksResolver)
222-
err = task.RunWithContext("keystore_changes_notify", func(ctx context.Context) error {
223+
task.RunWithContext("keystore_changes_notify", func(ctx context.Context) error {
223224
_, err = keystore.GetOrInitKeystore()
224225
if err != nil {
225226
log.Error(err)
226227
}
227228
keystore.Watch()
228229
return nil
229230
}, context.Background())
230-
if err != nil {
231-
panic(err)
232-
}
231+
233232
global.RegisterShutdownCallback(keystore.CloseWatch)
234233
app.environment.Init()
235234

core/task/task.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"infini.sh/framework/core/global"
88
"infini.sh/framework/core/task/chrono"
99
"infini.sh/framework/core/util"
10-
"infini.sh/framework/lib/goroutine"
1110
"runtime"
1211
"sync"
1312
"time"
@@ -24,11 +23,8 @@ const (
2423
Finished = "FINISHED"
2524
)
2625

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())
3228
}
3329

3430
func MustGetString(ctx context.Context, key string) string {
@@ -42,12 +38,17 @@ func MustGetString(ctx context.Context, key string) string {
4238
panic(errors.Errorf("invalid key: %v", key))
4339
}
4440

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) {
4646
task := ScheduleTask{}
4747
task.ID = util.GetUUID()
48+
task.Group=group
4849
task.Description = tag
4950
task.Type = Transient
50-
task.CreateTime=time.Now()
51+
task.CreateTime = time.Now()
5152
task.State = Pending
5253
task.Ctx = ctxInput
5354
Tasks.Store(task.ID, &task)
@@ -75,24 +76,25 @@ func RunWithContext(tag string, f func(ctx context.Context) error, ctxInput cont
7576
Tasks.Delete(task.ID)
7677
}()
7778

78-
t:=time.Now()
79+
t := time.Now()
7980
task.StartTime = &t
8081
task.State = Running
8182
err := func2(ctxInput)
8283
if err != nil {
8384
log.Error(err)
8485
}
8586
}(f)
86-
return nil
87+
return task.ID
8788
}
8889

8990
type ScheduleTask struct {
9091
ID string `config:"id" json:"id,omitempty"`
92+
Group string `config:"group" json:"group,omitempty"`
9193
Description string `config:"description" json:"description,omitempty"`
9294
Type string `config:"type" json:"type,omitempty"`
9395
Interval string `config:"interval" json:"interval,omitempty"`
9496
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"`
9698
StartTime *time.Time `config:"start_time" json:"start_time,omitempty"`
9799
EndTime *time.Time `config:"end_time" json:"end_time,omitempty"`
98100

@@ -106,12 +108,12 @@ const Interval = "interval"
106108
const Crontab = "crontab"
107109
const Transient = "transient"
108110

109-
func RegisterScheduleTask(task ScheduleTask) {
111+
func RegisterScheduleTask(task ScheduleTask) (taskID string) {
110112
if task.ID == "" {
111113
task.ID = util.GetUUID()
112114
}
113-
task.CreateTime=time.Now()
114-
task.State =Pending
115+
task.CreateTime = time.Now()
116+
task.State = Pending
115117
if task.Type == "" && task.Interval != "" {
116118
task.Type = Interval
117119
} else if task.Type == "" && task.Crontab != "" {
@@ -143,6 +145,7 @@ func RegisterScheduleTask(task ScheduleTask) {
143145
runTask(&task)
144146
}
145147

148+
return task.ID
146149
}
147150

148151
var quit = make(chan struct{})
@@ -213,7 +216,7 @@ func StopTask(id string) {
213216
if ok {
214217
item, ok := task.(*ScheduleTask)
215218
if ok {
216-
if item != nil{
219+
if item != nil {
217220
switch item.Type {
218221
case Interval:
219222
if item.taskItem != nil {

lib/goroutine/goroutine.go

-201
This file was deleted.

0 commit comments

Comments
 (0)