Skip to content

The open alternative to Salesforce, designed for AI.

Create webhook subscriptions on the OAuth connect path (#25211)

neo773e58f68cbcd54
diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts b/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts
index da2f61ba1a7..bbe5bdf7f78 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/services/google-apis.service.ts
@@ -1,4 +1,4 @@
-import { Injectable } from '@nestjs/common';
+import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import {
@@ -8,6 +8,7 @@ import {
MessageChannelSyncStage,
MessageChannelSyncStatus,
type MessageChannelVisibility,
+ WebhookSubscriptionChannelType,
} from 'twenty-shared/types';
import { v4 } from 'uuid';
import { EntityManager, Repository } from 'typeorm';
@@ -41,6 +42,11 @@ import {
import { CalendarChannelSyncStatusService } from 'src/modules/calendar/common/services/calendar-channel-sync-status.service';
import { EmailAliasManagerService } from 'src/modules/connected-account/email-alias-manager/services/email-alias-manager.service';
import { AccountsToReconnectService } from 'src/modules/connected-account/services/accounts-to-reconnect.service';
+import { WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-creation-retry-limit.constant';
+import {
+ CreateWebhookSubscriptionJob,
+ type CreateWebhookSubscriptionJobData,
+} from 'src/modules/connected-account/webhook-subscription-manager/jobs/create-webhook-subscription.job';
import { MessageChannelSyncStatusService } from 'src/modules/messaging/common/services/message-channel-sync-status.service';
import {
@@ -52,12 +58,16 @@ import { isDefined } from 'twenty-shared/utils';
@Injectable()
export class GoogleAPIsService {
+ private readonly logger = new Logger(GoogleAPIsService.name);
+
constructor(
private readonly workspaceOrmManager: WorkspaceOrmManager,
@InjectMessageQueue(MessageQueue.messagingQueue)
private readonly messageQueueService: MessageQueueService,
@InjectMessageQueue(MessageQueue.calendarQueue)
private readonly calendarQueueService: MessageQueueService,
+ @InjectMessageQueue(MessageQueue.webhookQueue)
+ private readonly webhookQueueService: MessageQueueService,
private readonly twentyConfigService: TwentyConfigService,
private readonly accountsToReconnectService: AccountsToReconnectService,
private readonly createMessageChannelService: CreateMessageChannelService,
@@ -340,6 +350,12 @@ export class GoogleAPIsService {
MessagingMessageListFetchJob.name,
{ workspaceId, messageChannelId: messageChannel.id },
);
+ await this.enqueueWebhookSubscriptionCreation({
+ channelType: WebhookSubscriptionChannelType.MESSAGING,
+ channelId: messageChannel.id,
+ isSyncEnabled: messageChannel.isSyncEnabled,
+ workspaceId,
+ });
this.onboardingRecentMessagesImportService
.importRecentMessages({
messageChannelId: messageChannel.id,
@@ -380,6 +396,12 @@ export class GoogleAPIsService {
CalendarEventListFetchJob.name,
{ workspaceId, calendarChannelId: calendarChannel.id },
);
+ await this.enqueueWebhookSubscriptionCreation({
+ channelType: WebhookSubscriptionChannelType.CALENDAR,
+ channelId: calendarChannel.id,
+ isSyncEnabled: calendarChannel.isSyncEnabled,
+ workspaceId,
+ });
}
}
}
@@ -388,4 +410,38 @@ export class GoogleAPIsService {
return newOrExistingConnectedAccountId;
}, authContext);
}
+
+ private async enqueueWebhookSubscriptionCreation({
+ channelType,
+ channelId,
+ isSyncEnabled,
+ workspaceId,
+ }: {
+ channelType: WebhookSubscriptionChannelType;
+ channelId: string;
+ isSyncEnabled: boolean;
+ workspaceId: string;
+ }): Promise<void> {
+ if (
+ !isSyncEnabled ||
+ !this.twentyConfigService.get(
+ 'IS_CONNECTED_ACCOUNT_WEBHOOK_SUBSCRIPTION_ENABLED',
+ )
+ ) {
+ return;
+ }
+
+ try {
+ await this.webhookQueueService.add<CreateWebhookSubscriptionJobData>(
+ CreateWebhookSubscriptionJob.name,
+ { channelType, channelId, workspaceId },
+ { retryLimit: WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT },
+ );
+ } catch (error) {
+ this.logger.warn(
+ `Failed to enqueue webhook subscription job for ${channelType} channel ${channelId}`,
+ error,
+ );
+ }
+ }
}
diff --git a/packages/twenty-server/src/engine/core-modules/auth/services/microsoft-apis.service.ts b/packages/twenty-server/src/engine/core-modules/auth/services/microsoft-apis.service.ts
index 8db9b48577c..da17a95b287 100644
--- a/packages/twenty-server/src/engine/core-modules/auth/services/microsoft-apis.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/auth/services/microsoft-apis.service.ts
@@ -1,4 +1,4 @@
-import { Injectable } from '@nestjs/common';
+import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import {
@@ -7,6 +7,7 @@ import {
ConnectedAccountProvider,
MessageChannelSyncStage,
type MessageChannelVisibility,
+ WebhookSubscriptionChannelType,
} from 'twenty-shared/types';
import { v4 } from 'uuid';
import { EntityManager, Repository } from 'typeorm';
@@ -39,6 +40,11 @@ import {
import { CalendarChannelSyncStatusService } from 'src/modules/calendar/common/services/calendar-channel-sync-status.service';
import { EmailAliasManagerService } from 'src/modules/connected-account/email-alias-manager/services/email-alias-manager.service';
import { AccountsToReconnectService } from 'src/modules/connected-account/services/accounts-to-reconnect.service';
+import { WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-creation-retry-limit.constant';
+import {
+ CreateWebhookSubscriptionJob,
+ type CreateWebhookSubscriptionJobData,
+} from 'src/modules/connected-account/webhook-subscription-manager/jobs/create-webhook-subscription.job';
import { MessageChannelSyncStatusService } from 'src/modules/messaging/common/services/message-channel-sync-status.service';
import {
@@ -50,12 +56,16 @@ import { isDefined } from 'twenty-shared/utils';
@Injectable()
export class MicrosoftAPIsService {
+ private readonly logger = new Logger(MicrosoftAPIsService.name);
+
constructor(
private readonly workspaceOrmManager: WorkspaceOrmManager,
@InjectMessageQueue(MessageQueue.messagingQueue)
private readonly messageQueueService: MessageQueueService,
@InjectMessageQueue(MessageQueue.calendarQueue)
private readonly calendarQueueService: MessageQueueService,
+ @InjectMessageQueue(MessageQueue.webhookQueue)
+ private readonly webhookQueueService: MessageQueueService,
private readonly accountsToReconnectService: AccountsToReconnectService,
private readonly messagingChannelSyncStatusService: MessageChannelSyncStatusService,
private readonly calendarChannelSyncStatusService: CalendarChannelSyncStatusService,
@@ -301,6 +311,12 @@ export class MicrosoftAPIsService {
messageChannelId: messageChannel.id,
},
);
+ await this.enqueueWebhookSubscriptionCreation({
+ channelType: WebhookSubscriptionChannelType.MESSAGING,
+ channelId: messageChannel.id,
+ isSyncEnabled: messageChannel.isSyncEnabled,
+ workspaceId,
+ });
this.onboardingRecentMessagesImportService
.importRecentMessages({
messageChannelId: messageChannel.id,
@@ -333,10 +349,50 @@ export class MicrosoftAPIsService {
workspaceId,
},
);
+ await this.enqueueWebhookSubscriptionCreation({
+ channelType: WebhookSubscriptionChannelType.CALENDAR,
+ channelId: calendarChannel.id,
+ isSyncEnabled: calendarChannel.isSyncEnabled,
+ workspaceId,
+ });
}
}
return newOrExistingConnectedAccountId;
}, authContext);
}
+
+ private async enqueueWebhookSubscriptionCreation({
+ channelType,
+ channelId,
+ isSyncEnabled,
+ workspaceId,
+ }: {
+ channelType: WebhookSubscriptionChannelType;
+ channelId: string;
+ isSyncEnabled: boolean;
+ workspaceId: string;
+ }): Promise<void> {
+ if (
+ !isSyncEnabled ||
+ !this.twentyConfigService.get(
+ 'IS_CONNECTED_ACCOUNT_WEBHOOK_SUBSCRIPTION_ENABLED',
+ )
+ ) {
+ return;
+ }
+
+ try {
+ await this.webhookQueueService.add<CreateWebhookSubscriptionJobData>(
+ CreateWebhookSubscriptionJob.name,
+ { channelType, channelId, workspaceId },
+ { retryLimit: WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT },
+ );
+ } catch (error) {
+ this.logger.warn(
+ `Failed to enqueue webhook subscription job for ${channelType} channel ${channelId}`,
+ error,
+ );
+ }
+ }
}
diff --git a/packages/twenty-server/src/modules/connected-account/channel-sync/services/channel-sync.service.ts b/packages/twenty-server/src/modules/connected-account/channel-sync/services/channel-sync.service.ts
index 157589ebd72..40acb0c0ee1 100644
--- a/packages/twenty-server/src/modules/connected-account/channel-sync/services/channel-sync.service.ts
+++ b/packages/twenty-server/src/modules/connected-account/channel-sync/services/channel-sync.service.ts
@@ -22,6 +22,7 @@ import {
CalendarEventListFetchJob,
type CalendarEventListFetchJobData,
} from 'src/modules/calendar/calendar-event-import-manager/jobs/calendar-event-list-fetch.job';
+import { WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-creation-retry-limit.constant';
import {
CreateWebhookSubscriptionJob,
type CreateWebhookSubscriptionJobData,
@@ -110,6 +111,7 @@ export class ChannelSyncService {
channelId: messageChannel.id,
workspaceId,
},
+ { retryLimit: WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT },
);
} catch (error) {
this.logger.warn(
@@ -170,6 +172,7 @@ export class ChannelSyncService {
channelId: calendarChannel.id,
workspaceId,
},
+ { retryLimit: WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT },
);
} catch (error) {
this.logger.warn(
diff --git a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/commands/create-webhook-subscription-for-connected-account.command.ts b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/commands/create-webhook-subscription-for-connected-account.command.ts
index b9b28ec36d6..f072aba4c92 100644
--- a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/commands/create-webhook-subscription-for-connected-account.command.ts
+++ b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/commands/create-webhook-subscription-for-connected-account.command.ts
@@ -2,7 +2,6 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Command } from 'nest-commander';
import {
- ConnectedAccountProvider,
WebhookSubscriptionChannelType,
WebhookSubscriptionStatus,
} from 'twenty-shared/types';
@@ -16,18 +15,14 @@ import { MessageQueue } from 'src/engine/core-modules/message-queue/message-queu
import { MessageQueueService } from 'src/engine/core-modules/message-queue/services/message-queue.service';
import { CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';
import { MessageChannelEntity } from 'src/engine/metadata-modules/message-channel/entities/message-channel.entity';
+import { WEBHOOK_CAPABLE_PROVIDERS } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-capable-providers.constant';
+import { WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-creation-retry-limit.constant';
import {
CreateWebhookSubscriptionJob,
type CreateWebhookSubscriptionJobData,
} from 'src/modules/connected-account/webhook-subscription-manager/jobs/create-webhook-subscription.job';
const WEBHOOK_BACKFILL_SPACING_MS = 2000;
-const WEBHOOK_BACKFILL_RETRY_LIMIT = 3;
-
-const WEBHOOK_CAPABLE_PROVIDERS = [
- ConnectedAccountProvider.GOOGLE,
- ConnectedAccountProvider.MICROSOFT,
-];
@Command({
name: 'connected-account:create-webhook-subscription',
@@ -125,7 +120,7 @@ export class CreateWebhookSubscriptionForConnectedAccountCommand extends Provisi
{ channelType, channelId, workspaceId },
{
delay: this.enqueueCursorMs,
- retryLimit: WEBHOOK_BACKFILL_RETRY_LIMIT,
+ retryLimit: WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT,
},
);
diff --git a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-capable-providers.constant.ts b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-capable-providers.constant.ts
new file mode 100644
index 00000000000..0b51acd0a96
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-capable-providers.constant.ts
@@ -0,0 +1,6 @@
+import { ConnectedAccountProvider } from 'twenty-shared/types';
+
+export const WEBHOOK_CAPABLE_PROVIDERS = [
+ ConnectedAccountProvider.GOOGLE,
+ ConnectedAccountProvider.MICROSOFT,
+];
diff --git a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-creation-retry-limit.constant.ts b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-creation-retry-limit.constant.ts
new file mode 100644
index 00000000000..7577e5b0790
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-creation-retry-limit.constant.ts
@@ -0,0 +1 @@
+export const WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT = 3;
diff --git a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-batch-size.constant.ts b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-batch-size.constant.ts
new file mode 100644
index 00000000000..d9c1b3d1da8
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-batch-size.constant.ts
@@ -0,0 +1 @@
+export const WEBHOOK_SUBSCRIPTION_RENEWAL_BATCH_SIZE = 200;
diff --git a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-max-total-delay-ms.constant.ts b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-max-total-delay-ms.constant.ts
new file mode 100644
index 00000000000..969c67e5056
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-max-total-delay-ms.constant.ts
@@ -0,0 +1 @@
+export const WEBHOOK_SUBSCRIPTION_RENEWAL_MAX_TOTAL_DELAY_MS = 30 * 60 * 1000;
diff --git a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-spacing-ms.constant.ts b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-spacing-ms.constant.ts
new file mode 100644
index 00000000000..347d9c14e9c
--- /dev/null
+++ b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-spacing-ms.constant.ts
@@ -0,0 +1 @@
+export const WEBHOOK_SUBSCRIPTION_RENEWAL_SPACING_MS = 2000;
diff --git a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/crons/jobs/webhook-subscription-renewal.cron.job.ts b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/crons/jobs/webhook-subscription-renewal.cron.job.ts
index f95b0865ea4..4ff96144e56 100644
--- a/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/crons/jobs/webhook-subscription-renewal.cron.job.ts
+++ b/packages/twenty-server/src/modules/connected-account/webhook-subscription-manager/crons/jobs/webhook-subscription-renewal.cron.job.ts
@@ -2,6 +2,8 @@ import { Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import {
+ CalendarChannelSyncStage,
+ MessageChannelSyncStage,
WebhookSubscriptionChannelType,
WebhookSubscriptionStatus,
} from 'twenty-shared/types';
@@ -11,6 +13,7 @@ import {
In,
IsNull,
LessThanOrEqual,
+ Not,
Repository,
} from 'typeorm';
@@ -20,11 +23,17 @@ import { Process } from 'src/engine/core-modules/message-queue/decorators/proces
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 { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
import { WorkspaceEntity } from 'src/engine/core-modules/workspace/workspace.entity';
import { CalendarChannelEntity } from 'src/engine/metadata-modules/calendar-channel/entities/calendar-channel.entity';
import { MessageChannelEntity } from 'src/engine/metadata-modules/message-channel/entities/message-channel.entity';
+import { WEBHOOK_CAPABLE_PROVIDERS } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-capable-providers.constant';
+import { WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-creation-retry-limit.constant';
+import { WEBHOOK_SUBSCRIPTION_RENEWAL_BATCH_SIZE } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-batch-size.constant';
import { WEBHOOK_SUBSCRIPTION_RENEWAL_BUFFER_MS } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-buffer-ms.constant';
import { WEBHOOK_SUBSCRIPTION_RENEWAL_CRON_PATTERN } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-cron-pattern.constant';
+import { WEBHOOK_SUBSCRIPTION_RENEWAL_MAX_TOTAL_DELAY_MS } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-max-total-delay-ms.constant';
+import { WEBHOOK_SUBSCRIPTION_RENEWAL_SPACING_MS } from 'src/modules/connected-account/webhook-subscription-manager/constants/webhook-subscription-renewal-spacing-ms.constant';
import {
RenewWebhookSubscriptionJob,
type RenewWebhookSubscriptionJobData,
@@ -34,6 +43,11 @@ type WebhookSubscribableChannel = MessageChannelEntity | CalendarChannelEntity;
type StaleChannel = Pick<WebhookSubscribableChannel, 'id' | 'workspaceId'>;
+type StaleChannelToEnqueue = {
+ channelType: WebhookSubscriptionChannelType;
+ channel: StaleChannel;
+};
+
@Processor(MessageQueue.cronQueue)
export class WebhookSubscriptionRenewalCronJob {
private readonly logger = new Logger(WebhookSubscriptionRenewalCronJob.name);
@@ -47,6 +61,7 @@ export class WebhookSubscriptionRenewalCronJob {
private readonly calendarChannelRepository: Repository<CalendarChannelEntity>,
@InjectMessageQueue(MessageQueue.webhookQueue)
private readonly webhookQueueService: MessageQueueService,
+ private readonly twentyConfigService: TwentyConfigService,
) {}
@Process(WebhookSubscriptionRenewalCronJob.name)
@@ -68,56 +83,100 @@ export class WebhookSubscriptionRenewalCronJob {
return;
}
- const [messageChannels, calendarChannels] = await Promise.all([
- this.findStaleChannels(this.messageChannelRepository, activeWorkspaceIds),
- this.findStaleChannels(
+ const isSubscriptionCreationEnabled = this.twentyConfigService.get(
+ 'IS_CONNECTED_ACCOUNT_WEBHOOK_SUBSCRIPTION_ENABLED',
+ );
+
+ const [
+ expiringMessageChannels,
+ expiringCalendarChannels,
+ missingMessageChannels,
+ missingCalendarChannels,
+ ] = await Promise.all([
+ this.findRenewableSubscriptions(
+ this.messageChannelRepository,
+ activeWorkspaceIds,
+ ),
+ this.findRenewableSubscriptions(
this.calendarChannelRepository,
activeWorkspaceIds,
),
+ isSubscriptionCreationEnabled
+ ? this.findMessageChannelsMissingSubscription(activeWorkspaceIds)
+ : Promise.resolve([]),
+ isSubscriptionCreationEnabled
+ ? this.findCalendarChannelsMissingSubscription(activeWorkspaceIds)
+ : Promise.resolve([]),
]);
- if (messageChannels.length === 0 && calendarChannels.length === 0) {
- return;
- }
-
- await Promise.all([
- this.enqueueRenewals(
+ const staleChannels: StaleChannelToEnqueue[] = [
+ ...this.toEnqueueEntries(
WebhookSubscriptionChannelType.MESSAGING,
- messageChannels,
+ expiringMessageChannels,
),
- this.enqueueRenewals(
+ ...this.toEnqueueEntries(
WebhookSubscriptionChannelType.CALENDAR,
- calendarChannels,
+ expiringCalendarChannels,
),
- ]);
+ ...this.toEnqueueEntries(
+ WebhookSubscriptionChannelType.MESSAGING,
+ missingMessageChannels,
+ ),
+ ...this.toEnqueueEntries(
+ WebhookSubscriptionChannelType.CALENDAR,
+ missingCalendarChannels,
+ ),
+ ];
+
+ if (staleChannels.length === 0) {
+ return;
+ }
+
+ const spacingMs = await this.enqueueRenewals(staleChannels);
this.logger.log(
- `Enqueued webhook subscription renewals: ${messageChannels.length} messaging, ${calendarChannels.length} calendar`,
+ `Enqueued webhook subscription renewals: ${expiringMessageChannels.length + expiringCalendarChannels.length} expiring, ${missingMessageChannels.length + missingCalendarChannels.length} missing, spaced ${spacingMs}ms apart`,
);
}
- private findStaleChannels<TChannel extends WebhookSubscribableChannel>(
+ private toEnqueueEntries(
+ channelType: WebhookSubscriptionChannelType,
+ channels: StaleChannel[],
+ ): StaleChannelToEnqueue[] {
+ return channels.map((channel) => ({ channelType, channel }));
+ }
+
+ private buildStaleChannelScope(activeWorkspaceIds: string[]) {
+ return {
+ workspaceId: In(activeWorkspaceIds),
+ isSyncEnabled: true,
+ connectedAccount: {
+ authFailedAt: IsNull(),
+ provider: In(WEBHOOK_CAPABLE_PROVIDERS),
+ },
+ };
+ }
+
+ private findRenewableSubscriptions<
+ TChannel extends WebhookSubscribableChannel,
+ >(
repository: Repository<TChannel>,
activeWorkspaceIds: string[],
): Promise<StaleChannel[]> {
- const workspaceScope = {
- workspaceId: In(activeWorkspaceIds),
- connectedAccount: { authFailedAt: IsNull() },
- };
- const renewalThreshold = new Date(
- Date.now() + WEBHOOK_SUBSCRIPTION_RENEWAL_BUFFER_MS,
- );
+ const scope = this.buildStaleChannelScope(activeWorkspaceIds);
const options: FindManyOptions<WebhookSubscribableChannel> = {
where: [
{
- ...workspaceScope,
- webhookSubscriptionStatus: WebhookSubscriptionStatus.FAILED,
+ ...scope,
+ webhookSubscriptionStatus: WebhookSubscriptionStatus.ACTIVE,
+ webhookSubscriptionExpiresAt: LessThanOrEqual(
+ new Date(Date.now() + WEBHOOK_SUBSCRIPTION_RENEWAL_BUFFER_MS),
+ ),
},
{
- ...workspaceScope,
- webhookSubscriptionStatus: WebhookSubscriptionStatus.ACTIVE,
- webhookSubscriptionExpiresAt: LessThanOrEqual(renewalThreshold),
+ ...scope,
+ webhookSubscriptionStatus: WebhookSubscriptionStatus.FAILED,
},
],
select: { id: true, workspaceId: true },
@@ -126,11 +185,61 @@ export class WebhookSubscriptionRenewalCronJob {
return repository.find(options as FindManyOptions<TChannel>);
}
+ private findMessageChannelsMissingSubscription(
+ activeWorkspaceIds: string[],
+ ): Promise<StaleChannel[]> {
+ const scope = this.buildStaleChannelScope(activeWorkspaceIds);
+
+ return this.messageChannelRepository.find({
+ where: [
+ {
+ ...scope,
+ webhookSubscriptionStatus: IsNull(),
+ syncStage: Not(MessageChannelSyncStage.PENDING_CONFIGURATION),
+ },
+ {
+ ...scope,
+ webhookSubscriptionStatus: WebhookSubscriptionStatus.EXPIRED,
+ },
+ ],
+ order: { updatedAt: 'ASC' },
+ take: WEBHOOK_SUBSCRIPTION_RENEWAL_BATCH_SIZE,
+ select: { id: true, workspaceId: true, updatedAt: true },
+ });
+ }
+
+ private findCalendarChannelsMissingSubscription(
+ activeWorkspaceIds: string[],
+ ): Promise<StaleChannel[]> {
+ const scope = this.buildStaleChannelScope(activeWorkspaceIds);
+
+ return this.calendarChannelRepository.find({
+ where: [
+ {
+ ...scope,
+ webhookSubscriptionStatus: IsNull(),
+ syncStage: Not(CalendarChannelSyncStage.PENDING_CONFIGURATION),
+ },
+ {
+ ...scope,
+ webhookSubscriptionStatus: WebhookSubscriptionStatus.EXPIRED,
+ },
+ ],
+ order: { updatedAt: 'ASC' },
+ take: WEBHOOK_SUBSCRIPTION_RENEWAL_BATCH_SIZE,
+ select: { id: true, workspaceId: true, updatedAt: true },
+ });
+ }
+
private async enqueueRenewals(
- channelType: WebhookSubscriptionChannelType,
- channels: StaleChannel[],
- ): Promise<void> {
- for (const channel of channels) {
+ staleChannels: StaleChannelToEnqueue[],
+ ): Promise<number> {
+ const spacingMs = Math.min(
+ WEBHOOK_SUBSCRIPTION_RENEWAL_SPACING_MS,
+ WEBHOOK_SUBSCRIPTION_RENEWAL_MAX_TOTAL_DELAY_MS / staleChannels.length,
+ );
+
+ for (const [index, { channelType, channel }] of staleChannels.entries()) {
await this.webhookQueueService.add<RenewWebhookSubscriptionJobData>(
RenewWebhookSubscriptionJob.name,
{
@@ -139,10 +248,12 @@ export class WebhookSubscriptionRenewalCronJob {
workspaceId: channel.workspaceId,
},
{
- id: `${RenewWebhookSubscriptionJob.name}:${channelType}:${channel.id}`,
- retryLimit: 3,
+ retryLimit: WEBHOOK_SUBSCRIPTION_CREATION_RETRY_LIMIT,
+ delay: Math.round(index * spacingMs),
},
);
}
+
+ return spacingMs;
}
}