test reloading dynamic parsing rules

This commit is contained in:
shoopea 2020-02-10 10:43:24 +08:00
parent 163093ef67
commit c3853fe527
2 changed files with 79 additions and 90 deletions

156
rules.go
View File

@ -30,101 +30,78 @@ func resetMsgParsingRules() error {
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Withdrawal acknowledgment",
Rule: "^/withdraw_(?P<Ref>[a-f0-9]{32})$",
MsgTypeID64: cacheObjSubType[`msg_job_gwithdraw_ack`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
chats := make([]int64, 0)
users := make([]int64, 0)
chats = append(chats, cfg.Bot.Mainchat)
users = append(users, 0)
r = MessageParsingRule{
Priority: 9999,
Description: "All item craft",
Rule: "^/craft_all$",
MsgTypeID64: cacheObjSubType[`msg_bot_craft_all`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
muxClients.RLock()
for id, _ := range clients {
chats = append(chats, id)
users = append(users, id)
}
rules2 = append(rules2, r)
muxClients.RUnlock()
r = MessageParsingRule{
Priority: 9998,
Description: "All item craft",
Rule: "^/craft_all$",
MsgTypeID64: cacheObjSubType[`msg_bot_craft_all`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: cfg.Bot.Admin,
}
rules2 = append(rules2, r)
muxObjItem.RLock()
defer muxObjItem.RUnlock()
for _, o := range objItems {
if o.Craft != nil {
r = MessageParsingRule{
Priority: 9998,
Description: fmt.Sprintf("Specific item craft %s", o.Code),
Rule: fmt.Sprintf("^(?P<Cmd>%s)(( )+(?P<Quantity>[0-9]+)){0,1}$", regexp.QuoteMeta(o.Craft.Command)),
MsgTypeID64: cacheObjSubType[`msg_bot_craft_item`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
// commands
for id, _ := range chats {
muxObjItem.RLock()
for _, o := range objItems {
if o.Craft != nil {
r = MessageParsingRule{
Priority: 9998,
Description: fmt.Sprintf("Specific item craft %s", o.Code),
Rule: fmt.Sprintf("^(?P<Cmd>%s)(( )+(?P<Quantity>[0-9]+)){0,1}$", regexp.QuoteMeta(o.Craft.Command)),
MsgTypeID64: cacheObjSubType[`msg_bot_craft_item`],
ChatID64: chats[id],
SenderUserID64: users[id],
}
rules2 = append(rules2, r)
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9998,
Description: fmt.Sprintf("Specific item craft %s", o.Code),
Rule: fmt.Sprintf("^(?P<Cmd>%s)(( )+(?P<Quantity>[0-9]+)){0,1}$", regexp.QuoteMeta(o.Craft.Command)),
MsgTypeID64: cacheObjSubType[`msg_bot_craft_item`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: cfg.Bot.Admin,
}
rules2 = append(rules2, r)
}
muxObjItem.RUnlock()
r = MessageParsingRule{
Priority: 9999,
Description: "Get Stock",
Rule: "^/g_stock$",
MsgTypeID64: cacheObjSubType[`msg_bot_g_stock`],
ChatID64: chats[id],
SenderUserID64: users[id],
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Get Shops list",
Rule: "^/shops$",
MsgTypeID64: cacheObjSubType[`msg_bot_shops`],
ChatID64: chats[id],
SenderUserID64: users[id],
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Withdrawal acknowledgment",
Rule: "^/withdraw_(?P<Ref>[a-f0-9]{32})$",
MsgTypeID64: cacheObjSubType[`msg_job_gwithdraw_ack`],
ChatID64: chats[id],
SenderUserID64: users[id],
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "All item craft",
Rule: "^/craft_all$",
MsgTypeID64: cacheObjSubType[`msg_bot_craft_all`],
ChatID64: chats[id],
SenderUserID64: users[id],
}
rules2 = append(rules2, r)
}
r = MessageParsingRule{
Priority: 9999,
Description: "Get Stock",
Rule: "^/g_stock$",
MsgTypeID64: cacheObjSubType[`msg_bot_g_stock`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Get Stock",
Rule: "^/g_stock$",
MsgTypeID64: cacheObjSubType[`msg_bot_g_stock`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: cfg.Bot.Admin,
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Get Shops list",
Rule: "^/shops$",
MsgTypeID64: cacheObjSubType[`msg_bot_shops`],
ChatID64: cfg.Bot.Mainchat,
SenderUserID64: 0,
}
rules2 = append(rules2, r)
r = MessageParsingRule{
Priority: 9999,
Description: "Get Shops list",
Rule: "^/shops$",
MsgTypeID64: cacheObjSubType[`msg_bot_shops`],
ChatID64: cfg.Bot.Admin,
SenderUserID64: cfg.Bot.Admin,
}
rules2 = append(rules2, r)
// chats
r = MessageParsingRule{
Priority: 1,
Description: "Default Main chat",
@ -145,6 +122,7 @@ func resetMsgParsingRules() error {
}
rules2 = append(rules2, r)
// sanitize
for _, r := range rules {
r2 := MessageParsingRule{
Priority: r.Priority,
@ -170,6 +148,7 @@ func resetMsgParsingRules() error {
rules2 = append(rules2, r2)
}
// insert to SQL
_, err = db.Exec(`TRUNCATE TABLE msg_rules;`)
logOnError(err, "resetMsgParsingRules : truncate table msg_rules")
if err != nil {
@ -185,7 +164,6 @@ func resetMsgParsingRules() error {
defer stmt.Close()
count = 0
for _, r2 := range rules2 {
_, err = stmt.Exec(r2.Priority, r2.MsgTypeID64, r2.ChatID64, r2.SenderUserID64, r2.Description, r2.Rule)
logOnError(err, "resetMsgParsingRules : insert statement "+r2.Description)
if err != nil {

View File

@ -96,7 +96,10 @@ func MQGetMsgWorker(id int, msgs chan<- ChatWarsMessage) {
func MQKeepAliveWorker() {
//log.Printf("MQKeepAliveWorker : Starting.")
var err error
var (
err error
reloadRules bool
)
c := MQClient{
User: cfg.Rabbit.User,
Password: cfg.Rabbit.Password,
@ -157,6 +160,7 @@ func MQKeepAliveWorker() {
for d := range m {
x := MQKeepAlive{}
reloadRules = false
err = json.Unmarshal(d.Body, &x)
logOnError(err, "MQKeepAliveWorker : Can't unmarshal.\n"+string(d.Body))
if err == nil {
@ -202,6 +206,7 @@ func MQKeepAliveWorker() {
clt.MQ.Connection.Close()
} else {
clt.Active = true
reloadRules = true
//log.Printf("MQKeepAliveWorker : Connected to %s.\n", x.Nickname)
}
}
@ -235,6 +240,12 @@ func MQKeepAliveWorker() {
TGCmdQueue <- c
}
}
if reloadRules {
log.Printf("New client, reloading rules ..\n")
resetMsgParsingRules()
msgParsingRules, err = loadMsgParsingRules()
logOnError(err, "initCache : message parsing rules")
}
}
}
}