Feat(booking-job): Allow concurrent job from different users
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:
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/go-rod/rod"
|
"github.com/go-rod/rod"
|
||||||
"github.com/go-rod/rod/lib/devices"
|
"github.com/go-rod/rod/lib/devices"
|
||||||
"github.com/go-rod/rod/lib/launcher"
|
"github.com/go-rod/rod/lib/launcher"
|
||||||
|
"github.com/google/uuid"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"gorm.io/gorm/logger"
|
"gorm.io/gorm/logger"
|
||||||
)
|
)
|
||||||
@@ -39,8 +40,22 @@ func (env *Env) BackgroundJobRunner() {
|
|||||||
|
|
||||||
tx := env.DB.Session(&gorm.Session{Logger: newLogger})
|
tx := env.DB.Session(&gorm.Session{Logger: newLogger})
|
||||||
for {
|
for {
|
||||||
|
|
||||||
|
var usersWithRunningTask []user.User
|
||||||
|
tx.Table("bookings").
|
||||||
|
Select("users.*").
|
||||||
|
Joins("left join users on users.id = bookings.user_id").
|
||||||
|
Where("bookings.status = ?", "running").
|
||||||
|
Find(&usersWithRunningTask)
|
||||||
|
|
||||||
|
var userIDArr []uuid.UUID
|
||||||
|
for _, currUser := range usersWithRunningTask {
|
||||||
|
userIDArr = append(userIDArr, currUser.ID)
|
||||||
|
}
|
||||||
|
|
||||||
var jobToDo Booking
|
var jobToDo Booking
|
||||||
err := tx.Model(&jobToDo).
|
err := tx.Model(&jobToDo).
|
||||||
|
Where("user_id NOT IN (?)", userIDArr).
|
||||||
Where("status = ?", "pending").
|
Where("status = ?", "pending").
|
||||||
Preload("User").
|
Preload("User").
|
||||||
First(&jobToDo).Error
|
First(&jobToDo).Error
|
||||||
@@ -52,8 +67,10 @@ func (env *Env) BackgroundJobRunner() {
|
|||||||
} else { // if there's job to do
|
} else { // if there's job to do
|
||||||
// Create next run where it's not the past (either from old NextRun or now())
|
// Create next run where it's not the past (either from old NextRun or now())
|
||||||
timeNow := time.Now()
|
timeNow := time.Now()
|
||||||
|
startTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 10, 0, 0, timeNow.Location())
|
||||||
|
endTime := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 16, 0, 0, timeNow.Location())
|
||||||
|
|
||||||
if timeNow.Hour() == 0 && timeNow.Minute() == 10 {
|
if timeNow.After(startTime) && timeNow.Before(endTime) {
|
||||||
err := env.DB.Where(&user.Profile{UserID: jobToDo.UserID}).First(&jobToDo.User.Profile).Error
|
err := env.DB.Where(&user.Profile{UserID: jobToDo.UserID}).First(&jobToDo.User.Profile).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
@@ -89,6 +106,8 @@ func (env *Env) BackgroundJobRunner() {
|
|||||||
jobToDo.Status = "running"
|
jobToDo.Status = "running"
|
||||||
env.DB.Save(&jobToDo)
|
env.DB.Save(&jobToDo)
|
||||||
log.Printf("Job Started: %v", jobToDo.ID)
|
log.Printf("Job Started: %v", jobToDo.ID)
|
||||||
|
} else {
|
||||||
|
time.Sleep(time.Second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user