Skip to content

feat(render): add render worker role and cached OG image pipeline - #393

Open
Anshuu2004 wants to merge 2 commits into
VarshithReddy2006:mainfrom
Anshuu2004:feat/issue-352a-og-render-worker
Open

Anshuu2004 wants to merge 2 commits into
VarshithReddy2006:mainfrom
Anshuu2004:feat/issue-352a-og-render-worker

Conversation

@Anshuu2004

Copy link
Copy Markdown
Contributor

Summary

First half of #352 (worker-render pool). Adds the render worker role and the dynamic OG image pipeline: GET /api/v1/profile/public/:username/og.png returns a 1200x630 PNG drawn with satori + resvg, cached by content hash in a new RenderArtifact collection. The API process never renders, it queues a job on domain-og-render and the render worker does the drawing.

Refs #352 (part 1 of 2). The dossier PDF worker and the signed object-storage URLs are the second PR, so this one does not close the issue.

What changed

  • render worker role in worker-runtime.ts (also part of all), consuming domain-og-render. Added npm run worker:render, a worker-render service in docker-compose.prod.yml, and docs in ARCHITECTURE.md / DEPLOYMENT.md.
  • RenderArtifact model: unique artifactKey, userId, type (og_image | pdf_dossier), contentHash, status, storageUrl, expiresAt (TTL index). OG cards are a few tens of KB so the PNG is stored inline in the row for now (content, not selected by default). One row per profile (og_image:<userId>), updated in place when the content changes.
  • OG pipeline in modules/profile/og/: card data, a satori element tree (plain objects, no JSX toolchain) and the satori -> resvg renderer. The card only contains what the public profile API would show: it uses the same section + privacy rule as the DTO (I moved that rule into visibleSections() so both share it). Avatar is a monogram, not the remote avatar, because the worker should not fetch arbitrary URLs.
  • Content-hash cache: renderOgArtifact() rebuilds the card from the DB, hashes it, and returns the stored artifact without calling the renderer when the hash matches an unexpired ready row.
  • Route + controller (ogImage.controller.ts): looks up the published profile, serves the cached PNG if the hash matches, otherwise queues one job (job id contains the hash so concurrent requests share it) and polls the artifact for up to 8s. No worker running -> 503 + Retry-After: 5. ETag / 304 is answered only after the visibility check.
  • Opt-in: unknown handle and unpublished profile go through the same commonErrors.notFound call, so the body is identical. seo.ogImageUrl in the public profile API and og:image in the /u/:username shell are set only for published profiles. The shell needs an absolute API origin, so there is a new optional PUBLIC_API_URL (dev default http://localhost:<PORT>, unset in prod keeps the static preview image).
  • Unpublishing also deletes the stored card.
  • satori and resvg load lazily, only inside a render. There is a test that importing the routes/controller/service does not load either package.
  • New deps: satori ^0.63.2 and @resvg/resvg-js ^2.6.2 (both MPL-2.0). Lockfile diff is additive only (generated with --package-lock-only).
  • Font: Inter Regular + Bold, Latin subset, WOFF (about 30 KB each), from the @fontsource/inter 5.3.0 package, licensed SIL Open Font License 1.1. Files and the license text are in backend/assets/fonts/; the Dockerfile copies assets/ into the runtime image. Because it is a Latin subset, characters outside it are dropped from names (name falls back to the handle if nothing is left).

How I tested

Run one file at a time from backend/ (the suite uses mongodb-memory-server and the mocked ioredis/bullmq from vitest.setup.ts, no real Redis or Mongo):

npx vitest run src/__tests__/modules/profile/og-image.test.ts            # 18 tests
npx vitest run src/__tests__/modules/profile/og-render-isolation.test.ts # 1 test
npx vitest run src/workers/worker-runtime.test.ts                        # 34 tests
npx vitest run src/__tests__/db/modelIndexes.test.ts                     # 2 tests
npx vitest run src/__tests__/modules/profile/og-shell.test.ts            # 5 tests
npx vitest run src/__tests__/modules/profile/public-profile-privacy.test.ts # 15 tests
npx vitest run src/__tests__/modules/profile/public-profile.test.ts      # 3 tests
npx vitest run src/__tests__/modules/profile/public-profile-lookup.test.ts # 4 tests
npx vitest run src/config/compose-config.test.ts                         # 3 tests

All of those pass. og-image.test.ts uses the real satori + resvg: PNG magic bytes 89504E47 0D0A1A0A, IHDR width/height 1200x630, cache hit verified with a vi.fn() renderer spy (second render of the same content: renderer called once), re-render in place on changed content, expired artifact not trusted, unique artifactKey (E11000), unpublished -> 404 with the same body as an unknown handle and nothing queued, unpublish removes the stored card, 304 handling, 503 when no worker answers, and seo.ogImageUrl / og:image only for published profiles.

worker-runtime.test.ts fails on main with the new role mapping cases (checked by running the updated test against the old worker-runtime.ts), and passes with this change.

tsc --noEmit shows only the 5 existing @devtrack/ats-core errors, nothing in these files.

Not tested / limits

  • The real BullMQ queue and a real Redis were not exercised (tests use the repo's global bullmq mock; the route test replaces the enqueue call with the worker's job body run in process). The domain-og-render worker wiring itself is covered by the role-mapping test only.
  • I did not build the Docker image or run the compose stack.
  • Only Latin glyphs are drawn (see font note above).
  • Remote avatar is not in the card (monogram only).

Not in this PR (rest of #352)

  • dossierRender.worker.ts (Puppeteer PDF), dossier-render queue, objectStorage.ts with signed 15 minute URLs: next PR.
  • The issue lists docs/v2/DEPLOYMENT_AND_SECURITY_V2.md and docs/v2/MODULE_IMPLEMENTATION_PLAN.md, but there is no docs/v2 in the repo, so I put the notes in ARCHITECTURE.md and DEPLOYMENT.md.
  • No RenderArtifact migration script: it is a brand new collection and the indexes are declared on the schema.

Adds the first half of the worker-render pool: a `render` worker role
that consumes the new `domain-og-render` queue, and a 1200x630 OG card
for published profiles drawn with satori and resvg.

- worker-runtime: new `render` role (and part of `all`), worker:render
  script, compose service, Dockerfile copies the font assets
- RenderArtifact model: unique artifactKey, contentHash, status, TTL;
  small PNGs are stored inline for now
- card data comes from the same section/privacy rule as the public
  profile DTO (visibleSections), so hidden sections never reach the image
- content-hash cache: a render with unchanged content returns the stored
  artifact and does not call the renderer
- GET /api/v1/profile/public/:username/og.png: the API only queues a job
  and waits up to 8s; unknown and unpublished handles get the same 404;
  ETag/304 after the visibility check; 503 + Retry-After if no worker
  answers in time
- seo.ogImageUrl in the public profile API and og:image in the /u/:slug
  shell are set only for published profiles (shell needs PUBLIC_API_URL)
- unpublishing deletes the stored card
- bundles Inter (latin subset, SIL OFL 1.1) under backend/assets/fonts
- adds satori and @resvg/resvg-js (both MPL-2.0)

The dossier PDF worker and signed object storage URLs come in a
follow-up PR.

Refs VarshithReddy2006#352

Signed-off-by: Abhishek <abhishekcse2004@gmail.com>
…into feat/issue-352a-og-render-worker

Signed-off-by: Abhishek <abhishekcse2004@gmail.com>

# Conflicts:
#	DEPLOYMENT.md
@VarshithReddy2006 VarshithReddy2006 added enhancement New feature or request help wanted Extra attention is needed nsoc26 level3 10 points - core logic, database changes, and complex features. labels Nov 16, 2026
@VarshithReddy2006

Copy link
Copy Markdown
Owner

@Anshuu2004 CI/CD checks are currently failing on Unit Tests:

AssertionError: expected undefined to match object { achievements: [], trust: { underReview: true } }
 ❯ src/__tests__/modules/profile/antiGaming.worker.test.ts:138:18

This failure occurs because public profiles are now opt-in / private by default following the merge of PR #381.

Please rebase/merge the latest main into your branch and fix this immediately so we can proceed with reviewing and merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request help wanted Extra attention is needed level3 10 points - core logic, database changes, and complex features. nsoc26

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants