2022-12-05 13:30:52 +01:00
|
|
|
import { KAFKA_SCHEDULED_TASKS } from '../../../src/config/kafka-topics'
|
2022-10-18 16:44:41 +02:00
|
|
|
import { runScheduledTasks } from '../../../src/main/graphile-worker/schedule'
|
|
|
|
import { Hub } from '../../../src/types'
|
2022-12-05 13:30:52 +01:00
|
|
|
import { KafkaProducerWrapper } from '../../../src/utils/db/kafka-producer-wrapper'
|
2022-10-18 16:44:41 +02:00
|
|
|
import { UUID } from '../../../src/utils/utils'
|
|
|
|
|
|
|
|
const mockHub: Hub = {
|
|
|
|
instanceId: new UUID('F8B2F832-6639-4596-ABFC-F9664BC88E84'),
|
|
|
|
JOB_QUEUES: 'fs',
|
|
|
|
} as Hub
|
|
|
|
|
|
|
|
describe('Graphile Worker schedule', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('runScheduledTasks()', async () => {
|
2022-12-03 01:13:27 +01:00
|
|
|
const mockPiscina = {
|
|
|
|
run: jest.fn(),
|
|
|
|
}
|
|
|
|
|
2022-10-18 16:44:41 +02:00
|
|
|
const mockHubWithPluginSchedule = {
|
|
|
|
...mockHub,
|
|
|
|
pluginSchedule: {
|
|
|
|
runEveryMinute: [1, 2, 3],
|
|
|
|
runEveryHour: [4, 5, 6],
|
|
|
|
runEveryDay: [7, 8, 9],
|
|
|
|
},
|
2022-12-05 13:30:52 +01:00
|
|
|
kafkaProducer: {
|
2023-05-04 15:02:44 +02:00
|
|
|
queueMessage: jest.fn(),
|
|
|
|
} as unknown as KafkaProducerWrapper,
|
2022-12-05 13:30:52 +01:00
|
|
|
USE_KAFKA_FOR_SCHEDULED_TASKS: true,
|
2022-10-18 16:44:41 +02:00
|
|
|
}
|
|
|
|
|
2022-12-05 13:30:52 +01:00
|
|
|
await runScheduledTasks(mockHubWithPluginSchedule, mockPiscina as any, 'runEveryMinute', {
|
|
|
|
job: { run_at: new Date() },
|
|
|
|
} as any)
|
2022-10-18 16:44:41 +02:00
|
|
|
|
2023-05-04 15:02:44 +02:00
|
|
|
expect(mockHubWithPluginSchedule.kafkaProducer.queueMessage).toHaveBeenNthCalledWith(1, {
|
2024-03-25 14:01:15 +01:00
|
|
|
kafkaMessage: {
|
|
|
|
topic: KAFKA_SCHEDULED_TASKS,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: '1',
|
|
|
|
value: JSON.stringify({
|
|
|
|
taskType: 'runEveryMinute',
|
|
|
|
pluginConfigId: 1,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
waitForAck: true,
|
2022-12-05 13:30:52 +01:00
|
|
|
})
|
2023-05-04 15:02:44 +02:00
|
|
|
expect(mockHubWithPluginSchedule.kafkaProducer.queueMessage).toHaveBeenNthCalledWith(2, {
|
2024-03-25 14:01:15 +01:00
|
|
|
kafkaMessage: {
|
|
|
|
topic: KAFKA_SCHEDULED_TASKS,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: '2',
|
|
|
|
value: JSON.stringify({
|
|
|
|
taskType: 'runEveryMinute',
|
|
|
|
pluginConfigId: 2,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
waitForAck: true,
|
2022-12-05 13:30:52 +01:00
|
|
|
})
|
2023-05-04 15:02:44 +02:00
|
|
|
expect(mockHubWithPluginSchedule.kafkaProducer.queueMessage).toHaveBeenNthCalledWith(3, {
|
2024-03-25 14:01:15 +01:00
|
|
|
kafkaMessage: {
|
|
|
|
topic: KAFKA_SCHEDULED_TASKS,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: '3',
|
|
|
|
value: JSON.stringify({
|
|
|
|
taskType: 'runEveryMinute',
|
|
|
|
pluginConfigId: 3,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
waitForAck: true,
|
2022-12-05 13:30:52 +01:00
|
|
|
})
|
2022-10-18 16:44:41 +02:00
|
|
|
|
2022-12-05 13:30:52 +01:00
|
|
|
await runScheduledTasks(mockHubWithPluginSchedule, mockPiscina as any, 'runEveryHour', {
|
|
|
|
job: { run_at: new Date() },
|
|
|
|
} as any)
|
2023-05-04 15:02:44 +02:00
|
|
|
expect(mockHubWithPluginSchedule.kafkaProducer.queueMessage).toHaveBeenNthCalledWith(4, {
|
2024-03-25 14:01:15 +01:00
|
|
|
kafkaMessage: {
|
|
|
|
topic: KAFKA_SCHEDULED_TASKS,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: '4',
|
|
|
|
value: JSON.stringify({
|
|
|
|
taskType: 'runEveryHour',
|
|
|
|
pluginConfigId: 4,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
waitForAck: true,
|
2022-12-05 13:30:52 +01:00
|
|
|
})
|
2023-05-04 15:02:44 +02:00
|
|
|
expect(mockHubWithPluginSchedule.kafkaProducer.queueMessage).toHaveBeenNthCalledWith(5, {
|
2024-03-25 14:01:15 +01:00
|
|
|
kafkaMessage: {
|
|
|
|
topic: KAFKA_SCHEDULED_TASKS,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: '5',
|
|
|
|
value: JSON.stringify({
|
|
|
|
taskType: 'runEveryHour',
|
|
|
|
pluginConfigId: 5,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
waitForAck: true,
|
2022-12-05 13:30:52 +01:00
|
|
|
})
|
2023-05-04 15:02:44 +02:00
|
|
|
expect(mockHubWithPluginSchedule.kafkaProducer.queueMessage).toHaveBeenNthCalledWith(6, {
|
2024-03-25 14:01:15 +01:00
|
|
|
kafkaMessage: {
|
|
|
|
topic: KAFKA_SCHEDULED_TASKS,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: '6',
|
|
|
|
value: JSON.stringify({
|
|
|
|
taskType: 'runEveryHour',
|
|
|
|
pluginConfigId: 6,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
waitForAck: true,
|
2022-12-05 13:30:52 +01:00
|
|
|
})
|
2022-10-18 16:44:41 +02:00
|
|
|
|
2022-12-05 13:30:52 +01:00
|
|
|
await runScheduledTasks(mockHubWithPluginSchedule, mockPiscina as any, 'runEveryDay', {
|
|
|
|
job: { run_at: new Date() },
|
|
|
|
} as any)
|
2023-05-04 15:02:44 +02:00
|
|
|
expect(mockHubWithPluginSchedule.kafkaProducer.queueMessage).toHaveBeenNthCalledWith(7, {
|
2024-03-25 14:01:15 +01:00
|
|
|
kafkaMessage: {
|
|
|
|
topic: KAFKA_SCHEDULED_TASKS,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: '7',
|
|
|
|
value: JSON.stringify({
|
|
|
|
taskType: 'runEveryDay',
|
|
|
|
pluginConfigId: 7,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
waitForAck: true,
|
2022-12-05 13:30:52 +01:00
|
|
|
})
|
2023-05-04 15:02:44 +02:00
|
|
|
expect(mockHubWithPluginSchedule.kafkaProducer.queueMessage).toHaveBeenNthCalledWith(8, {
|
2024-03-25 14:01:15 +01:00
|
|
|
kafkaMessage: {
|
|
|
|
topic: KAFKA_SCHEDULED_TASKS,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: '8',
|
|
|
|
value: JSON.stringify({
|
|
|
|
taskType: 'runEveryDay',
|
|
|
|
pluginConfigId: 8,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
waitForAck: true,
|
2022-12-05 13:30:52 +01:00
|
|
|
})
|
2023-05-04 15:02:44 +02:00
|
|
|
expect(mockHubWithPluginSchedule.kafkaProducer.queueMessage).toHaveBeenNthCalledWith(9, {
|
2024-03-25 14:01:15 +01:00
|
|
|
kafkaMessage: {
|
|
|
|
topic: KAFKA_SCHEDULED_TASKS,
|
|
|
|
messages: [
|
|
|
|
{
|
|
|
|
key: '9',
|
|
|
|
value: JSON.stringify({
|
|
|
|
taskType: 'runEveryDay',
|
|
|
|
pluginConfigId: 9,
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
waitForAck: true,
|
2022-12-05 13:30:52 +01:00
|
|
|
})
|
2022-10-18 16:44:41 +02:00
|
|
|
})
|
|
|
|
})
|