From 298c7d853af525aeae67b1cac31408d86384780b Mon Sep 17 00:00:00 2001 From: Samuel Pua Date: Thu, 27 Oct 2022 00:52:16 +0800 Subject: [PATCH] Feat(booking-job): Auto refresh after 9 minutes --- .../ktmtrainbot/backgroundbookingjob.go | 66 +++++++++++++++---- 1 file changed, 54 insertions(+), 12 deletions(-) diff --git a/backend/internal/ktmtrainbot/backgroundbookingjob.go b/backend/internal/ktmtrainbot/backgroundbookingjob.go index 5ec87c2..2117227 100644 --- a/backend/internal/ktmtrainbot/backgroundbookingjob.go +++ b/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 +}