-
Notifications
You must be signed in to change notification settings - Fork 4.5k
refactor reconnect account logic #15584
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 all commits
Commits
Show all changes
5 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 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
85 changes: 0 additions & 85 deletions
85
...endar-event-import-manager/commands/calendar-relaunch-failed-calendar-channels.command.ts
This file was deleted.
Oops, something went wrong.
35 changes: 35 additions & 0 deletions
35
...-import-manager/crons/commands/calendar-relaunch-failed-calendar-channels.cron.command.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,35 @@ | ||
| import { Command, CommandRunner } from 'nest-commander'; | ||
|
|
||
| import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator'; | ||
| import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants'; | ||
| import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service'; | ||
| import { | ||
| CALENDAR_RELAUNCH_FAILED_CALENDAR_CHANNELS_CRON_PATTERN, | ||
| CalendarRelaunchFailedCalendarChannelsCronJob, | ||
| } from 'src/modules/calendar/calendar-event-import-manager/crons/jobs/calendar-relaunch-failed-calendar-channels.cron.job'; | ||
|
|
||
| @Command({ | ||
| name: 'cron:calendar:relaunch-failed-calendar-channels', | ||
| description: | ||
| 'Starts a cron job to relaunch failed calendar channels every 30 minutes', | ||
| }) | ||
| export class CalendarRelaunchFailedCalendarChannelsCronCommand extends CommandRunner { | ||
| constructor( | ||
| @InjectMessageQueue(MessageQueue.cronQueue) | ||
| private readonly messageQueueService: MessageQueueService, | ||
| ) { | ||
| super(); | ||
| } | ||
|
|
||
| async run(): Promise<void> { | ||
| await this.messageQueueService.addCron<undefined>({ | ||
| jobName: CalendarRelaunchFailedCalendarChannelsCronJob.name, | ||
| data: undefined, | ||
| options: { | ||
| repeat: { | ||
| pattern: CALENDAR_RELAUNCH_FAILED_CALENDAR_CHANNELS_CRON_PATTERN, | ||
| }, | ||
| }, | ||
| }); | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
...ar-event-import-manager/crons/jobs/calendar-relaunch-failed-calendar-channels.cron.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,74 @@ | ||
| import { InjectDataSource, InjectRepository } from '@nestjs/typeorm'; | ||
|
|
||
| import { WorkspaceActivationStatus } from 'twenty-shared/workspace'; | ||
| import { DataSource, Repository } from 'typeorm'; | ||
|
|
||
| import { SentryCronMonitor } from 'src/engine/core-modules/cron/sentry-cron-monitor.decorator'; | ||
| import { ExceptionHandlerService } from 'src/engine/core-modules/exception-handler/exception-handler.service'; | ||
| import { InjectMessageQueue } from 'src/engine/core-modules/message-queue/decorators/message-queue.decorator'; | ||
| import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator'; | ||
| import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator'; | ||
| import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants'; | ||
| import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service'; | ||
| import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity'; | ||
| import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util'; | ||
| import { | ||
| CalendarRelaunchFailedCalendarChannelJob, | ||
| type CalendarRelaunchFailedCalendarChannelJobData, | ||
| } from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-relaunch-failed-calendar-channel.job'; | ||
| import { CalendarChannelSyncStage } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity'; | ||
|
|
||
| export const CALENDAR_RELAUNCH_FAILED_CALENDAR_CHANNELS_CRON_PATTERN = | ||
| '*/30 * * * *'; | ||
|
|
||
| @Processor(MessageQueue.cronQueue) | ||
| export class CalendarRelaunchFailedCalendarChannelsCronJob { | ||
| constructor( | ||
| @InjectRepository(WorkspaceEntity) | ||
| private readonly workspaceRepository: Repository<WorkspaceEntity>, | ||
| @InjectMessageQueue(MessageQueue.calendarQueue) | ||
| private readonly messageQueueService: MessageQueueService, | ||
| @InjectDataSource() | ||
| private readonly coreDataSource: DataSource, | ||
| private readonly exceptionHandlerService: ExceptionHandlerService, | ||
| ) {} | ||
|
|
||
| @Process(CalendarRelaunchFailedCalendarChannelsCronJob.name) | ||
| @SentryCronMonitor( | ||
| CalendarRelaunchFailedCalendarChannelsCronJob.name, | ||
| CALENDAR_RELAUNCH_FAILED_CALENDAR_CHANNELS_CRON_PATTERN, | ||
| ) | ||
| async handle(): Promise<void> { | ||
| const activeWorkspaces = await this.workspaceRepository.find({ | ||
| where: { | ||
| activationStatus: WorkspaceActivationStatus.ACTIVE, | ||
| }, | ||
| }); | ||
|
|
||
| for (const activeWorkspace of activeWorkspaces) { | ||
| try { | ||
| const schemaName = getWorkspaceSchemaName(activeWorkspace.id); | ||
|
|
||
| const failedCalendarChannels = await this.coreDataSource.query( | ||
| `SELECT * FROM ${schemaName}."calendarChannel" WHERE "syncStage" = '${CalendarChannelSyncStage.FAILED}'`, | ||
| ); | ||
|
|
||
| for (const calendarChannel of failedCalendarChannels) { | ||
| await this.messageQueueService.add<CalendarRelaunchFailedCalendarChannelJobData>( | ||
| CalendarRelaunchFailedCalendarChannelJob.name, | ||
| { | ||
| workspaceId: activeWorkspace.id, | ||
| calendarChannelId: calendarChannel.id, | ||
| }, | ||
| ); | ||
| } | ||
| } catch (error) { | ||
| this.exceptionHandlerService.captureExceptions([error], { | ||
| workspace: { | ||
| id: activeWorkspace.id, | ||
| }, | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
...endar/calendar-event-import-manager/jobs/calendar-relaunch-failed-calendar-channel.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,61 @@ | ||
| import { Scope } from '@nestjs/common'; | ||
|
|
||
| import { Process } from 'src/engine/core-modules/message-queue/decorators/process.decorator'; | ||
| import { Processor } from 'src/engine/core-modules/message-queue/decorators/processor.decorator'; | ||
| import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queue.constants'; | ||
| import { TwentyORMGlobalManager } from 'src/engine/twenty-orm/twenty-orm-global.manager'; | ||
| import { | ||
| CalendarChannelSyncStage, | ||
| CalendarChannelSyncStatus, | ||
| CalendarChannelWorkspaceEntity, | ||
| } from 'src/modules/calendar/common/standard-objects/calendar-channel.workspace-entity'; | ||
|
|
||
| export type CalendarRelaunchFailedCalendarChannelJobData = { | ||
| workspaceId: string; | ||
| calendarChannelId: string; | ||
| }; | ||
|
|
||
| @Processor({ | ||
| queueName: MessageQueue.calendarQueue, | ||
| scope: Scope.REQUEST, | ||
| }) | ||
| export class CalendarRelaunchFailedCalendarChannelJob { | ||
neo773 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| constructor( | ||
| private readonly twentyORMGlobalManager: TwentyORMGlobalManager, | ||
| ) {} | ||
|
|
||
| @Process(CalendarRelaunchFailedCalendarChannelJob.name) | ||
| async handle(data: CalendarRelaunchFailedCalendarChannelJobData) { | ||
| const { workspaceId, calendarChannelId } = data; | ||
|
|
||
| const calendarChannelRepository = | ||
| await this.twentyORMGlobalManager.getRepositoryForWorkspace<CalendarChannelWorkspaceEntity>( | ||
| workspaceId, | ||
| 'calendarChannel', | ||
| { shouldBypassPermissionChecks: true }, | ||
| ); | ||
|
|
||
| const calendarChannel = await calendarChannelRepository.findOne({ | ||
| where: { | ||
| id: calendarChannelId, | ||
| }, | ||
| relations: { | ||
| connectedAccount: { | ||
| accountOwner: true, | ||
| }, | ||
| }, | ||
| }); | ||
|
|
||
| if ( | ||
| !calendarChannel || | ||
| calendarChannel.syncStage !== CalendarChannelSyncStage.FAILED | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| await calendarChannelRepository.update(calendarChannelId, { | ||
| syncStage: CalendarChannelSyncStage.CALENDAR_EVENT_LIST_FETCH_PENDING, | ||
| syncStatus: CalendarChannelSyncStatus.ACTIVE, | ||
| }); | ||
| } | ||
| } | ||
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.