Skip to content
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@ tumlive.service

# osx
.DS_Store

# downloaded protoc binary
protoc-*.zip
2 changes: 2 additions & 0 deletions dao/dao_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type DaoWrapper struct {
TranscodingFailureDao
EmailDao
RunnerDao RunnerDao
JobDao JobDao
}

func NewDaoWrapper() DaoWrapper {
Expand Down Expand Up @@ -65,5 +66,6 @@ func NewDaoWrapper() DaoWrapper {
TranscodingFailureDao: NewTranscodingFailureDao(),
EmailDao: NewEmailDao(),
RunnerDao: NewRunnerDao(),
JobDao: NewJobDao(),
}
}
99 changes: 99 additions & 0 deletions dao/job.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package dao

import (
"context"

"gorm.io/gorm"

"github.com/TUM-Dev/gocast/model"
)

//go:generate go tool mockgen -source=job.go -destination ../mock_dao/job.go

// JobDao interface defines methods for job data access
type JobDao interface {
// Create creates a new job in the database
Create(context.Context, *model.Job) error

// Update updates an existing job in the database
Update(context.Context, *model.Job) error

// GetByJobID retrieves a job by its unique job ID
GetByJobID(context.Context, string) (model.Job, error)

// GetByStreamID retrieves all jobs for a given stream ID
GetByStreamID(context.Context, uint) ([]model.Job, error)

// GetByRunnerHostname retrieves all active jobs for a given runner
GetByRunnerHostname(context.Context, string) ([]model.Job, error)

// GetActiveJobs retrieves all active jobs (created or running)
GetActiveJobs(context.Context) ([]model.Job, error)

// Delete deletes a job by its job ID
Delete(context.Context, string) error

// DeleteByStreamID deletes all jobs for a given stream ID
DeleteByStreamID(context.Context, uint) error
}

type jobDao struct {
db *gorm.DB
}

// NewJobDao creates a new JobDao instance
func NewJobDao() JobDao {
return jobDao{db: DB}
}

// Create creates a new job in the database
func (d jobDao) Create(ctx context.Context, job *model.Job) error {
return d.db.WithContext(ctx).Create(job).Error
}

// Update updates an existing job in the database
func (d jobDao) Update(ctx context.Context, job *model.Job) error {
return d.db.WithContext(ctx).Save(job).Error
}

// GetByJobID retrieves a job by its unique job ID
func (d jobDao) GetByJobID(ctx context.Context, jobID string) (model.Job, error) {
var job model.Job
err := d.db.WithContext(ctx).Where("job_id = ?", jobID).First(&job).Error
return job, err
}

// GetByStreamID retrieves all jobs for a given stream ID
func (d jobDao) GetByStreamID(ctx context.Context, streamID uint) ([]model.Job, error) {
var jobs []model.Job
err := d.db.WithContext(ctx).Where("stream_id = ?", streamID).Find(&jobs).Error
return jobs, err
}

// GetByRunnerHostname retrieves all active jobs for a given runner
func (d jobDao) GetByRunnerHostname(ctx context.Context, hostname string) ([]model.Job, error) {
var jobs []model.Job
err := d.db.WithContext(ctx).
Where("runner_hostname = ? AND status IN ?", hostname, []model.JobStatus{model.JobStatusCreated, model.JobStatusRunning}).
Find(&jobs).Error
return jobs, err
}

// GetActiveJobs retrieves all active jobs (created or running)
func (d jobDao) GetActiveJobs(ctx context.Context) ([]model.Job, error) {
var jobs []model.Job
err := d.db.WithContext(ctx).
Where("status IN ?", []model.JobStatus{model.JobStatusCreated, model.JobStatusRunning}).
Find(&jobs).Error
return jobs, err
}

// Delete deletes a job by its job ID
func (d jobDao) Delete(ctx context.Context, jobID string) error {
return d.db.WithContext(ctx).Where("job_id = ?", jobID).Delete(&model.Job{}).Error
}

// DeleteByStreamID deletes all jobs for a given stream ID
func (d jobDao) DeleteByStreamID(ctx context.Context, streamID uint) error {
return d.db.WithContext(ctx).Where("stream_id = ?", streamID).Delete(&model.Job{}).Error
}
21 changes: 21 additions & 0 deletions dao/migrations/202412050.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package migrations

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot remove this, migrations happen automatically

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the migration file in c1c0a2a.


import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"

"github.com/TUM-Dev/gocast/model"
)

// Migrate202412050 creates the jobs table for tracking runner job status.
func Migrate202412050() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "202412050",
Migrate: func(tx *gorm.DB) error {
return tx.AutoMigrate(&model.Job{})
},
Rollback: func(tx *gorm.DB) error {
return tx.Migrator().DropTable("jobs")
},
}
}
1 change: 1 addition & 0 deletions dao/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func newMigrator() *migrator {
migrations.Migrate202212010(),
migrations.Migrate202212020(),
migrations.Migrate202301006(),
migrations.Migrate202412050(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot remove this as well

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in c1c0a2a - reverted migrator.go to its original state.

},
}
}
12 changes: 12 additions & 0 deletions dao/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ type RunnerDao interface {
// GetAll gets a list of all Runners.
GetAll(context.Context) ([]model.Runner, error)

// GetAllWithJobs gets a list of all Runners with their active jobs preloaded.
GetAllWithJobs(context.Context) ([]model.Runner, error)

// ReserveRunner returns the runner that currently runs the least jobs and is not draining.
// It also increments the number of jobs assigned to the runner.
ReserveRunner(context.Context) (model.Runner, error)
Expand Down Expand Up @@ -100,3 +103,12 @@ func (d runnerDao) GetAll(c context.Context) ([]model.Runner, error) {
err := d.db.WithContext(c).Find(&runners).Error
return runners, err
}

// GetAllWithJobs returns all Runners with their active jobs preloaded
func (d runnerDao) GetAllWithJobs(c context.Context) ([]model.Runner, error) {
var runners []model.Runner
err := d.db.WithContext(c).
Preload("Jobs", "status IN ?", []model.JobStatus{model.JobStatusCreated, model.JobStatusRunning}).
Find(&runners).Error
return runners, err
}
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ require (
github.com/gorilla/websocket v1.5.3 // indirect
github.com/jinzhu/now v1.1.5
github.com/microcosm-cc/bluemonday v1.0.27
github.com/pkg/profile v1.7.0
github.com/robfig/cron/v3 v3.0.1
github.com/russross/blackfriday/v2 v2.1.0
github.com/satori/go.uuid v1.2.0
Expand Down Expand Up @@ -70,12 +69,10 @@ require (
github.com/bytedance/sonic/loader v0.2.2 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/felixge/fgprof v0.9.5 // indirect
github.com/gabriel-vasile/mimetype v1.4.8 // indirect
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/golang-jwt/jwt/v4 v4.5.1 // indirect
github.com/golang/glog v1.2.4 // indirect
github.com/google/pprof v0.0.0-20241210010833-40e02aabc2ad // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
github.com/mailru/easyjson v0.9.0 // indirect
Expand Down
Loading
Loading