Skip to content

Commit

Permalink
feat: offloading upload to worker
Browse files Browse the repository at this point in the history
  • Loading branch information
cskiwi committed Dec 12, 2024
1 parent 8a6a0a9 commit 75969bb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
25 changes: 16 additions & 9 deletions libs/backend/ranking/src/controllers/upload.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import moment from 'moment';
import * as XLSX from 'xlsx';
import { MembersRolePerGroupData, UpdateRankingService } from '../services';
import { UploadGuard, MultipartFile, File } from '@badman/backend-utils';
import workerThreadFilePath from '../worker/config';
import { Worker } from 'worker_threads';

@Controller('ranking/upload')
export class UploadRankingController {
Expand Down Expand Up @@ -55,22 +57,27 @@ export class UploadRankingController {

res.send({ message: true });

this._updateRankingService
.processFileUpload(mappedData, {
const worker = new Worker(workerThreadFilePath, {
workerData: {
updateCompStatus,
updateRanking,
updatePossible,
updateClubs,
rankingDate: rankingDate.toDate(),
clubMembershipStartDate: clubMembershipStartDate.toDate(),
clubMembershipEndDate: clubMembershipEndDate.toDate(),
rankingDate,
clubMembershipStartDate,
clubMembershipEndDate,
removeAllRanking,
rankingSystemId,
createNewPlayers,
})
.then(() => {
this._logger.log('Ranking processed');
});
mappedData,
},
});

worker.on('message', () => {
this._logger.verbose('Done');
});
worker.on('error', (e) => this._logger.error('on error', e));
worker.on('exit', (code) => this._logger.log('on exit', code));
});
}

Expand Down
4 changes: 4 additions & 0 deletions libs/backend/ranking/src/worker/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// it will import the compiled js file from dist directory
const workerThreadFilePath = __dirname + '/process-ranking.js';

export default workerThreadFilePath;
41 changes: 41 additions & 0 deletions libs/backend/ranking/src/worker/process-ranking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { NestFactory } from '@nestjs/core';
import { isMainThread, workerData } from 'worker_threads';
import { RankingModule } from '../ranking.module';
import { UpdateRankingService } from '../services';

async function run() {
if (isMainThread) {
throw new Error('This script should be run as a worker thread');
}
const app = await NestFactory.createApplicationContext(RankingModule);
const updateRankingService = app.get(UpdateRankingService);

const {
updateCompStatus,
updateRanking,
updatePossible,
updateClubs,
rankingDate,
clubMembershipStartDate,
clubMembershipEndDate,
removeAllRanking,
rankingSystemId,
createNewPlayers,
mappedData,
} = workerData;

await updateRankingService.processFileUpload(mappedData, {
updateCompStatus,
updateRanking,
updatePossible,
updateClubs,
rankingDate,
clubMembershipStartDate,
clubMembershipEndDate,
removeAllRanking,
rankingSystemId,
createNewPlayers,
});
}

run();

0 comments on commit 75969bb

Please sign in to comment.