Browse Source

Feat(booking-job): Auto refresh after 9 minutes

master
Samuel Pua 3 years ago
parent
commit
298c7d853a
  1. 66
      backend/internal/ktmtrainbot/backgroundbookingjob.go

66
backend/internal/ktmtrainbot/backgroundbookingjob.go

@ -275,6 +275,44 @@ func (env *Env) startBooking(job *Booking, username string, password string, cre
default:
}
paymentSucceed := chooseAndMakePayment(timerCtx, creditCardType, creditCard, creditCardCVV, creditCardExpiry, browser, page)
// // Start debug screenshots
// debugScreenshotCtx, cancelDebugScreenshot := context.WithCancel(context.Background())
// go takeDebugScreenshots(debugScreenshotCtx, courtPage)
// // Defer done with debug screenshot
// defer cancelDebugScreenshot()
if !paymentSucceed {
log.Println("Payment failed.")
return false
}
time.Sleep(10 * time.Second)
_ = page
return true
}
func chooseAndMakePayment(timerCtx context.Context, creditCardType, creditCard, creditCardCVV, creditCardExpiry string, browser *rod.Browser, page *rod.Page) bool {
defer func() {
if r := recover(); r != nil {
log.Println("Recovering from chooseAndMakePayment panic...")
chooseAndMakePayment(timerCtx, creditCardType, creditCard, creditCardCVV, creditCardExpiry, browser, page)
}
}()
// Exits if context cancelled
select {
case <-timerCtx.Done():
browser.MustClose()
return false
default:
}
go triggerFuturePageRefresh(page)
if strings.ToUpper(creditCardType) == "VISA" || strings.ToUpper(creditCardType) == "MASTERCARD" {
log.Println("Credit card payment method chosen.")
page = choosePaymentCreditCard(page)
@ -310,24 +348,14 @@ func (env *Env) startBooking(job *Booking, username string, password string, cre
expiryMonth := strings.Split(creditCardExpiry, "/")[0]
expiryYear := strings.Split(creditCardExpiry, "/")[1]
page = makePayment(page, creditCardType, creditCard, expiryMonth, expiryYear, creditCardCVV)
makePayment(page, creditCardType, creditCard, expiryMonth, expiryYear, creditCardCVV)
log.Println("Credit card payment made.")
} else if strings.ToUpper(creditCardType) == "KTMWALLET" {
log.Println("KTM wallet payment method chosen.")
page = choosePaymentKTMWallet(page)
choosePaymentKTMWallet(page)
log.Println("KTM wallet payment made.")
}
// // Start debug screenshots
// debugScreenshotCtx, cancelDebugScreenshot := context.WithCancel(context.Background())
// go takeDebugScreenshots(debugScreenshotCtx, courtPage)
// // Defer done with debug screenshot
// defer cancelDebugScreenshot()
time.Sleep(10 * time.Second)
_ = page
return true
}
@ -592,3 +620,17 @@ func sensitiveCustomForm(
return page
}
func triggerFuturePageRefresh(page *rod.Page) func() {
ctx, cancelFunc := context.WithCancel(context.Background())
go func() {
select {
case <-ctx.Done():
return
// Reload page after 9 minutes
case <-time.After(540 * time.Second):
page.MustReload()
}
}()
return cancelFunc
}

Loading…
Cancel
Save