@@ -31,7 +31,7 @@ func (task *CronTask) GetConfig() *TaskConfig {
3131 DueTime : task .DueTime ,
3232 Interval : 0 ,
3333 Express : task .RawExpress ,
34- TaskData : task .Context (). TaskData ,
34+ TaskData : task .TaskData ,
3535 }
3636}
3737
@@ -58,7 +58,7 @@ func (task *CronTask) Reset(conf *TaskConfig) error {
5858 task .Stop ()
5959 task .IsRun = conf .IsRun
6060 if conf .TaskData != nil {
61- task .Context (). TaskData = conf .TaskData
61+ task .TaskData = conf .TaskData
6262 }
6363 if conf .Handler != nil {
6464 task .handler = conf .Handler
@@ -104,23 +104,20 @@ func (task *CronTask) Start() {
104104// no recover panic
105105// support for #6 新增RunOnce方法建议
106106func (task * CronTask ) RunOnce () error {
107- err := task .handler (task .context )
107+ err := task .handler (task .getTaskContext () )
108108 return err
109109}
110110
111111// NewCronTask create new cron task
112112func NewCronTask (taskID string , isRun bool , express string , handler TaskHandle , taskData interface {}) (Task , error ) {
113- context := new (TaskContext )
114- context .TaskID = taskID
115- context .TaskData = taskData
116-
117113 task := new (CronTask )
118114 task .initCounters ()
119- task .taskID = context . TaskID
115+ task .taskID = taskID
120116 task .TaskType = TaskType_Cron
121117 task .IsRun = isRun
122118 task .handler = handler
123119 task .RawExpress = express
120+ task .TaskData = taskData
124121 expressList := strings .Split (express , " " )
125122 if len (expressList ) != 6 {
126123 return nil , errors .New ("express is wrong format => not 6 parts" )
@@ -133,7 +130,6 @@ func NewCronTask(taskID string, isRun bool, express string, handler TaskHandle,
133130 task .time_Second = parseExpress (expressList [0 ], ExpressType_Second )
134131
135132 task .State = TaskState_Init
136- task .context = context
137133 return task , nil
138134}
139135
@@ -155,48 +151,71 @@ func startCronTask(task *CronTask) {
155151}
156152
157153func doCronTask (task * CronTask ) {
154+ taskCtx := task .getTaskContext ()
158155 defer func () {
159- task .Context ().Header = nil
160- task .Context ().Error = nil
156+ if taskCtx .TimeoutCancel != nil {
157+ taskCtx .TimeoutCancel ()
158+ }
159+ task .putTaskContext (taskCtx )
161160 if err := recover (); err != nil {
162161 task .CounterInfo ().ErrorCounter .Inc (1 )
163- task .taskService .Logger ().Debug (fmt .Sprint (task .TaskID , " cron handler recover error => " , err ))
162+ task .taskService .Logger ().Debug (fmt .Sprint (task .TaskID () , " cron handler recover error => " , err ))
164163 if task .taskService .ExceptionHandler != nil {
165- task .taskService .ExceptionHandler (task . Context () , fmt .Errorf ("%v" , err ))
164+ task .taskService .ExceptionHandler (taskCtx , fmt .Errorf ("%v" , err ))
166165 }
167166 //goroutine panic, restart cron task
168167 startCronTask (task )
169- task .taskService .Logger ().Debug (fmt .Sprint (task .TaskID , " goroutine panic, restart CronTask" ))
168+ task .taskService .Logger ().Debug (fmt .Sprint (task .TaskID () , " goroutine panic, restart CronTask" ))
170169 }
171170 }()
172- now := time .Now ()
173- task .context .Header = make (map [string ]interface {})
174- if task .time_WeekDay .IsMatch (now ) &&
175- task .time_Month .IsMatch (now ) &&
176- task .time_Day .IsMatch (now ) &&
177- task .time_Hour .IsMatch (now ) &&
178- task .time_Minute .IsMatch (now ) &&
179- task .time_Second .IsMatch (now ) {
180-
181- //inc run counter
182- task .CounterInfo ().RunCounter .Inc (1 )
183- //do log
184- if task .taskService != nil && task .taskService .OnBeforeHandler != nil {
185- task .taskService .OnBeforeHandler (task .Context ())
186- }
187- var err error
188- if ! task .Context ().IsEnd {
189- err = task .handler (task .Context ())
190- }
191- if err != nil {
192- task .Context ().Error = err
193- task .CounterInfo ().ErrorCounter .Inc (1 )
194- if task .taskService != nil && task .taskService .ExceptionHandler != nil {
195- task .taskService .ExceptionHandler (task .Context (), err )
171+
172+ handler := func () {
173+ defer func () {
174+ if task .Timeout > 0 {
175+ taskCtx .doneChan <- struct {}{}
176+ }
177+ }()
178+ now := time .Now ()
179+ if task .time_WeekDay .IsMatch (now ) &&
180+ task .time_Month .IsMatch (now ) &&
181+ task .time_Day .IsMatch (now ) &&
182+ task .time_Hour .IsMatch (now ) &&
183+ task .time_Minute .IsMatch (now ) &&
184+ task .time_Second .IsMatch (now ) {
185+
186+ //inc run counter
187+ task .CounterInfo ().RunCounter .Inc (1 )
188+ //do log
189+ if task .taskService != nil && task .taskService .OnBeforeHandler != nil {
190+ task .taskService .OnBeforeHandler (taskCtx )
191+ }
192+ var err error
193+ if ! taskCtx .IsEnd {
194+ err = task .handler (taskCtx )
195+ }
196+ if err != nil {
197+ taskCtx .Error = err
198+ task .CounterInfo ().ErrorCounter .Inc (1 )
199+ if task .taskService != nil && task .taskService .ExceptionHandler != nil {
200+ task .taskService .ExceptionHandler (taskCtx , err )
201+ }
202+ }
203+ if task .taskService != nil && task .taskService .OnEndHandler != nil {
204+ task .taskService .OnEndHandler (taskCtx )
196205 }
197206 }
198- if task .taskService != nil && task .taskService .OnEndHandler != nil {
199- task .taskService .OnEndHandler (task .Context ())
207+ }
208+
209+ if task .Timeout > 0 {
210+ go handler ()
211+ select {
212+ case <- taskCtx .TimeoutContext .Done ():
213+ task .taskService .Logger ().Debug (fmt .Sprint (task .TaskID (), "do handler timeout." ))
214+ case <- taskCtx .doneChan :
215+ return
200216 }
217+ } else {
218+ handler ()
201219 }
220+
202221}
0 commit comments