Skip to content

Commit ba70c75

Browse files
committed
fix: 스케줄러 bulk update 연산 수정
1 parent 7836c87 commit ba70c75

File tree

6 files changed

+57
-16
lines changed

6 files changed

+57
-16
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { IsString, IsJSON, IsOptional, IsNumber } from 'class-validator';
2+
import { ApiProperty } from '@nestjs/swagger';
3+
4+
export class UpdatePartialPageDto {
5+
@ApiProperty({
6+
example: 1,
7+
description: 'page PK',
8+
})
9+
@IsNumber()
10+
id: number;
11+
12+
@ApiProperty({
13+
example: '페이지 제목입니다.',
14+
description: '페이지 제목.',
15+
})
16+
@IsString()
17+
@IsOptional()
18+
title?: string;
19+
20+
@ApiProperty({
21+
example: "{'doc' : 'type'}",
22+
description: '페이지 내용 JSON 형태',
23+
})
24+
@IsJSON()
25+
@IsOptional()
26+
content?: JSON;
27+
28+
@ApiProperty({
29+
example: '📝',
30+
description: '페이지 이모지',
31+
required: false,
32+
})
33+
@IsString()
34+
@IsOptional()
35+
emoji?: string;
36+
}

apps/backend/src/page/page.repository.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
22
import { DataSource, Repository } from 'typeorm';
33
import { Page } from './page.entity';
44
import { InjectDataSource } from '@nestjs/typeorm';
5+
import { UpdatePartialPageDto } from './dtos/updatePartialPage.dto';
56

67
@Injectable()
78
export class PageRepository extends Repository<Page> {
@@ -20,12 +21,9 @@ export class PageRepository extends Repository<Page> {
2021
});
2122
}
2223

23-
async bulkUpdate(pages) {
24-
await this.createQueryBuilder()
25-
.insert()
26-
.into(Page, ['id', 'title', 'content', 'version'])
27-
.values(pages)
28-
.orUpdate(['title', 'content', 'version'], ['id'])
29-
.execute();
24+
async bulkUpdate(pages: UpdatePartialPageDto[]) {
25+
for await (const page of pages) {
26+
await this.update(page.id, page);
27+
}
3028
}
3129
}

apps/backend/src/page/page.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { PageRepository } from './page.repository';
55
import { Page } from './page.entity';
66
import { CreatePageDto } from './dtos/createPage.dto';
77
import { UpdatePageDto } from './dtos/updatePage.dto';
8+
import { UpdatePartialPageDto } from './dtos/updatePartialPage.dto';
89
import { PageNotFoundException } from '../exception/page.exception';
910
import { WorkspaceNotFoundException } from '../exception/workspace.exception';
1011

@@ -67,7 +68,6 @@ export class PageService {
6768
}
6869

6970
async updatePage(id: number, dto: UpdatePageDto): Promise<Page> {
70-
console.log(id, dto, 'asdfasdasd');
7171
// 갱신할 페이지를 조회한다.
7272
// 페이지를 조회한다.
7373
const page = await this.pageRepository.findOneBy({ id });
@@ -83,7 +83,7 @@ export class PageService {
8383
return await this.pageRepository.save(newPage);
8484
}
8585

86-
async updateBulkPage(pages: UpdatePageDto[]) {
86+
async updateBulkPage(pages: UpdatePartialPageDto[]) {
8787
await this.pageRepository.bulkUpdate(pages);
8888
}
8989

apps/backend/src/redis/redis.service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ export class RedisService {
2424

2525
async get(key: string) {
2626
const data = await this.redisClient.hgetall(key);
27-
console.log(data);
2827
return Object.fromEntries(
2928
Object.entries(data).map(([field, value]) => [field, value]),
3029
) as RedisPage;
3130
}
3231

3332
async set(key: string, value: object) {
34-
console.log('set', Object.entries(value));
3533
return await this.redisClient.hset(key, Object.entries(value));
3634
}
3735

apps/backend/src/tasks/tasks.service.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,19 @@ export class TasksService {
1414

1515
@Cron(CronExpression.EVERY_30_SECONDS)
1616
async handleCron() {
17-
console.log(await this.redisService.getAllKeys());
17+
this.logger.log('스케줄러 시작');
18+
// 시작 시간
19+
const startTime = performance.now();
20+
21+
// redis의 모든 값을 가져와서 database에 저장
1822
const keys = await this.redisService.getAllKeys();
1923
const pages = [];
20-
this.logger.log('스케줄러 시작');
2124
for await (const key of keys) {
2225
const { title, content } = await this.redisService.get(key);
2326
const jsonContent = JSON.parse(content);
27+
Object.assign({
28+
id: parseInt(key),
29+
});
2430
pages.push({
2531
id: parseInt(key),
2632
title,
@@ -33,7 +39,11 @@ export class TasksService {
3339
// });
3440
// this.logger.log('데이터베이스 갱신');
3541
}
36-
console.log(pages);
37-
this.pageService.updateBulkPage(pages);
42+
await this.pageService.updateBulkPage(pages);
43+
44+
// 끝 시간
45+
const endTime = performance.now();
46+
this.logger.log(`갱신 개수 : ${pages.length}개`);
47+
this.logger.log(`실행 시간 : ${(endTime - startTime) / 1000}초`);
3848
}
3949
}

apps/backend/src/yjs/yjs.service.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ export class YjsService
100100

101101
if (Object.keys(pageContent).length > 0) {
102102
this.transformText(pageContent);
103-
this.logger.error(pageContent);
104103
// this.logger.error(this.transformText(pageContent));
105104
this.initializePageContent(pageContent, editorDoc);
106105
}

0 commit comments

Comments
 (0)