Add feature recompute jobs
This commit is contained in:
1 parent
1a7a1cca68
commit
8668234065
8 files changed
+1176
-36
No files matched your search
@@ -1,7 +1,10 @@
|
||||
import type { AnalysisJob } from '../global/database/schemas';
|
||||
import type { ConnectionService } from '../global/database/connection.service';
|
||||
import type { UserAnalysisJobData } from './analysis.types';
|
||||
import { USER_ANALYSIS_JOB_NAME } from './analysis.constants';
|
||||
import {
|
||||
FEATURE_RECOMPUTE_JOB_NAME,
|
||||
USER_ANALYSIS_JOB_NAME,
|
||||
} from './analysis.constants';
|
||||
import { AnalysisQueueService } from './analysis-queue.service';
|
||||
|
||||
function createJobData(
|
||||
@@ -78,6 +81,59 @@ describe('AnalysisQueueService', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('creates a feature recompute job document and enqueues a BullMQ job', async () => {
|
||||
const add = jest.fn().mockResolvedValue({ id: 'job-from-bull' });
|
||||
const create = jest.fn().mockResolvedValue({});
|
||||
const updateOne = jest.fn(() => ({
|
||||
exec: jest.fn().mockResolvedValue({}),
|
||||
}));
|
||||
const analysisJobModel = {
|
||||
create,
|
||||
updateOne,
|
||||
};
|
||||
const connectionService = {
|
||||
getModel: jest.fn().mockResolvedValue(analysisJobModel),
|
||||
} as unknown as ConnectionService;
|
||||
const service = new AnalysisQueueService(
|
||||
{ add } as never,
|
||||
connectionService,
|
||||
() => 'recompute-job-1',
|
||||
);
|
||||
|
||||
const result = await service.enqueueFeatureRecompute({
|
||||
platformRoute: 'kr',
|
||||
queueId: 420,
|
||||
batchSize: 25,
|
||||
puuid: 'target-puuid',
|
||||
});
|
||||
|
||||
expect(result).toEqual({ jobId: 'recompute-job-1', status: 'queued' });
|
||||
expect(create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
jobId: 'recompute-job-1',
|
||||
jobType: FEATURE_RECOMPUTE_JOB_NAME,
|
||||
platformRoute: 'kr',
|
||||
queueId: 420,
|
||||
requestedMatchCount: 0,
|
||||
puuid: 'target-puuid',
|
||||
status: 'queued',
|
||||
}),
|
||||
);
|
||||
expect(add).toHaveBeenCalledWith(
|
||||
FEATURE_RECOMPUTE_JOB_NAME,
|
||||
{
|
||||
platformRoute: 'kr',
|
||||
queueId: 420,
|
||||
batchSize: 25,
|
||||
puuid: 'target-puuid',
|
||||
},
|
||||
expect.objectContaining({
|
||||
attempts: 1,
|
||||
jobId: 'recompute-job-1',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('returns an existing active job instead of enqueueing a duplicate', async () => {
|
||||
const add = jest.fn();
|
||||
const findOneChain = createFindOneChain({
|
||||
@@ -143,6 +199,44 @@ describe('AnalysisQueueService', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('marks a pre-created feature recompute job failed when BullMQ enqueue fails', async () => {
|
||||
const add = jest.fn().mockRejectedValue(new Error('redis unavailable'));
|
||||
const create = jest.fn().mockResolvedValue({});
|
||||
const updateOne = jest.fn(() => ({
|
||||
exec: jest.fn().mockResolvedValue({}),
|
||||
}));
|
||||
const analysisJobModel = {
|
||||
create,
|
||||
updateOne,
|
||||
};
|
||||
const connectionService = {
|
||||
getModel: jest.fn().mockResolvedValue(analysisJobModel),
|
||||
} as unknown as ConnectionService;
|
||||
const service = new AnalysisQueueService(
|
||||
{ add } as never,
|
||||
connectionService,
|
||||
() => 'recompute-job-1',
|
||||
);
|
||||
|
||||
await expect(
|
||||
service.enqueueFeatureRecompute({
|
||||
platformRoute: 'kr',
|
||||
queueId: 420,
|
||||
batchSize: 25,
|
||||
}),
|
||||
).rejects.toThrow('redis unavailable');
|
||||
expect(updateOne).toHaveBeenCalledWith(
|
||||
{ jobId: 'recompute-job-1' },
|
||||
{
|
||||
$set: {
|
||||
status: 'failed',
|
||||
stage: 'enqueue',
|
||||
error: 'Analysis could not be queued.',
|
||||
},
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it('serializes identical in-process requests before creating a duplicate job', async () => {
|
||||
const add = jest.fn().mockResolvedValue({ id: 'analysis-job-1' });
|
||||
let activeJob: Pick<AnalysisJob, 'jobId' | 'status'> | null = null;
|
||||
|
||||
Reference in new issue
Block a user