stop/start schedule
This commit is contained in:
parent
be7c36013f
commit
dc4fd7182e
47
scheduler.go
47
scheduler.go
@ -21,6 +21,7 @@ type Task struct {
|
|||||||
funcParams map[string]([]interface{})
|
funcParams map[string]([]interface{})
|
||||||
lastAt time.Time
|
lastAt time.Time
|
||||||
nextAt time.Time
|
nextAt time.Time
|
||||||
|
running bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTask(id string, intervel uint64) *Task {
|
func NewTask(id string, intervel uint64) *Task {
|
||||||
@ -33,6 +34,7 @@ func NewTask(id string, intervel uint64) *Task {
|
|||||||
make(map[string]([]interface{})),
|
make(map[string]([]interface{})),
|
||||||
time.Unix(0, 0),
|
time.Unix(0, 0),
|
||||||
time.Unix(0, 0),
|
time.Unix(0, 0),
|
||||||
|
true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,6 +43,9 @@ func (t *Task) runnable() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Task) run() {
|
func (t *Task) run() {
|
||||||
|
if !t.running {
|
||||||
|
return
|
||||||
|
}
|
||||||
taskFunc := reflect.ValueOf(t.taskFunc[t.TaskFunc])
|
taskFunc := reflect.ValueOf(t.taskFunc[t.TaskFunc])
|
||||||
taskParams := t.funcParams[t.TaskFunc]
|
taskParams := t.funcParams[t.TaskFunc]
|
||||||
in := make([]reflect.Value, len(taskParams))
|
in := make([]reflect.Value, len(taskParams))
|
||||||
@ -223,3 +228,45 @@ func (s *Scheduler) UpdateInterval(id string, interval uint64) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Scheduler) StartSchedule(id string) error {
|
||||||
|
i := 0
|
||||||
|
var ex bool
|
||||||
|
for ; i < s.size; i++ {
|
||||||
|
if s.Tasks[i].id == id {
|
||||||
|
ex = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !ex {
|
||||||
|
return errors.New("Cannot find the task. ID : " + id)
|
||||||
|
}
|
||||||
|
if s.Tasks[i].running {
|
||||||
|
return errors.New("Already started task. ID : " + id)
|
||||||
|
}
|
||||||
|
s.Tasks[i].running = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Scheduler) StopSchedule(id string) error {
|
||||||
|
i := 0
|
||||||
|
var ex bool
|
||||||
|
for ; i < s.size; i++ {
|
||||||
|
if s.Tasks[i].id == id {
|
||||||
|
ex = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if s.Tasks[i].running {
|
||||||
|
s.Tasks[i].running = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !ex {
|
||||||
|
return errors.New("Cannot find the task. ID : " + id)
|
||||||
|
}
|
||||||
|
if !s.Tasks[i].running {
|
||||||
|
return errors.New("Already stopped task. ID : " + id)
|
||||||
|
}
|
||||||
|
s.Tasks[i].running = false
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
@ -39,4 +39,17 @@ func test(id string) {
|
|||||||
if id == "#######################" {
|
if id == "#######################" {
|
||||||
log.Println(id)
|
log.Println(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSchedulerStopStart(t *testing.T) {
|
||||||
|
s := Scheduler{}
|
||||||
|
s.Start()
|
||||||
|
s.NewSchedule("1111", 3, callback)
|
||||||
|
time.Sleep(time.Second * 10)
|
||||||
|
s.StopSchedule("1111")
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
s.StartSchedule("1111")
|
||||||
|
|
||||||
|
time.Sleep(time.Second * 10)
|
||||||
|
s.Stop()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user