Fix(booking-job): Ensures triggerFuturePageRefresh is cancelled
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good
All checks were successful
ktm-booking-bot/ktm-booking-bot/pipeline/head This commit looks good
This commit is contained in:
@@ -106,13 +106,13 @@ func (env *Env) BackgroundJobRunner() {
|
|||||||
if success {
|
if success {
|
||||||
jobToDo.Status = "success"
|
jobToDo.Status = "success"
|
||||||
env.DB.Save(&jobToDo)
|
env.DB.Save(&jobToDo)
|
||||||
fmt.Println("Successfully made a booking.")
|
log.Println("Successfully made a booking.")
|
||||||
} else {
|
} else {
|
||||||
// Sleep for 10 minutes before setting status to pending
|
// Sleep for 10 minutes before setting status to pending
|
||||||
time.Sleep(10 * time.Minute)
|
time.Sleep(10 * time.Minute)
|
||||||
jobToDo.Status = "pending"
|
jobToDo.Status = "pending"
|
||||||
env.DB.Save(&jobToDo)
|
env.DB.Save(&jobToDo)
|
||||||
fmt.Println("Failed to make a booking.")
|
log.Println("Failed to make a booking.")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
jobToDo.Status = "running"
|
jobToDo.Status = "running"
|
||||||
@@ -296,13 +296,6 @@ func (env *Env) startBooking(job *Booking, username string, password string, cre
|
|||||||
}
|
}
|
||||||
|
|
||||||
func chooseAndMakePayment(timerCtx context.Context, creditCardType, creditCard, creditCardCVV, creditCardExpiry string, browser *rod.Browser, page *rod.Page) bool {
|
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
|
// Exits if context cancelled
|
||||||
select {
|
select {
|
||||||
case <-timerCtx.Done():
|
case <-timerCtx.Done():
|
||||||
@@ -311,7 +304,15 @@ func chooseAndMakePayment(timerCtx context.Context, creditCardType, creditCard,
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
go triggerFuturePageRefresh(page)
|
cancelRefresh := triggerFuturePageRefresh(page)
|
||||||
|
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
log.Println("Recovering from chooseAndMakePayment panic...")
|
||||||
|
cancelRefresh()
|
||||||
|
chooseAndMakePayment(timerCtx, creditCardType, creditCard, creditCardCVV, creditCardExpiry, browser, page)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
if strings.ToUpper(creditCardType) == "VISA" || strings.ToUpper(creditCardType) == "MASTERCARD" {
|
if strings.ToUpper(creditCardType) == "VISA" || strings.ToUpper(creditCardType) == "MASTERCARD" {
|
||||||
log.Println("Credit card payment method chosen.")
|
log.Println("Credit card payment method chosen.")
|
||||||
@@ -356,6 +357,7 @@ func chooseAndMakePayment(timerCtx context.Context, creditCardType, creditCard,
|
|||||||
log.Println("KTM wallet payment made.")
|
log.Println("KTM wallet payment made.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cancelRefresh()
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -629,6 +631,7 @@ func triggerFuturePageRefresh(page *rod.Page) func() {
|
|||||||
return
|
return
|
||||||
// Reload page after 9 minutes
|
// Reload page after 9 minutes
|
||||||
case <-time.After(540 * time.Second):
|
case <-time.After(540 * time.Second):
|
||||||
|
log.Println("Reloading page")
|
||||||
page.MustReload()
|
page.MustReload()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|||||||
Reference in New Issue
Block a user