mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-21 04:12:08 +03:00
refactor(data): strict dev/production stages and data naming
- services/data/stage.ts: stage(mode) throws on anything but dev or production, used by all three configs, build.ts and deploy.ts - packages/stats -> packages/data (@opencode/data-app, -core, -server) - StatsSyncWorkflow -> DataSyncWorkflow, instance ids data-hourly-/data-history-, Data* resource keys, data sync log lines - worker, route, workflow and cron names unchanged
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Data
|
||||
|
||||
Deploys the opencode.ai/data site from `packages/stats` as three Workers with `cf`:
|
||||
Deploys the opencode.ai/data site from `packages/data` as three Workers with `cf`:
|
||||
|
||||
| Worker | Config | What it is |
|
||||
| ---------------------------- | ---------------------- | ----------------------------------------------------------------------------- |
|
||||
@@ -18,7 +18,12 @@ its config declares.
|
||||
The database is the `opencode-stats` PlanetScale database: the `dev` branch for dev and `production`
|
||||
for production, through the `data-worker-<stage>` password. It is not managed as code.
|
||||
|
||||
Every config, `script/build.ts` and `script/deploy.ts` take the stage from `--mode`, which must be `dev` or
|
||||
`production` (`stage.ts`). Anything else, including no mode, throws instead of falling back to production, so pass
|
||||
`--mode` to `cf` commands that read the configs too (`cf deploy --dry-run --mode dev`).
|
||||
|
||||
```bash
|
||||
bun run build --mode dev
|
||||
bun run dev # the site on your machine (packages/data/app)
|
||||
bun run build:dev # or build:production
|
||||
bun run typecheck
|
||||
```
|
||||
|
||||
@@ -1,30 +1,31 @@
|
||||
import { bindings, defineConfig, triggers } from "cf/config"
|
||||
import { LOCALES } from "../../packages/stats/app/src/lib/language"
|
||||
import { LOCALES } from "../../packages/data/app/src/lib/language"
|
||||
import { ogWorkerName } from "./og.config"
|
||||
import { stage, stageHost } from "./stage"
|
||||
|
||||
// The data site (packages/stats/app, SolidStart) at /data and /<locale>/data on the main host. `--mode dev` deploys
|
||||
// opencode-data-dev on dev.opencode.ai; any other mode deploys opencode-data-production on opencode.ai. The routes
|
||||
// are its only way in: it has no custom domain or workers.dev URL. An exact `/data` pattern misses `/data?query`, so
|
||||
// each pattern is a `/data*` prefix, and route patterns can't wildcard the middle of a path, hence one per locale.
|
||||
// The data site (packages/data/app, SolidStart) at /data and /<locale>/data on the main host. `--mode dev` deploys
|
||||
// opencode-data-dev on dev.opencode.ai and `--mode production` opencode-data-production on opencode.ai; any other
|
||||
// mode throws (stage.ts). The routes are its only way in: it has no custom domain or workers.dev URL. An exact `/data`
|
||||
// pattern misses `/data?query`, so each pattern is a `/data*` prefix, and route patterns can't wildcard the middle of
|
||||
// a path, hence one per locale.
|
||||
// script/build.ts builds this, og.config.ts and sync.config.ts into one Build Output tree.
|
||||
export default defineConfig(({ mode }) => {
|
||||
const dev = mode === "dev"
|
||||
const stage = dev ? "dev" : "production"
|
||||
const host = dev ? "dev.opencode.ai" : "opencode.ai"
|
||||
const current = stage(mode)
|
||||
const host = stageHost(current)
|
||||
return {
|
||||
// Anomaly
|
||||
accountId: "15d29c8639fd3733b1b5486a2acfd968",
|
||||
worker: {
|
||||
name: `opencode-data-${stage}`,
|
||||
name: `opencode-data-${current}`,
|
||||
compatibilityDate: "2026-07-25",
|
||||
// The Nitro server is built with node compatibility, which also copies string bindings into process.env
|
||||
// (packages/stats/core/src/resource.ts).
|
||||
// (packages/data/core/src/resource.ts).
|
||||
compatibilityFlags: ["nodejs_compat"],
|
||||
env: {
|
||||
ASSETS: bindings.assets(),
|
||||
// The private Worker that renders Open Graph images (og.config.ts).
|
||||
OG_WORKER: bindings.worker({ worker: ogWorkerName(mode) }),
|
||||
STAGE: bindings.text(stage),
|
||||
STAGE: bindings.text(current),
|
||||
PUBLIC_URL: bindings.text(`https://${host}/data`),
|
||||
// Set from AWS Secrets Manager (opencode/<stage>/DATA_*) by the deploy job.
|
||||
DATA_DATABASE_HOST: bindings.secret(),
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { defineConfig } from "cf/config"
|
||||
import { stage } from "./stage"
|
||||
|
||||
export const ogWorkerName = (mode: string | undefined) => `opencode-data-og-${mode === "dev" ? "dev" : "production"}`
|
||||
export const ogWorkerName = (mode: string | undefined) => `opencode-data-og-${stage(mode)}`
|
||||
|
||||
// The private Worker that renders the data pages' Open Graph images (packages/stats/app/src/og/worker.ts). It has no
|
||||
// The private Worker that renders the data pages' Open Graph images (packages/data/app/src/og/worker.ts). It has no
|
||||
// URL or route; the data Worker reaches it through its OG_WORKER Service Binding. It is its own Worker so the data
|
||||
// Worker doesn't load the Wasm engines and fonts it ships as modules (script/build.ts).
|
||||
export default defineConfig(({ mode }) => ({
|
||||
|
||||
@@ -5,7 +5,9 @@
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"build": "bun script/build.ts",
|
||||
"dev": "bun run --cwd ../../packages/data/app dev",
|
||||
"build:dev": "bun script/build.ts --mode dev",
|
||||
"build:production": "bun script/build.ts --mode production",
|
||||
"deploy:dev": "bun script/deploy.ts --mode dev",
|
||||
"deploy:production": "bun script/deploy.ts --mode production",
|
||||
"typecheck": "bun check --no-script",
|
||||
@@ -14,8 +16,8 @@
|
||||
"devDependencies": {
|
||||
"@cloudflare/build-output-utils": "0.8.5",
|
||||
"@cloudflare/config": "0.23.0",
|
||||
"@opencode/stats-app": "workspace:*",
|
||||
"@opencode/stats-server": "workspace:*",
|
||||
"@opencode/data-app": "workspace:*",
|
||||
"@opencode/data-server": "workspace:*",
|
||||
"@types/bun": "catalog:",
|
||||
"@typescript/native": "catalog:",
|
||||
"cf": "1.0.0-beta.12",
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
// Builds the data site's three Workers into one Build Output Specification tree (.cloudflare/output) for
|
||||
// `cf deploy --prebuilt --worker <dir>` (script/deploy.ts):
|
||||
// - default: the SolidStart site (packages/stats/app, Nitro's cloudflare-module preset) with .output/public as assets
|
||||
// - default: the SolidStart site (packages/data/app, Nitro's cloudflare-module preset) with .output/public as assets
|
||||
// - og: the Open Graph image Worker, whose Wasm engines and fonts ship as modules
|
||||
// - sync: the StatsSyncWorkflow and its cron handler (packages/stats/server)
|
||||
// - sync: the DataSyncWorkflow and its cron handler (packages/data/server)
|
||||
// cf's own build step can't detect these projects, so they deploy prebuilt.
|
||||
//
|
||||
// bun script/build.ts [--mode dev|production]
|
||||
// bun script/build.ts --mode dev|production
|
||||
import { cp, mkdir, readdir } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import {
|
||||
@@ -16,14 +16,14 @@ import {
|
||||
writeWorkerConfig,
|
||||
} from "@cloudflare/build-output-utils"
|
||||
import { resolveAndParseConfig } from "@cloudflare/config"
|
||||
import { stage } from "../stage"
|
||||
|
||||
const root = path.resolve(import.meta.dir, "..")
|
||||
const app = path.resolve(root, "../../packages/stats/app")
|
||||
const server = path.resolve(root, "../../packages/stats/server")
|
||||
const app = path.resolve(root, "../../packages/data/app")
|
||||
const server = path.resolve(root, "../../packages/data/server")
|
||||
const modeIndex = process.argv.indexOf("--mode")
|
||||
const mode = modeIndex === -1 ? "production" : process.argv[modeIndex + 1]
|
||||
if (mode !== "dev" && mode !== "production") throw new Error(`Unknown --mode ${mode}; use dev or production`)
|
||||
// STAGE picks the stage's headers for the static assets (packages/stats/app/src/lib/noindex.ts).
|
||||
const mode = stage(modeIndex === -1 ? undefined : process.argv[modeIndex + 1])
|
||||
// STAGE picks the stage's headers for the static assets (packages/data/app/src/lib/noindex.ts).
|
||||
const env = { ...process.env, STAGE: mode }
|
||||
|
||||
await Bun.$`bun run build`.cwd(app).env(env)
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
import { rm } from "node:fs/promises"
|
||||
import path from "node:path"
|
||||
import { resolveAndParseConfig } from "@cloudflare/config"
|
||||
import { stage } from "../stage"
|
||||
|
||||
const root = path.resolve(import.meta.dir, "..")
|
||||
const flag = (name: string) => {
|
||||
const index = process.argv.indexOf(name)
|
||||
return index === -1 ? undefined : process.argv[index + 1]
|
||||
}
|
||||
const mode = flag("--mode")
|
||||
if (mode !== "dev" && mode !== "production") throw new Error(`Unknown --mode ${mode}; use dev or production`)
|
||||
const mode = stage(flag("--mode"))
|
||||
const secretsFile = flag("--secrets-file")
|
||||
const secrets: Record<string, string> = secretsFile ? await Bun.file(secretsFile).json() : {}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
// The stage a `--mode` deploys. Only `dev` and `production` are stages: anything else, including no mode at all, throws,
|
||||
// so a typo or a bare `cf deploy` can never fall through to production.
|
||||
const hosts = {
|
||||
dev: "dev.opencode.ai",
|
||||
production: "opencode.ai",
|
||||
} as const
|
||||
|
||||
export type Stage = keyof typeof hosts
|
||||
|
||||
export function stage(mode: string | undefined): Stage {
|
||||
if (mode === "dev" || mode === "production") return mode
|
||||
throw new Error(`Unknown mode ${JSON.stringify(mode)}; pass --mode dev or --mode production`)
|
||||
}
|
||||
|
||||
// The host the stage's site is served from.
|
||||
export function stageHost(value: Stage) {
|
||||
return hosts[value]
|
||||
}
|
||||
@@ -1,28 +1,29 @@
|
||||
import { bindings, defineConfig, exports, triggers } from "cf/config"
|
||||
import { stage } from "./stage"
|
||||
|
||||
// The Worker that syncs R2 SQL aggregates into the data database (packages/stats/server/src/worker.ts): the
|
||||
// StatsSyncWorkflow, and the cron handler that starts its runs. The schedules must match
|
||||
// packages/stats/server/src/schedule.ts, which maps the daily one to a history run.
|
||||
// The Worker that syncs R2 SQL aggregates into the data database (packages/data/server/src/worker.ts): the
|
||||
// DataSyncWorkflow, and the cron handler that starts its runs. The schedules must match
|
||||
// packages/data/server/src/schedule.ts, which maps the daily one to a history run.
|
||||
export default defineConfig(({ mode }) => {
|
||||
const stage = mode === "dev" ? "dev" : "production"
|
||||
const name = `opencode-data-sync-${stage}`
|
||||
const current = stage(mode)
|
||||
const name = `opencode-data-sync-${current}`
|
||||
return {
|
||||
// Anomaly
|
||||
accountId: "15d29c8639fd3733b1b5486a2acfd968",
|
||||
worker: {
|
||||
name,
|
||||
compatibilityDate: "2026-07-25",
|
||||
// Also copies string bindings into process.env (packages/stats/core/src/resource.ts).
|
||||
// Also copies string bindings into process.env (packages/data/core/src/resource.ts).
|
||||
compatibilityFlags: ["nodejs_compat"],
|
||||
// The 5-minute maximum, for ranking a full week of geo rows.
|
||||
limits: { cpuMs: 300_000 },
|
||||
exports: {
|
||||
// One instance per run: a plan step, then one step per stat period and retention cohort.
|
||||
StatsSyncWorkflow: exports.workflow({ name }),
|
||||
DataSyncWorkflow: exports.workflow({ name }),
|
||||
},
|
||||
env: {
|
||||
StatsSyncWorkflow: bindings.workflow({ name, worker: name, exportName: "StatsSyncWorkflow" }),
|
||||
STAGE: bindings.text(stage),
|
||||
DataSyncWorkflow: bindings.workflow({ name, worker: name, exportName: "DataSyncWorkflow" }),
|
||||
STAGE: bindings.text(current),
|
||||
// Set from AWS Secrets Manager (opencode/<stage>/*) by the deploy job.
|
||||
DATA_DATABASE_HOST: bindings.secret(),
|
||||
DATA_DATABASE_USERNAME: bindings.secret(),
|
||||
|
||||
Reference in New Issue
Block a user