master
兔子 2 years ago
parent d9af4978c6
commit 66828b2c23

@ -3,6 +3,6 @@ module b612.me/sdk/candy
go 1.20 go 1.20
require ( require (
b612.me/stardb v1.1.2 b612.me/stardb v1.1.3
b612.me/startimer v0.0.0-20230501080256-b0fd8947f0dc b612.me/startimer v0.0.0-20230501080256-b0fd8947f0dc
) )

@ -1,4 +1,4 @@
b612.me/stardb v1.1.2 h1:xr5Ovqd5kVSnV1W7eGJJwy+CyuqSwMmsFnPq/YxIWqg= b612.me/stardb v1.1.3 h1:cx16v64FmfCh+cCF6GDDiVUF+ZISpBxBH3t2GkBkeJY=
b612.me/stardb v1.1.2/go.mod h1:qtGEu+joEQxFESl3tse5xqiD767f6sAmHD284+Xoy48= b612.me/stardb v1.1.3/go.mod h1:qtGEu+joEQxFESl3tse5xqiD767f6sAmHD284+Xoy48=
b612.me/startimer v0.0.0-20230501080256-b0fd8947f0dc h1:WO8jZiLVssi3myTWX5Oo/+/i9vk+Alo4ymJhHi4TXp0= b612.me/startimer v0.0.0-20230501080256-b0fd8947f0dc h1:WO8jZiLVssi3myTWX5Oo/+/i9vk+Alo4ymJhHi4TXp0=
b612.me/startimer v0.0.0-20230501080256-b0fd8947f0dc/go.mod h1:8fw+OU7SnxzuLkNCYSbRKmvs4WtHs6SbCHuTf5F6s+U= b612.me/startimer v0.0.0-20230501080256-b0fd8947f0dc/go.mod h1:8fw+OU7SnxzuLkNCYSbRKmvs4WtHs6SbCHuTf5F6s+U=

@ -1,29 +1,149 @@
package remind package remind
import ( import (
"b612.me/sdk/candy/when"
"b612.me/stardb" "b612.me/stardb"
"b612.me/startimer" "b612.me/startimer"
"errors"
"sync" "sync"
"time"
) )
type Remind struct { type Remind struct {
db stardb.StarDB db *stardb.StarDB
tasks map[string]RemindTask tasks map[int]RemindTask
mu sync.Mutex mu sync.RWMutex
callBack func(remind RemindTask) callback func(remind RemindTask)
} }
type RemindTask struct { type RemindTask struct {
ID int `db:"id"` ID int `db:"id"`
Origin string `db:"text"` Origin string `db:"text"`
timer startimer.StarTimer timer *startimer.StarTimer
Key string `db:"key"` TmrInfo string `db:"code"`
Msg []byte `db:"msg"` Key string `db:"key"`
Msg []byte `db:"msg"`
} }
func getCreateSql() []string { func getCreateSql() []string {
return []string{ return []string{
"CREATE TABLE IF NOT EXISTS remind(id INTEGER PRIMARY KEY AUTOINCREMENT,key VARCHAR(64),text TEXT,msg BLOB)", "CREATE TABLE IF NOT EXISTS remind(id INTEGER PRIMARY KEY AUTOINCREMENT,key VARCHAR(64),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)",
} }
} }
func NewRemind(db *stardb.StarDB, callback func(task RemindTask)) (*Remind, error) {
if db == nil || db.Db == nil {
return nil, errors.New("Invalid hanlder of database")
}
if err := db.Ping(); err != nil {
return nil, err
}
for _, sql := range getCreateSql() {
_, err := db.Exec(sql)
if err != nil {
return nil, err
}
}
return innerLoadDB(db, callback)
}
func innerLoadDB(db *stardb.StarDB, callback func(task RemindTask)) (*Remind, error) {
var rem = Remind{
db: db,
mu: sync.RWMutex{},
callback: callback,
}
var res []RemindTask
data, err := db.Query("select * from remind")
if err != nil {
return nil, err
}
err = data.Orm(&res)
if err != nil {
return nil, err
}
if len(res) != 0 {
rem.tasks = make(map[int]RemindTask, len(res))
for _, task := range res {
tmr := startimer.NewTimer(time.Now())
err = tmr.ImportRepeats(task.TmrInfo)
if err != nil {
return nil, err
}
task.timer = &tmr
task.timer.AddTask(func() {
rem.callbackFn(task)
})
err = task.timer.Run()
if err != nil {
return nil, err
}
rem.tasks[task.ID] = task
}
}
return &rem, nil
}
func (t RemindTask) GetTimer() startimer.StarTimer {
return *t.timer
}
func (r *Remind) callbackFn(task RemindTask) {
r.callback(task)
}
func (r *Remind) AddTask(taskStr, key string, msg []byte) (RemindTask, error) {
tmr, err := when.WhenWithPeriod(taskStr)
if err != nil {
return RemindTask{}, err
}
exp, err := tmr.ExportRepeats()
if err != nil {
return RemindTask{}, err
}
var rmt = RemindTask{
Origin: taskStr,
TmrInfo: exp,
Key: key,
Msg: msg,
timer: &tmr,
}
rmt.timer.AddTask(func() {
r.callbackFn(rmt)
})
res, err := r.db.Insert(rmt, "remind", "id")
if err != nil {
return RemindTask{}, err
}
id, err := res.LastInsertId()
if err != nil {
return RemindTask{}, err
}
rmt.ID = int(id)
r.mu.Lock()
r.tasks[rmt.ID] = rmt
r.mu.Unlock()
rmt.timer.Run()
return rmt, nil
}
func (r *Remind) DeleteTask(id int) error {
r.mu.RLock()
data, ok := r.tasks[id]
r.mu.RUnlock()
if !ok {
return errors.New("no such id")
}
r.mu.Lock()
defer r.mu.Unlock()
err := data.timer.Stop()
if err != nil {
return err
}
_, err = r.db.Exec("delete from remind where id=?", id)
if err != nil {
return err
}
delete(r.tasks, id)
return nil
}

@ -3,12 +3,16 @@ package when
import ( import (
"fmt" "fmt"
"testing" "testing"
"time"
) )
func TestParse(t *testing.T) { func TestParse(t *testing.T) {
for _, code := range []string{"一个小时后告诉我事情", "三个小时后爱我", "每两个小时提醒我吃饭", "每五个月零二十五天三小时25分15秒告诉我时间", "5月23日上午3点24分12秒打我", "周四上午11点提醒我"} { for _, code := range []string{"一个小时后告诉我事情", "三个小时后爱我", "每两个小时提醒我吃饭",
"每五个月零二十五天三小时25分15秒告诉我时间", "5月23日上午3点24分12秒打我", "周四上午11点提醒我",
"每周一到周五上午8点提醒我吃饭"} {
a, _ := WhenWithPeriod(code) a, _ := WhenWithPeriod(code)
//fmt.Println(a.Repeats()[0]) //fmt.Println(a.Repeats()[0])
fmt.Println(a.NextTimer(), a.RunCountLimit(), code) fmt.Println(a.NextTimer(), a.RunCountLimit(), code)
fmt.Println(a.NextTimerAfterDate(time.Now().Add(time.Hour * 72)))
} }
} }

Loading…
Cancel
Save