Skip to content

Commit

Permalink
#### Version 0.9.1
Browse files Browse the repository at this point in the history
* feature: 新增输出Task信息与Counter信息的go原生HandlerFunc
* fix: 修复CronTask对RunCounter统计逻辑BUG
* refactor: 重构startCronTask实现
* About HttpOutput:
    - CounterOutputHttpHandler: 用于输出当前时刻所有Task的Counter信息
    - TaskOutputHttpHandler: 用于输出所有Task的配置信息
    - Code Example Link: https://github.com/devfeel/dotweb-start/blob/master/server/router.go#L20
* How to use:
~~~ go
	server.RegisterHandlerFunc(dotweb.RouteMethod_GET, "/task/counter", global.DotTask.CounterOutputHttpHandler)
~~~
* 2019-11-10 09:00
  • Loading branch information
devfeel committed Nov 10, 2019
1 parent 5e61612 commit d1bf90f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 41 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* 支持单独执行TaskHandler
* 支持代码级重设Task的相关设置
* 内建Task运行计数信息,包含执行与异常计数
* 内建针对Task与Counter的OutputHttpHandler,可方便与WebServer自动集成


## 安装:
Expand Down
87 changes: 46 additions & 41 deletions crontask.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,54 +140,59 @@ func NewCronTask(taskID string, isRun bool, express string, handler TaskHandle,
//start cron task
func startCronTask(task *CronTask) {
now := time.Now()
nowsecond := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), 0, time.Local)
afterTime := nowsecond.Add(time.Second).Sub(time.Now().Local())
nowSecond := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second(), 0, time.Local)
afterTime := nowSecond.Add(time.Second).Sub(time.Now().Local())
task.TimeTicker = time.NewTicker(DefaultPeriod)
go func() {
time.Sleep(afterTime)
for {
select {
case <-task.TimeTicker.C:
defer func() {
task.CounterInfo().RunCounter.Inc(1)
if err := recover(); err != nil {
task.CounterInfo().ErrorCounter.Inc(1)
task.taskService.Logger().Debug(task.TaskID, " cron handler recover error => ", err)
if task.taskService.ExceptionHandler != nil {
task.taskService.ExceptionHandler(task.Context(), fmt.Errorf("%v", err))
}
//goroutine panic, restart cron task
startCronTask(task)
task.taskService.Logger().Debug(task.TaskID, " goroutine panic, restart CronTask")
}
}()
now := time.Now()
if task.time_WeekDay.IsMatch(now) &&
task.time_Month.IsMatch(now) &&
task.time_Day.IsMatch(now) &&
task.time_Hour.IsMatch(now) &&
task.time_Minute.IsMatch(now) &&
task.time_Second.IsMatch(now) {
//do log
//task.taskService.Logger().Debug(task.TaskID, " begin dohandler")
if task.taskService != nil && task.taskService.OnBeforeHandler != nil {
task.taskService.OnBeforeHandler(task.Context())
}
var err error
if !task.Context().IsEnd {
err = task.handler(task.Context())
}
if err != nil {
task.CounterInfo().ErrorCounter.Inc(1)
if task.taskService != nil && task.taskService.ExceptionHandler != nil {
task.taskService.ExceptionHandler(task.Context(), err)
}
}
if task.taskService != nil && task.taskService.OnEndHandler != nil {
task.taskService.OnEndHandler(task.Context())
}
}
doCronTask(task)
}
}
}()
}

func doCronTask(task *CronTask) {
defer func() {
if err := recover(); err != nil {
task.CounterInfo().ErrorCounter.Inc(1)
task.taskService.Logger().Debug(task.TaskID, " cron handler recover error => ", err)
if task.taskService.ExceptionHandler != nil {
task.taskService.ExceptionHandler(task.Context(), fmt.Errorf("%v", err))
}
//goroutine panic, restart cron task
startCronTask(task)
task.taskService.Logger().Debug(task.TaskID, " goroutine panic, restart CronTask")
}
}()
now := time.Now()
if task.time_WeekDay.IsMatch(now) &&
task.time_Month.IsMatch(now) &&
task.time_Day.IsMatch(now) &&
task.time_Hour.IsMatch(now) &&
task.time_Minute.IsMatch(now) &&
task.time_Second.IsMatch(now) {

//inc run counter
task.CounterInfo().RunCounter.Inc(1)
//do log
if task.taskService != nil && task.taskService.OnBeforeHandler != nil {
task.taskService.OnBeforeHandler(task.Context())
}
var err error
if !task.Context().IsEnd {
err = task.handler(task.Context())
}
if err != nil {
task.CounterInfo().ErrorCounter.Inc(1)
if task.taskService != nil && task.taskService.ExceptionHandler != nil {
task.taskService.ExceptionHandler(task.Context(), err)
}
}
if task.taskService != nil && task.taskService.OnEndHandler != nil {
task.taskService.OnEndHandler(task.Context())
}
}
}
18 changes: 18 additions & 0 deletions tasks_httphandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package task

import (
"encoding/json"
"net/http"
)

// CounterOutputHttpHandler Http Handler for output counter info
func (service *TaskService) CounterOutputHttpHandler(w http.ResponseWriter, r *http.Request) {
str, _ := json.Marshal(service.GetAllTaskCountInfo())
w.Write([]byte(str))
}

// TaskOutputHttpHandler Http Handler for output task info
func (service *TaskService) TaskOutputHttpHandler(w http.ResponseWriter, r *http.Request) {
str, _ := json.Marshal(service.GetAllTasks())
w.Write([]byte(str))
}
14 changes: 14 additions & 0 deletions version.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
## devfeel/dottask

#### Version 0.9.1
* feature: 新增输出Task信息与Counter信息的go原生HandlerFunc
* fix: 修复CronTask对RunCounter统计逻辑BUG
* refactor: 重构startCronTask实现
* About HttpOutput:
- CounterOutputHttpHandler: 用于输出当前时刻所有Task的Counter信息
- TaskOutputHttpHandler: 用于输出所有Task的配置信息
- Code Example Link: https://github.com/devfeel/dotweb-start/blob/master/server/router.go#L20
* How to use:
~~~ go
server.RegisterHandlerFunc(dotweb.RouteMethod_GET, "/task/counter", global.DotTask.CounterOutputHttpHandler)
~~~
* 2019-11-10 09:00

#### Version 0.9
* feature: 新增计数器单元Counter,用于Task执行的统计度量
* feature: 新增NewCronTask\NewLoopTask\NewQueueTask,用于创建独立的非强耦合TaskService的Task
Expand Down

0 comments on commit d1bf90f

Please sign in to comment.