master
Evan Chen 2021-12-19 14:02:39 +08:00
parent 21c77899ce
commit 98305ad0d3
4 changed files with 47 additions and 47 deletions

View File

@ -24,28 +24,28 @@ var ReportReceiver Receiver = func(r *Report) {}
type Report struct { type Report struct {
// Entry // Entry
Next time.Time `json:"next"` Next time.Time
Prev time.Time `json:"prev"` Prev time.Time
Entry cron.EntryID `json:"entry_id"` Entry cron.EntryID
// Task // Task
Name string `json:"name"` ID string
ID string `json:"id"` Description string
Description string `json:"description"` Group string
Group string `json:"grp"`
// _task // _task
Spec string `json:"spec"` Name string
Once bool `json:"once"` Spec string
Args []interface{} `json:"args"` Once bool
Carry interface{} `json:"carry"` Args []interface{}
Carry interface{}
// Report // Report
Error error `json:"error,omitempty"` Error error
} }
type Profile struct { type Profile struct {
Scheduled []*Report `json:"scheduled"` Scheduled []*Report
} }
func GetProfile() *Profile { func GetProfile() *Profile {
@ -60,10 +60,10 @@ func GetProfile() *Profile {
Next: entries[i].Next, Next: entries[i].Next,
Prev: entries[i].Prev, Prev: entries[i].Prev,
Entry: entries[i].ID, Entry: entries[i].ID,
Name: t.Task.Name,
ID: t.Task.ID, ID: t.Task.ID,
Description: t.Task.Description, Description: t.Task.Description,
Group: t.Task.Group, Group: t.Task.Group,
Name: t.name,
Spec: t.spec, Spec: t.spec,
Once: t.once, Once: t.once,
Args: t.args, Args: t.args,

View File

@ -18,12 +18,12 @@ func AddTask(t *Task) {
stlok.Lock() stlok.Lock()
defer stlok.Unlock() defer stlok.Unlock()
if t.ID == "" { if t.ID == "" {
l.Warn().Str("taskname", t.Name).Msg("no ID, using random id") l.Warn().Str("task_id", t.ID).Msg("no ID, using random id")
t.ID = xid.New().String() t.ID = xid.New().String()
} }
_, ok := tasks[t.ID] _, ok := tasks[t.ID]
if ok { if ok {
l.Warn().Str("taskname", t.Name).Str("taskid", t.ID).Msg("task is already in store, skipping") l.Warn().Str("task_id", t.ID).Msg("task is already in store, skipping")
} else { } else {
tasks[t.ID] = t tasks[t.ID] = t
} }
@ -39,18 +39,16 @@ func GetTask(id string) *Task {
func NewTask() *Task { func NewTask() *Task {
return &Task{ return &Task{
Name: util.Caller(2), ID: xid.New().String(),
ID: xid.New().String(),
} }
} }
func RunFunc(spec string, cmd func()) error { func RunFunc(spec string, cmd func()) error {
th := &Task{ th := &Task{
Name: util.Caller(2),
ID: xid.New().String(), ID: xid.New().String(),
Func: func(carry *interface{}, i ...interface{}) error { cmd(); return nil }, Func: func(carry *interface{}, i ...interface{}) error { cmd(); return nil },
} }
t := &_task{Task: th, spec: spec} t := &_task{Task: th, spec: spec, name: util.Caller(2)}
id, err := c.AddJob(spec, t) id, err := c.AddJob(spec, t)
t.id = id t.id = id
return err return err
@ -58,24 +56,24 @@ func RunFunc(spec string, cmd func()) error {
func AddAndRunTask(spec string, t *Task, args ...interface{}) error { func AddAndRunTask(spec string, t *Task, args ...interface{}) error {
AddTask(t) AddTask(t)
_t := &_task{Task: t, args: args, spec: spec} _t := &_task{Task: t, args: args, spec: spec, name: t.ID}
id, err := c.AddJob(spec, _t) id, err := c.AddJob(spec, _t)
_t.id = id _t.id = id
return err return err
} }
func RunTask(spec, id string, args ...interface{}) error { func RunTask(spec, id, name string, args ...interface{}) error {
t := GetTask(id) t := GetTask(id)
if t == nil { if t == nil {
return fmt.Errorf("no task with id") return fmt.Errorf("no task with id")
} }
_t := &_task{Task: t, args: args, spec: spec} _t := &_task{Task: t, args: args, spec: spec, name: name}
_id, err := c.AddJob(spec, _t) _id, err := c.AddJob(spec, _t)
_t.id = _id _t.id = _id
return err return err
} }
func RunTaskAt(at time.Time, id string, args ...interface{}) error { func RunTaskAt(at time.Time, id, name string, args ...interface{}) error {
next := time.Until(at).Round(time.Second) next := time.Until(at).Round(time.Second)
if next < 0 { if next < 0 {
return fmt.Errorf("%v is passed by %s", at, next) return fmt.Errorf("%v is passed by %s", at, next)
@ -85,19 +83,19 @@ func RunTaskAt(at time.Time, id string, args ...interface{}) error {
return fmt.Errorf("no task with id") return fmt.Errorf("no task with id")
} }
spec := "@every " + next.String() spec := "@every " + next.String()
_t := &_task{Task: t, args: args, spec: spec, once: true} _t := &_task{Task: t, args: args, spec: spec, once: true, name: name}
_id, err := c.AddJob(spec, _t) _id, err := c.AddJob(spec, _t)
_t.id = _id _t.id = _id
return err return err
} }
func RunTaskAfter(d time.Duration, id string, args ...interface{}) error { func RunTaskAfter(d time.Duration, id, name string, args ...interface{}) error {
t := GetTask(id) t := GetTask(id)
if t == nil { if t == nil {
return fmt.Errorf("no task with id") return fmt.Errorf("no task with id")
} }
spec := "@every " + d.String() spec := "@every " + d.String()
_t := &_task{Task: t, args: args, spec: spec, once: true} _t := &_task{Task: t, args: args, spec: spec, once: true, name: name}
_id, err := c.AddJob(spec, _t) _id, err := c.AddJob(spec, _t)
_t.id = _id _t.id = _id
return err return err

View File

@ -24,7 +24,7 @@ func Init() {
cron.WithChain(func(j cron.Job) cron.Job { cron.WithChain(func(j cron.Job) cron.Job {
t, ok := j.(*_task) t, ok := j.(*_task)
if ok { if ok {
l.Debug().Str("taskname", t.Task.Name).Msg("") l.Debug().Str("taskid", t.Task.ID).Msg("")
} }
return j return j
}), }),
@ -32,7 +32,6 @@ func Init() {
} }
type Task struct { type Task struct {
Name string
ID string ID string
Description string Description string
Group string Group string
@ -46,6 +45,7 @@ type Task struct {
type _task struct { type _task struct {
Task *Task Task *Task
name string
id cron.EntryID id cron.EntryID
args []interface{} args []interface{}
spec string spec string
@ -65,21 +65,27 @@ func (t *_task) Run() {
c.Remove(t.id) c.Remove(t.id)
} }
}() }()
if err := t.Task.Func(t.carry, t.args...); err != nil { l.Info().Str("task_id", t.Task.ID).
l.Error().Err(err).Str("taskname", t.Task.Name).Msg("") Str("task name", t.name).
Reporter <- &Report{ Msg("task started.")
Entry: t.id, err := t.Task.Func(t.carry, t.args...)
Name: t.Task.Name, if err != nil {
ID: t.Task.ID, l.Error().Err(err).Str("task_id", t.Task.ID).
Description: t.Task.Description, Str("task name", t.name).Msg("task error")
Group: t.Task.Group,
Spec: t.spec,
Once: t.once,
Args: t.args,
Carry: t.carry,
Error: err,
}
} }
Reporter <- &Report{
Entry: t.id,
ID: t.Task.ID,
Description: t.Task.Description,
Group: t.Task.Group,
Name: t.name,
Spec: t.spec,
Once: t.once,
Args: t.args,
Carry: t.carry,
Error: err,
}
} }
func Start() { func Start() {

View File

@ -29,7 +29,6 @@ func TestOverall(t *testing.T) {
RunFunc("@every 2s", func() { l.Debug().Msg("@every 2s") }) RunFunc("@every 2s", func() { l.Debug().Msg("@every 2s") })
test1 := &Task{ test1 := &Task{
Name: "test1",
Description: "test1", Description: "test1",
ID: "id1", ID: "id1",
Group: "grp1", Group: "grp1",
@ -42,7 +41,6 @@ func TestOverall(t *testing.T) {
AddTask(test1) AddTask(test1)
test2 := &Task{ test2 := &Task{
Name: "test2",
Description: "test2", Description: "test2",
ID: "id2", ID: "id2",
Func: func(c *interface{}, args ...interface{}) error { Func: func(c *interface{}, args ...interface{}) error {
@ -63,7 +61,6 @@ func TestOverall(t *testing.T) {
RunTaskAt(time.Now().Add(time.Second*15), "id2", "ONCE!Again!!!") RunTaskAt(time.Now().Add(time.Second*15), "id2", "ONCE!Again!!!")
test3 := &Task{ test3 := &Task{
Name: "test3",
Description: "test3", Description: "test3",
ID: "id3", ID: "id3",
Func: func(c *interface{}, args ...interface{}) error { Func: func(c *interface{}, args ...interface{}) error {
@ -94,7 +91,6 @@ func TestReporter(t *testing.T) {
}) })
Init() Init()
AddAndRunTask("@every 2s", &Task{ AddAndRunTask("@every 2s", &Task{
Name: "test",
Func: func(c *interface{}, args ...interface{}) error { Func: func(c *interface{}, args ...interface{}) error {
carry, ok := (*c).(int) carry, ok := (*c).(int)
if !ok { if !ok {