master
兔子 2 years ago
parent 09f125c0fe
commit 5a4a27612d

@ -5,29 +5,31 @@ import (
"b612.me/stardb" "b612.me/stardb"
"b612.me/startimer" "b612.me/startimer"
"errors" "errors"
"fmt"
"sync" "sync"
"time" "time"
) )
type Remind struct { type Remind struct {
db *stardb.StarDB db *stardb.StarDB
tasks map[int]Task tasks map[int64]Task
mu *sync.RWMutex mu *sync.RWMutex
callback func(remind Task) callback func(remind Task)
} }
type Task struct { type Task struct {
ID int `db:"id"` ID int64 `db:"id"`
Origin string `db:"text"` Origin string `db:"text"`
timer *startimer.StarTimer timer *startimer.StarTimer
TmrInfo string `db:"code"` TmrInfo string `db:"code"`
Key string `db:"key"` Key string `db:"key"`
Msg []byte `db:"msg"` Msg []byte `db:"msg"`
Limit int64 `db:"limits"`
} }
func getCreateSql() []string { func getCreateSql() []string {
return []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)", "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, db: db,
mu: new(sync.RWMutex), mu: new(sync.RWMutex),
callback: callback, callback: callback,
tasks: make(map[int]Task), tasks: make(map[int64]Task),
} }
var res []Task var res []Task
data, err := db.Query("select * from remind") 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 return nil, err
} }
if len(res) != 0 { if len(res) != 0 {
rem.tasks = make(map[int]Task, len(res)) rem.tasks = make(map[int64]Task, len(res))
for _, task := range res { for _, task := range res {
tmr := startimer.NewTimer(time.Now()) tmr := startimer.NewTimer(time.Now())
err = tmr.ImportRepeats(task.TmrInfo) err = tmr.ImportRepeats(task.TmrInfo)
if err != nil { if err != nil {
return nil, err return nil, err
} }
tmr.SetRunCountLimit(int(task.Limit))
task.timer = &tmr task.timer = &tmr
task.timer.AddTask(func() { task.timer.AddTask(func() {
rem.callbackFn(task) rem.callbackFn(task)
}) })
err = task.timer.Run() err = task.timer.Run()
if err != nil { if err != nil {
return nil, err db.Exec(`delete from remind where id=?`, task.ID)
continue
} }
rem.tasks[task.ID] = task rem.tasks[task.ID] = task
fmt.Printf("%+v\n", task)
} }
} }
return &rem, nil return &rem, nil
@ -125,7 +130,7 @@ func (r *Remind) AddTask(taskStr, key string, msg []byte) (Task, error) {
if err != nil { if err != nil {
return Task{}, err return Task{}, err
} }
rmt.ID = int(id) rmt.ID = int64(id)
r.mu.Lock() r.mu.Lock()
r.tasks[rmt.ID] = rmt r.tasks[rmt.ID] = rmt
r.mu.Unlock() r.mu.Unlock()
@ -138,7 +143,7 @@ func (r *Remind) AddTask(taskStr, key string, msg []byte) (Task, error) {
return rmt, nil return rmt, nil
} }
func (r *Remind) DeleteTask(id int) error { func (r *Remind) DeleteTask(id int64) error {
r.mu.RLock() r.mu.RLock()
data, ok := r.tasks[id] data, ok := r.tasks[id]
r.mu.RUnlock() r.mu.RUnlock()
@ -181,7 +186,7 @@ func (r *Remind) GetTasksByKey(key string) []Task {
return res return res
} }
func (r *Remind) GetTaskByID(id int) Task { func (r *Remind) GetTaskByID(id int64) Task {
r.mu.RLock() r.mu.RLock()
defer r.mu.RUnlock() defer r.mu.RUnlock()
return r.tasks[id] return r.tasks[id]

@ -17,6 +17,22 @@ func exists(path string) bool {
return err == nil 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) { func TestRemind(t *testing.T) {
var db stardb.StarDB var db stardb.StarDB
err := db.Open("sqlite", "./remind.db") err := db.Open("sqlite", "./remind.db")

Loading…
Cancel
Save