From 52d157a40ae2d4c094b8e38b105aa185155edb0b Mon Sep 17 00:00:00 2001 From: Niek Candaele Date: Sat, 23 Dec 2023 19:47:22 +0100 Subject: [PATCH] chore: some better error handling for playerSync worker --- packages/app-api/src/db/playerOnGameserver.ts | 4 ++- .../app-api/src/workers/playerSyncWorker.ts | 32 +++++++++++++++---- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/packages/app-api/src/db/playerOnGameserver.ts b/packages/app-api/src/db/playerOnGameserver.ts index 929ec8bcd1..e8b3e9772a 100644 --- a/packages/app-api/src/db/playerOnGameserver.ts +++ b/packages/app-api/src/db/playerOnGameserver.ts @@ -347,7 +347,9 @@ export class PlayerOnGameServerRepo extends ITakaroRepo< try { await query.delete().where({ playerId }).transacting(trx); - await query2.insert(toInsert).transacting(trx); + if (toInsert.length) { + await query2.insert(toInsert).transacting(trx); + } // If everything is ok, commit the transaction await trx.commit(); diff --git a/packages/app-api/src/workers/playerSyncWorker.ts b/packages/app-api/src/workers/playerSyncWorker.ts index fa62100ce7..444ceb65d5 100644 --- a/packages/app-api/src/workers/playerSyncWorker.ts +++ b/packages/app-api/src/workers/playerSyncWorker.ts @@ -8,7 +8,7 @@ import { ctx } from '@takaro/util'; import { PlayerService } from '../service/PlayerService.js'; import { PlayerOnGameServerService, PlayerOnGameServerUpdateDTO } from '../service/PlayerOnGameserverService.js'; -const log = logger('worker:inventory'); +const log = logger('worker:playerSync'); export class PlayerSyncWorker extends TakaroWorker { constructor() { @@ -59,7 +59,18 @@ export async function processJob(job: Job) { }) ); - await Promise.allSettled(promises); + const res = await Promise.allSettled(promises); + + for (const r of res) { + if (r.status === 'rejected') { + log.error(r.reason); + await job.log(r.reason); + } + } + + if (res.some((r) => r.status === 'rejected')) { + throw new Error('Some promises failed'); + } } return; @@ -76,6 +87,9 @@ export async function processJob(job: Job) { const promises = []; + promises.push(playerOnGameServerService.setOnlinePlayers(gameServerId, onlinePlayers)); + promises.push(gameServerService.syncInventories(gameServerId)); + promises.push( ...onlinePlayers.map(async (player) => { log.debug(`Syncing player ${player.gameId} on game server ${gameServerId}`); @@ -94,12 +108,18 @@ export async function processJob(job: Job) { }) ); - promises.push(playerOnGameServerService.setOnlinePlayers(gameServerId, onlinePlayers)); + const res = await Promise.allSettled(promises); - await Promise.all(promises); + for (const r of res) { + if (r.status === 'rejected') { + log.error(r.reason); + await job.log(r.reason); + } + } - // Processing for a specific game server - await gameServerService.syncInventories(gameServerId); + if (res.some((r) => r.status === 'rejected')) { + throw new Error('Some promises failed'); + } return; }