diff --git a/bot.go b/bot.go index 8a75300..17cede4 100644 --- a/bot.go +++ b/bot.go @@ -649,6 +649,19 @@ func botGStock(m *tb.Message) { 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{ MsgID64: int64(m.ID), ChatID64: m.Chat.ID, @@ -656,7 +669,7 @@ func botGStock(m *tb.Message) { } b, _ := json.Marshal(p) 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 { c := TGCommand{ diff --git a/client.go b/client.go index 6e05313..a56048b 100644 --- a/client.go +++ b/client.go @@ -7,6 +7,57 @@ import ( "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) { muxClients.RLock() if c, ok := clients[id]; ok { diff --git a/def.go b/def.go index 5c0fee7..900d1e4 100644 --- a/def.go +++ b/def.go @@ -40,6 +40,7 @@ type ChirpClient struct { CWState string `json:"state"` CWBusyUntil time.Time `json:"busy_until"` CWLastUpdate time.Time `json:"last_update"` + CWIdle bool `json:"idle"` Mux sync.Mutex } diff --git a/job.go b/job.go index 78706f3..9d0f32f 100644 --- a/job.go +++ b/job.go @@ -135,7 +135,7 @@ func setJobStart(jobId int64) 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") if err != nil { return err