Skip to content

Commit

Permalink
Merge pull request #780 from gettakaro/better-error-handling-playerSYnc
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele authored Dec 23, 2023
2 parents 14d3a8c + 52d157a commit 766d036
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/app-api/src/db/playerOnGameserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
32 changes: 26 additions & 6 deletions packages/app-api/src/workers/playerSyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IGameServerQueueData> {
constructor() {
Expand Down Expand Up @@ -59,7 +59,18 @@ export async function processJob(job: Job<IGameServerQueueData>) {
})
);

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;
Expand All @@ -76,6 +87,9 @@ export async function processJob(job: Job<IGameServerQueueData>) {

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}`);
Expand All @@ -94,12 +108,18 @@ export async function processJob(job: Job<IGameServerQueueData>) {
})
);

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;
}
Expand Down

0 comments on commit 766d036

Please sign in to comment.