Skip to content

Commit

Permalink
feat(runner): implement runner registration with gocast
Browse files Browse the repository at this point in the history
  • Loading branch information
joschahenningsen committed Feb 2, 2025
1 parent 3e075f4 commit b806347
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 140 deletions.
25 changes: 18 additions & 7 deletions cmd/tumlive/tumlive.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"log/slog"
log "log/slog"
"net"
"net/http"
_ "net/http/pprof"
Expand All @@ -13,13 +14,6 @@ import (

"github.com/soheilhy/cmux"

"github.com/TUM-Dev/gocast/api"
apiv2 "github.com/TUM-Dev/gocast/apiv2/server"
"github.com/TUM-Dev/gocast/dao"
"github.com/TUM-Dev/gocast/model"
"github.com/TUM-Dev/gocast/tools"
"github.com/TUM-Dev/gocast/tools/tum"
"github.com/TUM-Dev/gocast/web"
"github.com/dgraph-io/ristretto"
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
Expand All @@ -29,6 +23,15 @@ import (
"github.com/pkg/profile"
"gorm.io/driver/mysql"
"gorm.io/gorm"

"github.com/TUM-Dev/gocast/api"
apiv2 "github.com/TUM-Dev/gocast/apiv2/server"
"github.com/TUM-Dev/gocast/dao"
"github.com/TUM-Dev/gocast/model"
"github.com/TUM-Dev/gocast/pkg/runner_manager"
"github.com/TUM-Dev/gocast/tools"
"github.com/TUM-Dev/gocast/tools/tum"
"github.com/TUM-Dev/gocast/web"
)

var VersionTag = "development"
Expand Down Expand Up @@ -209,6 +212,7 @@ func main() {
&model.Subtitles{},
&model.TranscodingFailure{},
&model.Email{},
&model.Runner{},
)
if err != nil {
sentry.CaptureException(err)
Expand All @@ -234,6 +238,13 @@ func main() {
}
dao.Cache = *cache

m := runner_manager.New(dao.NewDaoWrapper())
log.Info("running runner manager")
err = m.Run()
if err != nil {
log.Error("Failed to start runner manager", "err", err)
}

// init meili search index settings
go tools.NewMeiliExporter(dao.NewDaoWrapper()).SetIndexSettings()

Expand Down
2 changes: 2 additions & 0 deletions dao/dao_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type DaoWrapper struct {
SubtitlesDao
TranscodingFailureDao
EmailDao
RunnerDao RunnerDao
}

func NewDaoWrapper() DaoWrapper {
Expand Down Expand Up @@ -63,5 +64,6 @@ func NewDaoWrapper() DaoWrapper {
SubtitlesDao: NewSubtitlesDao(),
TranscodingFailureDao: NewTranscodingFailureDao(),
EmailDao: NewEmailDao(),
RunnerDao: NewRunnerDao(),
}
}
49 changes: 49 additions & 0 deletions dao/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package dao

import (
"context"

"gorm.io/gorm"
"gorm.io/gorm/clause"

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

//go:generate mockgen -source=runner.go -destination ../mock_dao/runner.go

type RunnerDao interface {
// Get Runner by ID
Get(context.Context, uint) (model.Runner, error)

// Create a new Runner for the database
Create(context.Context, *model.Runner) error

// Delete a Runner by id.
Delete(context.Context, uint) error
}

type runnerDao struct {
db *gorm.DB
}

func NewRunnerDao() RunnerDao {
return runnerDao{db: DB}
}

// Get a Runner by id.
func (d runnerDao) Get(c context.Context, id uint) (res model.Runner, err error) {
return res, d.db.WithContext(c).First(&res, id).Error
}

// Create a Runner.
func (d runnerDao) Create(c context.Context, it *model.Runner) error {
return d.db.WithContext(c).Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "hostname"}, {Name: "hostname"}}, // key column
DoUpdates: clause.AssignmentColumns([]string{"port"}), // column needed to be updated
}).Create(it).Error
}

// Delete a Runner by id.
func (d runnerDao) Delete(c context.Context, id uint) error {
return d.db.WithContext(c).Delete(&model.Runner{}, id).Error
}
79 changes: 79 additions & 0 deletions mock_dao/runner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 17 additions & 22 deletions mock_dao/statistics.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions model/runner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package model

import "gorm.io/gorm"

// Runner represents a runner handling streams, converting videos,
// extracting silence from audios, creating thumbnails, etc.
type Runner struct {
Hostname string `gorm:"column:hostname;primaryKey;unique;not null"`
Port uint32 `gorm:"column:port;not null"`
}

// TableName returns the name of the table for the Runner model in the database.
func (*Runner) TableName() string {
return "runners" // todo
}

// BeforeCreate returns an error, if Runner r is invalid
func (r *Runner) BeforeCreate(tx *gorm.DB) (err error) {
// this method currently is a noop
return nil
}

// AfterFind runs after the Runner is fetched from the db
func (r *Runner) AfterFind(tx *gorm.DB) (err error) {
// this method currently is a noop
return nil
}
Loading

0 comments on commit b806347

Please sign in to comment.