diff options
| -rw-r--r-- | crawler/database.go | 14 | ||||
| -rw-r--r-- | crawler/main.go | 1 | ||||
| -rw-r--r-- | crawler/scrape.go | 14 |
3 files changed, 15 insertions, 14 deletions
diff --git a/crawler/database.go b/crawler/database.go index e11c80b..e47f723 100644 --- a/crawler/database.go +++ b/crawler/database.go @@ -4,6 +4,8 @@ import ( "database/sql" "fmt" "strings" + + "github.com/jmoiron/sqlx" ) func (app *App) createTables() error { @@ -91,11 +93,11 @@ func (app *App) createTables() error { /** * Saves scrapped offers in database. Detects which offers are new. Runs inside tx. */ -func (app *App) save_offer(W []Angebot) error { +func (app *App) save_offer(Tx *sqlx.Tx, W []Angebot) error { query := `INSERT INTO angebot (shop, name, url, abv, volume, age, original_price, discounted_price, base_price, valid_until, image_url, website_raw, spirit_type, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)` - stmt, err := app.Tx.Prepare(query) + stmt, err := Tx.Prepare(query) if err != nil { Warn(err, "Save Offer: Preparing query failed") return err @@ -112,7 +114,7 @@ func (app *App) save_offer(W []Angebot) error { // resembles UNIQUE constraint detect_duplicate_query := fmt.Sprintf(`SELECT 1 FROM _intern_view WHERE name = ? AND shop_id = %d AND volume = %4.2f AND abv = %4.2f AND original_price = %d AND discounted_price = %d AND valid_until = %d`, o.Shop, o.Volume, o.Abv, o.Original_price, o.Discounted_price, o.Valid_until) - err := app.Tx.QueryRow(detect_duplicate_query, o.Name).Scan(&found) + err := Tx.QueryRow(detect_duplicate_query, o.Name).Scan(&found) if err == sql.ErrNoRows { @@ -144,12 +146,12 @@ func (app *App) save_offer(W []Angebot) error { /** * Invalidates expired offers by updating 'valid_until' column. Detects which offers are expired. Runs inside Tx. */ -func (app *App) remove_expired(W []Angebot, shop Shop) error { +func (app *App) remove_expired(Tx *sqlx.Tx, W []Angebot, shop Shop) error { query := `SELECT id, name, shop, volume, abv, original_price, discounted_price FROM angebot WHERE shop = ? AND created_at < ? AND (valid_until = 0 OR valid_until > ?)` - rows, err := app.Tx.Queryx(query, shop.Id, app.Now, app.Now) + rows, err := Tx.Queryx(query, shop.Id, app.Now, app.Now) if err != nil { Warn(err, "Remove expired: Query failed") return err @@ -168,7 +170,7 @@ func (app *App) remove_expired(W []Angebot, shop Shop) error { if !app.offer_contains(W, offer_db) { DebugOffer(offer_db, "Contains not - Set to expire") expire_query := `UPDATE angebot SET valid_until = ? WHERE id = ?` - _, err = app.Tx.Exec(expire_query, app.Now, offer_db.Id) + _, err = Tx.Exec(expire_query, app.Now, offer_db.Id) if err != nil { offer_db.error_msg = err.Error() offer_db.error_ctx = fmt.Sprintf("UPDATE angebot SET valid_until = %d WHERE id = %d", app.Now, offer_db.Id) diff --git a/crawler/main.go b/crawler/main.go index c44d8e9..b75c21a 100644 --- a/crawler/main.go +++ b/crawler/main.go @@ -17,7 +17,6 @@ type App struct { Shops []Shop Config *Config DB *sqlx.DB - Tx *sqlx.Tx Now int64 Debug bool } diff --git a/crawler/scrape.go b/crawler/scrape.go index 6ef9fcf..18aa4b9 100644 --- a/crawler/scrape.go +++ b/crawler/scrape.go @@ -29,40 +29,40 @@ func (app *App) Scrape(shop Shop, wait chan bool) { var err error txFailed := false - app.Tx, err = app.DB.Beginx() + Tx, err := app.DB.Beginx() if err != nil { Fatal(err, "scrape.go: Starting transaction failed. Shop: "+shop.Name) } // retry on error - for i := 0; i < 3; i++ { + for i := 1; i < 4; i++ { W = app.ScrapeShop(shop) - W = sanitize_offer(W, shop) + W = sanitize_offer(W, shop, i) if len(W) >= 1 { break } } - err = app.save_offer(W) + err = app.save_offer(Tx, W) if err != nil { txFailed = true Warn(err, "Saving offers failed. Shop: "+shop.Name) } - err = app.remove_expired(W, shop) + err = app.remove_expired(Tx, W, shop) if err != nil { txFailed = true Warn(err, "Removing expired offers failed. Shop: "+shop.Name) } if txFailed { - err = app.Tx.Rollback() + err = Tx.Rollback() if err != nil { Fatal(err, "scrape.go: Rollback transaction failed") } } else { - err = app.Tx.Commit() + err = Tx.Commit() if err != nil { Fatal(err, "scrape.go: Committing transaction failed") } |
