FEAT: Update Status Of Video Row On Media Convert Update

This commit is contained in:
FahdShalhoub
2026-08-27 20:04:38 +03:00
parent 5d35d4029e
commit 3f5301afb6
25 changed files with 1444 additions and 14 deletions
+103
View File
@@ -157,6 +157,47 @@ const docTemplate = `{
}
}
},
"/api/videos/{id}": {
"get": {
"description": "Returns the catalogue record for one video, including the status its transcoding job has reached. This is how a client follows an upload after POST /api/videos: the record starts \"processing\" and moves to \"ready\" or \"failed\" once MediaConvert reports the job finished.",
"produces": [
"application/json"
],
"tags": [
"videos"
],
"summary": "Read a video",
"parameters": [
{
"type": "string",
"description": "The video's id, as returned by POST /api/videos",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api.VideoResponse"
}
},
"404": {
"description": "No video has that id",
"schema": {
"$ref": "#/definitions/api.ProblemDetails"
}
},
"500": {
"description": "The video could not be read from the database",
"schema": {
"$ref": "#/definitions/api.ProblemDetails"
}
}
}
}
},
"/health": {
"get": {
"description": "Reports that the service is up and serving. Used as the ALB target group health check; it does not verify the database or S3 connections.",
@@ -359,6 +400,68 @@ const docTemplate = `{
"example": "about:blank"
}
}
},
"api.VideoResponse": {
"type": "object",
"properties": {
"categoryIds": {
"type": "array",
"items": {
"type": "integer"
},
"example": [
1,
2
]
},
"createdAt": {
"type": "string"
},
"description": {
"type": "string",
"example": "A behind-the-scenes look at the evening bulletin."
},
"fileName": {
"type": "string",
"example": "interview-cut.mov"
},
"id": {
"type": "string",
"example": "0199f3a1-7c2e-7b21-9f0d-1a2b3c4d5e6f"
},
"mediaConvertJobId": {
"type": "string",
"example": "1755300000000-abcdef"
},
"sizeBytes": {
"type": "integer",
"example": 60
},
"status": {
"type": "string",
"enum": [
"processing",
"ready",
"failed"
],
"example": "processing"
},
"storageKey": {
"type": "string",
"example": "videos/a1b2c3d4e5f6.mov"
},
"tags": {
"type": "string",
"example": "media, press, riyadh"
},
"title": {
"type": "string",
"example": "Inside the Newsroom"
},
"updatedAt": {
"type": "string"
}
}
}
}
}`
+103
View File
@@ -150,6 +150,47 @@
}
}
},
"/api/videos/{id}": {
"get": {
"description": "Returns the catalogue record for one video, including the status its transcoding job has reached. This is how a client follows an upload after POST /api/videos: the record starts \"processing\" and moves to \"ready\" or \"failed\" once MediaConvert reports the job finished.",
"produces": [
"application/json"
],
"tags": [
"videos"
],
"summary": "Read a video",
"parameters": [
{
"type": "string",
"description": "The video's id, as returned by POST /api/videos",
"name": "id",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/api.VideoResponse"
}
},
"404": {
"description": "No video has that id",
"schema": {
"$ref": "#/definitions/api.ProblemDetails"
}
},
"500": {
"description": "The video could not be read from the database",
"schema": {
"$ref": "#/definitions/api.ProblemDetails"
}
}
}
}
},
"/health": {
"get": {
"description": "Reports that the service is up and serving. Used as the ALB target group health check; it does not verify the database or S3 connections.",
@@ -352,6 +393,68 @@
"example": "about:blank"
}
}
},
"api.VideoResponse": {
"type": "object",
"properties": {
"categoryIds": {
"type": "array",
"items": {
"type": "integer"
},
"example": [
1,
2
]
},
"createdAt": {
"type": "string"
},
"description": {
"type": "string",
"example": "A behind-the-scenes look at the evening bulletin."
},
"fileName": {
"type": "string",
"example": "interview-cut.mov"
},
"id": {
"type": "string",
"example": "0199f3a1-7c2e-7b21-9f0d-1a2b3c4d5e6f"
},
"mediaConvertJobId": {
"type": "string",
"example": "1755300000000-abcdef"
},
"sizeBytes": {
"type": "integer",
"example": 60
},
"status": {
"type": "string",
"enum": [
"processing",
"ready",
"failed"
],
"example": "processing"
},
"storageKey": {
"type": "string",
"example": "videos/a1b2c3d4e5f6.mov"
},
"tags": {
"type": "string",
"example": "media, press, riyadh"
},
"title": {
"type": "string",
"example": "Inside the Newsroom"
},
"updatedAt": {
"type": "string"
}
}
}
}
}
+75
View File
@@ -129,6 +129,51 @@ definitions:
example: about:blank
type: string
type: object
api.VideoResponse:
properties:
categoryIds:
example:
- 1
- 2
items:
type: integer
type: array
createdAt:
type: string
description:
example: A behind-the-scenes look at the evening bulletin.
type: string
fileName:
example: interview-cut.mov
type: string
id:
example: 0199f3a1-7c2e-7b21-9f0d-1a2b3c4d5e6f
type: string
mediaConvertJobId:
example: 1755300000000-abcdef
type: string
sizeBytes:
example: 60
type: integer
status:
enum:
- processing
- ready
- failed
example: processing
type: string
storageKey:
example: videos/a1b2c3d4e5f6.mov
type: string
tags:
example: media, press, riyadh
type: string
title:
example: Inside the Newsroom
type: string
updatedAt:
type: string
type: object
info:
contact: {}
description: 'JSON API for ingesting videos into the Thamanyah catalogue. Uploads
@@ -208,6 +253,36 @@ paths:
summary: Register an uploaded video
tags:
- videos
/api/videos/{id}:
get:
description: 'Returns the catalogue record for one video, including the status
its transcoding job has reached. This is how a client follows an upload after
POST /api/videos: the record starts "processing" and moves to "ready" or "failed"
once MediaConvert reports the job finished.'
parameters:
- description: The video's id, as returned by POST /api/videos
in: path
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/api.VideoResponse'
"404":
description: No video has that id
schema:
$ref: '#/definitions/api.ProblemDetails'
"500":
description: The video could not be read from the database
schema:
$ref: '#/definitions/api.ProblemDetails'
summary: Read a video
tags:
- videos
/api/videos/presign:
post:
consumes:
+6 -4
View File
@@ -3,11 +3,13 @@ module thamanyah/cms/v2
go 1.25.12
require (
github.com/aws/aws-sdk-go-v2 v1.43.5
github.com/aws/aws-sdk-go-v2 v1.44.0
github.com/aws/aws-sdk-go-v2/config v1.32.36
github.com/aws/aws-sdk-go-v2/service/mediaconvert v1.97.2
github.com/aws/aws-sdk-go-v2/service/s3 v1.107.1
github.com/aws/aws-sdk-go-v2/service/sqs v1.47.0
github.com/golang-migrate/migrate/v4 v4.19.1
github.com/google/uuid v1.6.0
github.com/lib/pq v1.12.3
github.com/swaggo/http-swagger/v2 v2.0.2
github.com/swaggo/swag v1.16.6
@@ -18,8 +20,8 @@ require (
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.17 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.19.35 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.36 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.36 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.36 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.40 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.40 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.37 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.16 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.29 // indirect
@@ -29,7 +31,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/sso v1.33.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.38.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.45.5 // indirect
github.com/aws/smithy-go v1.27.7 // indirect
github.com/aws/smithy-go v1.28.1 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/spec v0.20.6 // indirect
+12 -8
View File
@@ -4,8 +4,8 @@ github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/aws/aws-sdk-go-v2 v1.43.5 h1:yKT5GYnFWhuDo+DqKvE5ZPwVn3RjC4MAeBtZGlh6AVM=
github.com/aws/aws-sdk-go-v2 v1.43.5/go.mod h1:wZjAJppCntyOGgVSmgVTfDyRJK5PHOasO6Wsy8U7Axk=
github.com/aws/aws-sdk-go-v2 v1.44.0 h1:4IbaHhtzy+4h37z4JQyO9a2QsiCml3CNYHtq5hIHigo=
github.com/aws/aws-sdk-go-v2 v1.44.0/go.mod h1:bttEH6JqnUL8LepvDVfdrds/fZ5bCIxzpe3abyUrhDU=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.17 h1:mn+Vxb9zgz/FE/yDTcFim3DZ1qpcrxR+qBQkBrl6bzA=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.17/go.mod h1:eDfmEFxu+BSVsUGLbzJhWjpOurv1mqczClS97yI8wdk=
github.com/aws/aws-sdk-go-v2/config v1.32.36 h1:mX6ietU7UlB4w/2IUaexJdsyUDvhTd+jYPjVePiyi6s=
@@ -14,10 +14,10 @@ github.com/aws/aws-sdk-go-v2/credentials v1.19.35 h1:Cxua2RVdRwL0sfjHM/SnQoOnQ7x
github.com/aws/aws-sdk-go-v2/credentials v1.19.35/go.mod h1:9XQ+RSIGPkycr+oCJYnB1uTv5kMVVR+rd2vYK0Hxj2w=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.36 h1:gucL1KH/PAYbpTpBg09CiVpBdTu4qkCl8C7xOTBixUg=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.36/go.mod h1:usTB+PHhNMhrx2dxUeHcM7OrT5pySvmjYI++IsefPN0=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.36 h1:5CrzwxDqf4w3x1Vs3/NiZ0nsC34Hbm3pIDMWbsLebOE=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.36/go.mod h1:A3gHdKZIvG/QXERzZwcxNS3RNDFcRCuhhTFBYp+V/nw=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.36 h1:A4N2f4YPcST0v+dWtX+xrpPPCL9VTBhoIFFUWYqbacE=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.36/go.mod h1:B/Qr859uxWUEfZeGotK5KAEoof4Q9YWgNtPSwV6jcyk=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.40 h1:UIXlbijuB2XK1Kr57fo8iIxCuaSHJzwZ1uo+2tbEYIk=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.40/go.mod h1:wcEsL6jscjZjVUinb0Q5qD/GXOG1yT3GNfmT9HuDwzU=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.40 h1:xLQVRDs2NddDmK9BEyh5KSlJ1Gpy5/GIJXrV6WcVGAE=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.40/go.mod h1:XRXnpFVFGLaEVK+olDdFIM1vNa04ETW452oFGEPUxAo=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.37 h1:oyd3ke4V9AhKcRR7rRgxk1VyI+DjK2CBQtbxh3OkdaA=
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.37/go.mod h1:aA9D7SqfG9IC1b7FLD7Iyc8Q4JN0a8gHhNjN4zPlIaI=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.16 h1:iE4NGbvqUZnHDqddQAauZzCILYtFjOHwRM5MOOKLB5A=
@@ -34,14 +34,16 @@ github.com/aws/aws-sdk-go-v2/service/s3 v1.107.1 h1:VUTtUJMuRNMkb/7NIKmd8NQaeQLP
github.com/aws/aws-sdk-go-v2/service/s3 v1.107.1/go.mod h1:WvUaO0lP5GNMs1R6cs6qvB3mqo16GLta8yfOuf55Rpc=
github.com/aws/aws-sdk-go-v2/service/signin v1.5.5 h1:0VTFBfOgPJrUSpGMgzoi8qLcXF5dbmiBuxpo14eBWUw=
github.com/aws/aws-sdk-go-v2/service/signin v1.5.5/go.mod h1:sNZYlBxoohYMBYl47BO/bFtAM6I8HSsPa1qwwPPRGoQ=
github.com/aws/aws-sdk-go-v2/service/sqs v1.47.0 h1:vNsYthHgT4sUo0KVqpkZlz+8ZDqy/MdlqdvZdP6IoAc=
github.com/aws/aws-sdk-go-v2/service/sqs v1.47.0/go.mod h1:FSB4mnod1TCBhs3vp2tWVVGHbqxluzA0Fo6LBOXZByw=
github.com/aws/aws-sdk-go-v2/service/sso v1.33.5 h1:jDQARFp1mJ2PEnllQf01nfFXGfWMJ59e0/HCHUTTZCk=
github.com/aws/aws-sdk-go-v2/service/sso v1.33.5/go.mod h1:OcT2AhgTuxGAwZk5hgxaNLGpS33W8s8dUQadGVDVY9I=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.38.5 h1:8xo1q9ttkYqMJ6vOXX67FPSpVEI7BWKVTKh77g82w+8=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.38.5/go.mod h1:hbBeEUrZg6VddXYZpbKPyF0tl4XEnM+Dbx92RW3vmZI=
github.com/aws/aws-sdk-go-v2/service/sts v1.45.5 h1:eQ5BtXDrPg2wK0AjtVPzeBhUpYPeqHE/ptiH7xJRGek=
github.com/aws/aws-sdk-go-v2/service/sts v1.45.5/go.mod h1:f9ImhnOISY7BuTZLM8qHepCYnglHBVLk5wVzatmP++w=
github.com/aws/smithy-go v1.27.7 h1:Zgj5z4LfcDYoQIVk+n/yGdTkP/2y6ZT5vYxe0fp7bqE=
github.com/aws/smithy-go v1.27.7/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
github.com/aws/smithy-go v1.28.1 h1:R/nXH00c8qcfCzQVELtRw+eLQWtzv+VAIEFJ1/xxXlQ=
github.com/aws/smithy-go v1.28.1/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE=
@@ -83,6 +85,8 @@ github.com/golang-migrate/migrate/v4 v4.19.1 h1:OCyb44lFuQfYXYLx1SCxPZQGU7mcaZ7g
github.com/golang-migrate/migrate/v4 v4.19.1/go.mod h1:CTcgfjxhaUtsLipnLoQRWCrjYXycRz/g5+RWDuYgPrE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
+19
View File
@@ -55,3 +55,22 @@ type CompleteResponse struct {
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
// VideoResponse is a persisted video record as returned by
// GET /api/videos/{id}. It carries the same members as CompleteResponse —
// deliberately a separate type, so the shape POST publishes and the shape GET
// publishes can diverge without one silently dragging the other with it.
type VideoResponse struct {
ID string `json:"id" example:"0199f3a1-7c2e-7b21-9f0d-1a2b3c4d5e6f"`
Title string `json:"title" example:"Inside the Newsroom"`
Description string `json:"description" example:"A behind-the-scenes look at the evening bulletin."`
CategoryIDs []int16 `json:"categoryIds" example:"1,2"`
Tags string `json:"tags" example:"media, press, riyadh"`
FileName string `json:"fileName" example:"interview-cut.mov"`
StorageKey string `json:"storageKey" example:"videos/a1b2c3d4e5f6.mov"`
MediaConvertJobID string `json:"mediaConvertJobId" example:"1755300000000-abcdef"`
Status string `json:"status" enums:"processing,ready,failed" example:"processing"`
SizeBytes int64 `json:"sizeBytes" example:"60"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
+135
View File
@@ -0,0 +1,135 @@
// Package consumers holds the queue-driven half of the service: work that
// arrives on a queue rather than as an HTTP request. It is to SQS what
// internal/handlers is to HTTP — it decodes a message, calls repositories, and
// decides what the message's fate is — so the same seams apply, and no SQL
// lives here.
package consumers
import (
"context"
"encoding/json"
"errors"
"log"
"thamanyah/cms/v2/internal/db/repositories"
"thamanyah/cms/v2/internal/models"
"thamanyah/cms/v2/internal/services"
"time"
)
// receiveBackoff is how long the loop waits after a failed receive, so a queue
// that is unreachable produces a slow trickle of log lines rather than a spin.
const receiveBackoff = 5 * time.Second
// jobStateChange is the EventBridge event MediaConvert emits on every job
// state transition. The subscription delivers it raw, so this is the whole
// message body — there is no SNS envelope to unwrap. Only the members this
// service acts on are modelled; AWS sends a good deal more.
type jobStateChange struct {
Source string `json:"source"`
DetailType string `json:"detail-type"`
Detail struct {
JobID string `json:"jobId"`
Status string `json:"status"`
} `json:"detail"`
}
// statusForJobState maps MediaConvert's job states onto the catalogue's. The
// second return is false for states that are not an outcome — the job is still
// running, and the record should stay where it is rather than being rewritten
// with what it already says.
func statusForJobState(state string) (models.VideoStatus, bool) {
switch state {
case "COMPLETE":
return models.VideoStatusReady, true
case "ERROR", "CANCELED":
return models.VideoStatusFailed, true
default:
return "", false
}
}
// RunMediaConvertEvents consumes job state changes until ctx is cancelled. It
// is meant to be run in its own goroutine for the lifetime of the process.
func RunMediaConvertEvents(ctx context.Context) {
log.Println("mediaconvert job events consumer started")
for {
if ctx.Err() != nil {
log.Println("mediaconvert job events consumer stopped")
return
}
messages, err := services.SQSClient.ReceiveMessages(ctx)
if err != nil {
if ctx.Err() != nil {
log.Println("mediaconvert job events consumer stopped")
return
}
log.Printf("Something Went Wrong Receiving Job Events: %s", err)
time.Sleep(receiveBackoff)
continue
}
for _, message := range messages {
if !handleJobEvent(ctx, message.Body) {
// Left on the queue on purpose: it becomes visible again when
// the visibility timeout expires, and reaches the dead-letter
// queue if it keeps failing.
continue
}
if err := services.SQSClient.DeleteMessage(ctx, message.ReceiptHandle); err != nil {
log.Printf("Something Went Wrong Acknowledging A Job Event: %s", err)
}
}
}
}
// handleJobEvent processes one message and reports whether it is finished
// with — that is, whether it should be deleted from the queue. It returns
// false only when a retry could plausibly succeed. Anything a retry cannot fix
// (a body that will never parse, an event for a job nobody has) is finished
// with, however little it accomplished: leaving it on the queue would only
// stall the events behind it.
func handleJobEvent(ctx context.Context, body string) bool {
var event jobStateChange
if err := json.Unmarshal([]byte(body), &event); err != nil {
log.Printf("Discarding A Job Event That Is Not JSON: %s", err)
return true
}
if event.Source != "aws.mediaconvert" || event.DetailType != "MediaConvert Job State Change" {
log.Printf("Discarding An Event That Is Not A MediaConvert Job State Change: source=%q detail-type=%q",
event.Source, event.DetailType)
return true
}
if event.Detail.JobID == "" {
log.Printf("Discarding A Job Event That Names No Job")
return true
}
status, isOutcome := statusForJobState(event.Detail.Status)
if !isOutcome {
// SUBMITTED, PROGRESSING and STATUS_UPDATE say the job is still
// running, which is what "processing" already records.
return true
}
err := repositories.VideoRepo.UpdateVideoStatusByJobID(ctx, event.Detail.JobID, status)
if errors.Is(err, repositories.ErrVideoNotFound) {
// Not a fault: the topic carries every job in the account, including
// ones this service never submitted.
log.Printf("Ignoring A Job Event For A Job No Video Has: job=%q status=%q",
event.Detail.JobID, event.Detail.Status)
return true
}
if err != nil {
log.Printf("Something Went Wrong Recording A Job Outcome: job=%q status=%q: %s",
event.Detail.JobID, event.Detail.Status, err)
return false
}
log.Printf("Recorded A Job Outcome: job=%q status=%q", event.Detail.JobID, status)
return true
}
@@ -0,0 +1 @@
DROP INDEX videos_mediaconvert_job_id_idx;
@@ -0,0 +1,4 @@
-- Job state changes arrive keyed by MediaConvert's job id, so every event the
-- consumer handles is a lookup on this column. Nothing indexed it before: the
-- column was only ever written, never searched.
CREATE INDEX videos_mediaconvert_job_id_idx ON videos (mediaconvert_job_id);
+9
View File
@@ -0,0 +1,9 @@
package repositories
import "errors"
// Sentinel errors the repositories return so handlers can tell "there is no
// such row" from "the query failed", without importing database/sql to do it.
// ErrVideoNotFound reports that no video has the requested id.
var ErrVideoNotFound = errors.New("video not found")
+61
View File
@@ -3,6 +3,7 @@ package repositories
import (
"context"
"database/sql"
"errors"
"fmt"
"strings"
"thamanyah/cms/v2/internal/models"
@@ -14,6 +15,66 @@ type VideoRepository struct {
var VideoRepo VideoRepository
// UpdateVideoStatusByJobID moves the video a transcoding job belongs to into a
// new status. It returns ErrVideoNotFound when no video carries that job id,
// which the consumer treats as a message to drop rather than a fault.
func (svc VideoRepository) UpdateVideoStatusByJobID(ctx context.Context, jobID string, status models.VideoStatus) error {
result, err := svc.SQLDB.ExecContext(ctx, `
UPDATE videos
SET status = $1, updated_at = now()
WHERE mediaconvert_job_id = $2
`, string(status), jobID)
if err != nil {
return err
}
affected, err := result.RowsAffected()
if err != nil {
return err
}
if affected == 0 {
return ErrVideoNotFound
}
return nil
}
// GetVideoByID reads one video and the ids of the categories it is filed
// under. It returns ErrVideoNotFound when no video has that id.
func (svc VideoRepository) GetVideoByID(ctx context.Context, id string) (models.Video, error) {
var v models.Video
err := svc.SQLDB.QueryRowContext(ctx, `
SELECT id, title, description, tags, file_name, storage_key, mediaconvert_job_id, status, size_bytes, created_at, updated_at
FROM videos
WHERE id = $1
`, id).Scan(&v.ID, &v.Title, &v.Description, &v.Tags, &v.FileName, &v.StorageKey,
&v.MediaConvertJobID, &v.Status, &v.SizeBytes, &v.CreatedAt, &v.UpdatedAt)
if errors.Is(err, sql.ErrNoRows) {
return models.Video{}, ErrVideoNotFound
}
if err != nil {
return models.Video{}, err
}
rows, err := svc.SQLDB.QueryContext(ctx,
`SELECT category_id FROM video_categories WHERE video_id = $1 ORDER BY category_id`, v.ID)
if err != nil {
return models.Video{}, err
}
defer rows.Close()
for rows.Next() {
var categoryID int16
if err := rows.Scan(&categoryID); err != nil {
return models.Video{}, err
}
v.CategoryIDs = append(v.CategoryIDs, categoryID)
}
return v, rows.Err()
}
// VideoExistsWithStorageKey reports whether a video has already been
// registered under a storage key. It is served by videos_storage_key_key, the
// index Postgres builds for the column's UNIQUE constraint.
+61
View File
@@ -17,6 +17,8 @@ import (
"thamanyah/cms/v2/internal/models"
"thamanyah/cms/v2/internal/services"
"time"
"github.com/google/uuid"
)
const (
@@ -253,3 +255,62 @@ func randomFilename(original string) (string, error) {
}
return hex.EncodeToString(buf) + ext, nil
}
// GetVideo returns one video by its id.
//
// @Summary Read a video
// @Description Returns the catalogue record for one video, including the status its transcoding job has reached. This is how a client follows an upload after POST /api/videos: the record starts "processing" and moves to "ready" or "failed" once MediaConvert reports the job finished.
// @Tags videos
// @Produce json
// @Param id path string true "The video's id, as returned by POST /api/videos"
// @Success 200 {object} api.VideoResponse
// @Failure 404 {object} api.ProblemDetails "No video has that id"
// @Failure 500 {object} api.ProblemDetails "The video could not be read from the database"
// @Router /api/videos/{id} [get]
func GetVideo(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
// Parsed before the query, because Postgres rejects anything that is not a
// uuid the moment it is compared against the column rather than simply
// matching no row — which would surface as a 500. An id that cannot name a
// video is a video that does not exist.
//
// The parsed value, not the raw one, is what goes to the database: uuid
// accepts spellings Postgres will not take verbatim — notably the
// "urn:uuid:" form — and String() puts them all back into the canonical
// form the column holds.
videoID, err := uuid.Parse(id)
if err != nil {
writeProblem(w, http.StatusNotFound, "Video Not Found",
fmt.Sprintf("No video has the id %q. Use the 'id' returned by POST /api/videos.", id))
return
}
video, err := repositories.VideoRepo.GetVideoByID(r.Context(), videoID.String())
if errors.Is(err, repositories.ErrVideoNotFound) {
writeProblem(w, http.StatusNotFound, "Video Not Found",
fmt.Sprintf("No video has the id %q. Use the 'id' returned by POST /api/videos.", id))
return
}
if err != nil {
log.Printf("Something Went Wrong Loading The Video: %s", err)
writeProblem(w, http.StatusInternalServerError, "Something Went Wrong Loading The Video",
"The video could not be read from the database. This is a server-side fault and the request was not processed; retrying in a few moments may succeed.")
return
}
writeJSON(w, http.StatusOK, api.VideoResponse{
ID: video.ID,
Title: video.Title,
Description: video.Description,
CategoryIDs: video.CategoryIDs,
Tags: video.Tags,
FileName: video.FileName,
StorageKey: video.StorageKey,
MediaConvertJobID: video.MediaConvertJobID,
Status: video.Status.String(),
SizeBytes: video.SizeBytes,
CreatedAt: video.CreatedAt,
UpdatedAt: video.UpdatedAt,
})
}
+80
View File
@@ -0,0 +1,80 @@
package services
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/service/sqs"
)
var SQSClient SQS
// QueueMessage is one message taken off the queue. ReceiptHandle is what
// identifies it for deletion — it belongs to this delivery, not to the
// message, so it cannot be held across receives.
type QueueMessage struct {
Body string
ReceiptHandle string
}
type SQS interface {
ReceiveMessages(ctx context.Context) ([]QueueMessage, error)
DeleteMessage(ctx context.Context, receiptHandle string) error
}
type SQSConcrete struct {
SQSClient *sqs.Client
QueueURL string
}
// receiveWaitSeconds turns every receive into a long poll: the call parks on
// the server until a message arrives or this elapses, so an event is picked up
// within milliseconds of being published while an idle consumer costs one
// request every twenty seconds rather than spinning. 20 is the AWS maximum.
const receiveWaitSeconds = 20
func (svc SQSConcrete) ReceiveMessages(ctx context.Context) ([]QueueMessage, error) {
output, err := svc.SQSClient.ReceiveMessage(ctx, &sqs.ReceiveMessageInput{
QueueUrl: &svc.QueueURL,
MaxNumberOfMessages: 10,
WaitTimeSeconds: receiveWaitSeconds,
})
if err != nil {
return nil, err
}
messages := make([]QueueMessage, 0, len(output.Messages))
for _, message := range output.Messages {
if message.Body == nil || message.ReceiptHandle == nil {
continue
}
messages = append(messages, QueueMessage{
Body: *message.Body,
ReceiptHandle: *message.ReceiptHandle,
})
}
return messages, nil
}
// DeleteMessage acknowledges a message. Until this is called the message is
// merely invisible, and it returns to the queue when its visibility timeout
// expires — which is how a handler that failed gets another attempt.
func (svc SQSConcrete) DeleteMessage(ctx context.Context, receiptHandle string) error {
_, err := svc.SQSClient.DeleteMessage(ctx, &sqs.DeleteMessageInput{
QueueUrl: &svc.QueueURL,
ReceiptHandle: &receiptHandle,
})
return err
}
// AssertSuccessfulConnection verifies the queue is reachable and readable with
// the configured credentials. It panics on failure, since a service whose
// consumer cannot start would silently leave every video stuck "processing".
func (svc SQSConcrete) AssertSuccessfulConnection(ctx context.Context) {
if _, err := svc.SQSClient.GetQueueAttributes(ctx, &sqs.GetQueueAttributesInput{
QueueUrl: &svc.QueueURL,
}); err != nil {
panic(fmt.Errorf("sqs: cannot connect to queue %q: %w", svc.QueueURL, err))
}
}
+24
View File
@@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"thamanyah/cms/v2/internal/consumers"
"thamanyah/cms/v2/internal/db"
"thamanyah/cms/v2/internal/db/repositories"
"thamanyah/cms/v2/internal/handlers"
@@ -14,6 +15,7 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/mediaconvert"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/sqs"
_ "github.com/lib/pq"
httpSwagger "github.com/swaggo/http-swagger/v2"
@@ -66,6 +68,11 @@ func runServer() {
panic(fmt.Errorf("missing required env var: MEDIACONVERT_OUTPUT_BUCKET"))
}
mediaConvertEventsQueueURL := os.Getenv("MEDIACONVERT_EVENTS_QUEUE_URL")
if mediaConvertEventsQueueURL == "" {
panic(fmt.Errorf("missing required env var: MEDIACONVERT_EVENTS_QUEUE_URL"))
}
// AWS_ENDPOINT_URL is only ever set when pointing at something other than
// real S3 — LocalStack, in docker-compose. Virtual-host addressing would
// resolve <bucket>.<endpoint host> there, which neither Docker's DNS nor
@@ -93,6 +100,14 @@ func runServer() {
OutputBucket: mediaConvertOutputBucket,
}
concreteSQSClient := &services.SQSConcrete{
SQSClient: sqs.NewFromConfig(awsConfig),
QueueURL: mediaConvertEventsQueueURL,
}
concreteSQSClient.AssertSuccessfulConnection(context.Background())
services.SQSClient = concreteSQSClient
concreteDBClient := db.CreateDBConnection(requireDBConnectionString())
defer db.CloseConnection(concreteDBClient)
db.AssertSuccessfulConnection(context.Background(), concreteDBClient)
@@ -105,12 +120,21 @@ func runServer() {
SQLDB: concreteDBClient,
}
// MediaConvert reports job state changes to a topic that fans out to this
// service's queue; the consumer runs alongside the HTTP server for the
// lifetime of the process, so a video's status catches up with its
// transcoding job without anything calling back into this service.
consumerCtx, stopConsumer := context.WithCancel(context.Background())
defer stopConsumer()
go consumers.RunMediaConvertEvents(consumerCtx)
mux := http.NewServeMux()
mux.HandleFunc("GET /health", handlers.Health)
mux.HandleFunc("GET /api/categories", handlers.ListCategories)
mux.HandleFunc("POST /api/videos/presign", handlers.PresignVideoUpload)
mux.HandleFunc("POST /api/videos", handlers.CompleteVideoUpload)
mux.HandleFunc("GET /api/videos/{id}", handlers.GetVideo)
// Swagger UI and the generated spec. The UI assets are embedded in the
// binary by swaggo/files, so this needs no static directory on disk.