Browse Source

Feat(booking-job): Handle lagging timeout with passenger details

master
Samuel Pua 3 years ago
parent
commit
ddc06c0c93
  1. 38
      backend/internal/ktmtrainbot/backgroundbookingjob.go

38
backend/internal/ktmtrainbot/backgroundbookingjob.go

@ -264,18 +264,29 @@ func (env *Env) startBooking(job *Booking, username string, password string, cre
default:
}
page = fillPassengerDetails(page, name, gender, passport, passportExpiry, contact)
log.Println("Passenger details filled.")
passthroughLaggingPage := false
// Exits if context cancelled
select {
case <-timerCtx.Done():
browser.MustClose()
return false
default:
}
for !passthroughLaggingPage {
filledPassengerDetailContext, filledPassengerDetails := context.WithTimeout(context.Background(), 15*time.Second)
go func() {
page = fillPassengerDetails(page, name, gender, passport, passportExpiry, contact)
log.Println("Passenger details filled.")
filledPassengerDetails()
}()
paymentSucceed := chooseAndMakePayment(timerCtx, creditCardType, creditCard, creditCardCVV, creditCardExpiry, browser, page)
<-filledPassengerDetailContext.Done()
passthroughLaggingPage = chooseAndMakePayment(timerCtx, creditCardType, creditCard, creditCardCVV, creditCardExpiry, browser, page)
// Exits if context cancelled
select {
case <-timerCtx.Done():
browser.MustClose()
return false
default:
}
}
// // Start debug screenshots
// debugScreenshotCtx, cancelDebugScreenshot := context.WithCancel(context.Background())
@ -284,11 +295,6 @@ func (env *Env) startBooking(job *Booking, username string, password string, cre
// // Defer done with debug screenshot
// defer cancelDebugScreenshot()
if !paymentSucceed {
log.Println("Payment failed.")
return false
}
time.Sleep(10 * time.Second)
_ = page
@ -310,7 +316,6 @@ func chooseAndMakePayment(timerCtx context.Context, creditCardType, creditCard,
if r := recover(); r != nil {
log.Println("Recovering from chooseAndMakePayment panic...")
cancelRefresh()
chooseAndMakePayment(timerCtx, creditCardType, creditCard, creditCardCVV, creditCardExpiry, browser, page)
}
}()
@ -642,6 +647,7 @@ func triggerFuturePageRefresh(page *rod.Page) func() {
case <-time.After(540 * time.Second):
log.Println("Reloading page")
page.MustReload()
page.MustWaitLoad()
}
}()
return cancelFunc

Loading…
Cancel
Save