-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feature: TaskContext增加TimeoutContext与TimeoutCancel属性,用于超时控制,应用可根据需要从TaskContext获取 * feature: TaskInfo增加SetTimeout,用于设置超时时间,单位为秒 * refactor: 移除TaskInfo.Context() * 2020-04-25 16:00 at ShangHai
- Loading branch information
Showing
10 changed files
with
263 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package task | ||
|
||
import "context" | ||
|
||
//Task上下文信息 | ||
type TaskContext struct { | ||
TaskID string | ||
TaskData interface{} //用于当前Task全局设置的数据项 | ||
Message interface{} //用于每次Task执行上下文消息传输 | ||
IsEnd bool //如果设置该属性为true,则停止当次任务的后续执行,一般用在OnBegin中 | ||
Error error | ||
Header map[string]interface{} | ||
TimeoutContext context.Context | ||
TimeoutCancel context.CancelFunc | ||
doneChan chan struct{} | ||
} | ||
|
||
func (c TaskContext) reset() { | ||
c.TaskID = "" | ||
c.TaskData = nil | ||
c.Message = nil | ||
c.IsEnd = false | ||
c.Error = nil | ||
c.Header = nil | ||
c.TimeoutContext = nil | ||
c.TimeoutCancel = nil | ||
c.doneChan = nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
. "github.com/devfeel/dottask" | ||
"time" | ||
) | ||
|
||
func main() { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
defer cancel() | ||
query(ctx) | ||
var service *TaskService | ||
|
||
time.Sleep(time.Hour) | ||
} | ||
var firstLoopTimeout = 0 | ||
var firstCronTimeout = 0 | ||
|
||
var patchLoop = 0 | ||
var patchCron = 0 | ||
|
||
func query(ctx context.Context) { | ||
notifyChan := make(chan string) | ||
doing := func() { | ||
func Job_Timeout_Test(ctx *TaskContext) error { | ||
patchCron += 1 | ||
patch := patchLoop | ||
|
||
if firstLoopTimeout <= 0 { | ||
firstLoopTimeout = 1 | ||
time.Sleep(time.Second * 15) | ||
} else { | ||
time.Sleep(time.Second) | ||
fmt.Println("1:", time.Now()) | ||
time.Sleep(time.Second * 3) | ||
fmt.Println("2:", time.Now()) | ||
time.Sleep(time.Second * 5) | ||
fmt.Println("3:", time.Now()) | ||
notifyChan <- "1" | ||
} | ||
go doing() | ||
select { | ||
case <-notifyChan: | ||
fmt.Println("done") | ||
case <-ctx.Done(): | ||
fmt.Println("timeout") | ||
|
||
fmt.Println(time.Now().String(), " => Job_Timeout_Test", patch) | ||
return nil | ||
} | ||
|
||
func Loop_Timeout_Test(ctx *TaskContext) error { | ||
patchLoop += 1 | ||
patch := patchLoop | ||
if firstCronTimeout <= 0 { | ||
firstCronTimeout = 1 | ||
time.Sleep(time.Second * 20) | ||
} | ||
|
||
fmt.Println(time.Now().String(), " => Loop_Timeout_Test", patch) | ||
return nil | ||
} | ||
|
||
func main() { | ||
service = StartNewService() | ||
|
||
t1, err := service.CreateCronTask("test-timeout-cron", true, "*/10 * * * * *", Job_Timeout_Test, nil) | ||
if err != nil { | ||
fmt.Println("service.CreateCronTask error! => ", err.Error()) | ||
} | ||
t1.SetTimeout(5) | ||
t2, err := service.CreateLoopTask("test-timeout-loop", true, 0, 10000, Loop_Timeout_Test, nil) | ||
if err != nil { | ||
fmt.Println("service.CreateLoopTask error! => ", err.Error()) | ||
} | ||
t2.SetTimeout(5) | ||
|
||
service.StartAllTask() | ||
|
||
for { | ||
time.Sleep(time.Hour) | ||
} | ||
|
||
} |
Oops, something went wrong.