-
Notifications
You must be signed in to change notification settings - Fork 734
fix: data sink worker improvements (CM-1054) #3996
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
Open
themarolt
wants to merge
9
commits into
main
Choose a base branch
from
fix/data-sink-worker-improvements-CM-1054
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1589183
fix: monitoring for results, incomming webhooks handling improved, fi…
themarolt b409a2c
Merge branch 'main' into fix/data-sink-worker-improvements-CM-1054
themarolt 84493c7
fix: comments
themarolt ec60e44
fix: comments
themarolt 969567e
fix: comments
themarolt ba8b4c2
fix: small ones
themarolt 896e55d
fix: small ones
themarolt 9fa141e
fix: small ones
themarolt fd0b6cd
Merge branch 'main' into fix/data-sink-worker-improvements-CM-1054
themarolt 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
111 changes: 111 additions & 0 deletions
111
services/apps/cron_service/src/jobs/incomingWebhooksCheck.job.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,111 @@ | ||
| import CronTime from 'cron-time-generator' | ||
|
|
||
| import { IS_PROD_ENV } from '@crowd/common' | ||
| import { IntegrationStreamWorkerEmitter } from '@crowd/common_services' | ||
| import { WRITE_DB_CONFIG, getDbConnection } from '@crowd/data-access-layer/src/database' | ||
| import { QUEUE_CONFIG, getKafkaClient, getKafkaMessageCounts } from '@crowd/queue' | ||
| import { KafkaQueueService } from '@crowd/queue/src/vendors/kafka/client' | ||
| import { WebhookState } from '@crowd/types' | ||
|
|
||
| import { IJobDefinition } from '../types' | ||
|
|
||
| const TOPIC = 'integration-stream-worker-high-production' | ||
| const GROUP_ID = 'integration-stream-worker-high-production' | ||
| const MAX_UNCONSUMED = 50000 | ||
|
|
||
| const job: IJobDefinition = { | ||
| name: 'incoming-webhooks-check', | ||
| cronTime: CronTime.everyDay(), | ||
| timeout: 30 * 60, // 30 minutes | ||
| enabled: async () => IS_PROD_ENV, | ||
| process: async (ctx) => { | ||
| const kafkaClient = getKafkaClient(QUEUE_CONFIG()) | ||
| const admin = kafkaClient.admin() | ||
| await admin.connect() | ||
|
|
||
| let counts: { total: number; consumed: number; unconsumed: number } | ||
| try { | ||
| counts = await getKafkaMessageCounts(ctx.log, admin, TOPIC, GROUP_ID) | ||
| } finally { | ||
| await admin.disconnect() | ||
| } | ||
|
|
||
| if (counts.unconsumed >= MAX_UNCONSUMED) { | ||
| ctx.log.info( | ||
| `Integration stream worker queue has ${counts.unconsumed} unconsumed messages, skipping!`, | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| const dbConnection = await getDbConnection(WRITE_DB_CONFIG()) | ||
|
|
||
| // Clean up orphaned webhooks whose integration was deleted (hard or soft). | ||
| // incomingWebhooks has no FK constraint on integrationId so these accumulate silently. | ||
| const deleted = await dbConnection.result( | ||
| ` | ||
| delete from "incomingWebhooks" iw | ||
| where not exists ( | ||
| select 1 from integrations i | ||
| where i.id = iw."integrationId" | ||
| and i."deletedAt" is null | ||
| ) | ||
| `, | ||
| ) | ||
| if (deleted.rowCount > 0) { | ||
| ctx.log.info( | ||
| `Deleted ${deleted.rowCount} orphaned webhooks with missing or deleted integrations!`, | ||
| ) | ||
| } | ||
|
|
||
| const count = ( | ||
| await dbConnection.one( | ||
| ` | ||
| select count(*)::int as count | ||
| from "incomingWebhooks" iw | ||
| join integrations i on iw."integrationId" = i.id and i."deletedAt" is null | ||
| where iw.state = $(state) | ||
| and iw."createdAt" < now() - interval '1 day' | ||
| `, | ||
| { state: WebhookState.PENDING }, | ||
| ) | ||
| ).count | ||
themarolt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (count <= counts.unconsumed) { | ||
| ctx.log.info(`All ${count} stuck pending webhooks are already in the queue, skipping!`) | ||
| return | ||
themarolt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| const webhooks = await dbConnection.any<{ id: string; platform: string }>( | ||
| ` | ||
| select iw.id, i.platform | ||
| from "incomingWebhooks" iw | ||
| join integrations i on iw."integrationId" = i.id and i."deletedAt" is null | ||
| where iw.state = $(state) | ||
| and iw."createdAt" < now() - interval '1 day' | ||
| order by iw."createdAt" asc | ||
| limit 10000 | ||
| `, | ||
| { state: WebhookState.PENDING }, | ||
| ) | ||
|
|
||
| if (webhooks.length === 0) { | ||
| ctx.log.info('No stuck pending webhooks found!') | ||
| return | ||
| } | ||
|
|
||
| ctx.log.info(`Found ${webhooks.length} stuck pending webhooks, re-triggering!`) | ||
|
|
||
| const queueService = new KafkaQueueService(kafkaClient, ctx.log) | ||
| const emitter = new IntegrationStreamWorkerEmitter(queueService, ctx.log) | ||
| await emitter.init() | ||
|
|
||
| await emitter.triggerWebhookProcessingBatch( | ||
| webhooks.map((w) => w.id), | ||
| true, | ||
| ) | ||
|
|
||
| ctx.log.info(`Re-triggered ${webhooks.length} stuck pending webhooks in total!`) | ||
| }, | ||
| } | ||
|
|
||
| export default job | ||
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.
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.