Skip to content

Commit

Permalink
feat: add cron scheduling for Brightspace
Browse files Browse the repository at this point in the history
  • Loading branch information
CK-7vn committed Dec 10, 2024
1 parent 3299cf1 commit 1813f98
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 5 additions & 1 deletion backend/tasks/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ func initDB() *gorm.DB {
}

func (jr *JobRunner) intoProviderPlatformTask(cj *models.CronJob, provId uint, task *models.RunnableTask) error {
if err := jr.db.Model(&models.RunnableTask{}).Preload("Job").
if err := jr.db.Model(&models.RunnableTask{}).Preload("Job").Preload("Provider").
Where(models.RunnableTask{ProviderPlatformID: &provId, JobID: cj.ID}).FirstOrCreate(&task).Error; err != nil {
log.Errorf("failed to create task for job: %v. error: %v", cj.Name, err)
return err
}
if err := jr.db.Model(&task).Preload("Job").Preload("Provider").First(&task).Error; err != nil {
log.Errorf("failed to reload task for job: %v. error: %v", cj.Name, err)
return err
}
params, err := models.JobType(cj.Name).GetParams(jr.db, provId, cj.ID)
if err != nil {
log.Errorf("failed to get params for job: %v", err)
Expand Down
24 changes: 20 additions & 4 deletions backend/tasks/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"UnlockEdv2/src/models"
"fmt"
"os"
"os/signal"
"syscall"
Expand All @@ -27,15 +29,29 @@ func main() {
log.Fatalf("failed to generate tasks: %v", err)
return
}
brightspaceHour := 1
for _, task := range tasks {
if task.Job == nil {
log.Errorf("Task %v has no job", task.ID)
continue
}
_, err := scheduler.NewJob(gocron.CronJob(task.Job.Schedule, false), gocron.NewTask(runner.runTask, task))
if err != nil {
log.Errorf("Failed to create job: %v", err)
continue
if task.Provider != nil && task.Provider.Type == models.Brightspace {
brightspaceCron := fmt.Sprintf("0 %d * * * ", brightspaceHour)
brightspaceHour++
if brightspaceHour > 23 {
brightspaceHour = 1
}
_, err := scheduler.NewJob(gocron.CronJob(brightspaceCron, false), gocron.NewTask(runner.runTask, &task))
if err != nil {
log.Errorf("Failed to create job: %v, err")
continue
}
} else {
_, err := scheduler.NewJob(gocron.CronJob(task.Job.Schedule, false), gocron.NewTask(runner.runTask, &task))
if err != nil {
log.Errorf("Failed to create job: %v", err)
continue
}
}
}
runner.execute()
Expand Down
3 changes: 3 additions & 0 deletions backend/tasks/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ func (jr *JobRunner) execute() {
}
log.Infof("Generated %v tasks", tasks)
for _, task := range tasks {
if task.Provider != nil && task.Provider.Type == models.Brightspace {
continue
}
log.Infof("Running task: %v", task.Job.Name)
jr.runTask(&task)
}
Expand Down

0 comments on commit 1813f98

Please sign in to comment.