update
parent
21c77899ce
commit
98305ad0d3
|
@ -24,28 +24,28 @@ var ReportReceiver Receiver = func(r *Report) {}
|
|||
|
||||
type Report struct {
|
||||
// Entry
|
||||
Next time.Time `json:"next"`
|
||||
Prev time.Time `json:"prev"`
|
||||
Entry cron.EntryID `json:"entry_id"`
|
||||
Next time.Time
|
||||
Prev time.Time
|
||||
Entry cron.EntryID
|
||||
|
||||
// Task
|
||||
Name string `json:"name"`
|
||||
ID string `json:"id"`
|
||||
Description string `json:"description"`
|
||||
Group string `json:"grp"`
|
||||
ID string
|
||||
Description string
|
||||
Group string
|
||||
|
||||
// _task
|
||||
Spec string `json:"spec"`
|
||||
Once bool `json:"once"`
|
||||
Args []interface{} `json:"args"`
|
||||
Carry interface{} `json:"carry"`
|
||||
Name string
|
||||
Spec string
|
||||
Once bool
|
||||
Args []interface{}
|
||||
Carry interface{}
|
||||
|
||||
// Report
|
||||
Error error `json:"error,omitempty"`
|
||||
Error error
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
Scheduled []*Report `json:"scheduled"`
|
||||
Scheduled []*Report
|
||||
}
|
||||
|
||||
func GetProfile() *Profile {
|
||||
|
@ -60,10 +60,10 @@ func GetProfile() *Profile {
|
|||
Next: entries[i].Next,
|
||||
Prev: entries[i].Prev,
|
||||
Entry: entries[i].ID,
|
||||
Name: t.Task.Name,
|
||||
ID: t.Task.ID,
|
||||
Description: t.Task.Description,
|
||||
Group: t.Task.Group,
|
||||
Name: t.name,
|
||||
Spec: t.spec,
|
||||
Once: t.once,
|
||||
Args: t.args,
|
||||
|
|
|
@ -18,12 +18,12 @@ func AddTask(t *Task) {
|
|||
stlok.Lock()
|
||||
defer stlok.Unlock()
|
||||
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()
|
||||
}
|
||||
_, ok := tasks[t.ID]
|
||||
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 {
|
||||
tasks[t.ID] = t
|
||||
}
|
||||
|
@ -39,18 +39,16 @@ func GetTask(id string) *Task {
|
|||
|
||||
func NewTask() *Task {
|
||||
return &Task{
|
||||
Name: util.Caller(2),
|
||||
ID: xid.New().String(),
|
||||
ID: xid.New().String(),
|
||||
}
|
||||
}
|
||||
|
||||
func RunFunc(spec string, cmd func()) error {
|
||||
th := &Task{
|
||||
Name: util.Caller(2),
|
||||
ID: xid.New().String(),
|
||||
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)
|
||||
t.id = id
|
||||
return err
|
||||
|
@ -58,24 +56,24 @@ func RunFunc(spec string, cmd func()) error {
|
|||
|
||||
func AddAndRunTask(spec string, t *Task, args ...interface{}) error {
|
||||
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)
|
||||
_t.id = id
|
||||
return err
|
||||
}
|
||||
|
||||
func RunTask(spec, id string, args ...interface{}) error {
|
||||
func RunTask(spec, id, name string, args ...interface{}) error {
|
||||
t := GetTask(id)
|
||||
if t == nil {
|
||||
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)
|
||||
_t.id = _id
|
||||
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)
|
||||
if next < 0 {
|
||||
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")
|
||||
}
|
||||
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)
|
||||
_t.id = _id
|
||||
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)
|
||||
if t == nil {
|
||||
return fmt.Errorf("no task with id")
|
||||
}
|
||||
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)
|
||||
_t.id = _id
|
||||
return err
|
||||
|
|
38
task/task.go
38
task/task.go
|
@ -24,7 +24,7 @@ func Init() {
|
|||
cron.WithChain(func(j cron.Job) cron.Job {
|
||||
t, ok := j.(*_task)
|
||||
if ok {
|
||||
l.Debug().Str("taskname", t.Task.Name).Msg("")
|
||||
l.Debug().Str("taskid", t.Task.ID).Msg("")
|
||||
}
|
||||
return j
|
||||
}),
|
||||
|
@ -32,7 +32,6 @@ func Init() {
|
|||
}
|
||||
|
||||
type Task struct {
|
||||
Name string
|
||||
ID string
|
||||
Description string
|
||||
Group string
|
||||
|
@ -46,6 +45,7 @@ type Task struct {
|
|||
|
||||
type _task struct {
|
||||
Task *Task
|
||||
name string
|
||||
id cron.EntryID
|
||||
args []interface{}
|
||||
spec string
|
||||
|
@ -65,21 +65,27 @@ func (t *_task) Run() {
|
|||
c.Remove(t.id)
|
||||
}
|
||||
}()
|
||||
if err := t.Task.Func(t.carry, t.args...); err != nil {
|
||||
l.Error().Err(err).Str("taskname", t.Task.Name).Msg("")
|
||||
Reporter <- &Report{
|
||||
Entry: t.id,
|
||||
Name: t.Task.Name,
|
||||
ID: t.Task.ID,
|
||||
Description: t.Task.Description,
|
||||
Group: t.Task.Group,
|
||||
Spec: t.spec,
|
||||
Once: t.once,
|
||||
Args: t.args,
|
||||
Carry: t.carry,
|
||||
Error: err,
|
||||
}
|
||||
l.Info().Str("task_id", t.Task.ID).
|
||||
Str("task name", t.name).
|
||||
Msg("task started.")
|
||||
err := t.Task.Func(t.carry, t.args...)
|
||||
if err != nil {
|
||||
l.Error().Err(err).Str("task_id", t.Task.ID).
|
||||
Str("task name", t.name).Msg("task error")
|
||||
}
|
||||
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() {
|
||||
|
|
|
@ -29,7 +29,6 @@ func TestOverall(t *testing.T) {
|
|||
RunFunc("@every 2s", func() { l.Debug().Msg("@every 2s") })
|
||||
|
||||
test1 := &Task{
|
||||
Name: "test1",
|
||||
Description: "test1",
|
||||
ID: "id1",
|
||||
Group: "grp1",
|
||||
|
@ -42,7 +41,6 @@ func TestOverall(t *testing.T) {
|
|||
AddTask(test1)
|
||||
|
||||
test2 := &Task{
|
||||
Name: "test2",
|
||||
Description: "test2",
|
||||
ID: "id2",
|
||||
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!!!")
|
||||
|
||||
test3 := &Task{
|
||||
Name: "test3",
|
||||
Description: "test3",
|
||||
ID: "id3",
|
||||
Func: func(c *interface{}, args ...interface{}) error {
|
||||
|
@ -94,7 +91,6 @@ func TestReporter(t *testing.T) {
|
|||
})
|
||||
Init()
|
||||
AddAndRunTask("@every 2s", &Task{
|
||||
Name: "test",
|
||||
Func: func(c *interface{}, args ...interface{}) error {
|
||||
carry, ok := (*c).(int)
|
||||
if !ok {
|
||||
|
|
Loading…
Reference in New Issue