Skip to content

Commit

Permalink
fix: Reduce open handles (#57)
Browse files Browse the repository at this point in the history
- Prevent the open handle on the tests due to the db connection and
makes failure on db migrations "fail fast".
- Introduce `AbortController` to clients to abort polling requests when
agents shut down.
  • Loading branch information
nadeesha authored Jan 12, 2024
1 parent 4984410 commit 0dbf051
Show file tree
Hide file tree
Showing 16 changed files with 667 additions and 153 deletions.
11 changes: 6 additions & 5 deletions control-plane/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
"version": "0.0.16",
"description": "",
"scripts": {
"test": "jest src --setupFiles dotenv/config --forceExit --detectOpenHandles",
"test:dev": "jest --watch src --setupFiles dotenv/config --forceExit --onlyChanged",
"dev": "nodemon src/index.ts",
"test": "tsx src/utilities/migrate.ts && jest src --runInBand --setupFiles dotenv/config --forceExit --detectOpenHandles",
"test:dev": "tsx src/utilities/migrate.ts && jest --runInBand --watch src --setupFiles dotenv/config --forceExit --onlyChanged",
"dev": "tsx src/utilities/migrate.ts && nodemon src/index.ts",
"migrations": "npx drizzle-kit generate:pg --schema src/modules/data.ts",
"build": "tsc",
"start": "node dist/index.js",
"start": "node dist/utilities/migrate.js && node dist/index.js",
"deploy": "git add . && git commit -m 'update' && git push origin main && fly deploy"
},
"author": "",
Expand Down Expand Up @@ -40,7 +40,8 @@
"devDependencies": {
"@types/jsonwebtoken": "^9.0.2",
"@types/pg": "^8.10.2",
"@types/ws": "^8.5.5"
"@types/ws": "^8.5.5",
"tsx": "^4.7.0"
},
"private": true
}
2 changes: 1 addition & 1 deletion control-plane/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require("dotenv").config();
import "./utilities/env";

import { initServer } from "@ts-rest/fastify";
import fastify from "fastify";
Expand Down
2 changes: 1 addition & 1 deletion control-plane/src/modules/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { eq } from "drizzle-orm";
import * as data from "./data";
import jwt from "jsonwebtoken";
import { invariant } from "../utils";
import { invariant } from "../utilities/invariant";

const jwtSecret = invariant(
process.env.JWT_SECRET,
Expand Down
1 change: 1 addition & 0 deletions control-plane/src/modules/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const registerCron = async (
};

process.on("beforeExit", () => {
console.log("%%%%%%% beforeExit");
intervals.forEach((intervalId) => {
clearInterval(intervalId);
});
Expand Down
15 changes: 2 additions & 13 deletions control-plane/src/modules/data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { drizzle } from "drizzle-orm/node-postgres";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import {
integer,
json,
Expand All @@ -10,7 +9,7 @@ import {
varchar,
} from "drizzle-orm/pg-core";
import { Pool } from "pg";
import { invariant } from "../utils";
import { invariant } from "../utilities/invariant";
import { sql } from "drizzle-orm";

const connectionString = invariant(
Expand All @@ -24,7 +23,7 @@ console.log("Attempting to connect to database", {
sslDisabled,
});

const pool = new Pool({
export const pool = new Pool({
connectionString,
ssl: sslDisabled
? false
Expand Down Expand Up @@ -130,13 +129,3 @@ export const db = drizzle(pool);
export const isAlive = async () => {
await db.execute(sql`select 1`);
};

(async function () {
console.log("Migrating database...");
try {
await migrate(db, { migrationsFolder: "./drizzle" });
} catch (e) {
console.error("Error migrating database", e);
process.exit(1);
}
})();
35 changes: 20 additions & 15 deletions control-plane/src/modules/influx.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InfluxDB } from '@influxdata/influxdb-client'
import { invariant } from '../utils';
import { InfluxDB } from "@influxdata/influxdb-client";
import { invariant } from "../utilities/invariant";

// Temporary feature flag for influxdb
let client: InfluxDB | undefined;
Expand All @@ -9,21 +9,26 @@ if (process.env.INFLUXDB_ENABLED) {
"INFLUXDB_TOKEN must be set"
);

const url = invariant(
process.env.INFLUXDB_URL,
"INFLUXDB_URL must be set"
);
const url = invariant(process.env.INFLUXDB_URL, "INFLUXDB_URL must be set");

client = new InfluxDB({url, token})
client = new InfluxDB({ url, token });
} else {
console.warn("INFLUXDB_ENABLED is not set, metrics will not be sent to influxdb");
console.warn(
"INFLUXDB_ENABLED is not set, metrics will not be sent to influxdb"
);
}

export const INFLUXDB_ORG = 'differential';
export const INFLUXDB_BUCKET = 'differential';

export const queryClient = client?.getQueryApi(INFLUXDB_ORG)
export const writeClient = client?.getWriteApi(INFLUXDB_ORG, INFLUXDB_BUCKET , 'ms', {
...(process.env['NODE_ENV'] !== 'production' ? { flushInterval: 1000 } : {})
})
export const INFLUXDB_ORG = "differential";
export const INFLUXDB_BUCKET = "differential";

export const queryClient = client?.getQueryApi(INFLUXDB_ORG);
export const writeClient = client?.getWriteApi(
INFLUXDB_ORG,
INFLUXDB_BUCKET,
"ms",
{
...(process.env["NODE_ENV"] !== "production"
? { flushInterval: 1000 }
: {}),
}
);
5 changes: 5 additions & 0 deletions control-plane/src/utilities/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// separating this and importing it allows
// tsx to run this before the rest of the app
// for dev purposes

require("dotenv").config();
File renamed without changes.
17 changes: 17 additions & 0 deletions control-plane/src/utilities/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import "./env";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import * as data from "../modules/data";

(async function runMigrations() {
console.log("Migrating database...");

try {
await migrate(data.db, { migrationsFolder: "./drizzle" });
console.log("Database migrated successfully");
} catch (e) {
console.error("Error migrating database", e);
process.exit(1);
}

await data.pool.end();
})();
Loading

0 comments on commit 0dbf051

Please sign in to comment.