Skip to content

Commit

Permalink
using singleton for OcrManager and InformationExtraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Joao-vi committed Oct 30, 2024
1 parent 81bdcf4 commit 48c2527
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
4 changes: 1 addition & 3 deletions app/api/files/ocrRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Application, Request, Response, NextFunction } from 'express';
import { storage } from 'api/files';
import needsAuthorization from 'api/auth/authMiddleware';
import { isOcrEnabled, getOcrStatus, OcrManager } from 'api/services/ocr/OcrManager';
import { isOcrEnabled, getOcrStatus, ocrManager } from 'api/services/ocr/OcrManager';
import { files } from './files';
import { validation, createError } from '../utils';

Expand Down Expand Up @@ -57,11 +57,9 @@ const ocrRoutes = (app: Application) => {
needsAuthorization(['admin', 'editor']),
validation.validateRequest(ocrRequestDecriptor),
async (req, res) => {
const ocrManager = new OcrManager();
const file = await fileFromRequest(req);

await ocrManager.addToQueue(file);
await ocrManager.stop();

res.sendStatus(200);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { InformationExtraction } from './InformationExtraction';

let informationExtraction: InformationExtraction;

export const getSingletonInformationExtraction = () => {
if (!informationExtraction) {
informationExtraction = new InformationExtraction();
}

return informationExtraction;
};
4 changes: 3 additions & 1 deletion app/api/services/ocr/OcrManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,4 +227,6 @@ class OcrManager {
}
}

export { OcrManager, isEnabled as isOcrEnabled, getStatus as getOcrStatus };
const ocrManager = new OcrManager();

export { ocrManager, OcrManager, isEnabled as isOcrEnabled, getStatus as getOcrStatus };
19 changes: 7 additions & 12 deletions app/api/suggestions/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,28 @@ import { Application, Request, Response } from 'express';
import { ObjectId } from 'mongodb';

import { Suggestions } from 'api/suggestions/suggestions';
import { InformationExtraction } from 'api/services/informationextraction/InformationExtraction';
import { validateAndCoerceRequest } from 'api/utils/validateRequest';
import { needsAuthorization } from 'api/auth';
import { parseQuery } from 'api/utils/parseQueryMiddleware';
import { ObjectIdSchema } from 'shared/types/commonTypes';
import { SuggestionsQueryFilterSchema } from 'shared/types/suggestionSchema';
import { objectIdSchema } from 'shared/types/commonSchemas';
import { getSingletonInformationExtraction } from 'api/services/informationextraction/SingletonInformationExtraction';
import {
IXAggregationQuery,
IXSuggestionAggregation,
IXSuggestionsQuery,
} from 'shared/types/suggestionType';
import { serviceMiddleware } from './serviceMiddleware';

const informationExtraction = getSingletonInformationExtraction();

async function processTrainFunction(
callback: (extractorId: ObjectIdSchema) => Promise<{ message: string; status: string }>,
req: Request,
res: Response
) {
const IX = new InformationExtraction();
if (!IX) {
if (!informationExtraction) {
res.status(500).json({
error: 'Information Extraction service is not available',
});
Expand Down Expand Up @@ -134,9 +135,7 @@ export const suggestionsRoutes = (app: Application) => {
needsAuthorization(['admin', 'editor']),
extractorIdRequestValidation('body'),
async (req, res, _next) => {
const IX = new InformationExtraction();
await processTrainFunction(IX.stopModel, req, res);
await IX.stop();
await processTrainFunction(informationExtraction.stopModel, req, res);
}
);

Expand All @@ -146,9 +145,7 @@ export const suggestionsRoutes = (app: Application) => {
needsAuthorization(['admin', 'editor']),
extractorIdRequestValidation('body'),
async (req, res, _next) => {
const IX = new InformationExtraction();
await processTrainFunction(IX.trainModel, req, res);
await IX.stop();
await processTrainFunction(informationExtraction.trainModel, req, res);
}
);

Expand All @@ -158,9 +155,7 @@ export const suggestionsRoutes = (app: Application) => {
needsAuthorization(['admin', 'editor']),
extractorIdRequestValidation('body'),
async (req, res, _next) => {
const IX = new InformationExtraction();
await processTrainFunction(IX.status, req, res);
await IX.stop();
await processTrainFunction(informationExtraction.status, req, res);
}
);

Expand Down
8 changes: 4 additions & 4 deletions app/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { DB } from 'api/odm';
import { config } from 'api/config';
import { tenants } from 'api/tenants';
import { permissionsContext } from 'api/permissions/permissionsContext';
import { OcrManager } from 'api/services/ocr/OcrManager';
import { ocrManager } from 'api/services/ocr/OcrManager';
import { PDFSegmentation } from 'api/services/pdfsegmentation/PDFSegmentation';
import { DistributedLoop } from 'api/services/tasksmanager/DistributedLoop';
import { TwitterIntegration } from 'api/services/twitterintegration/TwitterIntegration';
import { preserveSync } from 'api/services/preserve/preserveSync';
import { tocService } from 'api/toc_generation/tocService';
import { syncWorker } from 'api/sync/syncWorker';
import { InformationExtraction } from 'api/services/informationextraction/InformationExtraction';
import { getSingletonInformationExtraction } from 'api/services/informationextraction/SingletonInformationExtraction';
import { setupWorkerSockets } from 'api/socketio/setupSockets';
import { ConvertToPdfWorker } from 'api/services/convertToPDF/ConvertToPdfWorker';
import { ATServiceListener } from 'api/externalIntegrations.v2/automaticTranslation/adapters/driving/ATServiceListener';
Expand Down Expand Up @@ -45,9 +45,9 @@ DB.connect(config.DBHOST, dbAuth)
permissionsContext.setCommandContext();

const servicesList = [
new OcrManager(),
ocrManager,
new ATServiceListener(),
new InformationExtraction(),
getSingletonInformationExtraction(),
new ConvertToPdfWorker(),
] as any[];

Expand Down

0 comments on commit 48c2527

Please sign in to comment.