-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: related discussions refactor (#9557)
Signed-off-by: Matt Krick <[email protected]> Co-authored-by: Jordan Husney <[email protected]>
- Loading branch information
Showing
46 changed files
with
1,077 additions
and
451 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
This file was deleted.
Oops, something went wrong.
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,10 @@ | ||
import {JobType} from './custom' | ||
|
||
type SplitJobType<T extends string> = T extends `${infer W}:${infer S}` ? [W, S] : never | ||
export const EmbedderJobType = { | ||
join: (workflowName: string, stepName: string) => `${workflowName}:${stepName}` as JobType, | ||
split: (jobType: JobType) => { | ||
const [workflowName, stepName] = jobType.split(':') as SplitJobType<typeof jobType> | ||
return {workflowName, stepName} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export class JobQueueError extends Error { | ||
name = 'JobQueueError' as const | ||
retryDelay?: number | ||
maxRetries?: number | ||
jobData?: Record<string, any> | ||
|
||
constructor( | ||
message: string, | ||
retryDelay?: number, | ||
maxRetries?: number, | ||
jobData?: Record<string, any> | ||
) { | ||
super(message) | ||
this.message = message | ||
this.retryDelay = retryDelay | ||
this.maxRetries = maxRetries | ||
this.jobData = jobData | ||
} | ||
} |
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,117 @@ | ||
import {sql} from 'kysely' | ||
import RootDataLoader from 'parabol-server/dataloader/RootDataLoader' | ||
import getKysely from 'parabol-server/postgres/getKysely' | ||
import {Logger} from '../server/utils/Logger' | ||
import {EmbedderJobType} from './EmbedderJobType' | ||
import {JobQueueError} from './JobQueueError' | ||
import {DBJob, JobType, Workflow} from './custom' | ||
import {embedMetadata} from './workflows/embedMetadata' | ||
import {getSimilarRetroTopics} from './workflows/getSimilarRetroTopics' | ||
import {relatedDiscussionsStart} from './workflows/relatedDiscussionsStart' | ||
import {rerankRetroTopics} from './workflows/rerankRetroTopics' | ||
|
||
export class WorkflowOrchestrator { | ||
workflows: Record<string, Workflow> = { | ||
embed: { | ||
start: { | ||
run: embedMetadata | ||
} | ||
}, | ||
relatedDiscussions: { | ||
start: { | ||
run: relatedDiscussionsStart, | ||
getNextStep: () => 'embed' | ||
}, | ||
embed: { | ||
run: embedMetadata, | ||
getNextStep: () => 'getSimilarRetroTopics' | ||
}, | ||
getSimilarRetroTopics: { | ||
run: getSimilarRetroTopics, | ||
getNextStep: () => 'rerank' | ||
}, | ||
rerank: { | ||
run: rerankRetroTopics | ||
} | ||
} | ||
} | ||
|
||
private failJob = async (jobId: number, retryCount: number, error: JobQueueError) => { | ||
Logger.error(error) | ||
const pg = getKysely() | ||
const {message, retryDelay, jobData} = error | ||
const maxRetries = error.maxRetries ?? 1e6 | ||
await pg | ||
.updateTable('EmbeddingsJobQueue') | ||
.set((eb) => ({ | ||
state: 'failed', | ||
stateMessage: message, | ||
retryCount: eb('retryCount', '+', 1), | ||
retryAfter: | ||
retryDelay && retryCount < maxRetries ? new Date(Date.now() + retryDelay) : null, | ||
jobData: jobData ? sql`jsonb_concat("jobData", ${JSON.stringify({jobData})})` : undefined | ||
})) | ||
.where('id', '=', jobId) | ||
.executeTakeFirstOrThrow() | ||
} | ||
|
||
private finishJob = async (jobId: number) => { | ||
const pg = getKysely() | ||
await pg.deleteFrom('EmbeddingsJobQueue').where('id', '=', jobId).executeTakeFirstOrThrow() | ||
} | ||
|
||
private addNextJob = async ( | ||
jobType: JobType, | ||
priority: number, | ||
data: Record<string, any> | Record<string, any>[] | ||
) => { | ||
const pg = getKysely() | ||
const getValues = (datum: any, idx = 0) => { | ||
const {embeddingsMetadataId, model, ...jobData} = datum | ||
return { | ||
jobType, | ||
// increment by idx so the first item goes first | ||
priority: priority + idx, | ||
embeddingsMetadataId, | ||
model, | ||
jobData: JSON.stringify(jobData) | ||
} | ||
} | ||
const values = Array.isArray(data) ? data.map(getValues) : getValues(data) | ||
await pg.insertInto('EmbeddingsJobQueue').values(values).execute() | ||
} | ||
|
||
runStep = async (job: DBJob) => { | ||
const {id: jobId, jobData, jobType, priority, retryCount, embeddingsMetadataId, model} = job | ||
const {workflowName, stepName} = EmbedderJobType.split(jobType) | ||
const workflow = this.workflows[workflowName] | ||
if (!workflow) | ||
return this.failJob( | ||
jobId, | ||
retryCount, | ||
new JobQueueError(`Workflow ${workflowName} not found`) | ||
) | ||
const step = workflow[stepName] | ||
if (!step) | ||
return this.failJob(jobId, retryCount, new JobQueueError(`Step ${stepName} not found`)) | ||
const {run, getNextStep} = step | ||
const dataLoader = new RootDataLoader() | ||
let result: Awaited<ReturnType<typeof run>> = false | ||
const data = {...jobData, embeddingsMetadataId, model} | ||
try { | ||
result = await run({dataLoader, data}) | ||
} catch (e) { | ||
if (e instanceof Error) { | ||
result = new JobQueueError(`Uncaught error: ${e.message}`) | ||
result.stack = e.stack | ||
} | ||
} | ||
if (result instanceof JobQueueError) return this.failJob(jobId, retryCount, result) | ||
await this.finishJob(jobId) | ||
if (result === false) return | ||
const nextStepName = await getNextStep?.({dataLoader, data: {...data, ...result}}) | ||
if (!nextStepName) return | ||
const nextJobType = EmbedderJobType.join(workflowName, nextStepName) | ||
await this.addNextJob(nextJobType, priority, result) | ||
} | ||
} |
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.