mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-12-06 06:36:25 +00:00
Add /health and /ready for transcoder
This commit is contained in:
@@ -145,6 +145,7 @@ func main() {
|
||||
g.Use(RequireCorePlayPermission)
|
||||
}
|
||||
|
||||
api.RegisterHealthHandlers(e.Group("/video"), metadata.Database)
|
||||
api.RegisterStreamHandlers(g, transcoder)
|
||||
api.RegisterMetadataHandlers(g, metadata)
|
||||
api.RegisterPProfHandlers(e)
|
||||
|
||||
40
transcoder/src/api/health.go
Normal file
40
transcoder/src/api/health.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
type health struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func RegisterHealthHandlers(e *echo.Group, db *pgxpool.Pool) {
|
||||
h := health{db}
|
||||
e.GET("/health", h.CheckHealth)
|
||||
e.GET("/ready", h.CheckReady)
|
||||
}
|
||||
|
||||
func (h *health) CheckHealth(c echo.Context) error {
|
||||
return c.JSON(200, struct {
|
||||
Status string `json:"status"`
|
||||
}{Status: "healthy"})
|
||||
}
|
||||
|
||||
func (h *health) CheckReady(c echo.Context) error {
|
||||
_, err := h.db.Exec(c.Request().Context(), "select 1")
|
||||
|
||||
status := "healthy"
|
||||
db := "healthy"
|
||||
ret := 200
|
||||
if err != nil {
|
||||
status = "unhealthy"
|
||||
ret = 500
|
||||
db = err.Error()
|
||||
}
|
||||
|
||||
return c.JSON(ret, struct {
|
||||
Status string `json:"status"`
|
||||
Database string `json:"database"`
|
||||
}{Status: status, Database: db})
|
||||
}
|
||||
@@ -27,7 +27,7 @@ func (s *MetadataService) ExtractSubs(ctx context.Context, info *MediaInfo) (any
|
||||
log.Printf("Couldn't extract subs: %v", err)
|
||||
return set(nil, err)
|
||||
}
|
||||
_, err = s.database.Exec(ctx, `update gocoder.info set ver_extract = $2 where sha = $1`, info.Sha, ExtractVersion)
|
||||
_, err = s.Database.Exec(ctx, `update gocoder.info set ver_extract = $2 where sha = $1`, info.Sha, ExtractVersion)
|
||||
return set(nil, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -157,7 +157,7 @@ func (s *MetadataService) GetKeyframes(info *MediaInfo, isVideo bool, idx uint32
|
||||
}
|
||||
|
||||
kf.info.ready.Wait()
|
||||
tx, _ := s.database.Begin(ctx)
|
||||
tx, _ := s.Database.Begin(ctx)
|
||||
tx.Exec(
|
||||
ctx,
|
||||
fmt.Sprintf(`update %s set keyframes = $3 where sha = $1 and idx = $2`, table),
|
||||
|
||||
@@ -21,7 +21,7 @@ import (
|
||||
)
|
||||
|
||||
type MetadataService struct {
|
||||
database *pgxpool.Pool
|
||||
Database *pgxpool.Pool
|
||||
lock RunLock[string, *MediaInfo]
|
||||
thumbLock RunLock[string, any]
|
||||
extractLock RunLock[string, any]
|
||||
@@ -43,7 +43,7 @@ func NewMetadataService() (*MetadataService, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to setup database: %w", err)
|
||||
}
|
||||
s.database = db
|
||||
s.Database = db
|
||||
|
||||
storage, err := s.setupStorage(ctx)
|
||||
if err != nil {
|
||||
@@ -55,8 +55,8 @@ func NewMetadataService() (*MetadataService, error) {
|
||||
}
|
||||
|
||||
func (s *MetadataService) Close() error {
|
||||
if s.database != nil {
|
||||
s.database.Close()
|
||||
if s.Database != nil {
|
||||
s.Database.Close()
|
||||
}
|
||||
|
||||
if s.storage != nil {
|
||||
@@ -174,7 +174,7 @@ func (s *MetadataService) GetMetadata(ctx context.Context, path string, sha stri
|
||||
for _, audio := range ret.Audios {
|
||||
audio.Keyframes = nil
|
||||
}
|
||||
tx, err := s.database.Begin(ctx)
|
||||
tx, err := s.Database.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -191,7 +191,7 @@ func (s *MetadataService) GetMetadata(ctx context.Context, path string, sha stri
|
||||
}
|
||||
|
||||
func (s *MetadataService) getMetadata(ctx context.Context, path string, sha string) (*MediaInfo, error) {
|
||||
rows, _ := s.database.Query(
|
||||
rows, _ := s.Database.Query(
|
||||
ctx,
|
||||
`select
|
||||
i.sha, i.path, i.extension, i.mime_codec, i.size, i.duration, i.container, i.fonts,
|
||||
@@ -214,7 +214,7 @@ func (s *MetadataService) getMetadata(ctx context.Context, path string, sha stri
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, _ = s.database.Query(
|
||||
rows, _ = s.Database.Query(
|
||||
ctx,
|
||||
`select * from gocoder.videos as v where v.sha=$1`,
|
||||
sha,
|
||||
@@ -224,7 +224,7 @@ func (s *MetadataService) getMetadata(ctx context.Context, path string, sha stri
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, _ = s.database.Query(
|
||||
rows, _ = s.Database.Query(
|
||||
ctx,
|
||||
`select * from gocoder.audios as a where a.sha=$1`,
|
||||
sha,
|
||||
@@ -234,7 +234,7 @@ func (s *MetadataService) getMetadata(ctx context.Context, path string, sha stri
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rows, _ = s.database.Query(
|
||||
rows, _ = s.Database.Query(
|
||||
ctx,
|
||||
`select * from gocoder.subtitles as s where s.sha=$1`,
|
||||
sha,
|
||||
@@ -259,7 +259,7 @@ func (s *MetadataService) getMetadata(ctx context.Context, path string, sha stri
|
||||
fmt.Printf("Couldn't find external subtitles: %v", err)
|
||||
}
|
||||
|
||||
rows, _ = s.database.Query(
|
||||
rows, _ = s.Database.Query(
|
||||
ctx,
|
||||
`select * from gocoder.chapters as c where c.sha=$1`,
|
||||
sha,
|
||||
@@ -282,7 +282,7 @@ func (s *MetadataService) storeFreshMetadata(ctx context.Context, path string, s
|
||||
return set(nil, err)
|
||||
}
|
||||
|
||||
tx, err := s.database.Begin(ctx)
|
||||
tx, err := s.Database.Begin(ctx)
|
||||
if err != nil {
|
||||
return set(ret, err)
|
||||
}
|
||||
|
||||
@@ -77,7 +77,7 @@ func (s *MetadataService) ExtractThumbs(ctx context.Context, path string, sha st
|
||||
if err != nil {
|
||||
return set(nil, err)
|
||||
}
|
||||
_, err = s.database.Exec(ctx, `update gocoder.info set ver_thumbs = $2 where sha = $1`, sha, ThumbsVersion)
|
||||
_, err = s.Database.Exec(ctx, `update gocoder.info set ver_thumbs = $2 where sha = $1`, sha, ThumbsVersion)
|
||||
return set(nil, err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user