0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-12-01 04:12:23 +01:00
posthog/plugin-server/tests/schedule.test.ts

235 lines
7.5 KiB
TypeScript
Raw Normal View History

import Piscina from '@posthog/piscina'
import { PluginEvent } from '@posthog/plugin-scaffold/src/types'
import { Redis } from 'ioredis'
import * as nodeSchedule from 'node-schedule'
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
import {
loadPluginSchedule,
LOCKED_RESOURCE,
runScheduleDebounced,
startPluginSchedules,
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
waitForTasksToFinish,
} from '../src/main/services/schedule'
import { Hub, LogLevel, PluginScheduleControl } from '../src/types'
import { createHub } from '../src/utils/db/hub'
import { delay } from '../src/utils/utils'
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
import { createPromise } from './helpers/promises'
import { resetTestDatabase } from './helpers/sql'
import { setupPiscina } from './helpers/worker'
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
jest.mock('../src/utils/db/sql')
jest.mock('../src/utils/status')
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
jest.setTimeout(60000) // 60 sec timeout
function createEvent(index = 0): PluginEvent {
return {
distinct_id: 'my_id',
ip: '127.0.0.1',
site_url: 'http://localhost',
team_id: 2,
now: new Date().toISOString(),
event: 'default event',
properties: { key: 'value', index },
}
}
function numberOfScheduledJobs() {
return Object.keys(nodeSchedule.scheduledJobs).length
}
describe('schedule', () => {
test('runScheduleDebounced', async () => {
const workerThreads = 1
const testCode = `
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
const counterKey = 'test_counter_2'
async function setupPlugin (meta) {
await meta.cache.set(counterKey, 0)
}
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
async function processEvent (event, meta) {
event.properties['counter'] = await meta.cache.get(counterKey)
return event
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
}
async function runEveryMinute (meta) {
// stall for a second
await new Promise(resolve => __jestSetTimeout(resolve, 500))
await meta.cache.incr(counterKey)
}
`
await resetTestDatabase(testCode)
const piscina = setupPiscina(workerThreads, 10)
const processEvent = (event: PluginEvent) => piscina.run({ task: 'processEvent', args: { event } })
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
const [hub, closeHub] = await createHub({ LOG_LEVEL: LogLevel.Log })
hub.pluginSchedule = await loadPluginSchedule(piscina)
expect(hub.pluginSchedule).toEqual({ runEveryDay: [], runEveryHour: [], runEveryMinute: [39] })
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
const event1 = await processEvent(createEvent())
expect(event1.properties['counter']).toBe(0)
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
runScheduleDebounced(hub, piscina, 'runEveryMinute')
runScheduleDebounced(hub, piscina, 'runEveryMinute')
runScheduleDebounced(hub, piscina, 'runEveryMinute')
await delay(100)
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
const event2 = await processEvent(createEvent())
expect(event2.properties['counter']).toBe(0)
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
await delay(500)
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
const event3 = await processEvent(createEvent())
expect(event3.properties['counter']).toBe(1)
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
await waitForTasksToFinish(hub)
await delay(1000)
await piscina.destroy()
await closeHub()
})
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
test('runScheduleDebounced exception', async () => {
const workerThreads = 2
const testCode = `
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
async function runEveryMinute (meta) {
throw new Error('lol')
}
`
await resetTestDatabase(testCode)
const piscina = setupPiscina(workerThreads, 10)
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
const [hub, closeHub] = await createHub({ LOG_LEVEL: LogLevel.Log })
hub.pluginSchedule = await loadPluginSchedule(piscina)
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
runScheduleDebounced(hub, piscina, 'runEveryMinute')
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
await waitForTasksToFinish(hub)
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
// nothing bad should have happened. the error is in SQL via setError, but that ran in another worker (can't mock)
// and we're not testing it E2E so we can't check the DB either...
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
try {
await delay(1000)
await piscina.destroy()
await closeHub()
} catch {}
})
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
describe('startPluginSchedules', () => {
let hub: Hub, piscina: Piscina, closeHub: () => Promise<void>, redis: Redis
beforeEach(async () => {
const workerThreads = 2
const testCode = `
async function runEveryMinute (meta) {
throw new Error('lol')
}
`
await resetTestDatabase(testCode)
piscina = setupPiscina(workerThreads, 10)
;[hub, closeHub] = await createHub({ LOG_LEVEL: LogLevel.Log, SCHEDULE_LOCK_TTL: 3 })
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
redis = await hub.redisPool.acquire()
await redis.del(LOCKED_RESOURCE)
})
afterEach(async () => {
await redis.del(LOCKED_RESOURCE)
await hub.redisPool.release(redis)
await delay(1000)
await piscina.destroy()
await closeHub()
})
test('redlock', async () => {
const promises = [createPromise(), createPromise(), createPromise()]
let i = 0
let lock1 = false
let lock2 = false
let lock3 = false
const schedule1 = await startPluginSchedules(hub, piscina, () => {
lock1 = true
promises[i++].resolve()
})
await promises[0].promise
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
expect(lock1).toBe(true)
expect(lock2).toBe(false)
expect(lock3).toBe(false)
const schedule2 = await startPluginSchedules(hub, piscina, () => {
lock2 = true
promises[i++].resolve()
})
const schedule3 = await startPluginSchedules(hub, piscina, () => {
lock3 = true
promises[i++].resolve()
})
await schedule1.stopSchedule()
await promises[1].promise
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
expect(lock2 || lock3).toBe(true)
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
if (lock3) {
await schedule3.stopSchedule()
await promises[2].promise
expect(lock2).toBe(true)
await schedule2.stopSchedule()
} else {
await schedule2.stopSchedule()
await promises[2].promise
expect(lock3).toBe(true)
await schedule3.stopSchedule()
}
})
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
describe('loading the schedule', () => {
let schedule: PluginScheduleControl | null = null
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
afterEach(async () => {
await schedule?.stopSchedule()
schedule = null
})
test('loads successfully', async () => {
schedule = await startPluginSchedules(hub, piscina)
expect(hub.pluginSchedule).toEqual({
runEveryMinute: [39],
runEveryHour: [],
runEveryDay: [],
})
await resetTestDatabase(`
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
async function runEveryDay (meta) {
throw new Error('lol')
}
`)
await schedule.reloadSchedule()
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
expect(hub.pluginSchedule).toEqual({
runEveryMinute: [39],
runEveryHour: [],
runEveryDay: [],
})
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
})
test('node-schedule tasks are created and removed on stop', async () => {
expect(numberOfScheduledJobs()).toEqual(0)
const schedule = await startPluginSchedules(hub, piscina)
expect(numberOfScheduledJobs()).toEqual(3)
nodeSchedule.scheduleJob('1 1 1 1 1', () => 1)
expect(numberOfScheduledJobs()).toEqual(4)
await schedule.stopSchedule()
expect(numberOfScheduledJobs()).toEqual(0)
})
[plugin-server] Speed up boot times (https://github.com/PostHog/plugin-server/pull/234) * Shims to make it possible to load vms asynchronously in the background * Speed up vm.test.ts tests * Add tests for createLazyPluginVM * Make plugins tests pass again * Add test for loadSchedule * Handle plugin schedules being loaded asynchronously gracefully It adds a new 'reloadSchedule' handle, but this is (yet) unused * Add updated_at, created_at to types * Remove a debug log * Extract method, get rid of defaultConfigs code * Reload only changed plugins when calling `setupPlugins` multiple times * Extract separate files for plugins code * Improve plugins tests * Add test for loading reloading plugin order * Log how many scheduled tasks there are * Reload only changed plugins This depends on marius' PR against upstream piscina package. Still need to package that up properly * Make loading plugins more parallel This should slightly speed up initial loads. Note that I no real chunking was done here - it seems these tasks do not do much that affect any outside resources and our event loop should take care of the rest. * Move clearing errors to lazy VM code * Add test for scheduling * Use our own fork of piscana * Make clearing/adding errors async _somehow_ this manages to deadlock the whole process, not quite sure how though. * Readd needed bits to test * Load schedule properly in worker tasks * Forceexit * Less noisy e2e tests * Make schedule tests robust * Make createPromise reliable * Make workers test more stable * Increase max stack size on worker threads This can cause workers to become unresponsive * Experiment: make schedule loading async * Revert eagerness * worker.test schdule * Load schedule sync again * No more infinite timeouts * Wait until all VMs are loaded when initializing things Without this it seems like the promises are never resolved - this happens consistently in some tests. * Nudge plugin server to reload schedule as needed This seems needed because schedule.test.ts never finished executing otherwise unless code path changed in weird ways. Not sure what's going on there. * Kill lib.js support (#238) As per comment on https://github.com/PostHog/plugin-server/pull/234#discussion_r590483939 * Avoid dropping scheduled tasks due to plugin reloads * Make lazy VMs even lazier Now VM-like promise is set up as soon as we query the database. This avoids a race condition where plugins get reloaded and `.vm` is undefined for a while * Add status.warn * Add memoize comment * Run runTasksDebounced with 2 workerThreads * Update vm.lazy.test.ts * Make LazyPluginVM a class * Remove obsolete createLazyPluginVM import * Update vm.lazy.test.ts * Update plugins.test.ts * Fix LazyPluginVM import * Fix vm.lazy.test.ts * Revert "Run runTasksDebounced with 2 workerThreads" This reverts commit 0c436f3ac08310bfb1143b0de98b032c451a19e0. Co-authored-by: Michael Matloka <dev@twixes.com>
2021-03-16 17:06:15 +01:00
})
[plugin-server] Run scheduler on only one instance (https://github.com/PostHog/plugin-server/pull/79) * extract schedule service * run schedule only when we have a lock by node-redlock * test schedule locking * remove redis mock * fix vm tests for real redis * close -> stop * Revert "remove redis mock" This reverts commit 15de8056 * make redis mock optional per test * use better redis tools (llen) in queue test * fix worker test with redis mock * fix open handle * disable web by default * add type * set up a delicate balance of timeouts to make this work * run worker test with real redis * ignore lock redis errors if stopped * detect open handlers in jest test on githuv * add a bit more time * manually close unzipstream... * skip zip/tgz extract tests because jest thinks they are getting stuck * Revert "skip zip/tgz extract tests because jest thinks they are getting stuck" This reverts commit cac55ca3 * remove open handler detection * force tests to exit once done. not ideal, but could work * force benchmarks to exit as well * better redlock quitting * ignore redis errors when server is shutting down or is shut down (like for redlock) * rewrite locking to use our own cancellable timeouts * remove `shuttingDown` from server * fix typo * close open handles * add timeout for memory test * remove unrelated flyby change * RedLock to Redlock * lockedResource to LOCKED_RESOURCE * get the lock in the next tick * Revert "get the lock in the next tick" This reverts commit 77494b546cdc0bea46d5c5963c5fe2317d5ec8c4. Co-authored-by: Michael Matloka <dev@twixes.com>
2020-12-18 00:41:25 +01:00
})
})