Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Close runtime resources after standalone migrations (#1190)

Regan Bell106cf7130745
diff --git a/package.json b/package.json
index 335adeccd..6e5103398 100644
--- a/package.json
+++ b/package.json
@@ -36,7 +36,7 @@
"sandbox:local:build": "bash scripts/local-sandbox-build.sh",
"sandbox:porter:build": "docker build --platform linux/amd64 -t \"${PORTER_SANDBOX_IMAGE:?Set PORTER_SANDBOX_IMAGE to the registry tag to publish}\" porter && docker push \"$PORTER_SANDBOX_IMAGE\"",
"deploy:porter-runner-image": "docker build --platform linux/amd64 -t \"${PORTER_DEPLOY_RUNNER_IMAGE:?Set PORTER_DEPLOY_RUNNER_IMAGE to the registry tag to publish}\" -f deploy/porter-runner/Dockerfile . && docker push \"$PORTER_DEPLOY_RUNNER_IMAGE\"",
- "test:pg": "node --test --test-concurrency=1 test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-grant-store.test.ts test/postgres-admin-grants.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-map.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-migration-adoption.test.ts test/cron-queue.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/postgres-instance-registry.test.ts test/run-retry-delay.test.ts test/postgres-run-stream.test.ts",
+ "test:pg": "node --test --test-concurrency=1 test/postgres-swarms.test.ts test/postgres-store.test.ts test/postgres-grant-store.test.ts test/postgres-admin-grants.test.ts test/postgres-file-artifact-store.test.ts test/postgres-file-upload-store.test.ts test/postgres-directory-store.test.ts test/postgres-map.test.ts test/deploy-store-pg.test.ts test/postgres-cron-fires.test.ts test/postgres-migration-adoption.test.ts test/cron-queue.test.ts test/postgres-metrics-sink.test.ts test/postgres-error-log.test.ts test/postgres-audit-log.test.ts test/postgres-budget.test.ts test/postgres-rate-limiter.test.ts test/postgres-credential-usage-sink.test.ts test/postgres-config-store.test.ts test/postgres-delivery-store.test.ts test/postgres-replay-dedupe.test.ts test/postgres-broker-sessions.test.ts test/postgres-egress-audit-sink.test.ts test/postgres-memory-service.test.ts test/run-signal-store.test.ts test/postgres-listener.test.ts test/postgres-run-activity-store.test.ts test/leader-lease.test.ts test/advisory-lock.test.ts test/persistence-init-retry.test.ts test/migrate-principals.test.ts test/postgres-surface-cache.test.ts test/postgres-instance-registry.test.ts test/run-retry-delay.test.ts test/postgres-run-stream.test.ts test/migrate-main.test.ts",
"livetest": "node scripts/api-livetest.ts",
"bench:memory": "node --env-file-if-exists=.env scripts/memory-bench.ts",
"prepare": "node .husky/install.mjs",
diff --git a/src/migrate-main.ts b/src/migrate-main.ts
index 4a9dfdd0a..88b1cdaf5 100644
--- a/src/migrate-main.ts
+++ b/src/migrate-main.ts
@@ -3,6 +3,10 @@ import { migrateRegisteredPgSchemas } from "./persistence/pg-pool.ts";
import { buildApp } from "./wiring.ts";
const config = loadConfig();
-buildApp(config);
-await migrateRegisteredPgSchemas(config.databaseUrl);
-console.log("[qm:migrate] database migrations applied");
+const { runtime } = buildApp(config);
+try {
+ await migrateRegisteredPgSchemas(config.databaseUrl);
+ console.log("[qm:migrate] database migrations applied");
+} finally {
+ await runtime.stop();
+}
diff --git a/test/migrate-main.test.ts b/test/migrate-main.test.ts
new file mode 100644
index 000000000..d4fbed12e
--- /dev/null
+++ b/test/migrate-main.test.ts
@@ -0,0 +1,39 @@
+import assert from "node:assert/strict";
+import { execFile } from "node:child_process";
+import { mkdtemp, rm } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { fileURLToPath } from "node:url";
+import { promisify } from "node:util";
+import test from "node:test";
+
+const databaseUrl = process.env.DATABASE_URL;
+
+test(
+ "migration command closes runtime listeners and exits naturally",
+ { skip: !databaseUrl, timeout: 45_000 },
+ async () => {
+ const dataDir = await mkdtemp(join(tmpdir(), "qm-migrate-test-"));
+ try {
+ const { stdout } = await promisify(execFile)(process.execPath, ["src/migrate-main.ts"], {
+ cwd: fileURLToPath(new URL("..", import.meta.url)),
+ env: {
+ PATH: process.env.PATH,
+ NODE_ENV: "test",
+ DATABASE_URL: databaseUrl,
+ SESSION_STORE: "postgres",
+ HARNESS: "mock",
+ SANDBOX_BACKEND: "local",
+ CONNECTOR_SECRET_KEY: "migration-test-connector-secret",
+ CAPABILITY_SECRET: "migration-test-capability-secret",
+ DATA_DIR: dataDir,
+ },
+ timeout: 35_000,
+ killSignal: "SIGKILL",
+ });
+ assert.match(stdout, /\[qm:migrate\] database migrations applied/);
+ } finally {
+ await rm(dataDir, { recursive: true, force: true });
+ }
+ },
+);