-
Notifications
You must be signed in to change notification settings - Fork 334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: migrate ScheduledJob from rethinkdb to pg #9490
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'> | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using Updatable makes all the fields optional |
||
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) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will "AND ${field} = ${value}" for anything specified in the filter object |
||
} | ||
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/1709927822000_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() | ||
} |
59 changes: 59 additions & 0 deletions
59
packages/server/postgres/migrations/1709927835000_moveScheduledJob.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,59 @@ | ||
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 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() | ||
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) | ||
} | ||
|
||
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() | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Postgres now manages the id