mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
revert bulk insert
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { setTimeout } from 'timers/promises';
|
|
||||||
import { IsNull, LessThan, Repository } from 'typeorm';
|
import { IsNull, LessThan, Repository } from 'typeorm';
|
||||||
|
|
||||||
import { ClientEntity } from './client.entity';
|
import { ClientEntity } from './client.entity';
|
||||||
@@ -11,7 +9,7 @@ import { ClientEntity } from './client.entity';
|
|||||||
@Injectable()
|
@Injectable()
|
||||||
export class ClientService {
|
export class ClientService {
|
||||||
|
|
||||||
private bulkInsertClients: ClientEntity[] = [];
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(ClientEntity)
|
@InjectRepository(ClientEntity)
|
||||||
@@ -20,17 +18,6 @@ export class ClientService {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cron(CronExpression.EVERY_SECOND)
|
|
||||||
private async bulkInsert() {
|
|
||||||
|
|
||||||
if (this.bulkInsertClients.length < 1) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const clientsToInsert = [...this.bulkInsertClients];;
|
|
||||||
this.bulkInsertClients = [];
|
|
||||||
await this.clientRepository.insert(clientsToInsert);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async killDeadClients() {
|
public async killDeadClients() {
|
||||||
var tenMinutes = new Date(new Date().getTime() - (60 * 60 * 1000));
|
var tenMinutes = new Date(new Date().getTime() - (60 * 60 * 1000));
|
||||||
@@ -53,13 +40,14 @@ export class ClientService {
|
|||||||
|
|
||||||
|
|
||||||
public async insert(partialClient: Partial<ClientEntity>): Promise<ClientEntity> {
|
public async insert(partialClient: Partial<ClientEntity>): Promise<ClientEntity> {
|
||||||
|
const insertResult = await this.clientRepository.insert(partialClient);
|
||||||
|
|
||||||
const client = this.clientRepository.create(partialClient);
|
const client = {
|
||||||
this.bulkInsertClients.push(client);
|
...partialClient,
|
||||||
// wait for the bulk insert to go though
|
...insertResult.generatedMaps[0]
|
||||||
// while we await node is free to service other requests
|
};
|
||||||
await setTimeout(2000);
|
|
||||||
return client;
|
return client as ClientEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async delete(sessionId: string) {
|
public async delete(sessionId: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user