testtest
This commit is contained in:
parent
ef2310d26d
commit
55b1acda1d
6
bot.go
6
bot.go
@ -76,9 +76,9 @@ func botMsgRescan(m *tb.Message) (string, error) {
|
|||||||
r := regexp.MustCompile("^[0-9]+$")
|
r := regexp.MustCompile("^[0-9]+$")
|
||||||
if r.MatchString(m.Payload) {
|
if r.MatchString(m.Payload) {
|
||||||
p := JobPayloadRescanMsg{}
|
p := JobPayloadRescanMsg{}
|
||||||
fmt.Sprintf(p.Query, "SELECT o.id from obj o where o.id = %d and o.obj_type_id = %d and o.obj_sub_type_id = %d;", m.Payload, objTypeMessage, objSubTypeMessageUnknown)
|
fmt.Sprintf(p.Query, "SELECT o.id from obj o where o.id = %s and o.obj_type_id = %d and o.obj_sub_type_id = %d;", m.Payload, objTypeMessage, objSubTypeMessageUnknown)
|
||||||
b, _ := json.Marshal(p)
|
b, _ := json.Marshal(p)
|
||||||
err := createJob(objSubTypeJobRescanMsg, 2, time.Now(), b)
|
err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanMsg, time.Now(), b)
|
||||||
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
|
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "Error scheduling the rescan for msg #" + m.Payload, nil
|
return "Error scheduling the rescan for msg #" + m.Payload, nil
|
||||||
@ -92,7 +92,7 @@ func botMsgRescan(m *tb.Message) (string, error) {
|
|||||||
p := JobPayloadRescanMsg{}
|
p := JobPayloadRescanMsg{}
|
||||||
fmt.Sprintf(p.Query, "SELECT o.id from obj o where o.obj_type_id = %d and o.obj_sub_type_id = %d;", objTypeMessage, objSubTypeMessageUnknown)
|
fmt.Sprintf(p.Query, "SELECT o.id from obj o where o.obj_type_id = %d and o.obj_sub_type_id = %d;", objTypeMessage, objSubTypeMessageUnknown)
|
||||||
b, _ := json.Marshal(p)
|
b, _ := json.Marshal(p)
|
||||||
err := createJob(objSubTypeJobRescanMsg, 3, time.Now(), b)
|
err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanAllMsg, time.Now(), b)
|
||||||
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
|
logOnError(err, "botMsgRescan : createJob(objSubTypeJobRescanMsg)")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "Error scheduling the rescan for all msg", nil
|
return "Error scheduling the rescan for all msg", nil
|
||||||
|
12
def.go
12
def.go
@ -45,6 +45,7 @@ type BotMsg struct {
|
|||||||
type Job struct {
|
type Job struct {
|
||||||
ID64 int64
|
ID64 int64
|
||||||
JobTypeID int32
|
JobTypeID int32
|
||||||
|
Status int32
|
||||||
Payload []byte
|
Payload []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,6 +73,10 @@ type JobPayloadRescanMsg struct {
|
|||||||
Query string `json:"query"`
|
Query string `json:"query"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type JobPayloadSetDone struct {
|
||||||
|
JobID64 int64 `json:"job_id"`
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
objTypeUser = 1
|
objTypeUser = 1
|
||||||
objTypeGuild = 2
|
objTypeGuild = 2
|
||||||
@ -106,7 +111,7 @@ const (
|
|||||||
objSubTypeJobWithdrawal = 604
|
objSubTypeJobWithdrawal = 604
|
||||||
objSubTypeJobGStock = 605
|
objSubTypeJobGStock = 605
|
||||||
objSubTypeJobRescanMsg = 606
|
objSubTypeJobRescanMsg = 606
|
||||||
objSubTypeJobMarkAsDone = 607
|
objSubTypeJobSetJobDone = 607
|
||||||
objSubTypeItemResource = 701
|
objSubTypeItemResource = 701
|
||||||
objSubTypeItemAlch = 702
|
objSubTypeItemAlch = 702
|
||||||
objSubTypeItemMisc = 703
|
objSubTypeItemMisc = 703
|
||||||
@ -118,6 +123,11 @@ const (
|
|||||||
objJonStatusPending = 10
|
objJonStatusPending = 10
|
||||||
objJobStatusDone = 20
|
objJobStatusDone = 20
|
||||||
|
|
||||||
|
objJobPriority = 1
|
||||||
|
objJobPriorityRescanMsg = 2
|
||||||
|
objJobPriorityRescanChildMsg = 3
|
||||||
|
objJobPriorityRescanAllMsg = 4
|
||||||
|
|
||||||
MQGetMsgWorkers = 3
|
MQGetMsgWorkers = 3
|
||||||
SQLCWMsgWorkers = 6
|
SQLCWMsgWorkers = 6
|
||||||
SQLIdentifyMsgWorkers = 6
|
SQLIdentifyMsgWorkers = 6
|
||||||
|
46
job.go
Normal file
46
job.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func jobRescan(Job j) {
|
||||||
|
err := setJobStart(j.ID64)
|
||||||
|
logOnError(err, "jobRescan : setJobStart")
|
||||||
|
r := JobPayloadRescanMsg{}
|
||||||
|
err = json.Unmarshal(j.payload, &r)
|
||||||
|
ids := getSQLListID64(r.Query)
|
||||||
|
if len(ids) > 1 {
|
||||||
|
for _, id := range ids {
|
||||||
|
p := JobPayloadRescanMsg{}
|
||||||
|
fmt.Sprintf(p.Query, "SELECT o.id from obj o where o.id = %d and o.obj_type_id = %d and o.obj_sub_type_id = %d;", id, objTypeMessage, objSubTypeMessageUnknown)
|
||||||
|
b, _ := json.Marshal(p)
|
||||||
|
err := createJob(objSubTypeJobRescanMsg, objJobPriorityRescanChildMsg, time.Now(), b)
|
||||||
|
logOnError(err, "jobRescan : createJob(objSubTypeJobRescanMsg)")
|
||||||
|
}
|
||||||
|
p := JobPayloadSetDone{
|
||||||
|
JobID64: j.ID64,
|
||||||
|
}
|
||||||
|
b, _ := json.Marshal(p)
|
||||||
|
err := createJob(objSubTypeJobSetJobDone, objJobPriority, time.Now(), b)
|
||||||
|
logOnError(err, "jobRescan : createJob(objSubTypeJobSetJobDone)")
|
||||||
|
} else if len(ids) == 1 {
|
||||||
|
SQLMsgIdentifyQueue <- ids[0]
|
||||||
|
err = setJobDone(j.ID64)
|
||||||
|
logOnError(err, "jobRescan : setJobDone")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func jobSetDone(Job j) {
|
||||||
|
err := setJobStart(j.ID64)
|
||||||
|
logOnError(err, "jobSetDone : setJobStart")
|
||||||
|
r := JobPayloadSetDone{}
|
||||||
|
err = json.Unmarshal(j.payload, &r)
|
||||||
|
err = setJobDone(r.JobID64)
|
||||||
|
logOnError(err, "jobSetDone : setJobDone(child)")
|
||||||
|
err = setJobDone(j.ID64)
|
||||||
|
logOnError(err, "jobSetDone : setJobDone")
|
||||||
|
return
|
||||||
|
}
|
2
main.go
2
main.go
@ -116,7 +116,7 @@ func main() {
|
|||||||
go SQLJobWorker(w)
|
go SQLJobWorker(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Started !")
|
log.Println("Bot started !")
|
||||||
|
|
||||||
// Main loop
|
// Main loop
|
||||||
for {
|
for {
|
||||||
|
81
sql.go
81
sql.go
@ -199,8 +199,11 @@ func initDB() {
|
|||||||
,priority SMALLINT NOT NULL
|
,priority SMALLINT NOT NULL
|
||||||
,status SMALLINT NOT NULL
|
,status SMALLINT NOT NULL
|
||||||
,schedule DATETIME NOT NULL
|
,schedule DATETIME NOT NULL
|
||||||
,start TIMESTAMP
|
,is_done TINYINT NOT NULL
|
||||||
,end TIMESTAMP
|
,in_work TINYINT NOT NULL
|
||||||
|
,pulled TIMESTAMP
|
||||||
|
,started TIMESTAMP
|
||||||
|
,ended TIMESTAMP
|
||||||
,payload VARCHAR(4000)
|
,payload VARCHAR(4000)
|
||||||
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
|
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
|
||||||
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
|
) ENGINE = InnoDB CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci;`)
|
||||||
@ -240,7 +243,7 @@ func initDB() {
|
|||||||
,(` + strconv.Itoa(objSubTypeJobWithdrawal) + `, "job_withdraw", "Withdrawal job", ` + strconv.Itoa(objTypeJob) + `)
|
,(` + strconv.Itoa(objSubTypeJobWithdrawal) + `, "job_withdraw", "Withdrawal job", ` + strconv.Itoa(objTypeJob) + `)
|
||||||
,(` + strconv.Itoa(objSubTypeJobGStock) + `, "job_gstock", "GStock job", ` + strconv.Itoa(objTypeJob) + `)
|
,(` + strconv.Itoa(objSubTypeJobGStock) + `, "job_gstock", "GStock job", ` + strconv.Itoa(objTypeJob) + `)
|
||||||
,(` + strconv.Itoa(objSubTypeJobRescanMsg) + `, "job_rescan_msg", "Rescan message job", ` + strconv.Itoa(objTypeJob) + `)
|
,(` + strconv.Itoa(objSubTypeJobRescanMsg) + `, "job_rescan_msg", "Rescan message job", ` + strconv.Itoa(objTypeJob) + `)
|
||||||
,(` + strconv.Itoa(objSubTypeJobMarkAsDone) + `, "job_mark_as_done", "Mark job as done job", ` + strconv.Itoa(objTypeJob) + `)
|
,(` + strconv.Itoa(objSubTypeJobSetJobDone) + `, "job_set_done", "Set job as done job", ` + strconv.Itoa(objTypeJob) + `)
|
||||||
,(` + strconv.Itoa(objSubTypeItemResource) + `, "item_res", "Time", ` + strconv.Itoa(objTypeItem) + `)
|
,(` + strconv.Itoa(objSubTypeItemResource) + `, "item_res", "Time", ` + strconv.Itoa(objTypeItem) + `)
|
||||||
,(` + strconv.Itoa(objSubTypeItemAlch) + `, "item_alch", "Time", ` + strconv.Itoa(objTypeItem) + `)
|
,(` + strconv.Itoa(objSubTypeItemAlch) + `, "item_alch", "Time", ` + strconv.Itoa(objTypeItem) + `)
|
||||||
,(` + strconv.Itoa(objSubTypeItemMisc) + `, "item_misc", "Time", ` + strconv.Itoa(objTypeItem) + `)
|
,(` + strconv.Itoa(objSubTypeItemMisc) + `, "item_misc", "Time", ` + strconv.Itoa(objTypeItem) + `)
|
||||||
@ -1353,15 +1356,15 @@ func createJob(job_type_id int32, priority int32, schedule time.Time, payload []
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
stmt, err = db.Prepare(`INSERT INTO obj_job (obj_id, priority, status, schedule, start, end, payload)
|
stmt, err = db.Prepare(`INSERT INTO obj_job (obj_id, priority, status, schedule, is_done, in_work, inserted, pulled, started, end, payload)
|
||||||
VALUES (?, ?, ?, ?, NULL, NULL, ?);`)
|
VALUES (?, ?, ?, ?, 0, 0, , ?, NULL, NULL, NULL, ?);`)
|
||||||
logOnError(err, "createJob : prepare insert obj_job")
|
logOnError(err, "createJob : prepare insert obj_job")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
|
|
||||||
_, err = stmt.Exec(objId, priority, objJobStatusNew, schedule, payload)
|
_, err = stmt.Exec(objId, priority, objJobStatusNew, schedule, time.Now(), payload)
|
||||||
logOnError(err, "createJob : insert obj_job")
|
logOnError(err, "createJob : insert obj_job")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -1370,6 +1373,40 @@ func createJob(job_type_id int32, priority int32, schedule time.Time, payload []
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func setJobDone(jobId int64) error {
|
||||||
|
stmt, err := db.Prepare(`UPDATE obj_job j SET j.is_done = 1, j.in_work = 0, j.ended = ? WHERE j.obj_id = ?;`)
|
||||||
|
logOnError(err, "setJobDone : prepare update obj_job")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
|
||||||
|
res, err := stmt.Exec(time.Now(), jobId)
|
||||||
|
s := fmt.Sprintf("setJobDone, update obj_job(%d)", jobId)
|
||||||
|
logOnError(err, s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func setJobStart(jobId int64) error {
|
||||||
|
stmt, err := db.Prepare(`UPDATE obj_job j SET j.ended = ? WHERE j.obj_id = ?;`)
|
||||||
|
logOnError(err, "setJobStart : prepare update obj_job")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer stmt.Close()
|
||||||
|
|
||||||
|
res, err := stmt.Exec(time.Now(), jobId)
|
||||||
|
s := fmt.Sprintf("setJobStart, update obj_job(%d)", jobId)
|
||||||
|
logOnError(err, s)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func loadMsgParsingRules() (m map[int]MessageParsingRule, err error) {
|
func loadMsgParsingRules() (m map[int]MessageParsingRule, err error) {
|
||||||
var (
|
var (
|
||||||
id int32
|
id int32
|
||||||
@ -1420,6 +1457,7 @@ func loadCurrentJobs() ([]Job, error) {
|
|||||||
var (
|
var (
|
||||||
objId int64
|
objId int64
|
||||||
jobTypeId int32
|
jobTypeId int32
|
||||||
|
status int32
|
||||||
payload []byte
|
payload []byte
|
||||||
jobs []Job
|
jobs []Job
|
||||||
)
|
)
|
||||||
@ -1431,18 +1469,19 @@ func loadCurrentJobs() ([]Job, error) {
|
|||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
|
|
||||||
stmt, err := tx.Prepare("SELECT o.id, o.obj_sub_type_id, j.payload FROM obj_job j, obj o WHERE j.obj_id = o.id AND j.status = ? AND j.schedule <= ? ORDER BY j.priority ASC, j.obj_id ASC LIMIT ? FOR UPDATE;")
|
stmt, err := tx.Prepare("SELECT o.id, o.obj_sub_type_id, j.status, j.payload FROM obj_job j, obj o WHERE j.obj_id = o.id AND j.is_done = 0 and j.in_work = 0 AND j.schedule <= ? ORDER BY j.priority ASC, j.obj_id ASC LIMIT ? FOR UPDATE;")
|
||||||
logOnError(err, "loadCurrentJobs : prepare select statement")
|
logOnError(err, "loadCurrentJobs : prepare select statement")
|
||||||
|
|
||||||
rows, err := stmt.Query(objJobStatusNew, time.Now(), SQLJobSliceSize)
|
rows, err := stmt.Query(time.Now(), SQLJobSliceSize)
|
||||||
logOnError(err, "loadCurrentJobs : query select statement")
|
logOnError(err, "loadCurrentJobs : query select statement")
|
||||||
|
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
err = rows.Scan(&objId, &jobTypeId, &payload)
|
err = rows.Scan(&objId, &jobTypeId, &status, &payload)
|
||||||
logOnError(err, "loadCurrentJobs : scan query rows")
|
logOnError(err, "loadCurrentJobs : scan query rows")
|
||||||
job := Job{
|
job := Job{
|
||||||
ID64: objId,
|
ID64: objId,
|
||||||
JobTypeID: jobTypeId,
|
JobTypeID: jobTypeId,
|
||||||
|
Status: status,
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
}
|
}
|
||||||
jobs = append(jobs, job)
|
jobs = append(jobs, job)
|
||||||
@ -1454,11 +1493,11 @@ func loadCurrentJobs() ([]Job, error) {
|
|||||||
err = stmt.Close()
|
err = stmt.Close()
|
||||||
logOnError(err, "loadCurrentJobs : close select statement")
|
logOnError(err, "loadCurrentJobs : close select statement")
|
||||||
|
|
||||||
stmt, err = tx.Prepare("UPDATE obj_job j SET j.status = ? WHERE j.obj_id = ?;")
|
stmt, err = tx.Prepare("UPDATE obj_job j SET j.in_work = 1, j.pulled = ? WHERE j.obj_id = ?;")
|
||||||
logOnError(err, "loadCurrentJobs : prepare update statement")
|
logOnError(err, "loadCurrentJobs : prepare update statement")
|
||||||
|
|
||||||
for _, job := range jobs {
|
for _, job := range jobs {
|
||||||
_, err = stmt.Exec(objJonStatusPending, job.ID64)
|
_, err = stmt.Exec(time.Now(), job.ID64)
|
||||||
logOnError(err, "loadCurrentJobs : updating row")
|
logOnError(err, "loadCurrentJobs : updating row")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1470,3 +1509,23 @@ func loadCurrentJobs() ([]Job, error) {
|
|||||||
|
|
||||||
return jobs, nil
|
return jobs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getSQLListID64(q string) []int64 {
|
||||||
|
var id int64
|
||||||
|
|
||||||
|
rows, err := db.Query(q)
|
||||||
|
s := fmt.Sprintf("getSQLListID64 : Query(%s)", q)
|
||||||
|
logOnError(err, s)
|
||||||
|
|
||||||
|
ids := make([]int64)
|
||||||
|
|
||||||
|
for rows.Next() {
|
||||||
|
err = rows.Scan(&id)
|
||||||
|
logOnError(err, "getSQLListID64 : scan next val")
|
||||||
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
err = rows.Err()
|
||||||
|
loglOnError(err, "getSQLListID64 : query end")
|
||||||
|
rows.Close()
|
||||||
|
return ids
|
||||||
|
}
|
||||||
|
13
workers.go
13
workers.go
@ -146,11 +146,16 @@ func SQLJobWorker(id int) {
|
|||||||
if len(jobs) > 0 {
|
if len(jobs) > 0 {
|
||||||
log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : %d jobs.\n", len(jobs))
|
log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : %d jobs.\n", len(jobs))
|
||||||
}
|
}
|
||||||
/*
|
for _, j := range jobs {
|
||||||
for _, j := range jobs {
|
switch j.JobTypeID {
|
||||||
|
case objSubTypeJobRescanMsg:
|
||||||
|
jobRescan(j)
|
||||||
|
case objSubTypeJobSetJobDone:
|
||||||
|
jobSetDone(j)
|
||||||
|
default:
|
||||||
|
log.Printf("SQLJobWorker["+strconv.Itoa(id)+"] : No handler for job type #%d.\n", j.JobTypeID)
|
||||||
}
|
}
|
||||||
*/
|
}
|
||||||
if len(jobs) < SQLJobSliceSize {
|
if len(jobs) < SQLJobSliceSize {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user