-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[messaging] add cronjob for workspaces messages partial sync #3800
Merged
Weiko
merged 3 commits into
main
from
c--add-cronjob-for-workspaces-messages-partial-fetch
Feb 5, 2024
Merged
Changes from 1 commit
Commits
Show all changes
3 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
29 changes: 29 additions & 0 deletions
29
...etch-all-workspaces-messages/commands/start-fetch-all-workspaces-messages.cron.command.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,29 @@ | ||
import { Inject } from '@nestjs/common'; | ||
|
||
import { Command, CommandRunner } from 'nest-commander'; | ||
|
||
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants'; | ||
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service'; | ||
import { fetchAllWorkspacesMessagesCronPattern } from 'src/workspace/cron/fetch-all-workspaces-messages/fetch-all-workspaces-messages.cron.pattern'; | ||
import { FetchAllWorkspacesMessagesJob } from 'src/workspace/cron/fetch-all-workspaces-messages/fetch-all-workspaces-messages.job'; | ||
|
||
@Command({ | ||
name: 'fetch-all-workspaces-messages:cron:start', | ||
description: 'Starts a cron job to fetch all workspaces messages', | ||
}) | ||
export class StartFetchAllWorkspacesMessagesCronCommand extends CommandRunner { | ||
constructor( | ||
@Inject(MessageQueue.cronQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
) { | ||
super(); | ||
} | ||
|
||
async run(): Promise<void> { | ||
await this.messageQueueService.addCron<undefined>( | ||
FetchAllWorkspacesMessagesJob.name, | ||
undefined, | ||
fetchAllWorkspacesMessagesCronPattern, | ||
); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...fetch-all-workspaces-messages/commands/stop-fetch-all-workspaces-messages.cron.command.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,28 @@ | ||
import { Inject } from '@nestjs/common'; | ||
|
||
import { Command, CommandRunner } from 'nest-commander'; | ||
|
||
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants'; | ||
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service'; | ||
import { fetchAllWorkspacesMessagesCronPattern } from 'src/workspace/cron/fetch-all-workspaces-messages/fetch-all-workspaces-messages.cron.pattern'; | ||
import { FetchAllWorkspacesMessagesJob } from 'src/workspace/cron/fetch-all-workspaces-messages/fetch-all-workspaces-messages.job'; | ||
|
||
@Command({ | ||
name: 'fetch-all-workspaces-messages:cron:stop', | ||
description: 'Stops the fetch all workspaces messages cron job', | ||
}) | ||
export class StopFetchAllWorkspacesMessagesCronCommand extends CommandRunner { | ||
constructor( | ||
@Inject(MessageQueue.cronQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
) { | ||
super(); | ||
} | ||
|
||
async run(): Promise<void> { | ||
await this.messageQueueService.removeCron( | ||
FetchAllWorkspacesMessagesJob.name, | ||
fetchAllWorkspacesMessagesCronPattern, | ||
); | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...orkspace/cron/fetch-all-workspaces-messages/fetch-all-workspaces-messages.cron.pattern.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 @@ | ||
export const fetchAllWorkspacesMessagesCronPattern = '* * * * *'; |
66 changes: 66 additions & 0 deletions
66
...ver/src/workspace/cron/fetch-all-workspaces-messages/fetch-all-workspaces-messages.job.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,66 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
|
||
import { Repository } from 'typeorm'; | ||
|
||
import { MessageQueueJob } from 'src/integrations/message-queue/interfaces/message-queue-job.interface'; | ||
|
||
import { | ||
FeatureFlagEntity, | ||
FeatureFlagKeys, | ||
} from 'src/core/feature-flag/feature-flag.entity'; | ||
import { MessageQueue } from 'src/integrations/message-queue/message-queue.constants'; | ||
import { MessageQueueService } from 'src/integrations/message-queue/services/message-queue.service'; | ||
import { ConnectedAccountService } from 'src/workspace/messaging/connected-account/connected-account.service'; | ||
import { | ||
GmailPartialSyncJobData, | ||
GmailPartialSyncJob, | ||
} from 'src/workspace/messaging/jobs/gmail-partial-sync.job'; | ||
|
||
@Injectable() | ||
export class FetchAllWorkspacesMessagesJob | ||
implements MessageQueueJob<undefined> | ||
{ | ||
constructor( | ||
@InjectRepository(FeatureFlagEntity, 'core') | ||
private readonly featureFlagRepository: Repository<FeatureFlagEntity>, | ||
@Inject(MessageQueue.messagingQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
private readonly connectedAccountService: ConnectedAccountService, | ||
) {} | ||
|
||
async handle(): Promise<void> { | ||
const featureFlagsWithMessagingEnabled = | ||
await this.featureFlagRepository.findBy({ | ||
key: FeatureFlagKeys.IsMessagingEnabled, | ||
value: true, | ||
}); | ||
|
||
const workspaceIds = featureFlagsWithMessagingEnabled.map( | ||
(featureFlag) => featureFlag.workspaceId, | ||
); | ||
|
||
for (const workspaceId of workspaceIds) { | ||
await this.fetchWorkspaceMessages(workspaceId); | ||
} | ||
} | ||
|
||
private async fetchWorkspaceMessages(workspaceId: string): Promise<void> { | ||
const connectedAccounts = | ||
await this.connectedAccountService.getAll(workspaceId); | ||
|
||
for (const connectedAccount of connectedAccounts) { | ||
await this.messageQueueService.add<GmailPartialSyncJobData>( | ||
GmailPartialSyncJob.name, | ||
{ | ||
workspaceId, | ||
connectedAccountId: connectedAccount.id, | ||
}, | ||
{ | ||
id: `${workspaceId}-${connectedAccount.id}`, | ||
retryLimit: 2, | ||
}, | ||
); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -77,6 +77,10 @@ export class GmailPartialSyncService { | |
} | ||
|
||
if (newHistoryId === lastSyncHistoryId) { | ||
console.log( | ||
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. use our logger! |
||
`gmail partial-sync for workspace ${workspaceId} and account ${connectedAccountId} done with nothing to update.`, | ||
); | ||
|
||
return; | ||
} | ||
|
||
|
@@ -127,6 +131,10 @@ export class GmailPartialSyncService { | |
connectedAccount.id, | ||
workspaceId, | ||
); | ||
|
||
console.log( | ||
`gmail partial-sync for workspace ${workspaceId} and account ${connectedAccountId} done.`, | ||
); | ||
} | ||
|
||
private async getMessageIdsFromHistory( | ||
|
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.
bullmq needs the exact same pattern used by addCron when we want to remove a cron, pg-boss does not.