@clubedge/create-clubedge-app 0.12.0 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +12 -11
  3. package/dist/core/lockfile-peers.js +16 -1
  4. package/package.json +3 -3
  5. package/template/.env.example +8 -0
  6. package/template/README.md +3 -0
  7. package/template/SETUP.md +33 -0
  8. package/template/apps/start/.env.example +8 -0
  9. package/template/apps/start/package.json +3 -1
  10. package/template/apps/start/src/components/jobs/jobs-card.tsx +71 -0
  11. package/template/apps/start/src/jobs/index.ts +7 -0
  12. package/template/apps/start/src/jobs/sample.ts +10 -0
  13. package/template/apps/start/src/nitro/plugins/jobs.ts +8 -0
  14. package/template/apps/start/src/routes/api/jobs/run.ts +28 -0
  15. package/template/apps/start/src/routes/dashboard.tsx +11 -0
  16. package/template/apps/start/src/routes/jobs/sample.ts +11 -0
  17. package/template/apps/start/src/server/jobs-forms.ts +17 -0
  18. package/template/apps/start/src/server/jobs.functions.ts +29 -0
  19. package/template/apps/start/src/server/jobs.ts +59 -0
  20. package/template/apps/start/vite.config.ts +2 -1
  21. package/template/apps/web/package.json +3 -1
  22. package/template/apps/web/src/app/api/jobs/run/route.ts +23 -0
  23. package/template/apps/web/src/app/dashboard/_components/jobs-actions.ts +14 -0
  24. package/template/apps/web/src/app/dashboard/_components/jobs-card.tsx +83 -0
  25. package/template/apps/web/src/app/dashboard/page.tsx +7 -0
  26. package/template/apps/web/src/instrumentation.ts +8 -0
  27. package/template/apps/web/src/jobs/index.ts +7 -0
  28. package/template/apps/web/src/jobs/sample.ts +10 -0
  29. package/template/apps/web/src/server/jobs.ts +59 -0
  30. package/template/clubedge.template.json +33 -0
  31. package/template/e2e/jobs.spec.ts +33 -0
  32. package/template/package.json +1 -1
  33. package/template/packages/auth/package.json +1 -1
  34. package/template/packages/auth-better-auth/package.json +1 -1
  35. package/template/packages/auth-supabase/package.json +1 -1
  36. package/template/packages/cache/package.json +1 -1
  37. package/template/packages/cache-redis/package.json +1 -1
  38. package/template/packages/core/package.json +1 -1
  39. package/template/packages/db/package.json +1 -1
  40. package/template/packages/email/package.json +1 -1
  41. package/template/packages/email-resend/package.json +1 -1
  42. package/template/packages/email-smtp/package.json +1 -1
  43. package/template/packages/jobs/package.json +18 -0
  44. package/template/packages/jobs/src/index.test.ts +69 -0
  45. package/template/packages/jobs/src/index.ts +205 -0
  46. package/template/packages/jobs/tsconfig.json +4 -0
  47. package/template/packages/jobs-pg-boss/package.json +23 -0
  48. package/template/packages/jobs-pg-boss/src/index.test.ts +117 -0
  49. package/template/packages/jobs-pg-boss/src/index.ts +200 -0
  50. package/template/packages/jobs-pg-boss/tsconfig.json +4 -0
  51. package/template/packages/local-db/package.json +1 -1
  52. package/template/packages/payments/package.json +1 -1
  53. package/template/packages/payments-stripe/package.json +1 -1
  54. package/template/packages/storage/package.json +1 -1
  55. package/template/packages/storage-s3/package.json +1 -1
  56. package/template/packages/storage-supabase/package.json +1 -1
  57. package/template/packages/ui/package.json +1 -1
  58. package/template/pnpm-lock.yaml +286 -56
  59. package/template.lock.json +27 -2
package/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  All notable changes to `create-clubedge-app` are documented here.
4
4
 
5
+ ## 0.13.0
6
+
7
+ - Default to Starter `v0.12.0`, which adds background jobs (`--jobs pg-boss`): jobs stored in the project's PostgreSQL, run by a worker inside the app server, with retries and schedules, and `/api/jobs/run` for serverless hosts. `--jobs` needs no CLI code: it comes from the Starter's manifest.
8
+ - Fix generated lockfiles that pnpm rejected as broken when pruning an optional peer left a package with no dependencies, such as `pg-pool` once `pg` is pruned for `--jobs none`. Such entries are now written as `{}`, the way pnpm writes them.
9
+ - Test a jobs project in CI and in the release smoke test, and add the jobs module in the change-modules job.
10
+
5
11
  ## 0.12.0
6
12
 
7
13
  - Default to Starter `v0.11.0`, which adds payments (`--payments stripe`): Stripe Checkout, the customer portal, and verified webhooks behind a provider-neutral payments interface. `--payments` needs no CLI code: it comes from the Starter's manifest.
package/README.md CHANGED
@@ -12,27 +12,27 @@ The CLI downloads a tested Starter release, names the generated project, creates
12
12
  Each CLI release is tested against one specific Starter release:
13
13
 
14
14
  ```text
15
- create-clubedge-app v0.12.0
15
+ create-clubedge-app v0.13.0
16
16
  |
17
17
  v
18
- Clubedge Starter v0.11.0
19
- commit b5637afcf3748e03bb7ecb1f1941a4e6c17cfa65
18
+ Clubedge Starter v0.12.0
19
+ commit 445add73c3505cd8505faccb3b0b7061a29f5356
20
20
  ```
21
21
 
22
22
  The current relationship is:
23
23
 
24
24
  | CLI release | Default Starter release | Meaning |
25
25
  | --- | --- | --- |
26
- | `create-clubedge-app@0.12.0` | `clubedge-starter@v0.11.0` | The CLI's required, tested default |
26
+ | `create-clubedge-app@0.13.0` | `clubedge-starter@v0.12.0` | The CLI's required, tested default |
27
27
 
28
28
  This means the same CLI version always scaffolds the same Starter revision by default. The CLI does not silently follow Starter `main`.
29
29
 
30
- The `v0.11.0` tag is expected to resolve to commit
31
- `b5637afcf3748e03bb7ecb1f1941a4e6c17cfa65`. The CLI verifies this before
30
+ The `v0.12.0` tag is expected to resolve to commit
31
+ `445add73c3505cd8505faccb3b0b7061a29f5356`. The CLI verifies this before
32
32
  scaffolding with the default reference and fails rather than silently using a
33
33
  moved tag.
34
34
 
35
- When the Starter changes, its maintainers first publish a new versioned release, such as `v0.11.0`. The CLI is then updated to use that release, all generated-project checks must pass, and a new CLI version is published. This keeps Starter and CLI releases independently versioned while preserving a tested compatibility relationship.
35
+ When the Starter changes, its maintainers first publish a new versioned release, such as `v0.12.0`. The CLI is then updated to use that release, all generated-project checks must pass, and a new CLI version is published. This keeps Starter and CLI releases independently versioned while preserving a tested compatibility relationship.
36
36
 
37
37
  ## Requirements
38
38
 
@@ -85,7 +85,7 @@ Either way, the app lives in `apps/web` and uses the same shared packages, so th
85
85
 
86
86
  ### Choosing services
87
87
 
88
- Authentication, file storage, caching, email, payments, local infrastructure, and deployment are modules. Each has a default, and anything you leave out is removed completely: its package, its provider SDK, its environment variables, its documentation, and its lockfile entries. The Starter's `clubedge.template.json` defines the modules, so a module a newer Starter adds gets its `--<module>` flag without a CLI change; `--help` lists the modules of the Starter this CLI release creates.
88
+ Authentication, file storage, caching, email, payments, background jobs, local infrastructure, and deployment are modules. Each has a default, and anything you leave out is removed completely: its package, its provider SDK, its environment variables, its documentation, and its lockfile entries. The Starter's `clubedge.template.json` defines the modules, so a module a newer Starter adds gets its `--<module>` flag without a CLI change; `--help` lists the modules of the Starter this CLI release creates.
89
89
 
90
90
  | Flag | Options | Default |
91
91
  | ----------- | ------------------------------------------ | ---------- |
@@ -94,6 +94,7 @@ Authentication, file storage, caching, email, payments, local infrastructure, an
94
94
  | `--cache` | `redis` (falls back to memory without `REDIS_URL`), `memory` | `redis` |
95
95
  | `--email` | `smtp` (any provider, or a local inbox), `resend`, `none` | `smtp` |
96
96
  | `--payments` | `stripe`, `none` (needs an auth module) | `none` |
97
+ | `--jobs` | `pg-boss`, `none` | `none` |
97
98
  | `--deploy` | `docker` (any container host), `fly`, `railway`, `render`, `vercel`, `netlify` | `docker` |
98
99
  | `--infra` | `docker` (Docker Compose), `supabase` (Supabase CLI), `local` (no Docker: PGlite and a local folder), `none` | `docker` |
99
100
 
@@ -139,7 +140,7 @@ pnpm dlx @clubedge/create-clubedge-app my-app --yes --no-git --framework tanstac
139
140
  pnpm dlx @clubedge/create-clubedge-app my-app --ref main
140
141
 
141
142
  # Reproduce a specific Starter release or commit
142
- pnpm dlx @clubedge/create-clubedge-app my-app --ref v0.11.0
143
+ pnpm dlx @clubedge/create-clubedge-app my-app --ref v0.12.0
143
144
  pnpm dlx @clubedge/create-clubedge-app my-app --ref <commit-sha>
144
145
 
145
146
  # Test local Starter changes before releasing them
@@ -156,7 +157,7 @@ pnpm dlx @clubedge/create-clubedge-app add cache redis # or switch: add aut
156
157
  pnpm dlx @clubedge/create-clubedge-app remove storage
157
158
  pnpm dlx @clubedge/create-clubedge-app add infra local --dry-run
158
159
  pnpm dlx @clubedge/create-clubedge-app@latest upgrade # to the newest CLI's Starter release
159
- pnpm dlx @clubedge/create-clubedge-app upgrade --ref v0.11.0 # or a specific tag, branch, or commit
160
+ pnpm dlx @clubedge/create-clubedge-app upgrade --ref v0.12.0 # or a specific tag, branch, or commit
160
161
  ```
161
162
 
162
163
  The CLI renders the project twice, from the Starter commit and modules recorded in your `package.json` and from the new state, and merges the difference into your project. The new state has other modules for `add` and `remove`, and the newer Starter with the same modules for `upgrade`. Modules a newer Starter introduces start as `none` where possible, so an upgrade never adds a service on its own.
@@ -207,7 +208,7 @@ CI packs the CLI with `npm pack`, scaffolds a clean project from the packed tarb
207
208
  1. Make and validate changes in `clubedge-starter`.
208
209
  2. Run lint, typecheck, unit tests, browser tests, and production build.
209
210
  3. Update the Starter package versions.
210
- 4. Commit `main`, create a release tag such as `v0.11.0`, and publish the Starter release.
211
+ 4. Commit `main`, create a release tag such as `v0.12.0`, and publish the Starter release.
211
212
 
212
213
  ### CLI maintainers
213
214
 
@@ -170,5 +170,20 @@ export function pruneOptionalPeers(source) {
170
170
  }
171
171
  output.push(line);
172
172
  }
173
- return output.join("\n");
173
+ return closeEmptySnapshots(output).join("\n");
174
+ }
175
+ /**
176
+ * A snapshot whose only dependencies were removed links is left without a body, which pnpm reads
177
+ * as null and rejects as a broken lockfile; pnpm itself writes such entries as "key: {}".
178
+ */
179
+ function closeEmptySnapshots(lines) {
180
+ let section = "";
181
+ return lines.map((line, index) => {
182
+ if (/^\S/.test(line))
183
+ section = line.replace(/:.*$/, "");
184
+ if (section !== "snapshots" || !/^ {2}\S.*:$/.test(line))
185
+ return line;
186
+ const next = lines.slice(index + 1).find((candidate) => candidate.trim() !== "");
187
+ return next === undefined || !/^ {4}/.test(next) ? `${line} {}` : line;
188
+ });
174
189
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clubedge/create-clubedge-app",
3
- "version": "0.12.0",
3
+ "version": "0.13.0",
4
4
  "description": "Create a Next.js or TanStack Start application from the Clubedge Starter reference implementation.",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@10.9.0",
@@ -22,8 +22,8 @@
22
22
  },
23
23
  "clubedge": {
24
24
  "starterRepository": "Clubedge/clubedge-starter",
25
- "starterRef": "v0.11.0",
26
- "starterCommit": "b5637afcf3748e03bb7ecb1f1941a4e6c17cfa65"
25
+ "starterRef": "v0.12.0",
26
+ "starterCommit": "445add73c3505cd8505faccb3b0b7061a29f5356"
27
27
  },
28
28
  "license": "Apache-2.0",
29
29
  "repository": {
@@ -60,6 +60,14 @@ STRIPE_WEBHOOK_SECRET=
60
60
  STRIPE_PRICE_PRO=
61
61
  # clubedge:end
62
62
 
63
+ # clubedge:if jobs=pg-boss
64
+ # Background jobs are stored in DATABASE_URL and run by a worker inside the app server. Set
65
+ # JOBS_WORKER=off where no server keeps running (it is off by default on Vercel and Netlify);
66
+ # a scheduler then calls /api/jobs/run with CRON_SECRET (16 or more characters) instead.
67
+ JOBS_WORKER=
68
+ CRON_SECRET=
69
+ # clubedge:end
70
+
63
71
  # clubedge:if storage=s3
64
72
  # S3-compatible storage (AWS S3, Cloudflare R2, MinIO)
65
73
  STORAGE_BUCKET= # clubedge:only infra!=docker
@@ -28,6 +28,7 @@ This repository contains the same application for two frameworks, Next.js (`apps
28
28
  - Email over SMTP, for any provider or a local inbox; development prints emails to the server log until SMTP is configured. <!-- clubedge:only email=smtp -->
29
29
  - Email through Resend; development prints emails to the server log until the API key is set. <!-- clubedge:only email=resend -->
30
30
  - Subscriptions with Stripe Checkout and the customer portal, kept in sync by verified webhooks, behind a provider-neutral payments interface. <!-- clubedge:only payments=stripe -->
31
+ - Background jobs with retries and schedules, stored in PostgreSQL by pg-boss and run by a worker inside the app server, behind a provider-neutral jobs interface. <!-- clubedge:only jobs=pg-boss -->
31
32
  - Zod environment validation, security headers, structured errors, and `/api/health`.
32
33
  - Docker support, GitHub Actions CI, Vitest, Playwright, ESLint, and Prettier.
33
34
  - A production image that includes its own migrator (`node /app/migrate.mjs`), for running migrations before each release.
@@ -224,6 +225,8 @@ The TanStack Start app is `apps/start`, run with `pnpm --filter @clubedge/start
224
225
  - `packages/email-resend/`: Resend email provider <!-- clubedge:only email=resend -->
225
226
  - `packages/payments/`: `PaymentProvider` interface, billing logic, and the subscription tables <!-- clubedge:only payments!=none -->
226
227
  - `packages/payments-stripe/`: Stripe payment provider <!-- clubedge:only payments=stripe -->
228
+ - `packages/jobs/`: `JobQueue` interface, job definitions, and an in-memory queue for tests <!-- clubedge:only jobs!=none -->
229
+ - `packages/jobs-pg-boss/`: pg-boss job queue <!-- clubedge:only jobs=pg-boss -->
227
230
  - `packages/ui/`: shared shadcn/ui components, utilities, and global theme styles
228
231
  - `.github/`: CI workflow, issue forms, and pull request template
229
232
  - `SETUP.md`, `CONTRIBUTING.md`, `SECURITY.md`: setup, contribution, and security guides
package/template/SETUP.md CHANGED
@@ -46,6 +46,7 @@ The variables are:
46
46
  - `SMTP_URL` and `EMAIL_FROM` (for sending email): see [Email](#email). <!-- clubedge:only email=smtp -->
47
47
  - `RESEND_API_KEY` and `EMAIL_FROM` (for sending email): see [Email](#email). <!-- clubedge:only email=resend -->
48
48
  - `STRIPE_SECRET_KEY`, `STRIPE_WEBHOOK_SECRET`, and `STRIPE_PRICE_<PLAN>` (for payments): see [Payments](#payments). <!-- clubedge:only payments=stripe -->
49
+ - `JOBS_WORKER` and `CRON_SECRET` (optional): see [Background jobs](#background-jobs). <!-- clubedge:only jobs=pg-boss -->
49
50
  - `STORAGE_*` (for file storage): see [File storage](#file-storage). <!-- clubedge:only storage=s3 -->
50
51
  - `SUPABASE_STORAGE_BUCKET` (for file storage): see [File storage](#file-storage). <!-- clubedge:only storage=supabase -->
51
52
 
@@ -236,6 +237,33 @@ Turn on the customer portal in the dashboard's billing settings, so "Manage bill
236
237
 
237
238
  Payments go through the provider-neutral `PaymentProvider` interface in `packages/payments`. To use another provider, implement that interface in a new package and return it from `apps/web/src/server/payments.ts`; billing logic, the tables, the routes, and the billing card stay the same.
238
239
 
240
+ <!-- clubedge:end -->
241
+ <!-- clubedge:if jobs=pg-boss -->
242
+
243
+ ## Background jobs
244
+
245
+ Jobs run outside requests, with retries, in a worker that starts inside the app server: `pnpm dev` and the production server both run it, so there is no separate process to deploy. [pg-boss](https://github.com/timgit/pg-boss) stores them in your PostgreSQL, and creates and upgrades its own tables in the `pgboss` schema when the app starts.
246
+
247
+ Define a job in `apps/web/src/jobs` and add it to the list in `apps/web/src/jobs/index.ts`:
248
+
249
+ ```ts
250
+ import { defineJob } from "@clubedge/jobs";
251
+
252
+ export const welcomeEmail = defineJob<{ userId: string }>({
253
+ name: "welcome-email",
254
+ retryLimit: 3,
255
+ async run({ userId }) {
256
+ // Look the user up and send the email.
257
+ },
258
+ });
259
+ ```
260
+
261
+ Queue it from server code with `getJobs().enqueue(welcomeEmail, { userId })`, from `@/server/jobs`. A thrown error fails the attempt and the job is retried (`retryLimit`, `retryDelaySeconds`, `retryBackoff`). An attempt that runs past `timeoutSeconds`, or whose server stops, is retried too, so make jobs safe to run twice. Add `schedule: "0 3 * * *"` (cron syntax) to run a job on a schedule. Restart `pnpm dev` after adding or changing a job. The dashboard's jobs card queues the sample job from `apps/web/src/jobs/sample.ts` and lists its recent runs; remove both when you add your own.
262
+
263
+ Where no server keeps running, as on Vercel and Netlify, there is no worker: `JOBS_WORKER` defaults to `off` there, and you can set it yourself anywhere. Set `CRON_SECRET` to 16 or more random characters and call `GET /api/jobs/run` every minute or so with the header `Authorization: Bearer <CRON_SECRET>`, from Vercel Cron, a Netlify scheduled function, or any scheduler. Each call runs the jobs that are due. Scheduled jobs need a worker.
264
+
265
+ Jobs go through the provider-neutral `JobQueue` interface in `packages/jobs`. To use another queue, implement that interface in a new package and return it from `apps/web/src/server/jobs.ts`; job definitions, the routes, and the jobs card stay the same.
266
+
239
267
  <!-- clubedge:end -->
240
268
  <!-- clubedge:if storage!=none -->
241
269
 
@@ -338,6 +366,8 @@ Render runs `node /app/migrate.mjs` as the pre-deploy command before each deploy
338
366
 
339
367
  Vercel has no step that runs before a release, so the build command applies migrations (`pnpm db:migrate`) against the `DATABASE_URL` of the deployment's environment. Give Preview deployments their own database, or remove the migration step from `buildCommand` and run migrations yourself. Serverless functions open a connection per instance: use your provider's pooled connection URL (for example Supabase's transaction pooler or Neon's pooled endpoint).
340
368
 
369
+ Vercel runs no worker for [background jobs](#background-jobs). Set `CRON_SECRET` and add `"crons": [{ "path": "/api/jobs/run", "schedule": "* * * * *" }]` to `vercel.json`; Vercel Cron sends the secret itself. Cron jobs more frequent than daily need a paid Vercel plan; otherwise call the endpoint from another scheduler. <!-- clubedge:only jobs=pg-boss -->
370
+
341
371
  <!-- clubedge:end -->
342
372
  <!-- clubedge:if deploy=netlify -->
343
373
 
@@ -346,6 +376,8 @@ Vercel has no step that runs before a release, so the build command applies migr
346
376
 
347
377
  Netlify has no step that runs before a release, so the build command applies migrations (`pnpm db:migrate`) against the `DATABASE_URL` of the deploy context. Give deploy previews their own database, or remove the migration step from the build command and run migrations yourself. Serverless functions open a connection per instance: use your provider's pooled connection URL (for example Supabase's transaction pooler or Neon's pooled endpoint).
348
378
 
379
+ Netlify runs no worker for [background jobs](#background-jobs). Set `JOBS_WORKER=off` and `CRON_SECRET`, then call `/api/jobs/run` every minute from a [scheduled function](https://docs.netlify.com/build/functions/scheduled-functions/) or another scheduler, with the header `Authorization: Bearer <CRON_SECRET>`. <!-- clubedge:only jobs=pg-boss -->
380
+
349
381
  <!-- clubedge:end -->
350
382
 
351
383
  `.github/workflows/deploy.yml` deploys on demand: in GitHub, open Actions, then Deploy, then Run workflow.
@@ -371,6 +403,7 @@ Production needs these variables:
371
403
  - `RESEND_API_KEY` and `EMAIL_FROM`; without them, production refuses to send email. <!-- clubedge:only email=resend -->
372
404
  - `STRIPE_SECRET_KEY` (live mode), `STRIPE_WEBHOOK_SECRET` from the production webhook endpoint, and `STRIPE_PRICE_<PLAN>` for each plan. <!-- clubedge:only payments=stripe -->
373
405
  - `REDIS_URL`, when you run more than one instance. <!-- clubedge:only cache=redis -->
406
+ - `CRON_SECRET`, where no worker runs and a scheduler calls `/api/jobs/run`. <!-- clubedge:only jobs=pg-boss -->
374
407
  - The `STORAGE_*` variables for your S3-compatible bucket. <!-- clubedge:only storage=s3 -->
375
408
  - `SUPABASE_STORAGE_BUCKET`. <!-- clubedge:only storage=supabase -->
376
409
 
@@ -62,6 +62,14 @@ STRIPE_WEBHOOK_SECRET=
62
62
  STRIPE_PRICE_PRO=
63
63
  # clubedge:end
64
64
 
65
+ # clubedge:if jobs=pg-boss
66
+ # Background jobs are stored in DATABASE_URL and run by a worker inside the app server. Set
67
+ # JOBS_WORKER=off where no server keeps running (it is off by default on Vercel and Netlify);
68
+ # a scheduler then calls /api/jobs/run with CRON_SECRET (16 or more characters) instead.
69
+ JOBS_WORKER=
70
+ CRON_SECRET=
71
+ # clubedge:end
72
+
65
73
  # clubedge:if storage=s3
66
74
  # S3-compatible storage (AWS S3, Cloudflare R2, MinIO)
67
75
  STORAGE_BUCKET= # clubedge:only infra!=docker
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clubedge/start",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "private": true,
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
@@ -22,6 +22,8 @@
22
22
  "@clubedge/email": "workspace:*",
23
23
  "@clubedge/email-resend": "workspace:*",
24
24
  "@clubedge/email-smtp": "workspace:*",
25
+ "@clubedge/jobs": "workspace:*",
26
+ "@clubedge/jobs-pg-boss": "workspace:*",
25
27
  "@clubedge/payments": "workspace:*",
26
28
  "@clubedge/payments-stripe": "workspace:*",
27
29
  "@clubedge/storage": "workspace:*",
@@ -0,0 +1,71 @@
1
+ import { ListChecks } from "lucide-react";
2
+
3
+ import type { JobsStatus } from "@/server/jobs.functions";
4
+ import { Badge } from "@clubedge/ui/components/badge";
5
+ import { Button } from "@clubedge/ui/components/button";
6
+ import {
7
+ Card,
8
+ CardContent,
9
+ CardDescription,
10
+ CardHeader,
11
+ CardTitle,
12
+ } from "@clubedge/ui/components/card";
13
+
14
+ const timeFormat = new Intl.DateTimeFormat("en", { timeStyle: "medium", timeZone: "UTC" });
15
+
16
+ /** Starts the sample job and lists the current user's recent runs. The form posts to /jobs/sample. */
17
+ export function JobsCard({ status }: { status: JobsStatus }) {
18
+ const { jobs } = status;
19
+
20
+ return (
21
+ <Card className="gap-0 py-0" id="jobs">
22
+ <CardHeader className="flex-row items-start justify-between gap-3 border-b px-5 py-5 sm:px-6">
23
+ <div className="grid gap-1">
24
+ <CardTitle className="text-base">Background jobs</CardTitle>
25
+ <CardDescription>
26
+ {status.worker
27
+ ? "Work runs outside requests in this server's worker, with retries."
28
+ : "This server runs no worker: jobs run when /api/jobs/run is called."}
29
+ </CardDescription>
30
+ </div>
31
+ <span className="grid size-9 place-items-center rounded-lg bg-primary/10 text-primary">
32
+ <ListChecks aria-hidden="true" className="size-4" />
33
+ </span>
34
+ </CardHeader>
35
+ <CardContent className="grid gap-4 px-5 py-5 sm:px-6">
36
+ {jobs === null ? (
37
+ <p className="text-sm text-muted-foreground">
38
+ Jobs are unavailable: the database could not be reached.
39
+ </p>
40
+ ) : (
41
+ <>
42
+ <form action="/jobs/sample" method="post">
43
+ <Button type="submit" variant="outline">
44
+ Run a sample job
45
+ </Button>
46
+ </form>
47
+ {jobs.length === 0 ? (
48
+ <p className="text-sm text-muted-foreground">No jobs yet.</p>
49
+ ) : (
50
+ <ul aria-label="Recent jobs" className="grid gap-2">
51
+ {jobs.map((job) => (
52
+ <li className="flex items-center justify-between gap-3 text-sm" key={job.id}>
53
+ <span>
54
+ {job.name}{" "}
55
+ <span className="text-muted-foreground">
56
+ {timeFormat.format(new Date(job.createdAt))} UTC
57
+ </span>
58
+ </span>
59
+ <Badge variant={job.state === "completed" ? "default" : "secondary"}>
60
+ {job.state}
61
+ </Badge>
62
+ </li>
63
+ ))}
64
+ </ul>
65
+ )}
66
+ </>
67
+ )}
68
+ </CardContent>
69
+ </Card>
70
+ );
71
+ }
@@ -0,0 +1,7 @@
1
+ import type { JobDefinition } from "@clubedge/jobs";
2
+ import { sampleJob } from "./sample";
3
+
4
+ export { sampleJob };
5
+
6
+ /** Every job the worker runs. Define a job in this folder, then add it here. */
7
+ export const jobs: JobDefinition[] = [sampleJob];
@@ -0,0 +1,10 @@
1
+ import { defineJob } from "@clubedge/jobs";
2
+
3
+ /** An example job that the dashboard can start; replace it with your own. */
4
+ export const sampleJob = defineJob<{ requestedBy: string }>({
5
+ name: "sample",
6
+ async run({ requestedBy }, { attempt }) {
7
+ await new Promise((resolve) => setTimeout(resolve, 500));
8
+ return { message: `Hello, ${requestedBy}, from the jobs worker.`, attempt };
9
+ },
10
+ });
@@ -0,0 +1,8 @@
1
+ import { definePlugin } from "nitro";
2
+ import { getJobs, startJobsWorker } from "@/server/jobs";
3
+
4
+ /** Starts the background jobs worker with the server, and stops it with the server. */
5
+ export default definePlugin((nitroApp) => {
6
+ startJobsWorker();
7
+ nitroApp.hooks.hook("close", () => getJobs().stop());
8
+ });
@@ -0,0 +1,28 @@
1
+ import { timingSafeEqual } from "node:crypto";
2
+ import { createFileRoute } from "@tanstack/react-router";
3
+ import { getCronSecret, getJobs } from "@/server/jobs";
4
+
5
+ function isAuthorized(request: Request, secret: string): boolean {
6
+ const given = Buffer.from(request.headers.get("authorization") ?? "");
7
+ const expected = Buffer.from(`Bearer ${secret}`);
8
+ return given.length === expected.length && timingSafeEqual(given, expected);
9
+ }
10
+
11
+ /**
12
+ * Runs the jobs that are due, for hosts without a long-running worker: call it on a schedule
13
+ * with `Authorization: Bearer $CRON_SECRET` (Vercel Cron sends that header itself).
14
+ */
15
+ export const Route = createFileRoute("/api/jobs/run")({
16
+ server: {
17
+ handlers: {
18
+ GET: async ({ request }) => {
19
+ const secret = getCronSecret();
20
+ if (!secret) return Response.json({ error: "Not found" }, { status: 404 });
21
+ if (!isAuthorized(request, secret))
22
+ return Response.json({ error: "Unauthorized" }, { status: 401 });
23
+ const result = await getJobs().runDue();
24
+ return Response.json(result, { headers: { "Cache-Control": "no-store" } });
25
+ },
26
+ },
27
+ },
28
+ });
@@ -51,6 +51,10 @@ import { getPageUser } from "@/server/auth.functions";
51
51
  import { BillingCard } from "@/components/billing/billing-card";
52
52
  import { getBillingStatus } from "@/server/billing.functions";
53
53
  // clubedge:end
54
+ // clubedge:if jobs!=none
55
+ import { JobsCard } from "@/components/jobs/jobs-card";
56
+ import { getJobsStatus } from "@/server/jobs.functions";
57
+ // clubedge:end
54
58
  import { AccountButton } from "@/components/dashboard/account-button";
55
59
  import { AppSidebar } from "@/components/dashboard/app-sidebar";
56
60
  import { ThemeToggle } from "@/components/theme/theme-toggle";
@@ -116,6 +120,9 @@ export const Route = createFileRoute("/dashboard")({
116
120
  // clubedge:if payments!=none
117
121
  billing: await getBillingStatus(),
118
122
  // clubedge:end
123
+ // clubedge:if jobs!=none
124
+ jobs: await getJobsStatus(),
125
+ // clubedge:end
119
126
  }),
120
127
  component: DashboardPage,
121
128
  });
@@ -257,6 +264,10 @@ function DashboardPage() {
257
264
  <BillingCard billing={data.billing} />
258
265
  {/* clubedge:end */}
259
266
 
267
+ {/* clubedge:if jobs!=none */}
268
+ <JobsCard status={data.jobs} />
269
+ {/* clubedge:end */}
270
+
260
271
  <section className="grid gap-6 xl:grid-cols-[1.1fr_0.9fr]" id="modules">
261
272
  <Card className="gap-0 py-0">
262
273
  <CardHeader className="flex-row items-start justify-between gap-3 border-b px-5 py-5 sm:px-6">
@@ -0,0 +1,11 @@
1
+ import { createFileRoute } from "@tanstack/react-router";
2
+ import { handleSampleJob } from "@/server/jobs-forms";
3
+
4
+ export const Route = createFileRoute("/jobs/sample")({
5
+ server: {
6
+ handlers: {
7
+ GET: () => new Response(null, { status: 303, headers: { Location: "/dashboard" } }),
8
+ POST: () => handleSampleJob(),
9
+ },
10
+ },
11
+ });
@@ -0,0 +1,17 @@
1
+ import "@tanstack/react-start/server-only";
2
+ import { sampleJob } from "@/jobs";
3
+ import { getCurrentUser, isAuthConfigured } from "./auth";
4
+ import { getJobs } from "./jobs";
5
+
6
+ /** Who the sample job runs for: the signed-in user's email, or "anonymous". */
7
+ export async function sampleJobRequester(): Promise<string> {
8
+ const user = isAuthConfigured() ? await getCurrentUser() : null;
9
+ return user?.email ?? "anonymous";
10
+ }
11
+
12
+ /** The jobs card's form: queues the sample job; repeated clicks within seconds are ignored. */
13
+ export async function handleSampleJob(): Promise<Response> {
14
+ const requestedBy = await sampleJobRequester();
15
+ await getJobs().enqueue(sampleJob, { requestedBy }, { key: requestedBy, throttleSeconds: 2 });
16
+ return new Response(null, { status: 303, headers: { Location: "/dashboard" } });
17
+ }
@@ -0,0 +1,29 @@
1
+ import { createServerFn } from "@tanstack/react-start";
2
+ import { sampleJob } from "@/jobs";
3
+ import { getJobs, isWorkerEnabled } from "./jobs";
4
+ import { sampleJobRequester } from "./jobs-forms";
5
+
6
+ /** What the dashboard's jobs card shows; serializable, so it can cross to the browser. */
7
+ export const getJobsStatus = createServerFn({ method: "GET" }).handler(async () => {
8
+ const worker = isWorkerEnabled();
9
+ try {
10
+ const jobs = await getJobs().recent(sampleJob, {
11
+ data: { requestedBy: await sampleJobRequester() },
12
+ limit: 5,
13
+ });
14
+ return {
15
+ worker,
16
+ jobs: jobs.map(({ id, name, state, createdAt }) => ({
17
+ id,
18
+ name,
19
+ state,
20
+ createdAt: createdAt.toISOString(),
21
+ })),
22
+ };
23
+ } catch (error) {
24
+ console.error("Background jobs: could not list recent jobs.", error);
25
+ return { worker, jobs: null };
26
+ }
27
+ });
28
+
29
+ export type JobsStatus = Awaited<ReturnType<typeof getJobsStatus>>;
@@ -0,0 +1,59 @@
1
+ import "@tanstack/react-start/server-only";
2
+ import { z } from "zod";
3
+ import type { JobQueue } from "@clubedge/jobs";
4
+ import { createPgBossJobs } from "@clubedge/jobs-pg-boss";
5
+ import { jobs } from "@/jobs";
6
+ import { getServerEnv, optional, parseEnv } from "@/env/server";
7
+
8
+ export type { JobRecord } from "@clubedge/jobs";
9
+
10
+ // Jobs stored in the app's PostgreSQL through pg-boss, behind the provider-neutral JobQueue.
11
+ const jobsEnvSchema = z.object({
12
+ JOBS_WORKER: optional(z.enum(["on", "off"])),
13
+ // Authorizes /api/jobs/run, which runs due jobs where no worker can; unset disables it.
14
+ CRON_SECRET: optional(z.string().min(16)),
15
+ });
16
+
17
+ const jobsEnv = () => parseEnv(jobsEnvSchema, "Jobs");
18
+
19
+ /**
20
+ * Whether this server runs a worker. Serverless functions (Vercel, Netlify) stop between
21
+ * requests, so there jobs run when a scheduler calls /api/jobs/run instead.
22
+ */
23
+ export function isWorkerEnabled(): boolean {
24
+ const { JOBS_WORKER } = jobsEnv();
25
+ if (JOBS_WORKER) return JOBS_WORKER === "on";
26
+ return !process.env.VERCEL && !process.env.AWS_LAMBDA_FUNCTION_NAME;
27
+ }
28
+
29
+ export function getCronSecret(): string | null {
30
+ return jobsEnv().CRON_SECRET || null;
31
+ }
32
+
33
+ const globalForJobs = globalThis as typeof globalThis & { clubedgeJobs?: JobQueue };
34
+
35
+ /** One queue per process, shared with the worker and across dev reloads. */
36
+ export function getJobs(): JobQueue {
37
+ if (globalForJobs.clubedgeJobs) return globalForJobs.clubedgeJobs;
38
+ const env = getServerEnv();
39
+ globalForJobs.clubedgeJobs = createPgBossJobs({
40
+ connectionString: env.DATABASE_URL,
41
+ jobs,
42
+ worker: isWorkerEnabled(),
43
+ maxConnections: env.NODE_ENV === "production" ? 4 : 2,
44
+ });
45
+ return globalForJobs.clubedgeJobs;
46
+ }
47
+
48
+ /** Starts the worker when this server should run one, retrying until the database answers. */
49
+ export function startJobsWorker() {
50
+ if (!isWorkerEnabled()) return;
51
+ const start = () =>
52
+ getJobs()
53
+ .startWorker()
54
+ .catch((error: unknown) => {
55
+ console.error("Background jobs: the worker could not start, retrying in 5 seconds.", error);
56
+ setTimeout(start, 5_000).unref();
57
+ });
58
+ void start();
59
+ }
@@ -22,6 +22,7 @@ export default defineConfig(({ mode }) => {
22
22
  // Vite resolves externals from this app, so it is also a direct dependency here.
23
23
  environments: { ssr: { resolve: { external: ["use-sync-external-store"] } } },
24
24
  // The Start plugin must come before the React plugin.
25
- plugins: [tailwindcss(), tanstackStart(), nitro(), viteReact()],
25
+ // Nitro runs the plugins in src/nitro/plugins once when the server starts.
26
+ plugins: [tailwindcss(), tanstackStart(), nitro({ serverDir: "./src/nitro" }), viteReact()],
26
27
  };
27
28
  });
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@clubedge/web",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "private": true,
5
5
  "license": "Apache-2.0",
6
6
  "scripts": {
@@ -21,6 +21,8 @@
21
21
  "@clubedge/email": "workspace:*",
22
22
  "@clubedge/email-resend": "workspace:*",
23
23
  "@clubedge/email-smtp": "workspace:*",
24
+ "@clubedge/jobs": "workspace:*",
25
+ "@clubedge/jobs-pg-boss": "workspace:*",
24
26
  "@clubedge/payments": "workspace:*",
25
27
  "@clubedge/payments-stripe": "workspace:*",
26
28
  "@clubedge/storage": "workspace:*",
@@ -0,0 +1,23 @@
1
+ import { timingSafeEqual } from "node:crypto";
2
+ import { getCronSecret, getJobs } from "@/server/jobs";
3
+
4
+ export const dynamic = "force-dynamic";
5
+
6
+ function isAuthorized(request: Request, secret: string): boolean {
7
+ const given = Buffer.from(request.headers.get("authorization") ?? "");
8
+ const expected = Buffer.from(`Bearer ${secret}`);
9
+ return given.length === expected.length && timingSafeEqual(given, expected);
10
+ }
11
+
12
+ /**
13
+ * Runs the jobs that are due, for hosts without a long-running worker: call it on a schedule
14
+ * with `Authorization: Bearer $CRON_SECRET` (Vercel Cron sends that header itself).
15
+ */
16
+ export async function GET(request: Request) {
17
+ const secret = getCronSecret();
18
+ if (!secret) return Response.json({ error: "Not found" }, { status: 404 });
19
+ if (!isAuthorized(request, secret))
20
+ return Response.json({ error: "Unauthorized" }, { status: 401 });
21
+ const result = await getJobs().runDue();
22
+ return Response.json(result, { headers: { "Cache-Control": "no-store" } });
23
+ }
@@ -0,0 +1,14 @@
1
+ "use server";
2
+
3
+ import { revalidatePath } from "next/cache";
4
+ import { sampleJob } from "@/jobs";
5
+ import { getCurrentUser } from "@/server/auth";
6
+ import { getJobs } from "@/server/jobs";
7
+
8
+ /** Queues the sample job for the current user; repeated clicks within seconds are ignored. */
9
+ export async function runSampleJob() {
10
+ const user = await getCurrentUser();
11
+ const requestedBy = user?.email ?? "anonymous";
12
+ await getJobs().enqueue(sampleJob, { requestedBy }, { key: requestedBy, throttleSeconds: 2 });
13
+ revalidatePath("/dashboard");
14
+ }