-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: migrate to graphile worker v0.16.6 (#1097)
* migrate to graphile worker v0.16.6 * remove stale docs link * fix jobs cleanup query
- Loading branch information
Showing
6 changed files
with
298 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
apps/webapp/app/services/db/graphileMigrationHelper.server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { runMigrations } from "graphile-worker"; | ||
import { PrismaClient, prisma } from "~/db.server"; | ||
import { env } from "~/env.server"; | ||
import { logger } from "~/services/logger.server"; | ||
import { PgNotifyService } from "./pgNotify.server"; | ||
import { z } from "zod"; | ||
|
||
export class GraphileMigrationHelperService { | ||
#prismaClient: PrismaClient; | ||
|
||
constructor(prismaClient: PrismaClient = prisma) { | ||
this.#prismaClient = prismaClient; | ||
} | ||
|
||
public async call() { | ||
this.#logDebug("GraphileMigrationHelperService.call"); | ||
|
||
await this.#detectAndPrepareForMigrations(); | ||
|
||
await runMigrations({ | ||
connectionString: env.DATABASE_URL, | ||
schema: env.WORKER_SCHEMA, | ||
}); | ||
} | ||
|
||
#logDebug(message: string, args?: any) { | ||
logger.debug(`[migrationHelper] ${message}`, args); | ||
} | ||
|
||
async #getLatestMigration() { | ||
const migrationQueryResult = await this.#prismaClient.$queryRawUnsafe(` | ||
SELECT id FROM ${env.WORKER_SCHEMA}.migrations | ||
ORDER BY id DESC LIMIT 1 | ||
`); | ||
|
||
const MigrationQueryResultSchema = z.array(z.object({ id: z.number() })); | ||
|
||
const migrationResults = MigrationQueryResultSchema.parse(migrationQueryResult); | ||
|
||
if (!migrationResults.length) { | ||
// no migrations applied yet | ||
return -1; | ||
} | ||
|
||
return migrationResults[0].id; | ||
} | ||
|
||
async #graphileSchemaExists() { | ||
const schemaCount = await this.#prismaClient.$executeRaw` | ||
SELECT schema_name FROM information_schema.schemata | ||
WHERE schema_name = ${env.WORKER_SCHEMA} | ||
`; | ||
|
||
return schemaCount === 1; | ||
} | ||
|
||
/** Helper for graphile-worker v0.14.0 migration. No-op if already migrated. */ | ||
async #detectAndPrepareForMigrations() { | ||
if (!(await this.#graphileSchemaExists())) { | ||
// no schema yet, likely first start | ||
return; | ||
} | ||
|
||
const latestMigration = await this.#getLatestMigration(); | ||
|
||
if (latestMigration < 0) { | ||
// no migrations found | ||
return; | ||
} | ||
|
||
// the first v0.14.0 migration has ID 11 | ||
if (latestMigration > 10) { | ||
// already migrated | ||
return; | ||
} | ||
|
||
// add 15s to graceful shutdown timeout, just to be safe | ||
const migrationDelayInMs = env.GRACEFUL_SHUTDOWN_TIMEOUT + 15000; | ||
|
||
this.#logDebug("Delaying worker startup due to pending migration", { | ||
latestMigration, | ||
migrationDelayInMs, | ||
}); | ||
|
||
console.log(`⚠️ detected pending graphile migration`); | ||
console.log(`⚠️ notifying running workers`); | ||
|
||
const pgNotify = new PgNotifyService(); | ||
await pgNotify.call("trigger:graphile:migrate", { latestMigration }); | ||
|
||
console.log(`⚠️ delaying worker startup by ${migrationDelayInMs}ms`); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, migrationDelayInMs)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.