Skip to content

Commit

Permalink
fix: re-add tasks (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibbp committed Jun 2, 2024
1 parent 7e6ee31 commit 4825322
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
41 changes: 41 additions & 0 deletions internal/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
entChannel "github.com/zibbp/ganymede/ent/channel"
entVod "github.com/zibbp/ganymede/ent/vod"
"github.com/zibbp/ganymede/internal/archive"
"github.com/zibbp/ganymede/internal/auth"
"github.com/zibbp/ganymede/internal/database"
"github.com/zibbp/ganymede/internal/live"
"github.com/zibbp/ganymede/internal/twitch"
Expand All @@ -31,6 +32,46 @@ func NewService(store *database.Database, liveService *live.Service, archiveServ
return &Service{Store: store, LiveService: liveService, ArchiveService: archiveService}
}

func (s *Service) StartTask(c echo.Context, task string) error {
log.Info().Msgf("Manually starting task %s", task)

switch task {
case "check_live":
err := s.LiveService.Check()
if err != nil {
return fmt.Errorf("error checking live: %v", err)
}

case "check_vod":
go s.LiveService.CheckVodWatchedChannels()

case "get_jwks":
err := auth.FetchJWKS()
if err != nil {
return fmt.Errorf("error fetching jwks: %v", err)
}

case "twitch_auth":
err := twitch.Authenticate()
if err != nil {
return fmt.Errorf("error authenticating twitch: %v", err)
}

case "storage_migration":
go func() {
err := s.StorageMigration()
if err != nil {
log.Error().Err(err).Msg("Error migrating storage")
}
}()

case "prune_videos":
go PruneVideos()
}

return nil
}

func (s *Service) StorageMigration() error {
// Get all videos in db
videos, err := s.Store.Client.Vod.Query().WithChannel().All(context.Background())
Expand Down
3 changes: 2 additions & 1 deletion internal/transport/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ func groupV1Routes(e *echo.Group, h *Handler) {
execGroup.POST("/ffprobe", h.GetFfprobeData, auth.GuardMiddleware, auth.GetUserMiddleware, auth.UserRoleMiddleware(utils.ArchiverRole))

// Task
// taskGroup := e.Group("/task")
taskGroup := e.Group("/task")
taskGroup.POST("/start", h.StartTask, auth.GuardMiddleware, auth.GetUserMiddleware, auth.UserRoleMiddleware(utils.AdminRole))

// Notification
notificationGroup := e.Group("/notification")
Expand Down
37 changes: 37 additions & 0 deletions internal/transport/http/task.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,41 @@
package http

import (
"net/http"

"github.com/labstack/echo/v4"
)

type TaskService interface {
StartTask(c echo.Context, task string) error
}

type StartTaskRequest struct {
Task string `json:"task" validate:"required,oneof=check_live check_vod get_jwks twitch_auth storage_migration prune_videos"`
}

// StartTask godoc
//
// @Summary Start a task
// @Description Start a task
// @Tags task
// @Accept json
// @Produce json
// @Param body body StartTaskRequest true "StartTaskRequest"
// @Success 200
// @Failure 500 {object} utils.ErrorResponse
// @Router /task/start [post]
// @Security ApiKeyCookieAuth
func (h *Handler) StartTask(c echo.Context) error {
str := new(StartTaskRequest)
if err := c.Bind(str); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err := c.Validate(str); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err := h.Service.TaskService.StartTask(c, str.Task); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}
return c.NoContent(http.StatusOK)
}

0 comments on commit 4825322

Please sign in to comment.