Skip to content

Commit

Permalink
Add util to load worker conf based on env
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Dec 30, 2024
1 parent ba62597 commit c93c0e3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
12 changes: 4 additions & 8 deletions api/src/modules/import-data/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ import * as XLSX from 'xlsx';
import { WorkBook } from 'xlsx';
import { difference } from 'lodash';
import { Worker } from 'worker_threads';
import * as path from 'path';
import { getWorkerConfig } from 'modules/import-data/utils';

@Injectable()
export class FileService<T extends Record<string, any[]>> {
private readonly logger: Logger = new Logger(FileService.name);

async transformToJsonInWorker(filePath: string, sheetMap: any): Promise<T> {
await this.isFilePresentInFs(filePath);
const workerFile: string =
process.env.NODE_ENV === 'test'
? path.resolve(__dirname, './workers/xlsx.worker.ts') // En tests usa el archivo TypeScript
: path.resolve(__dirname, './workers/xlsx.worker.js');
const { workerPath, execArgv } = getWorkerConfig('xlsx.worker');
this.logger.log(`Starting worker to parse ${filePath}...`);
try {
const parsedSheet: any = await new Promise((resolve, reject) => {
const worker: Worker = new Worker(workerFile, {
const worker: Worker = new Worker(workerPath, {
workerData: { filePath, sheetMap },
execArgv:
process.env.NODE_ENV === 'test' ? ['-r', 'ts-node/register'] : [],
execArgv,
});

worker.on('message', (data: T) => {
Expand Down
21 changes: 21 additions & 0 deletions api/src/modules/import-data/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as path from 'path';

interface WorkerConfig {
workerPath: string;
execArgv: string[];
}

// Since node workers do not support ts out of the box, we need to specify the JS file for the app, but TS file for the tests.

export const getWorkerConfig = (fileName: string): WorkerConfig => {
const isTestEnvironment: boolean = process.env.NODE_ENV === 'test';
const workerPath: WorkerConfig['workerPath'] = isTestEnvironment
? path.resolve(__dirname, `./workers/${fileName}.ts`)
: path.resolve(__dirname, `./workers/${fileName}.js`);

const execArgv: WorkerConfig['execArgv'] = isTestEnvironment
? ['-r', 'ts-node/register']
: [];

return { workerPath, execArgv };
};

0 comments on commit c93c0e3

Please sign in to comment.