Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat(cron): narrow a scheduler sync to the drifted job instead of the whole fleet

nirholas7b75dd05b6c3
diff --git a/scripts/create-gcp-scheduler.mjs b/scripts/create-gcp-scheduler.mjs
index c3b619cdc..fb6919c76 100644
--- a/scripts/create-gcp-scheduler.mjs
+++ b/scripts/create-gcp-scheduler.mjs
@@ -31,12 +31,21 @@
// node scripts/create-gcp-scheduler.mjs --env-file <prod.env> --resume # sync + resume EVERY job (recover from --pause)
// gcloud scheduler jobs resume cron--api-cron-uptime-check --location us-central1 # one job
//
+// `--only <substring>[,<substring>]` narrows any of the above to the crons whose
+// declared path contains one of those substrings, so repairing the single job
+// `npm run check:cron-drift` reports MISSING does not re-touch the rest of the
+// fleet:
+//
+// node scripts/create-gcp-scheduler.mjs --env-file <prod.env> --only garment-job-sweep
+//
// Auth: each job sends `Authorization: Bearer $CRON_SECRET`, exactly what the
-// api/cron/* handlers already validate. The secret is read from --env-file or
-// process.env, never hardcoded.
+// api/cron/* handlers already validate. The secret is read from --env-file, then
+// process.env, then the live three-ws-api Cloud Run service (production's
+// authoritative copy, and the only source on a machine whose .env lacks it).
+// It is never hardcoded.
import { readFileSync } from 'node:fs';
-import { execFile } from 'node:child_process';
+import { execFile, execFileSync } from 'node:child_process';
import './lib/gcloud-path.mjs';
import { promisify } from 'node:util';
import path from 'node:path';
@@ -81,6 +90,37 @@ export function runStateAction(argv) {
return null;
}
+/**
+ * Narrow a sync to the crons named by `--only <substring>[,<substring>...]`,
+ * matched against the declared cron path. Without the flag every declared cron
+ * is synced, which is the right default for a fleet-wide re-sync and the wrong
+ * one for repairing a single drifted job: `npm run check:cron-drift` names one
+ * path, and touching the other 110 healthy jobs to fix it is blast radius
+ * nobody asked for. With `--pause`/`--resume` the flag bounds those levers to
+ * the same subset instead of the whole fleet.
+ *
+ * A filter that matches nothing throws rather than syncing zero jobs, because a
+ * mistyped path would otherwise report a clean run having done nothing at all.
+ */
+export function selectCrons(crons, argv) {
+ const idx = argv.indexOf('--only');
+ if (idx === -1) return crons;
+ const raw = argv[idx + 1];
+ if (!raw || raw.startsWith('--')) {
+ throw new Error('--only needs a value, e.g. --only garment-job-sweep');
+ }
+ const needles = raw
+ .split(',')
+ .map((s) => s.trim())
+ .filter(Boolean);
+ if (!needles.length) throw new Error('--only needs a value, e.g. --only garment-job-sweep');
+ const picked = crons.filter((c) => needles.some((n) => c.path.includes(n)));
+ if (!picked.length) {
+ throw new Error(`--only ${raw} matched none of the ${crons.length} crons declared in vercel.json.`);
+ }
+ return picked;
+}
+
/** Read CRON_SECRET out of a `--env-file <path>` argument, if one was given. */
export function cronSecretFromArgs(argv, readFile = (p) => readFileSync(p, 'utf8')) {
const idx = argv.indexOf('--env-file');
@@ -92,6 +132,43 @@ export function cronSecretFromArgs(argv, readFile = (p) => readFileSync(p, 'utf8
return null;
}
+/**
+ * Read CRON_SECRET off the live Cloud Run service, which is where production's
+ * authoritative copy lives. `.env` does not carry it and `vercel env pull`
+ * returns empty for secret-type vars, so on a fresh machine this is the only
+ * place it can come from. Every other thing this script does already requires
+ * an authenticated gcloud session, so needing one here costs nothing extra.
+ *
+ * Returns null rather than throwing: a caller that also has `--env-file` or a
+ * process env should not be denied by an unrelated gcloud failure.
+ */
+export function cronSecretFromService(describe = defaultDescribeService) {
+ let svc;
+ try {
+ svc = JSON.parse(describe());
+ } catch {
+ return null;
+ }
+ const env = svc?.spec?.template?.spec?.containers?.[0]?.env || [];
+ return env.find((e) => e.name === 'CRON_SECRET')?.value || null;
+}
+
+function defaultDescribeService() {
+ return execFileSync(
+ 'gcloud',
+ [
+ 'run',
+ 'services',
+ 'describe',
+ 'three-ws-api',
+ `--region=${LOCATION}`,
+ `--project=${PROJECT}`,
+ '--format=json',
+ ],
+ { encoding: 'utf8', maxBuffer: 8 * 1024 * 1024, stdio: ['ignore', 'pipe', 'ignore'] },
+ );
+}
+
async function gcloud(cmdArgs) {
return execFileP(
'gcloud',
@@ -141,9 +218,11 @@ async function main() {
process.exit(1);
}
- const secret = cronSecretFromArgs(argv) || process.env.CRON_SECRET;
+ const secret = cronSecretFromArgs(argv) || process.env.CRON_SECRET || cronSecretFromService();
if (!secret) {
- console.error('CRON_SECRET not set: pass --env-file <pulled prod.env> or export it.');
+ console.error(
+ 'CRON_SECRET not set: pass --env-file <pulled prod.env>, export it, or authenticate gcloud so it can be read off the three-ws-api service.',
+ );
process.exit(1);
}
@@ -153,7 +232,18 @@ async function main() {
process.exit(1);
}
- const queue = [...crons];
+ let selected;
+ try {
+ selected = selectCrons(crons, argv);
+ } catch (err) {
+ console.error(err.message);
+ process.exit(1);
+ }
+ if (selected.length !== crons.length) {
+ console.log(`--only: syncing ${selected.length} of ${crons.length} declared crons.`);
+ }
+
+ const queue = [...selected];
const results = [];
const failures = [];
await Promise.all(
@@ -174,7 +264,7 @@ async function main() {
const created = results.filter((r) => r.action === 'created');
console.log(
- `\n${results.length}/${crons.length} jobs synced; ${created.length} created ENABLED; ${failures.length} failed.`,
+ `\n${results.length}/${selected.length} jobs synced; ${created.length} created ENABLED; ${failures.length} failed.`,
);
console.log(
stateAction
diff --git a/tests/cron-scheduler-sync.test.js b/tests/cron-scheduler-sync.test.js
index 47cca0072..0eb758b65 100644
--- a/tests/cron-scheduler-sync.test.js
+++ b/tests/cron-scheduler-sync.test.js
@@ -15,7 +15,13 @@
import { describe, it, expect } from 'vitest';
import { readFileSync } from 'node:fs';
-import { jobId, runStateAction, cronSecretFromArgs } from '../scripts/create-gcp-scheduler.mjs';
+import {
+ jobId,
+ runStateAction,
+ cronSecretFromArgs,
+ cronSecretFromService,
+ selectCrons,
+} from '../scripts/create-gcp-scheduler.mjs';
describe('runStateAction', () => {
it('leaves run state alone for a bare config sync', () => {
@@ -36,6 +42,84 @@ describe('runStateAction', () => {
});
});
+// Production's authoritative CRON_SECRET lives on the Cloud Run service, not in
+// any checked-out file: .env does not carry it and `vercel env pull` returns
+// empty for secret-type vars. Without this fallback, repairing a drifted cron on
+// a fresh machine dead-ends on "CRON_SECRET not set" even with gcloud working.
+describe('cronSecretFromService', () => {
+ const service = (env) => () => JSON.stringify({ spec: { template: { spec: { containers: [{ env }] } } } });
+
+ it('reads the secret out of the service env', () => {
+ expect(
+ cronSecretFromService(service([{ name: 'DATABASE_URL', value: 'postgres://x' }, { name: 'CRON_SECRET', value: 's3cr3t' }])),
+ ).toBe('s3cr3t');
+ });
+
+ it('returns null when the service does not carry one', () => {
+ expect(cronSecretFromService(service([{ name: 'DATABASE_URL', value: 'postgres://x' }]))).toBe(null);
+ });
+
+ // A secret-manager-backed var has valueFrom instead of value; treating that
+ // empty string as a secret would write an unauthenticated header to 111 jobs.
+ it('rejects a secret-manager reference that carries no inline value', () => {
+ expect(cronSecretFromService(service([{ name: 'CRON_SECRET', valueFrom: { secretKeyRef: { key: 'latest' } } }]))).toBe(
+ null,
+ );
+ });
+
+ // An unauthenticated gcloud must not deny a caller who passed --env-file.
+ it('returns null instead of throwing when gcloud fails', () => {
+ expect(
+ cronSecretFromService(() => {
+ throw new Error('Reauthentication failed.');
+ }),
+ ).toBe(null);
+ });
+});
+
+describe('selectCrons', () => {
+ const crons = [
+ { path: '/api/cron/garment-job-sweep', schedule: '*/10 * * * *' },
+ { path: '/api/cron/garment-catalog-audit', schedule: '20 6 * * *' },
+ { path: '/api/cron/economy-tick', schedule: '* * * * *' },
+ ];
+
+ it('syncs every declared cron when no filter is passed', () => {
+ expect(selectCrons(crons, ['--env-file', '/tmp/prod.env'])).toEqual(crons);
+ });
+
+ it('narrows a repair to the one drifted job instead of the whole fleet', () => {
+ expect(selectCrons(crons, ['--only', 'garment-job-sweep']).map((c) => c.path)).toEqual([
+ '/api/cron/garment-job-sweep',
+ ]);
+ });
+
+ it('accepts a comma-separated list', () => {
+ expect(selectCrons(crons, ['--only', 'garment-job-sweep,economy-tick']).map((c) => c.path)).toEqual([
+ '/api/cron/garment-job-sweep',
+ '/api/cron/economy-tick',
+ ]);
+ });
+
+ // A filter that quietly matched nothing would print a clean "0/0 jobs synced"
+ // and leave the drifted cron exactly as missing as it was.
+ it('refuses a filter that matches nothing rather than syncing zero jobs', () => {
+ expect(() => selectCrons(crons, ['--only', 'no-such-cron'])).toThrow(/matched none/);
+ });
+
+ it('refuses a bare --only with no value', () => {
+ expect(() => selectCrons(crons, ['--only'])).toThrow(/needs a value/);
+ expect(() => selectCrons(crons, ['--only', '--resume'])).toThrow(/needs a value/);
+ });
+
+ it('matches a real declared cron path from vercel.json', () => {
+ const { crons: declared } = JSON.parse(readFileSync(new URL('../vercel.json', import.meta.url), 'utf8'));
+ const picked = selectCrons(declared, ['--only', 'garment-job-sweep']);
+ expect(picked).toHaveLength(1);
+ expect(jobId(picked[0].path)).toBe('cron--api-cron-garment-job-sweep');
+ });
+});
+
describe('jobId', () => {
// The leading slash of the path becomes a hyphen of its own, so a live job id
// carries a DOUBLE hyphen after the prefix. Every hand-typed `gcloud scheduler