mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 12:00:55 +00:00
Store nearest error info
This commit is contained in:
parent
d6779c4a90
commit
7258ef113a
@ -102,6 +102,8 @@ type taskerr struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Task task struct
|
// Task task struct
|
||||||
|
// It's not a thread-safe structure.
|
||||||
|
// Only nearest errors will be saved in ErrList
|
||||||
type Task struct {
|
type Task struct {
|
||||||
Taskname string
|
Taskname string
|
||||||
Spec *Schedule
|
Spec *Schedule
|
||||||
@ -111,6 +113,7 @@ type Task struct {
|
|||||||
Next time.Time
|
Next time.Time
|
||||||
Errlist []*taskerr // like errtime:errinfo
|
Errlist []*taskerr // like errtime:errinfo
|
||||||
ErrLimit int // max length for the errlist, 0 stand for no limit
|
ErrLimit int // max length for the errlist, 0 stand for no limit
|
||||||
|
errCnt int // records the error count during the execution
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTask add new task with name, time and func
|
// NewTask add new task with name, time and func
|
||||||
@ -119,8 +122,11 @@ func NewTask(tname string, spec string, f TaskFunc) *Task {
|
|||||||
task := &Task{
|
task := &Task{
|
||||||
Taskname: tname,
|
Taskname: tname,
|
||||||
DoFunc: f,
|
DoFunc: f,
|
||||||
|
// Make configurable
|
||||||
ErrLimit: 100,
|
ErrLimit: 100,
|
||||||
SpecStr: spec,
|
SpecStr: spec,
|
||||||
|
// we only store the pointer, so it won't use too many space
|
||||||
|
Errlist: make([]*taskerr, 100, 100),
|
||||||
}
|
}
|
||||||
task.SetCron(spec)
|
task.SetCron(spec)
|
||||||
return task
|
return task
|
||||||
@ -144,9 +150,9 @@ func (t *Task) GetStatus() string {
|
|||||||
func (t *Task) Run() error {
|
func (t *Task) Run() error {
|
||||||
err := t.DoFunc()
|
err := t.DoFunc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if t.ErrLimit > 0 && t.ErrLimit > len(t.Errlist) {
|
index := t.errCnt % t.ErrLimit
|
||||||
t.Errlist = append(t.Errlist, &taskerr{t: t.Next, errinfo: err.Error()})
|
t.Errlist[index] = &taskerr{t: t.Next, errinfo: err.Error()}
|
||||||
}
|
t.errCnt++
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -15,10 +15,13 @@
|
|||||||
package toolbox
|
package toolbox
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestParse(t *testing.T) {
|
func TestParse(t *testing.T) {
|
||||||
@ -53,6 +56,25 @@ func TestSpec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTask_Run(t *testing.T) {
|
||||||
|
cnt := -1
|
||||||
|
task := func() error {
|
||||||
|
cnt ++
|
||||||
|
fmt.Printf("Hello, world! %d \n", cnt)
|
||||||
|
return errors.New(fmt.Sprintf("Hello, world! %d", cnt))
|
||||||
|
}
|
||||||
|
tk := NewTask("taska", "0/30 * * * * *", task)
|
||||||
|
for i := 0; i < 200 ; i ++ {
|
||||||
|
e := tk.Run()
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
l := tk.Errlist
|
||||||
|
assert.Equal(t, 100, len(l))
|
||||||
|
assert.Equal(t, "Hello, world! 100", l[0].errinfo)
|
||||||
|
assert.Equal(t, "Hello, world! 101", l[1].errinfo)
|
||||||
|
}
|
||||||
|
|
||||||
func wait(wg *sync.WaitGroup) chan bool {
|
func wait(wg *sync.WaitGroup) chan bool {
|
||||||
ch := make(chan bool)
|
ch := make(chan bool)
|
||||||
go func() {
|
go func() {
|
||||||
|
Loading…
Reference in New Issue
Block a user