Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | /**
* BackgroundJobManager
*
* This service manages the queueing of background AI jobs for entity/document processing.
*/
import { v4 as uuidv4 } from "uuid";
import logger from "../utils/logger.js";
import initializeDbClient from "../db/client.js";
export class BackgroundJobManager {
/**
* Creates a new BackgroundJobManager instance
*/
constructor() {
this.dbClient = null;
this.initialized = false;
}
/**
* Initialize the BackgroundJobManager
* @returns {Promise<void>}
*/
async initialize() {
try {
logger.info("Initializing BackgroundJobManager");
this.dbClient = initializeDbClient();
this.initialized = true;
logger.info("BackgroundJobManager initialized successfully");
} catch (error) {
logger.error(
`Error initializing BackgroundJobManager: ${error.message}`,
{ error }
);
throw error;
}
}
/**
* Enqueue a new background AI job
* @param {Object} jobDetails - Details of the job to enqueue
* @param {string} jobDetails.task_type - Type of task to perform (e.g., 'enrich_entity_summary_keywords')
* @param {string} jobDetails.target_entity_id - ID of the entity/document to process
* @param {string} jobDetails.target_entity_type - Type of the target entity (e.g., 'code_entity', 'project_document')
* @param {Object} [jobDetails.payload] - Optional additional data needed for the job
* @returns {Promise<{job_id: string, success: boolean}>} - Result of the enqueue operation
*/
async enqueueJob(jobDetails) {
if (!this.initialized) {
await this.initialize();
}
try {
logger.info(
`Enqueueing job of type '${jobDetails.task_type}' for entity ID '${jobDetails.target_entity_id}'`
);
// Generate a unique job_id (UUID)
const job_id = uuidv4();
// Prepare the job record
const jobData = {
job_id,
target_entity_id: jobDetails.target_entity_id,
target_entity_type: jobDetails.target_entity_type,
task_type: jobDetails.task_type,
status: "pending",
payload: jobDetails.payload ? JSON.stringify(jobDetails.payload) : null,
// max_attempts will use the default value from the schema
};
// In a real implementation, we would call the DB function to insert the job
// However, as Task 046 (which creates this function) is not completed yet,
// we'll simply log the job insertion for now
logger.info(
`Would insert job with ID '${job_id}' into background_ai_jobs table`,
{ jobData }
);
// Once Task 046 is completed, uncomment the following code:
/*
// Import the DB query function (created in Task 046)
import { addBackgroundAiJob } from '../db/queries.js';
// Insert the job record into the database
await addBackgroundAiJob(this.dbClient, jobData);
*/
logger.info(`Successfully enqueued job with ID '${job_id}'`);
return { job_id, success: true };
} catch (error) {
logger.error(`Error enqueueing job: ${error.message}`, {
error,
jobDetails,
});
return { success: false, error: error.message };
}
}
}
// Export a singleton instance
export default new BackgroundJobManager();
|