master
兔子 2 years ago
parent 96419e47fa
commit 0aaaa38e03

@ -5,7 +5,6 @@ import (
"b612.me/stardb" "b612.me/stardb"
"b612.me/startimer" "b612.me/startimer"
"errors" "errors"
"fmt"
"sync" "sync"
"time" "time"
) )
@ -69,23 +68,24 @@ func innerLoadDB(db *stardb.StarDB, callback func(task Task)) (*Remind, error) {
if len(res) != 0 { if len(res) != 0 {
rem.tasks = make(map[int64]Task, len(res)) rem.tasks = make(map[int64]Task, len(res))
for _, task := range res { for _, task := range res {
itask := new(Task)
*itask = task
tmr := startimer.NewTimer(time.Now()) tmr := startimer.NewTimer(time.Now())
err = tmr.ImportRepeats(task.TmrInfo) err = tmr.ImportRepeats(itask.TmrInfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }
tmr.SetRunCountLimit(int(task.Limit)) tmr.SetRunCountLimit(int(itask.Limit))
task.timer = &tmr itask.timer = &tmr
task.timer.AddTask(func() { itask.timer.AddTask(func() {
rem.callbackFn(&task) rem.callbackFn(itask)
}) })
err = task.timer.Run() err = itask.timer.Run()
if err != nil { if err != nil {
db.Exec(`delete from remind where id=?`, task.ID) db.Exec(`delete from remind where id=?`, task.ID)
continue continue
} }
rem.tasks[task.ID] = task rem.tasks[itask.ID] = *itask
fmt.Printf("%v %+v\n", tmr.NextTimer(), task)
} }
} }
return &rem, nil return &rem, nil

@ -49,7 +49,7 @@ func TestRemind(t *testing.T) {
} }
fmt.Println("length of remind", len(r.ListTasks())) fmt.Println("length of remind", len(r.ListTasks()))
if len(r.ListTasks()) < 2 { if len(r.ListTasks()) < 2 {
tk, err := r.AddTask("每8秒提醒我吃饭", "干饭", []byte{1, 2, 3}) tk, err := r.AddTask("每10秒提醒我吃饭", "干饭", []byte{1, 2, 3})
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

Loading…
Cancel
Save