diff --git a/src/worker/plugins/run.ts b/src/worker/plugins/run.ts index 632ef078..3d4fd1db 100644 --- a/src/worker/plugins/run.ts +++ b/src/worker/plugins/run.ts @@ -58,6 +58,7 @@ export async function runProcessEvent(server: Hub, event: PluginEvent): Promise< const pluginsSucceeded = [] const pluginsFailed = [] + const pluginsDeferred = [] for (const pluginConfig of pluginsToRun) { const processEvent = await pluginConfig.vm?.getProcessEvent() @@ -85,13 +86,20 @@ export async function runProcessEvent(server: Hub, event: PluginEvent): Promise< return null } } + + const onEvent = await pluginConfig.vm?.getOnEvent() + const onSnapshot = await pluginConfig.vm?.getOnSnapshot() + if (onEvent || onSnapshot) { + pluginsDeferred.push(`${pluginConfig.plugin?.name} (${pluginConfig.id})`) + } } - if (pluginsSucceeded.length > 0 || pluginsFailed.length > 0) { + if (pluginsSucceeded.length > 0 || pluginsFailed.length > 0 || pluginsDeferred.length > 0) { event.properties = { ...event.properties, $plugins_succeeded: pluginsSucceeded, $plugins_failed: pluginsFailed, + $plugins_deferred: pluginsDeferred, } } diff --git a/tests/plugins.test.ts b/tests/plugins.test.ts index 4f101fd5..dc546824 100644 --- a/tests/plugins.test.ts +++ b/tests/plugins.test.ts @@ -138,6 +138,7 @@ test('plugin meta has what it should have', async () => { const returnedEvent = await runProcessEvent(hub, event) expect(Object.keys(returnedEvent!.properties!).sort()).toEqual([ + '$plugins_deferred', '$plugins_failed', '$plugins_succeeded', 'attachments', @@ -234,6 +235,7 @@ test('plugin changing event.team_id throws error', async () => { properties: { $plugins_failed: ['test-maxmind-plugin (39)'], $plugins_succeeded: [], + $plugins_deferred: [], }, team_id: 2, } @@ -275,6 +277,7 @@ test('plugin throwing error does not prevent ingestion and failure is noted in e properties: { $plugins_failed: ['test-maxmind-plugin (39)'], $plugins_succeeded: [], + $plugins_deferred: [], }, } expect(returnedEvent).toEqual(expectedReturnEvent) @@ -308,6 +311,41 @@ test('events have property $plugins_succeeded set to the plugins that succeeded' properties: { $plugins_failed: [], $plugins_succeeded: ['test-maxmind-plugin (39)'], + $plugins_deferred: [], + }, + } + expect(returnedEvent).toEqual(expectedReturnEvent) +}) + +test('events have property $plugins_deferred set to the plugins that run after processEvent', async () => { + // silence some spam + console.log = jest.fn() + console.error = jest.fn() + + getPluginRows.mockReturnValueOnce([ + mockPluginWithArchive(` + function onEvent (event) { + return event + } + `), + ]) + getPluginConfigRows.mockReturnValueOnce([pluginConfig39]) + getPluginAttachmentRows.mockReturnValueOnce([pluginAttachment1]) + + await setupPlugins(hub) + const { pluginConfigs } = hub + + expect(await pluginConfigs.get(39)!.vm!.getTasks(PluginTaskType.Schedule)).toEqual({}) + + const event = { event: '$test', properties: {}, team_id: 2 } as PluginEvent + const returnedEvent = await runProcessEvent(hub, { ...event }) + + const expectedReturnEvent = { + ...event, + properties: { + $plugins_failed: [], + $plugins_succeeded: [], + $plugins_deferred: ['test-maxmind-plugin (39)'], }, } expect(returnedEvent).toEqual(expectedReturnEvent)