diff --git a/apps/sim/instrumentation-node.ts b/apps/sim/instrumentation-node.ts index c5f384ee145..521e717722e 100644 --- a/apps/sim/instrumentation-node.ts +++ b/apps/sim/instrumentation-node.ts @@ -427,4 +427,7 @@ export async function register() { '@/lib/mothership/request/application/abandoned-runs' ) startAbandonedRunTakeover() + + const { startMemberActivityRecorder } = await import('@/lib/organizations/member-activity/record') + startMemberActivityRecorder() } diff --git a/apps/sim/lib/core/config/trigger-attempt.ts b/apps/sim/lib/core/config/trigger-attempt.ts index c932f59614f..5fd0144b69f 100644 --- a/apps/sim/lib/core/config/trigger-attempt.ts +++ b/apps/sim/lib/core/config/trigger-attempt.ts @@ -1,13 +1,16 @@ import { refreshAwsCredentialsForRun } from '@/lib/core/config/aws' import { warmRedisConnection } from '@/lib/core/config/redis' +import { startMemberActivityRecorder } from '@/lib/organizations/member-activity/record' /** * The per-attempt setup the Trigger.dev `init` hook awaits before `run()`. Warms the shared Redis * connection, and refreshes workload-identity AWS credentials while the attempt is executing and a * token can still be minted, so work a run leaves behind (the hosted-key metrics flush) has - * credentials after the attempt ends. Never throws, like each step: a throw in `init` fails the - * attempt. + * credentials after the attempt ends. Also starts the member activity recorder, because workflow + * runs record uses here and this process never runs `instrumentation-node.ts`. Never throws, like + * each step: a throw in `init` fails the attempt. */ export async function prepareTriggerAttempt(): Promise { + startMemberActivityRecorder() await Promise.all([warmRedisConnection(), refreshAwsCredentialsForRun()]) } diff --git a/apps/sim/lib/organizations/member-activity/record.ts b/apps/sim/lib/organizations/member-activity/record.ts index 60d35d12d66..f019ff20211 100644 --- a/apps/sim/lib/organizations/member-activity/record.ts +++ b/apps/sim/lib/organizations/member-activity/record.ts @@ -69,10 +69,11 @@ function ensureBackground(): void { } /** - * Registered when the module loads, not at the first use, so a process that recorded nothing yet - * still knows it is shutting down when a draining request records its use. + * Called when a process starts (server boot, each Trigger.dev attempt), before any use is recorded. + * Only the first call registers handlers, so a process that recorded nothing yet still knows it is + * shutting down when a draining request records its use. */ -function registerShutdownHandlers(): void { +export function startMemberActivityRecorder(): void { if (handlersRegistered) return handlersRegistered = true const onExit = async () => { @@ -84,8 +85,6 @@ function registerShutdownHandlers(): void { process.once('beforeExit', onExit) } -registerShutdownHandlers() - /** * Counts one use of a surface by a person, on the current UTC day, for the organization that owns * the scope: the organization itself, or the workspace's. Uses are added up in this process and diff --git a/apps/sim/lib/organizations/member-activity/shutdown.integration.ts b/apps/sim/lib/organizations/member-activity/shutdown.integration.ts index 72b97ffc8b0..38f54b7b79c 100644 --- a/apps/sim/lib/organizations/member-activity/shutdown.integration.ts +++ b/apps/sim/lib/organizations/member-activity/shutdown.integration.ts @@ -12,6 +12,7 @@ import { afterAll, beforeAll, expect, it, vi } from 'vitest' import { flushMemberActivity, recordMemberActivity, + startMemberActivityRecorder, } from '@/lib/organizations/member-activity/record' const organizationId = generateId() @@ -34,6 +35,7 @@ async function slackUses() { } beforeAll(async () => { + startMemberActivityRecorder() vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }) const now = new Date() await db.insert(user).values({