update jobs

This commit is contained in:
shoopea 2019-12-15 15:37:58 +08:00
parent a0afe80575
commit 3023eea74e
4 changed files with 67 additions and 2 deletions

15
bot.go
View File

@ -649,6 +649,19 @@ func botGStock(m *tb.Message) {
return return
} }
clt, err := getLockedIdleClient()
if err != nil {
c := TGCommand{
Type: commandReplyMsg,
Text: "Busy, please retry later.",
FromMsgID64: int64(m.ID),
FromChatID64: m.Chat.ID,
}
TGCmdQueue <- c
}
userID64 := clt.TGUserID64
clt.Mux.Unlock()
p := JobPayloadGStock{ p := JobPayloadGStock{
MsgID64: int64(m.ID), MsgID64: int64(m.ID),
ChatID64: m.Chat.ID, ChatID64: m.Chat.ID,
@ -656,7 +669,7 @@ func botGStock(m *tb.Message) {
} }
b, _ := json.Marshal(p) b, _ := json.Marshal(p)
t := time.Now().UTC().Add(1 * time.Second) t := time.Now().UTC().Add(1 * time.Second)
_, err := createJob(objSubTypeJobGStock, objJobPriority, cfg.Bot.Admin, 0, t, b) _, err := createJob(objSubTypeJobGStock, objJobPriority, userID64, 0, t, b)
if err != nil { if err != nil {
c := TGCommand{ c := TGCommand{

View File

@ -7,6 +7,57 @@ import (
"time" "time"
) )
func setClientBusy(userID64 int64, from time.Time, duration time.Duration) error {
if from.UTC().Add(duration).After(time.Now().UTC()) {
if clt, ok := getLockedClient(m.Chat.ID, false); ok {
clt.CWIdle = false
clt.CWBusyUntil = from.UTC().Add(duration)
clt.Mux.Unlock()
return nil
} else {
return errors.New("Client not found.")
}
}
return nil
}
func setClientIdle(userID64 int64, from time.Time) error {
if clt, ok := getLockedClient(m.Chat.ID, false); ok {
if from.UTC().After(clt.CWLastUpdate.UTC()) {
clt.CWBusyUntil = from
clt.CWIdle = true
clt.CWLastUpdate = from
}
clt.Mux.Unlock()
return nil
} else {
return errors.New("Client not found.")
}
}
func getLockedIdleClient() (*ChirpClient, error) {
muxClients.RLock()
ids := make([]int64, 0)
for id, c := range clients {
if c.CWIdle {
ids := append(ids, c.TGUserID64)
}
}
muxClients.RUnlock()
if len(ids) == 0 {
return nil, errors.New("No idle client.")
}
RndMux.Lock()
id := RndSrc.Intn(len(ids))
RndMux.Unlock()
clients[ids[id]].Mux.Lock()
return clients[ids[id]], nil
}
func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) { func getLockedClient(id int64, createMissing bool) (*ChirpClient, bool) {
muxClients.RLock() muxClients.RLock()
if c, ok := clients[id]; ok { if c, ok := clients[id]; ok {

1
def.go
View File

@ -40,6 +40,7 @@ type ChirpClient struct {
CWState string `json:"state"` CWState string `json:"state"`
CWBusyUntil time.Time `json:"busy_until"` CWBusyUntil time.Time `json:"busy_until"`
CWLastUpdate time.Time `json:"last_update"` CWLastUpdate time.Time `json:"last_update"`
CWIdle bool `json:"idle"`
Mux sync.Mutex Mux sync.Mutex
} }

2
job.go
View File

@ -135,7 +135,7 @@ func setJobStart(jobId int64) error {
} }
func rescheduleJob(jobID64 int64, trigger int64, schedule time.Time) error { func rescheduleJob(jobID64 int64, trigger int64, schedule time.Time) error {
stmt, err := db.Prepare(`UPDATE obj_job j SET j.is_done = 0, j.in_work = 0, j.schedule = ?, j.trigger_id = ? WHERE j.obj_id = ?;`) stmt, err := db.Prepare(`UPDATE obj_job j SET j.in_work = 0, j.schedule = ?, j.trigger_id = ? WHERE j.obj_id = ?;`)
logOnError(err, "rescheduleJob : prepare update obj_job") logOnError(err, "rescheduleJob : prepare update obj_job")
if err != nil { if err != nil {
return err return err