Skip to content

Commit

Permalink
approach to load worker in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Dec 30, 2024
1 parent 9d269a0 commit 50a0682
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions api/src/modules/import-data/file.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@ import * as XLSX from 'xlsx';
import { WorkBook } from 'xlsx';
import { difference } from 'lodash';
import { Worker } from 'worker_threads';
import * as path from 'path';

@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');
this.logger.log(`Starting worker to parse ${filePath}...`);
try {
const parsedSheet: any = await new Promise((resolve, reject) => {
const worker: Worker = new Worker(
__dirname + '/workers/xlsx.worker.js',
{
workerData: { filePath, sheetMap },
},
);
const worker: Worker = new Worker(workerFile, {
workerData: { filePath, sheetMap },
execArgv:
process.env.NODE_ENV === 'test' ? ['-r', 'ts-node/register'] : [],
});

worker.on('message', (data: T) => {
this.logger.warn(`Worker finished processing ${filePath}`);
Expand Down

0 comments on commit 50a0682

Please sign in to comment.