-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: migrate ScheduledJob from rethinkdb to pg
- Loading branch information
Showing
8 changed files
with
164 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
import generateUID from '../../generateUID' | ||
|
||
export type ScheduledJobType = | ||
| 'MEETING_STAGE_TIME_LIMIT_END' | ||
| 'LOCK_ORGANIZATION' | ||
| 'WARN_ORGANIZATION' | ||
|
||
export default abstract class ScheduledJob { | ||
id = generateUID() | ||
protected constructor(public type: ScheduledJobType, public runAt: Date) {} | ||
} |
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
19 changes: 15 additions & 4 deletions
19
packages/server/graphql/mutations/helpers/removeScheduledJobs.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 |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import getRethink from '../../../database/rethinkDriver' | ||
import {Updateable} from 'kysely' | ||
import {DB} from '../../../postgres/pg' | ||
import getKysely from '../../../postgres/getKysely' | ||
|
||
const removeScheduledJobs = async (runAt: Date, filter: {[key: string]: any}) => { | ||
const r = await getRethink() | ||
return r.table('ScheduledJob').getAll(runAt, {index: 'runAt'}).filter(filter).delete().run() | ||
type FilterType = Omit<Updateable<DB['ScheduledJob']>, 'runAt'> | ||
|
||
const removeScheduledJobs = async (runAt: Date, filter?: FilterType) => { | ||
const pg = getKysely() | ||
let query = pg.deleteFrom('ScheduledJob').where('runAt', '=', runAt) | ||
if (filter) { | ||
Object.keys(filter).forEach((key) => { | ||
const value = filter[key as keyof FilterType] | ||
if (value) query = query.where(key as keyof FilterType, '=', value) | ||
}) | ||
} | ||
return query.execute() | ||
} | ||
|
||
export default removeScheduledJobs |
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
38 changes: 38 additions & 0 deletions
38
packages/server/postgres/migrations/1709306178000_addScheduledJob.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,38 @@ | ||
import {Client} from 'pg' | ||
import getPgConfig from '../getPgConfig' | ||
|
||
export async function up() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(` | ||
DO $$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'ScheduledJobTypeEnum') THEN | ||
EXECUTE 'CREATE TYPE "ScheduledJobTypeEnum" AS ENUM (''MEETING_STAGE_TIME_LIMIT_END'', ''LOCK_ORGANIZATION'', ''WARN_ORGANIZATION'')'; | ||
END IF; | ||
END $$; | ||
CREATE TABLE "ScheduledJob" ( | ||
"id" SERIAL PRIMARY KEY, | ||
"runAt" TIMESTAMP WITH TIME ZONE DEFAULT NOW() NOT NULL, | ||
"type" "ScheduledJobTypeEnum" NOT NULL, | ||
"orgId" VARCHAR(100), | ||
"meetingId" VARCHAR(100) | ||
); | ||
CREATE INDEX IF NOT EXISTS "idx_ScheduledJob_orgId" ON "ScheduledJob"("orgId"); | ||
CREATE INDEX IF NOT EXISTS "idx_ScheduledJob_runAt" ON "ScheduledJob"("runAt"); | ||
CREATE INDEX IF NOT EXISTS "idx_ScheduledJob_type" ON "ScheduledJob"("type"); | ||
`) | ||
await client.end() | ||
} | ||
|
||
export async function down() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(` | ||
DROP TABLE IF EXISTS "ScheduledJob"; | ||
DROP TYPE IF EXISTS "ScheduledJobTypeEnum"; | ||
`) | ||
await client.end() | ||
} |
71 changes: 71 additions & 0 deletions
71
packages/server/postgres/migrations/1709312768000_scheduledJobMove.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,71 @@ | ||
import {FirstParam} from 'parabol-client/types/generics' | ||
import {Client} from 'pg' | ||
import {r} from 'rethinkdb-ts' | ||
import getPgConfig from '../getPgConfig' | ||
import connectRethinkDB from '../../database/connectRethinkDB' | ||
import getPgp from '../getPgp' | ||
|
||
export async function up() { | ||
await connectRethinkDB() | ||
const {pgp, pg} = getPgp() | ||
const batchSize = 1000 | ||
|
||
const columnSet = new pgp.helpers.ColumnSet( | ||
['runAt', 'type', {name: 'orgId', def: null}, {name: 'meetingId', def: null}], | ||
{table: 'ScheduledJob'} | ||
) | ||
|
||
const transformRethinkRow = (row: any) => { | ||
const {runAt, type, orgId, meetingId} = row | ||
return { | ||
runAt, | ||
type, | ||
orgId, | ||
meetingId | ||
} | ||
} | ||
|
||
const getNextData = async (leftBoundCursor: Date | undefined) => { | ||
const startAt = leftBoundCursor || r.minval | ||
const nextBatch = ( | ||
await r | ||
.table('ScheduledJob') | ||
.between(startAt, r.maxval, {index: 'runAt', leftBound: 'open'}) | ||
.orderBy({index: 'runAt'}) | ||
.limit(batchSize) | ||
.run() | ||
).map(transformRethinkRow) | ||
if (nextBatch.length === 0) return null | ||
if (nextBatch.length < batchSize) return nextBatch | ||
const lastItem = nextBatch.pop() | ||
const lastMatchingRunAt = nextBatch.findLastIndex((item) => item.runAt !== lastItem!.runAt) | ||
if (lastMatchingRunAt === -1) { | ||
throw new Error( | ||
'batchSize is smaller than the number of items that share the same cursor. Increase batchSize' | ||
) | ||
} | ||
return nextBatch.slice(0, lastMatchingRunAt + 1) | ||
} | ||
|
||
await pg.tx('ScheduledJob', (task) => { | ||
const fetchAndProcess: FirstParam<typeof task.sequence> = async ( | ||
_index, | ||
leftBoundCursor: undefined | Date | ||
) => { | ||
const nextData = await getNextData(leftBoundCursor) | ||
if (!nextData) return undefined | ||
const insert = pgp.helpers.insert(nextData, columnSet) | ||
await task.none(insert) | ||
return nextData.at(-1)!.runAt | ||
} | ||
return task.sequence(fetchAndProcess) | ||
}) | ||
await r.getPoolMaster()?.drain() | ||
} | ||
|
||
export async function down() { | ||
const client = new Client(getPgConfig()) | ||
await client.connect() | ||
await client.query(`DELETE FROM "ScheduledJob"`) | ||
await client.end() | ||
} |