bcc69f0a2e
Node-agent — full render pipeline (items 1-3):
- render-svc: ClaimedJob now includes aep_download_url (presigned MinIO GET,
2h TTL, path=templates/{original_project_id}/template.aep)
- render-svc: POST /v1/internal/render/jobs/:id/output-upload-url
allocates Export row + returns presigned MinIO PUT URL + export_id
- render-svc: db.CreateExportForJob() inserts export row with 30-day retention
- render-svc: InternalHandler now owns minio client (templatesBucket + exportsBucket)
MINIO_TEMPLATES_BUCKET env var (default flatrender-templates)
- node-agent: runner/download.go — DownloadFile() + UploadFile() (stdlib only)
- node-agent: client.GetOutputUploadURL() + ClaimedJob.AEPDownloadURL field
- node-agent: runJob() full flow: download AEP → render → get upload URL →
PUT output to MinIO → Complete(export_id)
All steps are non-fatal with fallback (AEP miss → mock, upload fail → no export)
TLS reverse proxy (item 15):
- Caddyfile: three virtual hosts (DOMAIN, API_DOMAIN, STORAGE_DOMAIN)
auto-TLS via Let's Encrypt; security headers; 512MB upload limit on API
- docker-compose.v2.yml: caddy:2-alpine service, ports 80/443/443udp,
caddy_data + caddy_config volumes; env vars DOMAIN/API_DOMAIN/STORAGE_DOMAIN/ACME_EMAIL
- .env.v2.example: new Caddy + MINIO_TEMPLATES_BUCKET entries
Billing portal (item 5):
- Identity: POST /v1/users/me/plan/cancel — sets cancelled_at, auto_renew=false
(access continues to expiry); 404 when no active plan
- POST /api/billing/cancel — frontend proxy, validates auth
- GET /api/billing/portal — redirects to /dashboard/settings?tab=billing
- SettingsBilling: "Cancel plan" button with confirm dialog + optimistic UI,
"Change plan" button; becomes "use client" component
Password reset UI (item 7):
- POST /api/auth/password-reset — proxies /v1/auth/password/reset/request
(always 200, anti-enumeration)
- POST /api/auth/password-reset-confirm — proxies /v1/auth/password/reset/confirm
- AuthPageContent: "Forgot password?" link on sign-in tab opens 2-step reset flow
(email → OTP+new-password) without leaving the auth page
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
348 lines
11 KiB
Go
348 lines
11 KiB
Go
// FlatRender V2 Node Agent
|
|
//
|
|
// Runs on every Windows render node. Registers itself with the V2 render
|
|
// orchestrator, sends heartbeats, claims render jobs, executes them via
|
|
// After Effects (or mock), and reports progress / completion back.
|
|
//
|
|
// Required environment variables:
|
|
// NODE_ID — UUID of this node (pre-created in render.render_nodes)
|
|
//
|
|
// Optional environment variables (all have sensible defaults):
|
|
// ORCHESTRATOR_URL — gateway base URL (default: http://localhost:8088)
|
|
// NODE_HMAC_SECRET — shared secret (default: node-secret-change-me)
|
|
// NODE_REGION — region label, e.g. "iran-tehran-1"
|
|
// AE_PATH — path to aerender.exe; empty = mock render
|
|
// WORK_DIR — scratch directory (default: system temp)
|
|
// AGENT_VERSION — semver string (default: 0.1.0)
|
|
// AE_VERSION — AE version string reported to orchestrator (default: 2024)
|
|
// HEARTBEAT_INTERVAL_SEC — seconds between heartbeats (default: 5)
|
|
// POLL_INTERVAL_SEC — seconds between job-claim attempts when idle (default: 3)
|
|
// LISTEN_PORT — port for the health endpoint (default: 7777)
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"runtime"
|
|
"sync"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/flatrender/node-agent/internal/client"
|
|
"github.com/flatrender/node-agent/internal/config"
|
|
"github.com/flatrender/node-agent/internal/runner"
|
|
)
|
|
|
|
// ── Agent state ───────────────────────────────────────────────────────────────
|
|
|
|
type Agent struct {
|
|
cfg *config.Config
|
|
orch *client.Client
|
|
mu sync.Mutex
|
|
currentJob *client.ClaimedJob
|
|
status string // "Ready" | "Busy"
|
|
}
|
|
|
|
func newAgent(cfg *config.Config) *Agent {
|
|
return &Agent{
|
|
cfg: cfg,
|
|
orch: client.New(cfg.OrchestratorURL, cfg.NodeHMACSecret),
|
|
status: "Ready",
|
|
}
|
|
}
|
|
|
|
func (a *Agent) setJob(job *client.ClaimedJob) {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
a.currentJob = job
|
|
if job != nil {
|
|
a.status = "Busy"
|
|
} else {
|
|
a.status = "Ready"
|
|
}
|
|
}
|
|
|
|
func (a *Agent) getStatus() (string, *string) {
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
if a.currentJob != nil {
|
|
jobID := a.currentJob.JobID
|
|
return a.status, &jobID
|
|
}
|
|
return a.status, nil
|
|
}
|
|
|
|
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
|
|
func main() {
|
|
log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds)
|
|
log.Printf("FlatRender Node Agent v%s starting (OS: %s, Arch: %s)",
|
|
"0.1.0", runtime.GOOS, runtime.GOARCH)
|
|
|
|
cfg, err := config.Load()
|
|
if err != nil {
|
|
log.Fatalf("config: %v", err)
|
|
}
|
|
log.Printf("Node ID: %s | Region: %q | Orchestrator: %s",
|
|
cfg.NodeID, cfg.Region, cfg.OrchestratorURL)
|
|
if cfg.AEPath == "" {
|
|
log.Printf("AE_PATH not set — using mock renderer (development mode)")
|
|
} else {
|
|
log.Printf("AE binary: %s", cfg.AEPath)
|
|
}
|
|
|
|
agent := newAgent(cfg)
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
// Graceful shutdown on SIGTERM / SIGINT
|
|
sigs := make(chan os.Signal, 1)
|
|
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
|
|
go func() {
|
|
s := <-sigs
|
|
log.Printf("received %s, shutting down…", s)
|
|
cancel()
|
|
}()
|
|
|
|
// Register as online
|
|
if err := agent.registerOnline(ctx); err != nil {
|
|
log.Fatalf("failed to register with orchestrator: %v", err)
|
|
}
|
|
|
|
// Start health endpoint
|
|
go agent.serveHealth(ctx)
|
|
|
|
// Main loops
|
|
var wg sync.WaitGroup
|
|
wg.Add(2)
|
|
go func() { defer wg.Done(); agent.heartbeatLoop(ctx) }()
|
|
go func() { defer wg.Done(); agent.pollLoop(ctx) }()
|
|
wg.Wait()
|
|
log.Printf("shutdown complete")
|
|
}
|
|
|
|
// ── Registration ──────────────────────────────────────────────────────────────
|
|
|
|
func (a *Agent) registerOnline(ctx context.Context) error {
|
|
req := client.OnlineRequest{
|
|
NodeAgentVersion: a.cfg.AgentVersion,
|
|
CurrentAEVersion: a.cfg.AEVersion,
|
|
AvailableAEVersions: []string{a.cfg.AEVersion},
|
|
CachedTemplateMD5s: []string{},
|
|
}
|
|
if err := a.orch.Online(ctx, a.cfg.NodeID, req); err != nil {
|
|
return fmt.Errorf("online: %w", err)
|
|
}
|
|
log.Printf("registered as online with orchestrator")
|
|
return nil
|
|
}
|
|
|
|
// ── Heartbeat loop ────────────────────────────────────────────────────────────
|
|
|
|
func (a *Agent) heartbeatLoop(ctx context.Context) {
|
|
ticker := time.NewTicker(time.Duration(a.cfg.HeartbeatIntervalSec) * time.Second)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
a.sendHeartbeat(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (a *Agent) sendHeartbeat(ctx context.Context) {
|
|
status, jobID := a.getStatus()
|
|
req := client.HeartbeatRequest{
|
|
NodeID: a.cfg.NodeID,
|
|
Status: status,
|
|
CurrentJobID: jobID,
|
|
}
|
|
hbCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
|
|
resp, err := a.orch.Heartbeat(hbCtx, a.cfg.NodeID, req)
|
|
if err != nil {
|
|
log.Printf("heartbeat error: %v", err)
|
|
return
|
|
}
|
|
if len(resp.PendingCommands) > 0 {
|
|
log.Printf("orchestrator commands: %v", resp.PendingCommands)
|
|
}
|
|
}
|
|
|
|
// ── Job poll loop ─────────────────────────────────────────────────────────────
|
|
|
|
func (a *Agent) pollLoop(ctx context.Context) {
|
|
ticker := time.NewTicker(time.Duration(a.cfg.PollIntervalSec) * time.Second)
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ticker.C:
|
|
status, _ := a.getStatus()
|
|
if status == "Busy" {
|
|
continue // already rendering
|
|
}
|
|
a.tryClaimAndRun(ctx)
|
|
}
|
|
}
|
|
}
|
|
|
|
func (a *Agent) tryClaimAndRun(ctx context.Context) {
|
|
claimCtx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
|
|
job, err := a.orch.ClaimJob(claimCtx, a.cfg.NodeID, a.cfg.Region)
|
|
if err != nil {
|
|
log.Printf("claim error: %v", err)
|
|
return
|
|
}
|
|
if job == nil {
|
|
return // queue empty
|
|
}
|
|
|
|
log.Printf("claimed job %s (project %s, %s %s %dfps)",
|
|
job.JobID, job.SavedProjectID, job.Quality, job.Resolution, job.FrameRate)
|
|
|
|
a.setJob(job)
|
|
go func() {
|
|
defer a.setJob(nil)
|
|
a.runJob(ctx, job)
|
|
}()
|
|
}
|
|
|
|
// ── Render execution ──────────────────────────────────────────────────────────
|
|
|
|
func (a *Agent) runJob(ctx context.Context, job *client.ClaimedJob) {
|
|
log.Printf("[job %s] starting render", job.JobID)
|
|
|
|
// ── Step 1: Download .aep template ───────────────────────────────────────
|
|
aepPath := ""
|
|
if job.AEPDownloadURL != "" && a.cfg.AEPath != "" {
|
|
localAEP := filepath.Join(a.cfg.WorkDir, "templates", job.JobID, "template.aep")
|
|
dlCtx, dlCancel := context.WithTimeout(ctx, 10*time.Minute)
|
|
n, dlErr := runner.DownloadFile(dlCtx, job.AEPDownloadURL, localAEP)
|
|
dlCancel()
|
|
if dlErr != nil {
|
|
log.Printf("[job %s] AEP download failed (%v) — falling back to mock", job.JobID, dlErr)
|
|
} else {
|
|
log.Printf("[job %s] AEP downloaded (%d bytes) → %s", job.JobID, n, localAEP)
|
|
aepPath = localAEP
|
|
}
|
|
}
|
|
|
|
rJob := &runner.Job{
|
|
JobID: job.JobID,
|
|
SavedProjectID: job.SavedProjectID,
|
|
Quality: job.Quality,
|
|
Resolution: job.Resolution,
|
|
FrameRate: job.FrameRate,
|
|
HasMusic: job.HasMusic,
|
|
HasVoiceover: job.HasVoiceover,
|
|
AEPFilePath: aepPath,
|
|
}
|
|
|
|
onProgress := func(ctx context.Context, pct int, msg string) error {
|
|
log.Printf("[job %s] %d%% %s", job.JobID, pct, msg)
|
|
return nil
|
|
}
|
|
|
|
onPreview := func(ctx context.Context, imageB64 string) error {
|
|
pvCtx, cancel := context.WithTimeout(ctx, 8*time.Second)
|
|
defer cancel()
|
|
if err := a.orch.UpdatePreview(pvCtx, job.JobID, imageB64); err != nil {
|
|
log.Printf("[job %s] preview push error: %v", job.JobID, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ── Step 2: Render ───────────────────────────────────────────────────────
|
|
outputPath, err := runner.Run(ctx, a.cfg.AEPath, a.cfg.WorkDir, rJob, onProgress, onPreview)
|
|
if err != nil {
|
|
if ctx.Err() != nil {
|
|
log.Printf("[job %s] render cancelled", job.JobID)
|
|
return
|
|
}
|
|
log.Printf("[job %s] render failed: %v", job.JobID, err)
|
|
failCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
if ferr := a.orch.Fail(failCtx, job.JobID, err.Error(), "Rendering"); ferr != nil {
|
|
log.Printf("[job %s] fail report error: %v", job.JobID, ferr)
|
|
}
|
|
return
|
|
}
|
|
log.Printf("[job %s] render done → %s", job.JobID, outputPath)
|
|
|
|
// ── Step 3: Get presigned upload URL + upload output to MinIO ─────────────
|
|
var exportID *string
|
|
uploadCtx, uploadCancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
defer uploadCancel()
|
|
|
|
uploadInfo, urlErr := a.orch.GetOutputUploadURL(uploadCtx, job.JobID)
|
|
if urlErr != nil {
|
|
log.Printf("[job %s] get upload URL failed: %v — completing without export", job.JobID, urlErr)
|
|
} else {
|
|
log.Printf("[job %s] uploading output to %s", job.JobID, uploadInfo.ObjectKey)
|
|
if _, upErr := runner.UploadFile(uploadCtx, uploadInfo.UploadURL, outputPath); upErr != nil {
|
|
log.Printf("[job %s] upload failed: %v — completing without export", job.JobID, upErr)
|
|
} else {
|
|
log.Printf("[job %s] upload complete (export %s)", job.JobID, uploadInfo.ExportID)
|
|
exportID = &uploadInfo.ExportID
|
|
}
|
|
}
|
|
|
|
// ── Step 4: Report complete ───────────────────────────────────────────────
|
|
completeCtx, completeCancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer completeCancel()
|
|
if err := a.orch.Complete(completeCtx, job.JobID, exportID); err != nil {
|
|
log.Printf("[job %s] complete report error: %v", job.JobID, err)
|
|
} else {
|
|
log.Printf("[job %s] reported as completed (export=%v)", job.JobID, exportID)
|
|
}
|
|
}
|
|
|
|
// ── Health endpoint ───────────────────────────────────────────────────────────
|
|
|
|
func (a *Agent) serveHealth(ctx context.Context) {
|
|
mux := http.NewServeMux()
|
|
mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
status, jobID := a.getStatus()
|
|
w.Header().Set("Content-Type", "application/json")
|
|
resp := map[string]any{
|
|
"ok": true,
|
|
"node_id": a.cfg.NodeID,
|
|
"status": status,
|
|
"current_job": jobID,
|
|
"version": a.cfg.AgentVersion,
|
|
}
|
|
_ = json.NewEncoder(w).Encode(resp)
|
|
})
|
|
|
|
srv := &http.Server{
|
|
Addr: fmt.Sprintf(":%d", a.cfg.ListenPort),
|
|
Handler: mux,
|
|
}
|
|
|
|
go func() {
|
|
<-ctx.Done()
|
|
_ = srv.Shutdown(context.Background())
|
|
}()
|
|
|
|
log.Printf("health endpoint listening on :%d", a.cfg.ListenPort)
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Printf("health server error: %v", err)
|
|
}
|
|
}
|