-
Notifications
You must be signed in to change notification settings - Fork 728
feat: adding onboarding worker as standard pattern (CM-1171) #4530
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
Merged
Merged
Changes from 3 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
2 changes: 2 additions & 0 deletions
2
backend/src/database/migrations/V1787916364__onboarding-error-to-project-catalog.sql
This file contains hidden or 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,2 @@ | ||
| ALTER TABLE "projectCatalog" | ||
| ADD COLUMN IF NOT EXISTS "onboardingError" TEXT; |
This file contains hidden or 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 +1 @@ | ||
| export {} | ||
| export * from './activities/activities' |
93 changes: 93 additions & 0 deletions
93
services/apps/automatic_onboarding_worker/src/activities/activities.ts
This file contains hidden or 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,93 @@ | ||
| import { | ||
| findProjectCatalogById, | ||
| findProjectCatalogPendingOnboarding, | ||
| markProjectCatalogOnboardingFailed, | ||
| updateProjectCatalog, | ||
| } from '@crowd/data-access-layer' | ||
| import { IDbProjectCatalog } from '@crowd/data-access-layer/src/project-catalog/types' | ||
| import { pgpQx } from '@crowd/data-access-layer/src/queryExecutor' | ||
| import { getServiceLogger } from '@crowd/logging' | ||
|
|
||
| import { svc } from '../main' | ||
| import { onboardProject } from '../onboarder/onboarder' | ||
|
|
||
| const log = getServiceLogger() | ||
|
|
||
| export async function fetchProjectsPendingOnboarding( | ||
| batchSize: number, | ||
| ): Promise<IDbProjectCatalog[]> { | ||
| const qx = pgpQx(svc.postgres.reader.connection()) | ||
|
|
||
| const projects = await findProjectCatalogPendingOnboarding(qx, { limit: batchSize }) | ||
|
|
||
| log.info({ count: projects.length, batchSize }, 'Fetched projects pending onboarding.') | ||
|
|
||
| return projects | ||
| } | ||
|
|
||
| async function findAlreadyOnboarded( | ||
| qx: ReturnType<typeof pgpQx>, | ||
| projectId: string, | ||
| ): Promise<IDbProjectCatalog | null> { | ||
| const fresh = await findProjectCatalogById(qx, projectId) | ||
| return fresh?.onboardedAt ? fresh : null | ||
| } | ||
|
|
||
| export async function onboardAndUpdateProject(project: IDbProjectCatalog): Promise<void> { | ||
| const qx = pgpQx(svc.postgres.writer.connection()) | ||
| const startTime = Date.now() | ||
|
|
||
| // Guard: uses the writer connection to avoid replica lag missing a just-written onboardedAt. | ||
| const fresh = await findAlreadyOnboarded(qx, project.id) | ||
| if (fresh) { | ||
| log.info( | ||
| { id: project.id, repoUrl: project.repoUrl, onboardedAt: fresh.onboardedAt }, | ||
| 'Project already onboarded, skipping API call.', | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| log.info({ id: project.id, repoUrl: project.repoUrl }, 'Starting onboarding.') | ||
|
|
||
| const result = await onboardProject({ | ||
| id: project.id, | ||
| repoUrl: project.repoUrl, | ||
| repoName: project.repoName, | ||
| projectSlug: project.projectSlug, | ||
| }) | ||
|
|
||
| if (result.outcome === 'error') { | ||
| throw new Error(result.error ?? 'Unknown onboarding error') | ||
| } | ||
|
|
||
| await updateProjectCatalog(qx, project.id, { | ||
| onboardedAt: new Date().toISOString(), | ||
| onboardingError: null, | ||
| }) | ||
|
ulemons marked this conversation as resolved.
ulemons marked this conversation as resolved.
|
||
|
|
||
| const elapsedSeconds = ((Date.now() - startTime) / 1000).toFixed(1) | ||
|
|
||
| log.info( | ||
| { id: project.id, repoUrl: project.repoUrl, segmentId: result.segmentId, elapsedSeconds }, | ||
| 'Onboarding complete.', | ||
| ) | ||
| } | ||
|
|
||
| export async function markProjectOnboardingFailed( | ||
| projectId: string, | ||
| reason: string, | ||
| ): Promise<void> { | ||
| const qx = pgpQx(svc.postgres.writer.connection()) | ||
|
|
||
| const updatedRows = await markProjectCatalogOnboardingFailed(qx, projectId, reason) | ||
|
|
||
| if (updatedRows === 0) { | ||
| log.info( | ||
| { id: projectId }, | ||
| 'Project was already onboarded or no longer pending, not marking as error.', | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| log.error({ id: projectId, reason }, 'Onboarding permanently failed, marked as error.') | ||
| } | ||
This file contains hidden or 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
45 changes: 45 additions & 0 deletions
45
services/apps/automatic_onboarding_worker/src/schedules/scheduleProjectsOnboarding.ts
This file contains hidden or 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,45 @@ | ||
| import { ScheduleAlreadyRunning, ScheduleOverlapPolicy } from '@temporalio/client' | ||
|
|
||
| import { svc } from '../main' | ||
| import { IOnboardProjectsInput, onboardProjects } from '../workflows' | ||
|
|
||
| const ONBOARDING_ARGS: IOnboardProjectsInput = { | ||
| batchSize: 50, | ||
| } | ||
|
|
||
| export const scheduleProjectsOnboarding = async () => { | ||
| svc.log.info('Scheduling projects onboarding') | ||
|
|
||
| try { | ||
| await svc.temporal.schedule.create({ | ||
| scheduleId: 'projectsOnboarding', | ||
| spec: { | ||
| // Daily: catches up on whatever landed in 'onboard' state, independent of the evaluation schedule's timing. | ||
| cronExpressions: ['0 8 * * *'], | ||
|
ulemons marked this conversation as resolved.
|
||
| }, | ||
| policies: { | ||
| overlap: ScheduleOverlapPolicy.SKIP, | ||
| catchupWindow: '1 hour', | ||
| }, | ||
| action: { | ||
| type: 'startWorkflow', | ||
| workflowType: onboardProjects, | ||
| taskQueue: 'automatic-onboarding', | ||
| args: [ONBOARDING_ARGS], | ||
| workflowExecutionTimeout: '14 hours', | ||
| retry: { | ||
| initialInterval: '30 seconds', | ||
| backoffCoefficient: 2, | ||
| maximumAttempts: 3, | ||
| }, | ||
| }, | ||
| }) | ||
| } catch (err) { | ||
| if (err instanceof ScheduleAlreadyRunning) { | ||
| svc.log.info('Schedule already registered in Temporal.') | ||
| svc.log.info('Configuration may have changed since. Please make sure they are in sync.') | ||
| } else { | ||
| throw new Error(err) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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,3 @@ | ||
| export interface IOnboardProjectsInput { | ||
| batchSize?: number | ||
| } |
This file contains hidden or 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,5 @@ | ||
| import type { IOnboardProjectsInput } from './types' | ||
| import { onboardProjects } from './workflows/onboardProjects' | ||
|
|
||
| export { onboardProjects } | ||
| export type { IOnboardProjectsInput } |
1 change: 0 additions & 1 deletion
1
services/apps/automatic_onboarding_worker/src/workflows/index.ts
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
services/apps/automatic_onboarding_worker/src/workflows/onboardProjects.ts
This file contains hidden or 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,69 @@ | ||
| import { log, proxyActivities, rootCause } from '@temporalio/workflow' | ||
|
|
||
| import type * as activities from '../activities' | ||
| import type { IOnboardProjectsInput } from '../types' | ||
|
|
||
| // Short timeout: just a DB read. | ||
| const fetchActivities = proxyActivities<typeof activities>({ | ||
|
ulemons marked this conversation as resolved.
|
||
| startToCloseTimeout: '2 minutes', | ||
| retry: { maximumAttempts: 3 }, | ||
| }) | ||
|
|
||
| // Each onboarding call chains a segment create/query plus GitHub enrichment and integration calls, | ||
| // each of which can individually approach a ~30s backend timeout; give generous headroom per project. | ||
| const onboardActivities = proxyActivities<typeof activities>({ | ||
| startToCloseTimeout: '5 minutes', | ||
| retry: { maximumAttempts: 2 }, | ||
| }) | ||
|
|
||
| const failureActivities = proxyActivities<typeof activities>({ | ||
| startToCloseTimeout: '2 minutes', | ||
| retry: { maximumAttempts: 2 }, | ||
| }) | ||
|
ulemons marked this conversation as resolved.
|
||
|
|
||
| export async function onboardProjects(input: IOnboardProjectsInput = {}): Promise<void> { | ||
| const { batchSize = 50 } = input | ||
|
|
||
| log.info('onboardProjects workflow started.') | ||
|
|
||
| const projects = await fetchActivities.fetchProjectsPendingOnboarding(batchSize) | ||
|
|
||
| if (projects.length === 0) { | ||
| log.info('No projects pending onboarding. Nothing to do.') | ||
| return | ||
| } | ||
|
|
||
| log.info(`Onboarding ${projects.length} project(s) (batch size: ${batchSize}).`) | ||
|
|
||
| let succeeded = 0 | ||
| let failed = 0 | ||
|
|
||
| for (let i = 0; i < projects.length; i++) { | ||
| const project = projects[i] | ||
| log.info(`[${i + 1}/${projects.length}] Onboarding: ${project.repoUrl}`) | ||
|
|
||
| try { | ||
| await onboardActivities.onboardAndUpdateProject(project) | ||
| succeeded++ | ||
| } catch (err) { | ||
| // Activity-level retries are already exhausted at this point — mark as a | ||
| // terminal error so the daily schedule stops retrying this project forever. | ||
| failed++ | ||
| const reason = rootCause(err) ?? String(err) | ||
| log.error( | ||
| `Onboarding failed for project id=${project.id} repoUrl=${project.repoUrl}: ${reason}`, | ||
| ) | ||
|
|
||
| try { | ||
| await failureActivities.markProjectOnboardingFailed(project.id, reason) | ||
| } catch (markErr) { | ||
| // Don't let a failure to record the error state abort the rest of the batch. | ||
| log.error(`Failed to mark project id=${project.id} as errored: ${String(markErr)}`) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| log.info( | ||
| `Batch onboarding complete. total=${projects.length} succeeded=${succeeded} failed=${failed}`, | ||
| ) | ||
| } | ||
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.