Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gameserver deletes taking extremely long #1826

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"id": "59f2cdbf-ef7d-495f-be9e-531d4fd6ee29",
"createdAt": "2023-12-24T15:22:57.559Z",
"updatedAt": "2023-12-24T15:22:57.559Z",
"deletedAt": null,
"name": "Test gameserver",
"enabled": true,
"connectionInfo": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"id": "2600aac2-d5b8-455d-bf6a-12a242f5ee0a",
"createdAt": "2023-12-24T15:22:57.034Z",
"updatedAt": "2023-12-24T15:22:57.034Z",
"deletedAt": null,
"name": "Test gameserver",
"enabled": true,
"connectionInfo": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"id": "4218a6e4-001a-4175-a346-f9b8a920c3f4",
"createdAt": "2023-12-24T15:22:57.938Z",
"updatedAt": "2023-12-24T15:22:58.000Z",
"deletedAt": null,
"name": "Test gameserver 2",
"enabled": true,
"connectionInfo": {
Expand Down

Large diffs are not rendered by default.

Loading