diff --git a/remind/remind.go b/remind/remind.go index f39a837..df0dd9a 100644 --- a/remind/remind.go +++ b/remind/remind.go @@ -5,29 +5,31 @@ import ( "b612.me/stardb" "b612.me/startimer" "errors" + "fmt" "sync" "time" ) type Remind struct { db *stardb.StarDB - tasks map[int]Task + tasks map[int64]Task mu *sync.RWMutex callback func(remind Task) } type Task struct { - ID int `db:"id"` + ID int64 `db:"id"` Origin string `db:"text"` timer *startimer.StarTimer TmrInfo string `db:"code"` Key string `db:"key"` Msg []byte `db:"msg"` + Limit int64 `db:"limits"` } func getCreateSql() []string { return []string{ - "CREATE TABLE IF NOT EXISTS remind(id INTEGER PRIMARY KEY AUTOINCREMENT,key VARCHAR(64),code TEXT,text TEXT,msg BLOB)", + "CREATE TABLE IF NOT EXISTS remind(id INTEGER PRIMARY KEY AUTOINCREMENT,key VARCHAR(64),limits INTEGER,code TEXT,text TEXT,msg BLOB)", "CREATE INDEX IF NOT EXISTS key_idx ON remind (key)", } } @@ -53,7 +55,7 @@ func innerLoadDB(db *stardb.StarDB, callback func(task Task)) (*Remind, error) { db: db, mu: new(sync.RWMutex), callback: callback, - tasks: make(map[int]Task), + tasks: make(map[int64]Task), } var res []Task data, err := db.Query("select * from remind") @@ -65,22 +67,25 @@ func innerLoadDB(db *stardb.StarDB, callback func(task Task)) (*Remind, error) { return nil, err } if len(res) != 0 { - rem.tasks = make(map[int]Task, len(res)) + rem.tasks = make(map[int64]Task, len(res)) for _, task := range res { tmr := startimer.NewTimer(time.Now()) err = tmr.ImportRepeats(task.TmrInfo) if err != nil { return nil, err } + tmr.SetRunCountLimit(int(task.Limit)) task.timer = &tmr task.timer.AddTask(func() { rem.callbackFn(task) }) err = task.timer.Run() if err != nil { - return nil, err + db.Exec(`delete from remind where id=?`, task.ID) + continue } rem.tasks[task.ID] = task + fmt.Printf("%+v\n", task) } } return &rem, nil @@ -125,7 +130,7 @@ func (r *Remind) AddTask(taskStr, key string, msg []byte) (Task, error) { if err != nil { return Task{}, err } - rmt.ID = int(id) + rmt.ID = int64(id) r.mu.Lock() r.tasks[rmt.ID] = rmt r.mu.Unlock() @@ -138,7 +143,7 @@ func (r *Remind) AddTask(taskStr, key string, msg []byte) (Task, error) { return rmt, nil } -func (r *Remind) DeleteTask(id int) error { +func (r *Remind) DeleteTask(id int64) error { r.mu.RLock() data, ok := r.tasks[id] r.mu.RUnlock() @@ -181,7 +186,7 @@ func (r *Remind) GetTasksByKey(key string) []Task { return res } -func (r *Remind) GetTaskByID(id int) Task { +func (r *Remind) GetTaskByID(id int64) Task { r.mu.RLock() defer r.mu.RUnlock() return r.tasks[id] diff --git a/remind/reminde_test.go b/remind/reminde_test.go index 1b80a0a..db01260 100644 --- a/remind/reminde_test.go +++ b/remind/reminde_test.go @@ -17,6 +17,22 @@ func exists(path string) bool { return err == nil } +func TestOtherDb(t *testing.T) { + var db stardb.StarDB + err := db.Open("sqlite", "./db.sqlite") + if err != nil { + t.Fatal(err) + } + r, err := NewRemind(&db, func(task Task) { + fmt.Println("hello world!", time.Now(), task.ID, task.Key, task.TmrInfo, task.Msg, task.Origin) + }) + + if err != nil { + t.Fatal(err) + } + fmt.Println(len(r.GetTasksByKey("705457681"))) +} + func TestRemind(t *testing.T) { var db stardb.StarDB err := db.Open("sqlite", "./remind.db")