Skip to content

The open alternative to Salesforce, designed for AI.

fix message channels stuck in ONGOING (#17492)

neo7738fb8b5d2f1e0
diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/jobs/calendar-ongoing-stale.job.ts b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/jobs/calendar-ongoing-stale.job.ts
index cae524cbd2f..887e6aa200e 100644
--- a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/jobs/calendar-ongoing-stale.job.ts
+++ b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/jobs/calendar-ongoing-stale.job.ts
@@ -54,10 +54,7 @@ export class CalendarOngoingStaleJob {
});
for (const calendarChannel of calendarChannels) {
- if (
- calendarChannel.syncStageStartedAt &&
- isSyncStale(calendarChannel.syncStageStartedAt)
- ) {
+ if (isSyncStale(calendarChannel.syncStageStartedAt)) {
await this.calendarChannelSyncStatusService.resetSyncStageStartedAt(
[calendarChannel.id],
workspaceId,
diff --git a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/utils/is-sync-stale.util.ts b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/utils/is-sync-stale.util.ts
index a204ae83e21..9f5cff68238 100644
--- a/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/utils/is-sync-stale.util.ts
+++ b/packages/twenty-server/src/modules/calendar/calendar-event-import-manager/utils/is-sync-stale.util.ts
@@ -1,6 +1,12 @@
+import { isDefined } from 'twenty-shared/utils';
+
import { CALENDAR_IMPORT_ONGOING_SYNC_TIMEOUT } from 'src/modules/calendar/calendar-event-import-manager/constants/calendar-import-ongoing-sync-timeout.constant';
-export const isSyncStale = (syncStageStartedAt: string): boolean => {
+export const isSyncStale = (syncStageStartedAt?: string | null): boolean => {
+ if (!isDefined(syncStageStartedAt)) {
+ return false;
+ }
+
const syncStageStartedTime = new Date(syncStageStartedAt).getTime();
if (isNaN(syncStageStartedTime)) {
diff --git a/packages/twenty-server/src/modules/calendar/common/services/calendar-channel-sync-status.service.ts b/packages/twenty-server/src/modules/calendar/common/services/calendar-channel-sync-status.service.ts
index 166888abae2..b6f6f3de141 100644
--- a/packages/twenty-server/src/modules/calendar/common/services/calendar-channel-sync-status.service.ts
+++ b/packages/twenty-server/src/modules/calendar/common/services/calendar-channel-sync-status.service.ts
@@ -182,6 +182,7 @@ export class CalendarChannelSyncStatusService {
await calendarChannelRepository.update(calendarChannelIds, {
syncStage: CalendarChannelSyncStage.CALENDAR_EVENTS_IMPORT_ONGOING,
syncStatus: CalendarChannelSyncStatus.ONGOING,
+ syncStageStartedAt: new Date().toISOString(),
});
}, authContext);
}
diff --git a/packages/twenty-server/src/modules/messaging/common/services/message-channel-sync-status.service.ts b/packages/twenty-server/src/modules/messaging/common/services/message-channel-sync-status.service.ts
index 81d89232609..d18e0486b09 100644
--- a/packages/twenty-server/src/modules/messaging/common/services/message-channel-sync-status.service.ts
+++ b/packages/twenty-server/src/modules/messaging/common/services/message-channel-sync-status.service.ts
@@ -199,6 +199,7 @@ export class MessageChannelSyncStatusService {
await messageChannelRepository.update(messageChannelIds, {
syncStage: MessageChannelSyncStage.MESSAGE_LIST_FETCH_ONGOING,
syncStatus: MessageChannelSyncStatus.ONGOING,
+ syncStageStartedAt: new Date().toISOString(),
});
}, authContext);
}
@@ -277,6 +278,7 @@ export class MessageChannelSyncStatusService {
await messageChannelRepository.update(messageChannelIds, {
syncStage: MessageChannelSyncStage.MESSAGES_IMPORT_ONGOING,
+ syncStatus: MessageChannelSyncStatus.ONGOING,
syncStageStartedAt: new Date().toISOString(),
});
}, authContext);
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/jobs/messaging-ongoing-stale.job.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/jobs/messaging-ongoing-stale.job.ts
index 701acd366e5..561850742cb 100644
--- a/packages/twenty-server/src/modules/messaging/message-import-manager/jobs/messaging-ongoing-stale.job.ts
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/jobs/messaging-ongoing-stale.job.ts
@@ -54,10 +54,7 @@ export class MessagingOngoingStaleJob {
});
for (const messageChannel of messageChannels) {
- if (
- messageChannel.syncStageStartedAt &&
- isSyncStale(messageChannel.syncStageStartedAt)
- ) {
+ if (isSyncStale(messageChannel.syncStageStartedAt)) {
await this.messageChannelSyncStatusService.resetSyncStageStartedAt(
[messageChannel.id],
workspaceId,
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/is-sync-stale.util.spec.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/is-sync-stale.util.spec.ts
index 9935eb61081..07704739194 100644
--- a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/is-sync-stale.util.spec.ts
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/__tests__/is-sync-stale.util.spec.ts
@@ -24,7 +24,19 @@ describe('isSyncStale', () => {
expect(result).toBe(false);
});
- it('should return false if syncStageStartedAt is invalid', () => {
+ it('should return false if syncStageStartedAt is undefined', () => {
+ const result = isSyncStale(undefined);
+
+ expect(result).toBe(false);
+ });
+
+ it('should return false if syncStageStartedAt is null', () => {
+ const result = isSyncStale(null);
+
+ expect(result).toBe(false);
+ });
+
+ it('should throw an error if syncStageStartedAt is invalid', () => {
const syncStageStartedAt = 'invalid-date';
expect(() => {
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/is-sync-stale.util.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/is-sync-stale.util.ts
index 7a1ae5e2029..45661f5dd6c 100644
--- a/packages/twenty-server/src/modules/messaging/message-import-manager/utils/is-sync-stale.util.ts
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/utils/is-sync-stale.util.ts
@@ -1,6 +1,12 @@
+import { isDefined } from 'twenty-shared/utils';
+
import { MESSAGING_IMPORT_ONGOING_SYNC_TIMEOUT } from 'src/modules/messaging/message-import-manager/constants/messaging-import-ongoing-sync-timeout.constant';
-export const isSyncStale = (syncStageStartedAt: string): boolean => {
+export const isSyncStale = (syncStageStartedAt?: string | null): boolean => {
+ if (!isDefined(syncStageStartedAt)) {
+ return false;
+ }
+
const syncStageStartedTime = new Date(syncStageStartedAt).getTime();
if (isNaN(syncStageStartedTime)) {