Skip to content

The open alternative to Salesforce, designed for AI.

Add Last Contact execution and backfill logging (#28214)

martmull1d37e5aa014d
diff --git a/packages/twenty-apps/public/last-contact/CHANGELOG.md b/packages/twenty-apps/public/last-contact/CHANGELOG.md
index 7967768813c..b34bf345884 100644
--- a/packages/twenty-apps/public/last-contact/CHANGELOG.md
+++ b/packages/twenty-apps/public/last-contact/CHANGELOG.md
@@ -2,6 +2,7 @@
## 1.10.0
+- Log logic function executions and backfill progress, including batch counts, resume cursors, pauses, and completion.
- Use the new Last contact logo from the Twenty app icon family: clock hands inside a message bubble on a violet tile.
## 1.9.1
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/backfill-last-contact.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/backfill-last-contact.ts
index 991bbab1722..25111480d20 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/backfill-last-contact.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/backfill-last-contact.ts
@@ -51,9 +51,19 @@ const handler = async (payload: BackfillPayload): Promise<object> => {
);
const { resumeFrom } = payload;
+ console.log('backfill-last-contact started', {
+ previousVersion: payload.previousVersion,
+ newVersion: payload.newVersion,
+ resumeFrom,
+ stalledRunCount: payload.stalledRunCount ?? 0,
+ batchSize: getBackfillBatchSize(),
+ });
+
if (!isDefined(resumeFrom)) {
+ console.log('Backfill scheduling upcoming meetings');
await scheduleUpcomingPersonMeetings(client);
+ console.log('Backfill applying recently started meetings');
const recentlyStartedParticipants = await collectPersonMeetingParticipants(
client,
{ from: new Date(Date.now() - MEETING_INSTALL_LOOKBACK_MS), to: new Date() },
@@ -62,15 +72,10 @@ const handler = async (payload: BackfillPayload): Promise<object> => {
await applyMeetingInteractions(client, recentlyStartedParticipants);
if(!shouldRunPostInstall(payload)) {
- console.log('Post install skipped');
+ console.log('Backfill skipped: this upgrade does not require a backfill');
return {};
}
-
- console.log(
- 'Backfill params',
- JSON.stringify({ batchSize: getBackfillBatchSize() }),
- );
}
const { phases, pause } = await runLastContactBackfill(client, {
@@ -79,6 +84,8 @@ const handler = async (payload: BackfillPayload): Promise<object> => {
});
if (!isDefined(pause)) {
+ console.log('Backfill completed', { phases });
+
return { outcome: 'completed', phases };
}
@@ -87,6 +94,11 @@ const handler = async (payload: BackfillPayload): Promise<object> => {
const stalledRunCount = hasStalled ? (payload.stalledRunCount ?? 0) + 1 : 0;
if (stalledRunCount >= BACKFILL_MAX_STALLED_RUNS) {
+ console.error('Backfill stopped after repeated stalled runs', {
+ stalledRunCount,
+ resumeFrom: pause.resumeFrom,
+ });
+
throw new Error(
`Backfill stopped: ${stalledRunCount} runs in a row failed before finishing a batch at ${JSON.stringify(pause.resumeFrom)}`,
);
@@ -94,7 +106,12 @@ const handler = async (payload: BackfillPayload): Promise<object> => {
console.log(
'Backfill paused',
- JSON.stringify({ reason: pause.reason, resumeFrom: pause.resumeFrom }),
+ JSON.stringify({
+ reason: pause.reason,
+ resumeFrom: pause.resumeFrom,
+ phases,
+ stalledRunCount,
+ }),
);
await executeWithRetry(() =>
@@ -108,6 +125,15 @@ const handler = async (payload: BackfillPayload): Promise<object> => {
}),
);
+ console.log('Backfill continuation enqueued', {
+ resumeFrom: pause.resumeFrom,
+ stalledRunCount,
+ delayMs:
+ pause.reason === 'rate-limited'
+ ? BACKFILL_RATE_LIMITED_RESUME_DELAY_MS
+ : 0,
+ });
+
return { outcome: 'paused', phases, resumeFrom: pause.resumeFrom };
};
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/health-check.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/health-check.ts
index 780aef09b7b..a3240f8cf26 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/health-check.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/health-check.ts
@@ -7,6 +7,8 @@ import {
import { HEALTH_CHECK_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER } from 'src/constants/universal-identifiers';
const handler = async (): Promise<ApplicationHealthCheckResult> => {
+ console.log('health-check started');
+
const { messageParticipants, calendarEventParticipants } =
await new CoreApiClient().query({
messageParticipants: {
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/on-calendar-event-rescheduled.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/on-calendar-event-rescheduled.ts
index b3dd4bc1595..8081cdeb36d 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/on-calendar-event-rescheduled.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/on-calendar-event-rescheduled.ts
@@ -21,6 +21,10 @@ const handler = async (
ObjectRecordUpdateEvent<CalendarEventUpdate>
>,
): Promise<void> => {
+ console.log('on-calendar-event-rescheduled started', {
+ eventCount: batch.events.length,
+ });
+
const now = new Date();
const upcomingMeetingStartsAts: string[] = [];
const startedCalendarEventIds: string[] = [];
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/on-calendar-interaction.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/on-calendar-interaction.ts
index 180b4bceae9..8b9d968a5f2 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/on-calendar-interaction.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/on-calendar-interaction.ts
@@ -23,6 +23,10 @@ const handler = async (
ObjectRecordUpdateEvent<CalendarEventParticipantUpdate>
>,
): Promise<void> => {
+ console.log('on-calendar-interaction started', {
+ eventCount: batch.events.length,
+ });
+
const linkByKey = new Map<string, CalendarEventParticipantLink>();
for (const event of batch.events) {
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/on-email-interaction.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/on-email-interaction.ts
index 55551a9f132..26ed74feb15 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/on-email-interaction.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/on-email-interaction.ts
@@ -22,6 +22,8 @@ const handler = async (
ObjectRecordUpdateEvent<MessageParticipantUpdate>
>,
): Promise<void> => {
+ console.log('on-email-interaction started', { eventCount: batch.events.length });
+
const linkByKey = new Map<string, MessageParticipantLink>();
for (const event of batch.events) {
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/on-meeting-horizon-reached.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/on-meeting-horizon-reached.ts
index 5d41eae4d10..3a5466bd208 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/on-meeting-horizon-reached.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/on-meeting-horizon-reached.ts
@@ -6,6 +6,8 @@ import { MEETING_HORIZON_REACHED_LOGIC_FUNCTION_UNIVERSAL_IDENTIFIER } from 'src
import { scheduleUpcomingPersonMeetings } from 'src/utils/schedule-meetings';
const handler = async (): Promise<void> => {
+ console.log('on-meeting-horizon-reached started');
+
await scheduleUpcomingPersonMeetings(new CoreApiClient());
};
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/on-meeting-slot-reached.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/on-meeting-slot-reached.ts
index d0af94ec4c5..a099145a560 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/on-meeting-slot-reached.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/on-meeting-slot-reached.ts
@@ -11,6 +11,8 @@ const handler = async ({
slotStart,
slotEnd,
}: MeetingSlotPayload): Promise<void> => {
+ console.log('on-meeting-slot-reached started', { slotStart, slotEnd });
+
const client = new CoreApiClient();
const participants = await collectPersonMeetingParticipants(client, {
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/on-opportunity-created.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/on-opportunity-created.ts
index ea9b8a582d4..1253e69f1f4 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/on-opportunity-created.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/on-opportunity-created.ts
@@ -14,6 +14,10 @@ type OpportunityCreate = { id?: string | null };
const handler = async (
batch: DatabaseEventBatchPayload<ObjectRecordCreateEvent<OpportunityCreate>>,
): Promise<void> => {
+ console.log('on-opportunity-created started', {
+ eventCount: batch.events.length,
+ });
+
const opportunityIds = new Set<string>();
for (const event of batch.events) {
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/on-opportunity-updated.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/on-opportunity-updated.ts
index 266695e238e..8b561fbcc34 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/on-opportunity-updated.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/on-opportunity-updated.ts
@@ -14,6 +14,10 @@ type OpportunityUpdate = { id?: string | null };
const handler = async (
batch: DatabaseEventBatchPayload<ObjectRecordUpdateEvent<OpportunityUpdate>>,
): Promise<void> => {
+ console.log('on-opportunity-updated started', {
+ eventCount: batch.events.length,
+ });
+
const opportunityIds = new Set<string>();
for (const event of batch.events) {
diff --git a/packages/twenty-apps/public/last-contact/src/logic-functions/on-person-updated.ts b/packages/twenty-apps/public/last-contact/src/logic-functions/on-person-updated.ts
index 33fdd1c735b..e6f9e5c7667 100644
--- a/packages/twenty-apps/public/last-contact/src/logic-functions/on-person-updated.ts
+++ b/packages/twenty-apps/public/last-contact/src/logic-functions/on-person-updated.ts
@@ -14,6 +14,8 @@ type PersonUpdate = { companyId?: string | null };
const handler = async (
batch: DatabaseEventBatchPayload<ObjectRecordUpdateEvent<PersonUpdate>>,
): Promise<void> => {
+ console.log('on-person-updated started', { eventCount: batch.events.length });
+
const companyIds = new Set<string>();
// Both the person's former and current company can lose or gain their most
diff --git a/packages/twenty-apps/public/last-contact/src/utils/run-last-contact-backfill.ts b/packages/twenty-apps/public/last-contact/src/utils/run-last-contact-backfill.ts
index 880a01b2515..95120f49f6c 100644
--- a/packages/twenty-apps/public/last-contact/src/utils/run-last-contact-backfill.ts
+++ b/packages/twenty-apps/public/last-contact/src/utils/run-last-contact-backfill.ts
@@ -65,6 +65,8 @@ const backfillPhaseInBatches = async ({
}
try {
+ console.log('Backfill batch started', { phase, after, batchSize });
+
const result = await executeWithRetry(() =>
client.query({
[phase]: {
@@ -87,11 +89,20 @@ const backfillPhaseInBatches = async ({
after = connection?.pageInfo.hasNextPage
? (connection.pageInfo.endCursor ?? undefined)
: undefined;
+
+ console.log('Backfill batch completed', {
+ phase,
+ batchCount: recordIds.length,
+ processedCount: count,
+ nextCursor: after,
+ });
} catch (error) {
if (error instanceof RetryableLogicFunctionError) {
return { count, pause: { reason: 'rate-limited', after } };
}
+ console.error('Backfill batch failed', { phase, after, count }, error);
+
throw error;
}
} while (after);