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

View File

@ -13,6 +13,7 @@ type CardPrice struct {
Ctime time.Time `db:"ctime" cc:"ctime"`
}
// InsertCardPriceOpts -
type InsertCardPriceOpts struct {
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 `
insStr := ""
vals := make([]interface{}, 0, len(tmp)*3)
for i := 0; i < len(tmp); i++ {
if len(insStr) > 0 {
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
}

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,
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 {
return nil, err
}

View File

@ -55,9 +55,11 @@ func CountSets(arg ListSetsOpts) (c int, err error) {
if err != nil {
return 0, err
}
query, args, err = sqlx.In(query, args)
if err != nil {
return 0, err
if len(arg.Code) > 0 {
query, args, err = sqlx.In(query, args)
if err != nil {
return 0, err
}
}
query = x.Rebind(query)
@ -73,7 +75,7 @@ func ListSets(arg ListSetsOpts) (sets []*Sets, err error) {
if 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 {
Code []string `db:"code"`
@ -100,9 +102,11 @@ func ListSets(arg ListSetsOpts) (sets []*Sets, err error) {
if err != nil {
return nil, err
}
query, args, err = sqlx.In(query, args)
if err != nil {
return nil, err
if len(arg.Code) > 0 {
query, args, err = sqlx.In(query, args)
if err != nil {
return nil, err
}
}
query = x.Rebind(query)

View File

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

View File

@ -1,12 +1,20 @@
package background
import (
"log"
"time"
"git.trj.tw/golang/mtgbot/models"
"git.trj.tw/golang/mtgbot/modules/apiact"
"git.trj.tw/golang/mtgbot/modules/pool"
"git.trj.tw/golang/utils"
)
func RunMTG() {
log.Println("run :::::::: ")
runMTGPriceJob()
}
func runMTGPriceJob() {
api, err := apiact.NewTCGApi("v1.19.0")
if err != nil {
@ -33,6 +41,9 @@ func runMTGPriceJob() {
pool := pool.NewPool(5)
for _, set := range sets {
if set.TCGPlayerID <= 0 {
continue
}
pool.Add()
go getPriceFromAPI(pool, api, set.TCGPlayerID)
}
@ -41,7 +52,10 @@ func runMTGPriceJob() {
}
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)
if err != nil {