add background get price job

This commit is contained in:
Jay 2019-01-18 16:39:02 +08:00
parent efaf0c2fdd
commit ca453dd773
6 changed files with 58 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"git.trj.tw/golang/mtgbot/models" "git.trj.tw/golang/mtgbot/models"
"git.trj.tw/golang/mtgbot/modules/background"
"git.trj.tw/golang/mtgbot/modules/cmd" "git.trj.tw/golang/mtgbot/modules/cmd"
"git.trj.tw/golang/mtgbot/modules/config" "git.trj.tw/golang/mtgbot/modules/config"
"git.trj.tw/golang/mtgbot/modules/options" "git.trj.tw/golang/mtgbot/modules/options"
@ -37,6 +38,8 @@ func main() {
// register session object types // register session object types
regTypes() regTypes()
background.SetCronJob()
engine := routes.NewEngine() engine := routes.NewEngine()
routes.SetRoutes(engine) routes.SetRoutes(engine)
engine.Run(fmt.Sprintf(":%d", conf.Port)) engine.Run(fmt.Sprintf(":%d", conf.Port))

View File

@ -13,6 +13,7 @@ type CardPrice struct {
Ctime time.Time `db:"ctime" cc:"ctime"` Ctime time.Time `db:"ctime" cc:"ctime"`
} }
// InsertCardPriceOpts -
type InsertCardPriceOpts struct { type InsertCardPriceOpts struct {
Data []CardPrice Data []CardPrice
} }
@ -34,15 +35,34 @@ func InsertCardPrice(arg InsertCardPriceOpts) (err error) {
query := `insert into "public"."card_price" ("card", "price_n", "price_f", "ctime") values ` query := `insert into "public"."card_price" ("card", "price_n", "price_f", "ctime") values `
insStr := "" insStr := ""
vals := make([]interface{}, 0, len(tmp)*3)
for i := 0; i < len(tmp); i++ { for i := 0; i < len(tmp); i++ {
if len(insStr) > 0 { if len(insStr) > 0 {
insStr += "," insStr += ","
} }
insStr += " (:card, :price_n, :price_f, now()) " insStr += " (?, ?, ?, now()) "
vals = append(vals, tmp[i].Card, tmp[i].PriceN, tmp[i].PriceF)
} }
_, err = x.NamedExec(query+insStr, tmp) query = x.Rebind(query + insStr)
_, err = x.Exec(query, vals...)
return
}
// GetLastPriceByCard -
func GetLastPriceByCard(id string) (price *CardPrice, err error) {
if len(id) == 0 {
return nil, errors.New("id is empty")
}
price = &CardPrice{}
query := `select cp.card, cp.price_n, cp.price_f, cp.ctime from "public"."card_price" cp where "card" = $1`
err = x.Get(price, query, id)
if err != nil {
return nil, err
}
return return
} }

View File

@ -155,7 +155,12 @@ func GetCardByTCGPlayerIDs(ids []int) (cards []*Cards, err error) {
query := `select c.id, c.name, c.cmc, c.mana_cost, c.text, c.layout, c.image_url, c.loyalty, query := `select c.id, c.name, c.cmc, c.mana_cost, c.text, c.layout, c.image_url, c.loyalty,
c.type, c.number, c.power, c.toughness, c.set, c.tcgplayerid from "public"."cards" c where c.tcgplayerid in (?)` c.type, c.number, c.power, c.toughness, c.set, c.tcgplayerid from "public"."cards" c where c.tcgplayerid in (?)`
err = x.Select(&cards, query, ids) query, args, err := sqlx.In(query, ids)
if err != nil {
return nil, err
}
query = x.Rebind(query)
err = x.Select(&cards, query, args...)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -55,10 +55,12 @@ func CountSets(arg ListSetsOpts) (c int, err error) {
if err != nil { if err != nil {
return 0, err return 0, err
} }
if len(arg.Code) > 0 {
query, args, err = sqlx.In(query, args) query, args, err = sqlx.In(query, args)
if err != nil { if err != nil {
return 0, err return 0, err
} }
}
query = x.Rebind(query) query = x.Rebind(query)
err = x.Get(&c, query, args...) err = x.Get(&c, query, args...)
@ -73,7 +75,7 @@ func ListSets(arg ListSetsOpts) (sets []*Sets, err error) {
if arg.Offset < 0 { if arg.Offset < 0 {
arg.Offset = 0 arg.Offset = 0
} }
query := `select s.code, s.name, s.release_date from "public"."sets" s` query := `select s.code, s.name, s.release_date, s.tcgplayerid from "public"."sets" s`
where := struct { where := struct {
Code []string `db:"code"` Code []string `db:"code"`
@ -100,10 +102,12 @@ func ListSets(arg ListSetsOpts) (sets []*Sets, err error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(arg.Code) > 0 {
query, args, err = sqlx.In(query, args) query, args, err = sqlx.In(query, args)
if err != nil { if err != nil {
return nil, err return nil, err
} }
}
query = x.Rebind(query) query = x.Rebind(query)
err = x.Select(&sets, query, args...) err = x.Select(&sets, query, args...)

View File

@ -7,6 +7,6 @@ var c *cron.Cron
// SetCronJob - // SetCronJob -
func SetCronJob() { func SetCronJob() {
c = cron.New() c = cron.New()
c.AddFunc("0 0 0,12 * * *", runMTGPriceJob)
c.Start() c.Start()
} }

View File

@ -1,12 +1,20 @@
package background package background
import ( import (
"log"
"time"
"git.trj.tw/golang/mtgbot/models" "git.trj.tw/golang/mtgbot/models"
"git.trj.tw/golang/mtgbot/modules/apiact" "git.trj.tw/golang/mtgbot/modules/apiact"
"git.trj.tw/golang/mtgbot/modules/pool" "git.trj.tw/golang/mtgbot/modules/pool"
"git.trj.tw/golang/utils" "git.trj.tw/golang/utils"
) )
func RunMTG() {
log.Println("run :::::::: ")
runMTGPriceJob()
}
func runMTGPriceJob() { func runMTGPriceJob() {
api, err := apiact.NewTCGApi("v1.19.0") api, err := apiact.NewTCGApi("v1.19.0")
if err != nil { if err != nil {
@ -33,6 +41,9 @@ func runMTGPriceJob() {
pool := pool.NewPool(5) pool := pool.NewPool(5)
for _, set := range sets { for _, set := range sets {
if set.TCGPlayerID <= 0 {
continue
}
pool.Add() pool.Add()
go getPriceFromAPI(pool, api, set.TCGPlayerID) go getPriceFromAPI(pool, api, set.TCGPlayerID)
} }
@ -41,7 +52,10 @@ func runMTGPriceJob() {
} }
func getPriceFromAPI(pool *pool.Pool, api *apiact.TCGPlayer, groupid int) { func getPriceFromAPI(pool *pool.Pool, api *apiact.TCGPlayer, groupid int) {
defer pool.Done() defer func() {
time.Sleep(time.Millisecond * 200)
pool.Done()
}()
prices, err := api.ListGroupProductPrice(groupid) prices, err := api.ListGroupProductPrice(groupid)
if err != nil { if err != nil {