Skip to content

Commit

Permalink
fix: gameserver deletes taking extremely long
Browse files Browse the repository at this point in the history
  • Loading branch information
niekcandaele committed Dec 7, 2024
1 parent 5ee1723 commit 09940cb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
11 changes: 7 additions & 4 deletions packages/app-api/src/db/gameserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class GameServerModel extends TakaroModel {

connectionInfo!: Record<string, unknown>;
reachable!: boolean;

deletedAt?: Date;
type!: GAME_SERVER_TYPE;

static get relationMappings() {
Expand Down Expand Up @@ -108,7 +108,10 @@ export class GameServerRepo extends ITakaroRepo<

async find(filters: ITakaroQuery<GameServerOutputDTO>) {
const { query } = await this.getModel();
const result = await new QueryBuilder<GameServerModel, GameServerOutputDTO>(filters).build(query);
const qry = new QueryBuilder<GameServerModel, GameServerOutputDTO>(filters).build(query);
qry.andWhere('deletedAt', null);
const result = await qry;

return {
total: result.total,
results: await Promise.all(result.results.map((item) => new GameServerOutputDTO(item))),
Expand All @@ -117,7 +120,7 @@ export class GameServerRepo extends ITakaroRepo<

async findOne(id: string, decryptConnectionInfo: boolean): Promise<GameServerOutputDTO> {
const { query } = await this.getModel();
const data = await query.findById(id);
const data = await query.findById(id).andWhere('deletedAt', null);

if (!data) {
throw new errors.NotFoundError(`Record with id ${id} not found`);
Expand Down Expand Up @@ -148,7 +151,7 @@ export class GameServerRepo extends ITakaroRepo<

async delete(id: string): Promise<boolean> {
const { query } = await this.getModel();
const data = await query.deleteById(id);
const data = await query.update({ deletedAt: new Date() }).where({ id });
return !!data;
}

Expand Down
7 changes: 7 additions & 0 deletions packages/app-api/src/service/GameServerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,13 @@ export class GameServerService extends TakaroService<
time: new Date().toISOString(),
});
await this.repo.delete(id);
await queueService.queues.system.queue.add(
{
domainId: this.domainId,
},
{},
'gameServerDelete',
);
return id;
}

Expand Down
11 changes: 11 additions & 0 deletions packages/app-api/src/workers/systemWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { DomainRepo } from '../db/domain.js';
import ms from 'ms';
import { EventService } from '../service/EventService.js';
import { VariablesService } from '../service/VariablesService.js';
import { GameServerService } from '../service/GameServerService.js';

export class SystemWorker extends TakaroWorker<IBaseJobData> {
constructor() {
Expand Down Expand Up @@ -35,9 +36,12 @@ export async function processJob(job: Job<IBaseJobData>) {
},
);
}
} else if (job.name === 'gameServerDelete') {
await deleteGameServers(job.data.domainId);
} else {
await cleanEvents(job.data.domainId);
await cleanExpiringVariables(job.data.domainId);
await deleteGameServers(job.data.domainId);
}
}

Expand All @@ -52,3 +56,10 @@ async function cleanExpiringVariables(domainId: string) {
const variableService = new VariablesService(domainId);
await variableService.cleanExpiringVariables();
}

async function deleteGameServers(domainId: string) {
const gameserverService = new GameServerService(domainId);
const repo = gameserverService.repo;
const { query } = await repo.getModel();
await query.whereNotNull('deletedAt').delete();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.schema.alterTable('gameservers', (table) => {
table.timestamp('deletedAt').nullable().defaultTo(null);
});
}

export async function down(knex: Knex): Promise<void> {
await knex.schema.alterTable('gameservers', (table) => {
table.dropColumn('deletedAt');
});
}
6 changes: 3 additions & 3 deletions packages/lib-queues/src/TakaroQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export class TakaroQueue<T extends Record<string, unknown>> {
return hash;
}

add(data: T, extra: JobsOptions = {}) {
add(data: T, extra: JobsOptions = {}, name = this.name) {
const jobId = extra.jobId ?? this.getJobId(data);
const isRepeatable = extra.repeat ? true : false;
if (isRepeatable) {
return this.bullQueue.add(this.name, data, extra);
return this.bullQueue.add(name, data, extra);
} else {
return this.bullQueue.add(this.name, data, { jobId, ...extra });
return this.bullQueue.add(name, data, { jobId, ...extra });
}
}

Expand Down

0 comments on commit 09940cb

Please sign in to comment.