Repository navigation
Expand file tree
/
Copy pathecosystem.config.js
More file actions
322 lines (298 loc) · 11.9 KB
/
Copy pathecosystem.config.js
File metadata and controls
322 lines (298 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/**
* PM2 Ecosystem Configuration for Lightdash Local Development
*
* This file configures PM2 to manage all development processes.
* Use `pnpm pm2:start` to start all processes.
*
* Multi-instance support:
* Set LD_INSTANCE_ID to namespace PM2 process names (default: 'lightdash').
* Port env vars (PORT, FE_PORT, SCHEDULER_PORT, etc.) override defaults.
* Use scripts/dev-ports.sh to manage port allocation across worktrees.
*
* Prerequisites:
* - Docker services running: `/docker-dev`
* - Dependencies installed: `pnpm install`
*
* Process overview:
* - <instanceId>-api: Backend API server (default port 8080)
* - <instanceId>-api-routes-watch: Regenerates TSOA routes on controller changes
* - <instanceId>-scheduler: Background job processor (default port 8081)
* - <instanceId>-frontend: Vite dev server (default port 3000)
* - <instanceId>-common-watch: TypeScript watcher for common package
* - <instanceId>-warehouses-watch: TypeScript watcher for warehouses package
* - <instanceId>-maple: Maple local-mode tracing server (default port 4320)
*
* Logs are stored in ~/.pm2/logs/ (PM2 default location)
*/
const { spawnSync } = require('child_process');
const os = require('os');
const path = require('path');
const dotenv = require('dotenv');
// Load environment variables: base config first, then local overrides
const baseEnvPath = path.resolve(__dirname, '.env.development');
const localEnvPath = path.resolve(__dirname, '.env.development.local');
const baseEnv = dotenv.config({ path: baseEnvPath }).parsed || {};
const localEnv = dotenv.config({ path: localEnvPath }).parsed || {};
// Merge: local overrides base
const env = { ...baseEnv, ...localEnv };
// Add venv/bin to PATH for dbt access
const venvBinPath = path.join(__dirname, 'venv', 'bin');
const envWithPath = {
...env,
PATH: `${venvBinPath}:${process.env.PATH}`,
// Production runs UTC. Without this, node-postgres parses the DB's naive-UTC
// timestamps as local time, skewing every timestamp by the host's UTC offset
// (false "message timed out" errors, wrong relative times in the UI).
TZ: 'UTC',
};
// Instance ID for namespacing PM2 process names (supports multiple worktrees)
const instanceId = env.LD_INSTANCE_ID || 'lightdash';
// Configurable ports (defaults match single-instance behavior)
const apiPort = env.PORT || '8080';
const schedulerPort = env.SCHEDULER_PORT || '8081';
const debugPort = env.DEBUG_PORT || '9229';
const fePort = env.FE_PORT || undefined; // Vite auto-detects if not set
const sdkTestPort = env.SDK_TEST_PORT || '3030';
const sdkTestEnabled =
(process.env.LD_ENABLE_SDK_TEST ?? env.LD_ENABLE_SDK_TEST) === 'true';
const maplePort = env.MAPLE_PORT || '4320';
// Maple is a standalone binary (not a node_modules bin), so it may be absent.
// Resolve it up front: without it there is nothing to export traces to, and an
// OTLP exporter pointed at a dead port just logs export failures every batch.
const mapleBin = (() => {
const { stdout } = spawnSync('sh', ['-c', 'command -v maple'], {
encoding: 'utf8',
});
return (stdout || '').trim() || undefined;
})();
// Maple anchors its pidfile and store markers in the data dir's PARENT, so
// each instance needs its own parent directory — sibling data dirs directly
// under ~/.maple would share one maple.pid and lock each other out.
const mapleDataDir = path.join(
os.homedir(),
'.maple',
'instances',
instanceId,
'data',
);
// Each app adds its own OTEL_SERVICE_NAME on top of this: service.name is what
// namespaces traces per checkout in the Maple UI.
const tracingEnv = mapleBin
? {
LIGHTDASH_OTEL_TRACES_ENABLED: 'true',
OTEL_EXPORTER_OTLP_ENDPOINT: `http://127.0.0.1:${maplePort}`,
}
: {};
// Log the root directory so it's obvious which worktree PM2 is running from
console.log(`\n Lightdash PM2 root: ${__dirname}`);
console.log(` Instance ID: ${instanceId}\n`);
if (!mapleBin) {
console.log(
' maple not found on PATH — local tracing disabled.\n' +
' Install it with: curl -fsSL https://maple.dev/cli/install | sh\n',
);
}
const frontendArgs = fePort ? `--port ${fePort}` : undefined;
// Opt-in via LD_WATCHER_MEMORY_CAP (e.g. 4G in .env.development.local): hard
// backstop that bounces a runaway tsc watcher. Keep it well above GOMEMLIMIT —
// a cap below the full-rebuild peak kills tsc mid-build and loops on cold
// rebuilds. Unset = no cap.
const watcherMemoryCap =
process.env.LD_WATCHER_MEMORY_CAP ?? env.LD_WATCHER_MEMORY_CAP;
const watcherMemoryCapConfig = watcherMemoryCap
? { max_memory_restart: watcherMemoryCap }
: {};
// tsgo (TS7 native) is a Go binary: GOMEMLIMIT is a soft cap its GC works to
// stay under, releasing freed memory back to the OS, so branch-switch rebuilds
// can't balloon to multi-GB peaks. LD_WATCHER_GOMEMLIMIT overrides ('off'
// disables).
const watcherGoMemLimit =
process.env.LD_WATCHER_GOMEMLIMIT ?? env.LD_WATCHER_GOMEMLIMIT ?? '1500MiB';
const watcherEnv =
watcherGoMemLimit === 'off' ? {} : { GOMEMLIMIT: watcherGoMemLimit };
module.exports = {
apps: [
// Backend API Server
{
name: `${instanceId}-api`,
script: 'src/index.ts',
interpreter: 'node',
node_args: `--import tsx --inspect=0.0.0.0:${debugPort}`,
cwd: path.join(__dirname, 'packages/backend'),
env: {
...envWithPath,
LIGHTDASH_MODE: 'development',
HEADLESS: 'true',
NODE_ENV: 'development',
...tracingEnv,
OTEL_SERVICE_NAME: instanceId,
PORT: apiPort,
},
// Restart when common's CJS build completes (single sentinel
// file) rather than on every emitted dist file.
watch: ['src', '../common/dist/cjs/.tsbuildinfo'],
// Setting ignore_watch replaces chokidar's default node_modules
// ignore; without the explicit entries + followSymlinks:false,
// mcp-chart-app/node_modules (a symlink back into packages/common)
// turns every common rebuild into thousands of API restarts.
ignore_watch: [
'src/generated/swagger.json',
'**/*.test.ts',
'**/node_modules',
'**/node_modules/**',
],
watch_options: { followSymlinks: false },
watch_delay: 500,
autorestart: true,
kill_timeout: 5000,
merge_logs: true,
time: true,
},
// TSOA route generation watcher
{
name: `${instanceId}-api-routes-watch`,
script: 'pnpm',
// common-watch already emits ../common/dist, so skip the root
// script's extra common-build: it duplicates work and crash-loops
// at 1Hz whenever a stale incremental build reports errors.
args: '-F backend generate-api-dev',
interpreter: 'none',
cwd: __dirname,
env: envWithPath,
watch: false,
autorestart: true,
exp_backoff_restart_delay: 1000,
kill_timeout: 3000,
merge_logs: true,
time: true,
},
// Background Job Scheduler
{
name: `${instanceId}-scheduler`,
script: 'src/scheduler.ts',
interpreter: 'node',
node_args: '--import tsx',
cwd: path.join(__dirname, 'packages/backend'),
env: {
...envWithPath,
NODE_ENV: 'development',
...tracingEnv,
OTEL_SERVICE_NAME: `${instanceId}-scheduler`,
PORT: schedulerPort,
LIGHTDASH_PROMETHEUS_ENABLED: 'false',
},
watch: false,
autorestart: true,
kill_timeout: 5000,
merge_logs: true,
time: true,
},
// Frontend Vite Dev Server
{
name: `${instanceId}-frontend`,
script: 'node_modules/.bin/vite',
...(frontendArgs ? { args: frontendArgs } : {}),
interpreter: 'none',
cwd: path.join(__dirname, 'packages/frontend'),
env: {
NODE_ENV: 'development',
PORT: apiPort,
},
watch: false,
autorestart: false,
kill_timeout: 5000,
merge_logs: true,
time: true,
},
// Common Package TypeScript Watcher
{
name: `${instanceId}-common-watch`,
script: '../../node_modules/.bin/tsc',
args: '--build --watch --preserveWatchOutput --incremental tsconfig.build.json',
interpreter: 'none',
cwd: path.join(__dirname, 'packages/common'),
env: watcherEnv,
watch: false,
autorestart: false,
...watcherMemoryCapConfig,
kill_timeout: 3000,
merge_logs: true,
time: true,
},
// Formula Package TypeScript Watcher
{
name: `${instanceId}-formula-watch`,
script: '../../node_modules/.bin/tsc',
args: '--build --watch --preserveWatchOutput tsconfig.json',
interpreter: 'none',
cwd: path.join(__dirname, 'packages/formula'),
env: watcherEnv,
watch: false,
autorestart: false,
...watcherMemoryCapConfig,
kill_timeout: 3000,
merge_logs: true,
time: true,
},
// Warehouses Package TypeScript Watcher
{
name: `${instanceId}-warehouses-watch`,
script: '../../node_modules/.bin/tsc',
args: '--build --watch --preserveWatchOutput tsconfig.json',
interpreter: 'none',
cwd: path.join(__dirname, 'packages/warehouses'),
env: watcherEnv,
watch: false,
autorestart: false,
...watcherMemoryCapConfig,
kill_timeout: 3000,
merge_logs: true,
time: true,
},
// SDK Test App (opt-in via LD_ENABLE_SDK_TEST=true — a full Vite dev
// server that costs ~1.5GB RSS, so it must not start by default)
...(sdkTestEnabled
? [
{
name: `${instanceId}-sdk-test`,
script: 'node_modules/.bin/vite',
args: `--port ${sdkTestPort}`,
interpreter: 'none',
cwd: path.join(__dirname, 'packages/sdk-test-app'),
env: {
NODE_ENV: 'development',
},
watch: false,
autorestart: false,
kill_timeout: 3000,
merge_logs: true,
time: true,
},
]
: []),
// Maple local mode: OTLP ingest + embedded ClickHouse + trace UI.
// --offline serves the UI from the binary (same-origin, no internet, no
// Chrome local-network prompt). --on-dirty-store wipe keeps it from
// refusing to boot after PM2 SIGKILLs it; local traces are disposable.
...(mapleBin
? [
{
name: `${instanceId}-maple`,
script: mapleBin,
args: `start --port ${maplePort} --data-dir ${mapleDataDir} --offline --on-dirty-store wipe`,
interpreter: 'none',
cwd: __dirname,
env: {
NODE_ENV: 'development',
MAPLE_NO_UPDATE_CHECK: '1',
},
watch: false,
autorestart: true,
kill_timeout: 3000,
merge_logs: true,
time: true,
},
]
: []),
],
};