Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ JWT_REFRESH_TTL=168h
INTERNAL_TOKEN=change-me-internal-token-32chars
# Upload limit (bytes). 5GB default for Phase 9, Phase 11 will use presigned multipart for bigger files
UPLOAD_MAX_BYTES=5368709120
# Upload reconciler: a video row is created before the bytes land, so an upload
# that never completes (closed tab, lost resume key) would hang in 'uploaded'
# forever. Every UPLOAD_RECONCILE_INTERVAL_SECONDS the upload service looks at
# 'uploaded' rows older than UPLOAD_STALE_TTL_SECONDS: it re-queues them when the
# raw file exists (lost event) or marks them failed (abandoned upload).
UPLOAD_STALE_TTL_SECONDS=3600
UPLOAD_RECONCILE_INTERVAL_SECONDS=300

# Transcoder (Phase 10) — limit CPU/RAM per job, sequential renditions
FFMPEG_THREADS=2
Expand Down
2 changes: 2 additions & 0 deletions deploy/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ services:
INTERNAL_TOKEN: ${INTERNAL_TOKEN:?INTERNAL_TOKEN is required — set it in .env (see .env.example)}
JWT_SECRET: ${JWT_SECRET:?JWT_SECRET is required — set it in .env (see .env.example)}
UPLOAD_MAX_BYTES: ${UPLOAD_MAX_BYTES:-5368709120}
UPLOAD_STALE_TTL_SECONDS: ${UPLOAD_STALE_TTL_SECONDS:-3600}
UPLOAD_RECONCILE_INTERVAL_SECONDS: ${UPLOAD_RECONCILE_INTERVAL_SECONDS:-300}
MINIO_PUBLIC_ENDPOINT: ${MINIO_PUBLIC_ENDPOINT:-http://localhost:9000}
# grace для Shutdown меньше stop_grace_period (issue #63)
SHUTDOWN_GRACE: 4m
Expand Down
2 changes: 2 additions & 0 deletions docs/services-pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ Flowix — MVP видеоплатформы с адаптивным стрими

`uploaded` → (transcoder забрал) → `processing` → `ready` | `failed`. Frontend поллит `GET /api/v1/videos/:id` пока не `ready`.

Застрявшие загрузки само-лечатся: сервис upload раз в `UPLOAD_RECONCILE_INTERVAL_SECONDS` берёт записи в `uploaded` старше `UPLOAD_STALE_TTL_SECONDS` и переопубликовывает `video.uploaded` (если original есть в MinIO) либо помечает `failed` (загрузка брошена). Лента `GET /api/v1/videos` показывает всем только `public`+`ready`, владелец видит и свои не-`ready` (uploaded/processing/failed) для удаления/перезалива.

### События (контракты)

- `video.uploaded {video_id: UUID, s3_key: string, owner_id: UUID}`
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/app/watch/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ export default function WatchPage() {
) : (
<div className="flex aspect-video items-center justify-center rounded-xl border bg-white text-zinc-500">
{video.status === "processing" || video.status === "uploaded" ? (
<span className="animate-pulse">Transcoding… status: {video.status}</span>
<span className="animate-pulse">
{video.status === "uploaded" ? "Waiting for upload…" : "Transcoding…"} status:{" "}
{video.status}
</span>
) : video.status === "failed" ? (
<span className="text-red-600">Transcoding failed</span>
) : (
Expand Down
3 changes: 3 additions & 0 deletions scripts/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ if [ -n "${TOKEN:-}" ]; then
code=$(curl -s -o "$TMP/body" -w '%{http_code}' -X PUT --data-binary "@$TMP/part2" -H "Authorization: Bearer $TOKEN" -H "Content-Type: video/mp4" -H "Content-Range: bytes $half-$end2/$total" "$GATEWAY/api/v1/videos/$RESUME2_ID/resumable")
[ "$code" = "200" ] || fail "second half resumable PUT not 200: $code $(cat "$TMP/body")"
say " 50% resume: ok (308→200)"
# complete so the video is queued and does not linger in 'uploaded'
code=$(curl -s -o "$TMP/body" -w '%{http_code}' -X POST "$GATEWAY/api/v1/videos/$RESUME2_ID/complete" -H "Authorization: Bearer $TOKEN")
[ "$code" = "200" ] || say " WARN: resume2 complete not 200: $code $(cat "$TMP/body")"
else
say " WARN: presign for resumable failed ($code)"
fi
Expand Down
1 change: 1 addition & 0 deletions services/auth/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.14
1 change: 1 addition & 0 deletions services/metadata/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func main() {
r.Group(func(r chi.Router) {
r.Use(mw.InternalAuth(internalToken))
r.Patch("/internal/videos/{id}/status", vh.UpdateStatus)
r.Get("/internal/videos/stale", vh.ListStale)
r.Get("/internal/videos/{id}", vh.GetInternal)
r.Get("/internal/videos/{id}/vod", vh.GetVODMapping)
})
Expand Down
29 changes: 29 additions & 0 deletions services/metadata/internal/handler/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type VideoStore interface {
Delete(ctx context.Context, id, ownerID string) error
UpdateStatus(ctx context.Context, id string, status model.VideoStatus, renditions []model.Rendition) error
UpdateThumbnail(ctx context.Context, id string, thumbnailS3Key string) error
ListStaleUploaded(ctx context.Context, olderThan time.Duration, limit int) ([]model.StaleUpload, error)
}

// StorageRemover deletes S3 objects (raw + renditions + thumbnail). Nil = skip (tests).
Expand Down Expand Up @@ -95,6 +96,7 @@ func (h *VideoHandler) Register(r chi.Router) {
})
// internal (no auth) for transcoder/upload
r.Patch("/internal/videos/{id}/status", h.UpdateStatus)
r.Get("/internal/videos/stale", h.ListStale)
r.Get("/internal/videos/{id}", h.GetInternal)
r.Get("/internal/videos/{id}/vod", h.GetVODMapping)
}
Expand Down Expand Up @@ -188,6 +190,33 @@ func (h *VideoHandler) getVideo(w http.ResponseWriter, r *http.Request, enforceV
writeJSON(w, r, http.StatusOK, v)
}

// ListStale godoc
// @Summary List videos stuck in 'uploaded' older than the given age (internal)
// @Description Used by the upload-service reconciler: a video row is created before the bytes land, so an upload that never completes stays in 'uploaded' forever.
// @Tags internal
// @Produce json
// @Param older_than_seconds query int false "Minimum age in seconds" default(3600)
// @Param limit query int false "Max rows (<=500)" default(100)
// @Success 200 {object} map[string]interface{}
// @Router /internal/videos/stale [get]
func (h *VideoHandler) ListStale(w http.ResponseWriter, r *http.Request) {
olderThan := 3600
if v, err := strconv.Atoi(r.URL.Query().Get("older_than_seconds")); err == nil && v > 0 {
olderThan = v
}
limit := 100
if v, err := strconv.Atoi(r.URL.Query().Get("limit")); err == nil && v > 0 && v <= 500 {
limit = v
}
list, err := h.repo.ListStaleUploaded(r.Context(), time.Duration(olderThan)*time.Second, limit)
if err != nil {
slog.Error("list stale uploads failed", "error", err)
writeError(w, r, http.StatusInternalServerError, "internal error")
return
}
writeJSON(w, r, http.StatusOK, map[string]interface{}{"data": list})
}

type vodMapping struct {
Sequences []vodSequence `json:"sequences"`
}
Expand Down
90 changes: 88 additions & 2 deletions services/metadata/internal/handler/video_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ func (f *fakeStore) GetByID(_ context.Context, id string) (*model.Video, error)
func (f *fakeStore) List(_ context.Context, limit, offset int, viewerID string) ([]model.Video, error) {
out := []model.Video{}
for _, v := range f.videos {
// mirror the SQL visibility filter (issue #44); empty = default public
// mirror the SQL feed filter (issue #44 + stale-upload fix): everyone
// sees only public+ready, the owner also sees their non-ready videos.
vis := v.Visibility
if vis == "" {
vis = model.VisibilityPublic
}
if vis != model.VisibilityPublic && (viewerID == "" || v.OwnerID != viewerID) {
isOwner := viewerID != "" && v.OwnerID == viewerID
publicReady := vis == model.VisibilityPublic && v.Status == model.StatusReady
if !publicReady && !isOwner {
continue
}
out = append(out, *v)
Expand Down Expand Up @@ -139,6 +142,19 @@ func (f *fakeStore) UpdateThumbnail(_ context.Context, id string, thumbnailS3Key
v.ThumbnailURL = &u
return nil
}
func (f *fakeStore) ListStaleUploaded(_ context.Context, olderThan time.Duration, limit int) ([]model.StaleUpload, error) {
out := []model.StaleUpload{}
for _, v := range f.videos {
if v.Status != model.StatusUploaded || time.Since(v.CreatedAt) < olderThan {
continue
}
out = append(out, model.StaleUpload{ID: v.ID, OwnerID: v.OwnerID})
if limit > 0 && len(out) >= limit {
break
}
}
return out, nil
}

// Issue #58: wrappers that simulate a failing DB for handler 500-mapping tests.
type failingGetStore struct {
Expand Down Expand Up @@ -175,6 +191,7 @@ func testRouter(store VideoStore) chi.Router {
r.With(optAuth).Get("/api/v1/videos", vh.List)
r.With(optAuth).Get("/api/v1/videos/{id}", vh.Get)
r.Patch("/internal/videos/{id}/status", vh.UpdateStatus)
r.Get("/internal/videos/stale", vh.ListStale)
r.Get("/internal/videos/{id}", vh.GetInternal)
r.Get("/internal/videos/{id}/vod", vh.GetVODMapping)
return r
Expand Down Expand Up @@ -344,6 +361,51 @@ func TestGetAndList(t *testing.T) {
}
}

// Feed must hide non-ready videos from everyone but their owner.
func TestListHidesNonReadyFromOthers(t *testing.T) {
store := newFake()
store.videos["pub-ready"] = &model.Video{ID: "pub-ready", OwnerID: "o1", Title: "a", Status: model.StatusReady, Visibility: model.VisibilityPublic}
store.videos["pub-failed"] = &model.Video{ID: "pub-failed", OwnerID: "o1", Title: "b", Status: model.StatusFailed, Visibility: model.VisibilityPublic}
store.videos["pub-uploaded"] = &model.Video{ID: "pub-uploaded", OwnerID: "o1", Title: "c", Status: model.StatusUploaded, Visibility: model.VisibilityPublic}
r := testRouter(store)

ids := func(token string) map[string]bool {
req := httptest.NewRequest("GET", "/api/v1/videos?limit=50&offset=0", nil)
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Fatalf("want 200 got %d %s", w.Code, w.Body.String())
}
var out struct {
Data []model.Video `json:"data"`
}
if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil {
t.Fatal(err)
}
got := map[string]bool{}
for _, v := range out.Data {
got[v.ID] = true
}
return got
}

anon := ids("")
if !anon["pub-ready"] || anon["pub-failed"] || anon["pub-uploaded"] {
t.Fatalf("anonymous feed must show only ready: %v", anon)
}
owner := ids(signToken("o1"))
if !owner["pub-ready"] || !owner["pub-failed"] || !owner["pub-uploaded"] {
t.Fatalf("owner must see own non-ready videos: %v", owner)
}
other := ids(signToken("o2"))
if other["pub-failed"] || other["pub-uploaded"] {
t.Fatalf("other user must not see non-ready videos: %v", other)
}
}

func TestUpdateForbidden(t *testing.T) {
store := newFake()
store.videos["vid-1"] = &model.Video{ID: "vid-1", OwnerID: "owner1", Title: "t", Status: model.StatusUploaded}
Expand All @@ -370,6 +432,30 @@ func TestUpdateForbidden(t *testing.T) {
}
}

// Reconciler endpoint: only 'uploaded' rows older than the threshold are listed.
func TestListStaleUploads(t *testing.T) {
store := newFake()
store.videos["old"] = &model.Video{ID: "old", OwnerID: "o1", Title: "t", Status: model.StatusUploaded, CreatedAt: time.Now().Add(-2 * time.Hour)}
store.videos["fresh"] = &model.Video{ID: "fresh", OwnerID: "o1", Title: "t", Status: model.StatusUploaded, CreatedAt: time.Now()}
store.videos["done"] = &model.Video{ID: "done", OwnerID: "o1", Title: "t", Status: model.StatusReady, CreatedAt: time.Now().Add(-2 * time.Hour)}
r := testRouter(store)
req := httptest.NewRequest("GET", "/internal/videos/stale?older_than_seconds=3600&limit=10", nil)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
if w.Code != 200 {
t.Fatalf("want 200 got %d %s", w.Code, w.Body.String())
}
var out struct {
Data []model.StaleUpload `json:"data"`
}
if err := json.Unmarshal(w.Body.Bytes(), &out); err != nil {
t.Fatal(err)
}
if len(out.Data) != 1 || out.Data[0].ID != "old" || out.Data[0].OwnerID != "o1" {
t.Fatalf("want only 'old', got %+v", out.Data)
}
}

func TestDelete(t *testing.T) {
store := newFake()
store.videos["vid-1"] = &model.Video{ID: "vid-1", OwnerID: "owner1", Title: "t", Status: model.StatusUploaded}
Expand Down
8 changes: 8 additions & 0 deletions services/metadata/internal/model/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ type Video struct {
Renditions []Rendition `json:"renditions,omitempty"`
}

// StaleUpload is a minimal view of a video still in 'uploaded' — the
// upload-service reconciler uses it to re-queue a lost event or fail an
// abandoned upload (a row is created before the bytes land).
type StaleUpload struct {
ID string `json:"id"`
OwnerID string `json:"owner_id"`
}

type Rendition struct {
VideoID string `json:"video_id"`
Quality string `json:"quality"`
Expand Down
30 changes: 29 additions & 1 deletion services/metadata/internal/repository/video.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"errors"
"fmt"
"time"

"flowix/metadata/internal/model"
"github.com/google/uuid"
Expand Down Expand Up @@ -81,7 +82,10 @@ func (r *VideoRepo) GetByID(ctx context.Context, id string) (*model.Video, error
func (r *VideoRepo) List(ctx context.Context, limit, offset int, viewerID string) ([]model.Video, error) {
// $3 участвует и в текстовом сравнении ($3 <> ''), и в сравнении с uuid-колонкой:
// без ::uuid Postgres выводит тип $3 как text и падает с SQLSTATE 42883 (uuid = text)
q := `SELECT v.id, v.owner_id, u.email, v.title, v.description, v.duration, v.status, COALESCE(v.visibility::text,'public'), v.thumbnail_s3_key, v.created_at, v.updated_at FROM videos v LEFT JOIN users u ON u.id=v.owner_id WHERE v.visibility='public' OR ($3 <> '' AND v.owner_id=$3::uuid) ORDER BY v.created_at DESC LIMIT $1 OFFSET $2`
// Лента: всем — только public+ready (неготовые/упавшие не светятся чужим);
// владелец видит свои в любом статусе (uploaded/processing/failed), чтобы
// удалить или перезалить (issue: зомби-видео засоряли ленту).
q := `SELECT v.id, v.owner_id, u.email, v.title, v.description, v.duration, v.status, COALESCE(v.visibility::text,'public'), v.thumbnail_s3_key, v.created_at, v.updated_at FROM videos v LEFT JOIN users u ON u.id=v.owner_id WHERE (v.visibility='public' AND v.status='ready') OR ($3 <> '' AND v.owner_id=$3::uuid) ORDER BY v.created_at DESC LIMIT $1 OFFSET $2`
rows, err := r.pool.Query(ctx, q, limit, offset, viewerID)
if err != nil {
return nil, err
Expand Down Expand Up @@ -153,6 +157,30 @@ func (r *VideoRepo) UpdateThumbnail(ctx context.Context, id string, thumbnailS3K
return err
}

// ListStaleUploaded returns videos still in 'uploaded' created more than
// olderThan ago. A row is inserted before the bytes are uploaded, so a video
// whose upload never completes sits in 'uploaded' forever; the upload-service
// reconciler uses this list to re-queue or fail such rows.
func (r *VideoRepo) ListStaleUploaded(ctx context.Context, olderThan time.Duration, limit int) ([]model.StaleUpload, error) {
if limit <= 0 || limit > 500 {
limit = 100
}
rows, err := r.pool.Query(ctx, `SELECT id, owner_id FROM videos WHERE status='uploaded' AND created_at < now() - ($1::int * interval '1 second') ORDER BY created_at ASC LIMIT $2`, int(olderThan.Seconds()), limit)
if err != nil {
return nil, err
}
defer rows.Close()
out := []model.StaleUpload{}
for rows.Next() {
var s model.StaleUpload
if err := rows.Scan(&s.ID, &s.OwnerID); err != nil {
return nil, err
}
out = append(out, s)
}
return out, rows.Err()
}

func (r *VideoRepo) UpdateStatus(ctx context.Context, id string, status model.VideoStatus, renditions []model.Rendition) error {
tx, err := r.pool.Begin(ctx)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions services/transcoder/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.14
17 changes: 17 additions & 0 deletions services/upload/cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ package main
// @name Authorization
// @description Type "Bearer {token}"
import (
"context"
"log"
"net/http"
"os"
"strconv"
"time"

pkghttp "flowix/pkg/httpserver"
pkglogger "flowix/pkg/logger"
Expand All @@ -22,6 +24,7 @@ import (
"flowix/upload/internal/client"
"flowix/upload/internal/handler"
"flowix/upload/internal/queue"
"flowix/upload/internal/reconciler"
"flowix/upload/internal/storage"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
Expand Down Expand Up @@ -73,6 +76,12 @@ func main() {
rh := handler.NewResumableHandler(store, metaCl)
mp := handler.NewMultipartHandler(store, pub, metaCl, metaCl)

// Self-heal videos stuck in 'uploaded': an upload row is created before the
// bytes land, so an upload that never completes hangs forever (see package
// reconciler). Runs for the lifetime of the process.
rec := reconciler.New(metaCl, store, pub, secondsEnv("UPLOAD_STALE_TTL_SECONDS", 3600), 100)
go rec.Run(context.Background(), secondsEnv("UPLOAD_RECONCILE_INTERVAL_SECONDS", 300))

r := chi.NewRouter()
r.Use(middleware.RequestID, middleware.Recoverer, pkgmw.RequestLogger("upload"))

Expand Down Expand Up @@ -119,3 +128,11 @@ func requireEnv(k string) string {
}
return v
}

// secondsEnv reads a positive number of seconds from env, falling back to def.
func secondsEnv(k string, def int) time.Duration {
if n, err := strconv.Atoi(os.Getenv(k)); err == nil && n > 0 {
return time.Duration(n) * time.Second
}
return time.Duration(def) * time.Second
}
Loading
Loading