2021-05-25 13:49:41 +02:00
|
|
|
import Piscina from '@posthog/piscina'
|
2021-02-08 15:55:24 +01:00
|
|
|
import { PluginEvent } from '@posthog/plugin-scaffold/src/types'
|
2021-05-25 13:49:41 +02:00
|
|
|
import { Redis } from 'ioredis'
|
2022-05-11 08:01:54 +02:00
|
|
|
import * as nodeSchedule from 'node-schedule'
|
2021-02-08 15:55:24 +01:00
|
|
|
|
2021-03-16 17:06:15 +01:00
|
|
|
import {
|
|
|
|
loadPluginSchedule,
|
|
|
|
LOCKED_RESOURCE,
|
2021-05-04 16:05:05 +02:00
|
|
|
runScheduleDebounced,
|
2022-05-11 08:01:54 +02:00
|
|
|
startPluginSchedules,
|
2021-03-16 17:06:15 +01:00
|
|
|
waitForTasksToFinish,
|
2021-03-22 12:39:24 +01:00
|
|
|
} from '../src/main/services/schedule'
|
2022-05-11 08:01:54 +02:00
|
|
|
import { Hub, LogLevel, PluginScheduleControl } from '../src/types'
|
2021-05-25 13:49:41 +02:00
|
|
|
import { createHub } from '../src/utils/db/hub'
|
2021-05-03 13:14:30 +02:00
|
|
|
import { delay } from '../src/utils/utils'
|
2021-03-16 17:06:15 +01:00
|
|
|
import { createPromise } from './helpers/promises'
|
2021-01-06 23:16:10 +01:00
|
|
|
import { resetTestDatabase } from './helpers/sql'
|
2021-02-08 15:55:24 +01:00
|
|
|
import { setupPiscina } from './helpers/worker'
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2021-05-03 13:14:30 +02:00
|
|
|
jest.mock('../src/utils/db/sql')
|
|
|
|
jest.mock('../src/utils/status')
|
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 },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-11 08:01:54 +02:00
|
|
|
function numberOfScheduledJobs() {
|
|
|
|
return Object.keys(nodeSchedule.scheduledJobs).length
|
|
|
|
}
|
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
describe('schedule', () => {
|
|
|
|
test('runScheduleDebounced', async () => {
|
|
|
|
const workerThreads = 1
|
|
|
|
const testCode = `
|
2020-12-18 00:41:25 +01:00
|
|
|
const counterKey = 'test_counter_2'
|
|
|
|
async function setupPlugin (meta) {
|
|
|
|
await meta.cache.set(counterKey, 0)
|
2021-03-10 11:20:28 +01:00
|
|
|
}
|
2020-12-18 00:41:25 +01:00
|
|
|
async function processEvent (event, meta) {
|
|
|
|
event.properties['counter'] = await meta.cache.get(counterKey)
|
2021-03-10 11:20:28 +01:00
|
|
|
return event
|
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)
|
|
|
|
}
|
|
|
|
`
|
2022-03-10 12:18:24 +01:00
|
|
|
await resetTestDatabase(testCode)
|
|
|
|
const piscina = setupPiscina(workerThreads, 10)
|
|
|
|
const processEvent = (event: PluginEvent) => piscina.run({ task: 'processEvent', args: { event } })
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
const [hub, closeHub] = await createHub({ LOG_LEVEL: LogLevel.Log })
|
|
|
|
hub.pluginSchedule = await loadPluginSchedule(piscina)
|
|
|
|
expect(hub.pluginSchedule).toEqual({ runEveryDay: [], runEveryHour: [], runEveryMinute: [39] })
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
const event1 = await processEvent(createEvent())
|
|
|
|
expect(event1.properties['counter']).toBe(0)
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
runScheduleDebounced(hub, piscina, 'runEveryMinute')
|
|
|
|
runScheduleDebounced(hub, piscina, 'runEveryMinute')
|
|
|
|
runScheduleDebounced(hub, piscina, 'runEveryMinute')
|
|
|
|
await delay(100)
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
const event2 = await processEvent(createEvent())
|
|
|
|
expect(event2.properties['counter']).toBe(0)
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
await delay(500)
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
const event3 = await processEvent(createEvent())
|
|
|
|
expect(event3.properties['counter']).toBe(1)
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
await waitForTasksToFinish(hub)
|
|
|
|
await delay(1000)
|
|
|
|
await piscina.destroy()
|
|
|
|
await closeHub()
|
|
|
|
})
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
test('runScheduleDebounced exception', async () => {
|
|
|
|
const workerThreads = 2
|
|
|
|
const testCode = `
|
2020-12-18 00:41:25 +01:00
|
|
|
async function runEveryMinute (meta) {
|
|
|
|
throw new Error('lol')
|
|
|
|
}
|
|
|
|
`
|
2022-03-10 12:18:24 +01:00
|
|
|
await resetTestDatabase(testCode)
|
|
|
|
const piscina = setupPiscina(workerThreads, 10)
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
const [hub, closeHub] = await createHub({ LOG_LEVEL: LogLevel.Log })
|
|
|
|
hub.pluginSchedule = await loadPluginSchedule(piscina)
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
runScheduleDebounced(hub, piscina, 'runEveryMinute')
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
await waitForTasksToFinish(hub)
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +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...
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
try {
|
|
|
|
await delay(1000)
|
|
|
|
await piscina.destroy()
|
|
|
|
await closeHub()
|
|
|
|
} catch {}
|
|
|
|
})
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-05-11 08:01:54 +02:00
|
|
|
describe('startPluginSchedules', () => {
|
2022-03-10 12:18:24 +01:00
|
|
|
let hub: Hub, piscina: Piscina, closeHub: () => Promise<void>, redis: Redis
|
2021-03-10 11:20:28 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
beforeEach(async () => {
|
|
|
|
const workerThreads = 2
|
|
|
|
const testCode = `
|
2021-03-10 11:20:28 +01:00
|
|
|
async function runEveryMinute (meta) {
|
|
|
|
throw new Error('lol')
|
|
|
|
}
|
|
|
|
`
|
2022-03-10 12:18:24 +01:00
|
|
|
await resetTestDatabase(testCode)
|
|
|
|
piscina = setupPiscina(workerThreads, 10)
|
|
|
|
;[hub, closeHub] = await createHub({ LOG_LEVEL: LogLevel.Log, SCHEDULE_LOCK_TTL: 3 })
|
2021-03-16 17:06:15 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
redis = await hub.redisPool.acquire()
|
|
|
|
await redis.del(LOCKED_RESOURCE)
|
|
|
|
})
|
2021-03-10 11:20:28 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
afterEach(async () => {
|
|
|
|
await redis.del(LOCKED_RESOURCE)
|
|
|
|
await hub.redisPool.release(redis)
|
|
|
|
await delay(1000)
|
|
|
|
await piscina.destroy()
|
|
|
|
await closeHub()
|
2021-03-10 11:20:28 +01:00
|
|
|
})
|
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
test('redlock', async () => {
|
|
|
|
const promises = [createPromise(), createPromise(), createPromise()]
|
|
|
|
let i = 0
|
2021-03-10 11:20:28 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
let lock1 = false
|
|
|
|
let lock2 = false
|
|
|
|
let lock3 = false
|
2021-03-10 11:20:28 +01:00
|
|
|
|
2022-05-11 08:01:54 +02:00
|
|
|
const schedule1 = await startPluginSchedules(hub, piscina, () => {
|
2022-03-10 12:18:24 +01:00
|
|
|
lock1 = true
|
|
|
|
promises[i++].resolve()
|
|
|
|
})
|
|
|
|
|
|
|
|
await promises[0].promise
|
2021-03-16 17:06:15 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
expect(lock1).toBe(true)
|
|
|
|
expect(lock2).toBe(false)
|
|
|
|
expect(lock3).toBe(false)
|
2021-03-10 11:20:28 +01:00
|
|
|
|
2022-05-11 08:01:54 +02:00
|
|
|
const schedule2 = await startPluginSchedules(hub, piscina, () => {
|
2022-03-10 12:18:24 +01:00
|
|
|
lock2 = true
|
|
|
|
promises[i++].resolve()
|
|
|
|
})
|
2022-05-11 08:01:54 +02:00
|
|
|
const schedule3 = await startPluginSchedules(hub, piscina, () => {
|
2022-03-10 12:18:24 +01:00
|
|
|
lock3 = true
|
|
|
|
promises[i++].resolve()
|
|
|
|
})
|
2021-03-10 11:20:28 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
await schedule1.stopSchedule()
|
2021-03-10 11:20:28 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
await promises[1].promise
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
expect(lock2 || lock3).toBe(true)
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +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()
|
|
|
|
}
|
2021-03-10 11:20:28 +01:00
|
|
|
})
|
2020-12-18 00:41:25 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
describe('loading the schedule', () => {
|
2022-05-11 08:01:54 +02:00
|
|
|
let schedule: PluginScheduleControl | null = null
|
2021-03-16 17:06:15 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
afterEach(async () => {
|
2022-05-11 08:01:54 +02:00
|
|
|
await schedule?.stopSchedule()
|
|
|
|
schedule = null
|
2022-03-10 12:18:24 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
test('loads successfully', async () => {
|
2022-05-11 08:01:54 +02:00
|
|
|
schedule = await startPluginSchedules(hub, piscina)
|
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
expect(hub.pluginSchedule).toEqual({
|
|
|
|
runEveryMinute: [39],
|
|
|
|
runEveryHour: [],
|
|
|
|
runEveryDay: [],
|
|
|
|
})
|
|
|
|
|
|
|
|
await resetTestDatabase(`
|
2021-03-16 17:06:15 +01:00
|
|
|
async function runEveryDay (meta) {
|
|
|
|
throw new Error('lol')
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
await schedule.reloadSchedule()
|
2021-03-16 17:06:15 +01:00
|
|
|
|
2022-03-10 12:18:24 +01:00
|
|
|
expect(hub.pluginSchedule).toEqual({
|
|
|
|
runEveryMinute: [39],
|
|
|
|
runEveryHour: [],
|
|
|
|
runEveryDay: [],
|
|
|
|
})
|
2021-03-16 17:06:15 +01:00
|
|
|
})
|
2022-05-11 08:01:54 +02: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)
|
|
|
|
})
|
2021-03-16 17:06:15 +01:00
|
|
|
})
|
2020-12-18 00:41:25 +01:00
|
|
|
})
|
|
|
|
})
|