Skip to content

Commit 5ec619e

Browse files
authored
refactor(insights): start the member activity recorder at process start, not on import (#10596)
* fix(insights): register the member activity shutdown flush at server boot, not on import Registering process handlers when record.ts loads ran a side effect in every importer, which broke tests that stub the global process (app/api/chat/utils .test.ts: "process.on is not a function", failing test shard 2/2 on staging). startMemberActivityRecorder() now runs from instrumentation-node like the other background starters, still before any use is recorded. * fix(insights): start the member activity recorder in Trigger.dev workers too Workflow, schedule and webhook runs record uses through preprocessing inside Trigger.dev, which never runs instrumentation-node.ts, so the init hook starts the recorder as well (idempotent per process). * fix(insights): start the recorder from prepareTriggerAttempt instead of trigger.config.ts trigger.config.ts only takes relative imports, which the source-conventions ratchet does not let grow; prepareTriggerAttempt is the per-attempt setup the init hook already awaits. * docs(insights): say where the member activity recorder starts
1 parent 21a981f commit 5ec619e

4 files changed

Lines changed: 14 additions & 7 deletions

File tree

‎apps/sim/instrumentation-node.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,7 @@ export async function register() {
427427
'@/lib/mothership/request/application/abandoned-runs'
428428
)
429429
startAbandonedRunTakeover()
430+
431+
const { startMemberActivityRecorder } = await import('@/lib/organizations/member-activity/record')
432+
startMemberActivityRecorder()
430433
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import { refreshAwsCredentialsForRun } from '@/lib/core/config/aws'
22
import { warmRedisConnection } from '@/lib/core/config/redis'
3+
import { startMemberActivityRecorder } from '@/lib/organizations/member-activity/record'
34

45
/**
56
* The per-attempt setup the Trigger.dev `init` hook awaits before `run()`. Warms the shared Redis
67
* connection, and refreshes workload-identity AWS credentials while the attempt is executing and a
78
* token can still be minted, so work a run leaves behind (the hosted-key metrics flush) has
8-
* credentials after the attempt ends. Never throws, like each step: a throw in `init` fails the
9-
* attempt.
9+
* credentials after the attempt ends. Also starts the member activity recorder, because workflow
10+
* runs record uses here and this process never runs `instrumentation-node.ts`. Never throws, like
11+
* each step: a throw in `init` fails the attempt.
1012
*/
1113
export async function prepareTriggerAttempt(): Promise<void> {
14+
startMemberActivityRecorder()
1215
await Promise.all([warmRedisConnection(), refreshAwsCredentialsForRun()])
1316
}

‎apps/sim/lib/organizations/member-activity/record.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ function ensureBackground(): void {
6969
}
7070

7171
/**
72-
* Registered when the module loads, not at the first use, so a process that recorded nothing yet
73-
* still knows it is shutting down when a draining request records its use.
72+
* Called when a process starts (server boot, each Trigger.dev attempt), before any use is recorded.
73+
* Only the first call registers handlers, so a process that recorded nothing yet still knows it is
74+
* shutting down when a draining request records its use.
7475
*/
75-
function registerShutdownHandlers(): void {
76+
export function startMemberActivityRecorder(): void {
7677
if (handlersRegistered) return
7778
handlersRegistered = true
7879
const onExit = async () => {
@@ -84,8 +85,6 @@ function registerShutdownHandlers(): void {
8485
process.once('beforeExit', onExit)
8586
}
8687

87-
registerShutdownHandlers()
88-
8988
/**
9089
* Counts one use of a surface by a person, on the current UTC day, for the organization that owns
9190
* the scope: the organization itself, or the workspace's. Uses are added up in this process and

‎apps/sim/lib/organizations/member-activity/shutdown.integration.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { afterAll, beforeAll, expect, it, vi } from 'vitest'
1212
import {
1313
flushMemberActivity,
1414
recordMemberActivity,
15+
startMemberActivityRecorder,
1516
} from '@/lib/organizations/member-activity/record'
1617

1718
const organizationId = generateId()
@@ -34,6 +35,7 @@ async function slackUses() {
3435
}
3536

3637
beforeAll(async () => {
38+
startMemberActivityRecorder()
3739
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] })
3840
const now = new Date()
3941
await db.insert(user).values({

0 commit comments

Comments
 (0)