From d5c5a53708877a6260e7afa46d755486ebc930db Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Fri, 28 May 2021 00:51:49 +0200 Subject: [PATCH 1/2] add $plugins_deferred --- src/worker/plugins/run.ts | 10 +++++++++- tests/plugins.test.ts | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/worker/plugins/run.ts b/src/worker/plugins/run.ts index 5f02ecd2..4688c189 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 3cd8222a..2feb9add 100644 --- a/tests/plugins.test.ts +++ b/tests/plugins.test.ts @@ -235,6 +235,7 @@ test('plugin changing event.team_id throws error (single)', async () => { properties: { $plugins_failed: ['test-maxmind-plugin (39)'], $plugins_succeeded: [], + $plugins_deferred: [], }, team_id: 2, } @@ -274,6 +275,7 @@ test('plugin changing event.team_id throws error (batch)', async () => { properties: { $plugins_failed: ['test-maxmind-plugin (39)'], $plugins_succeeded: [], + $plugins_deferred: [], }, team_id: 2, } @@ -315,6 +317,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) @@ -348,6 +351,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) From f96c33d08223754a3410761caa66611d149b8804 Mon Sep 17 00:00:00 2001 From: Marius Andra Date: Fri, 28 May 2021 08:57:25 +0200 Subject: [PATCH 2/2] remove this, as we will remove the entire batching code ASAP as well --- tests/plugins.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/plugins.test.ts b/tests/plugins.test.ts index 2feb9add..a4c6cb86 100644 --- a/tests/plugins.test.ts +++ b/tests/plugins.test.ts @@ -139,6 +139,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', @@ -275,7 +276,6 @@ test('plugin changing event.team_id throws error (batch)', async () => { properties: { $plugins_failed: ['test-maxmind-plugin (39)'], $plugins_succeeded: [], - $plugins_deferred: [], }, team_id: 2, }