From ddc06c0c93952fe49fb045fd823555ec63862240 Mon Sep 17 00:00:00 2001 From: Samuel Pua Date: Tue, 1 Nov 2022 00:04:26 +0800 Subject: [PATCH] Feat(booking-job): Handle lagging timeout with passenger details --- .../ktmtrainbot/backgroundbookingjob.go | 38 +++++++++++-------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/backend/internal/ktmtrainbot/backgroundbookingjob.go b/backend/internal/ktmtrainbot/backgroundbookingjob.go index 68bb9db..18f7f03 100644 --- a/backend/internal/ktmtrainbot/backgroundbookingjob.go +++ b/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