Skip to content

Commit 14afa1b

Browse files
committed
Merge branch 'feat/delete-post-read' into development
2 parents bf5e6e6 + 1ca0622 commit 14afa1b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+221
-140
lines changed

apps/cron/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ ARG TURBO_TOKEN
2727
ENV TURBO_TOKEN=${TURBO_TOKEN}
2828

2929
ARG TURBO_REMOTE_CACHE_SIGNATURE_KEY
30-
ENV TURBO_REMOTE_CACHE_SIGNATURE_KEY=${TURBO_REMOTE_CACHE_SIGNATURE_KEY}
30+
ENV TURBO_REMOTE_CACHE_SIGNATURE_KEY=$TURBO_REMOTE_CACHE_SIGNATURE_KEY
3131

3232
ENV APP_NAME="cron"
3333
ENV APP_DIR="apps/${APP_NAME}"

apps/cron/src/app.mts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import Fastify from 'fastify'
22
import fastifyCron from 'fastify-cron'
3-
import routes from '@routes/index.js'
4-
import closePlugin from '@plugins/encapsulated/closePlugin.js'
3+
import routes from '@routes/index.mjs'
4+
import closePlugin from '@plugins/encapsulated/closePlugin.mjs'
55
import checkApiKeyPlugin from '@plugins/global/checkApiKeyPlugin.mjs'
66
import corsPlugin from '@plugins/global/corsPlugin.mjs'
77
import errorHandlerPlugin from '@plugins/global/errorHandlerPlugin.mjs'
8+
import cronPlugin from '@plugins/global/cronPlugin.mjs'
89

910
const app = Fastify({
1011
logger: true,
12+
pluginTimeout: 60000,
1113
})
1214

1315
app.register(checkApiKeyPlugin)
1416
app.register(corsPlugin)
1517
app.register(errorHandlerPlugin)
1618
app.register(closePlugin)
17-
app.register(fastifyCron.default)
19+
await app.register(fastifyCron.default)
20+
21+
await app.register(cronPlugin)
1822

1923
app.register(routes)
2024

apps/cron/src/common/errors/BadRequestErrors.ts renamed to apps/cron/src/common/errors/BadRequestErrors.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpError } from './HttpError.js'
1+
import { HttpError } from './HttpError.mjs'
22

33
export class BadRequestError extends HttpError {
44
constructor(message = 'BAD_REQUEST') {

apps/cron/src/common/errors/ConfilctError.ts renamed to apps/cron/src/common/errors/ConfilctError.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpError } from './HttpError.js'
1+
import { HttpError } from './HttpError.mjs'
22

33
export class ConfilctError extends HttpError {
44
constructor(message = 'CONFILCT') {

apps/cron/src/common/errors/ForbiddenError.ts renamed to apps/cron/src/common/errors/ForbiddenError.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpError } from './HttpError.js'
1+
import { HttpError } from './HttpError.mjs'
22

33
export class ForbiddenError extends HttpError {
44
constructor(message = 'FORBIDDEN') {

apps/cron/src/common/errors/NotfoundError.ts renamed to apps/cron/src/common/errors/NotfoundError.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpError } from './HttpError.js'
1+
import { HttpError } from './HttpError.mjs'
22

33
export class NotFoundError extends HttpError {
44
constructor(message = 'NOT_FOUND') {

apps/cron/src/common/errors/UnauthorizedError.ts renamed to apps/cron/src/common/errors/UnauthorizedError.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpError } from './HttpError.js'
1+
import { HttpError } from './HttpError.mjs'
22

33
export class UnauthorizedError extends HttpError {
44
constructor(message = 'Unauthorized') {

apps/cron/src/common/errors/index.mts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export { BadRequestError } from './BadRequestErrors.mjs'
2+
export { ConfilctError } from './ConfilctError.mjs'
3+
export { ForbiddenError } from './ForbiddenError.mjs'
4+
export { HttpError, isHttpError } from './HttpError.mjs'
5+
export { NotFoundError } from './NotfoundError.mjs'
6+
export { UnauthorizedError } from './UnauthorizedError.mjs'

apps/cron/src/common/errors/index.ts

Lines changed: 0 additions & 6 deletions
This file was deleted.

apps/cron/src/common/plugins/global/checkApiKeyPlugin.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ENV } from '@env'
2-
import { UnauthorizedError } from '@errors/UnauthorizedError.js'
2+
import { UnauthorizedError } from '@errors/UnauthorizedError.mjs'
33
import { FastifyPluginCallback } from 'fastify'
44
import fp from 'fastify-plugin'
55

apps/cron/src/common/plugins/global/corsPlugin.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { FastifyPluginCallback } from 'fastify'
22
import { ENV } from '@env'
33
import cors from '@fastify/cors'
4-
import { ForbiddenError } from '@errors/ForbiddenError.js'
4+
import { ForbiddenError } from '@errors/ForbiddenError.mjs'
55
import fp from 'fastify-plugin'
66

77
const corsPlugin: FastifyPluginCallback = (fastify, opts, done) => {

apps/cron/src/common/plugins/global/cronPlugin.mts

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import fp from 'fastify-plugin'
12
import { StatsDaily, StatsWeekly, StatsMonthly } from '@jobs/stats/index.js'
2-
import { GenerateFeedJob } from '@jobs/GenerateFeedJob.js'
3-
import { CalculatePostScoreJob } from '@jobs/CalculatePostScoreJob.js'
4-
import { GenerateTrendingWritersJob } from '@jobs/GenerateTrendingWritersJob.js'
5-
import { DeleteFeedJob } from '@jobs/DeleteFeedJob.js'
6-
import { FastifyPluginCallback } from 'fastify'
3+
import { GenerateFeedJob } from '@jobs/GenerateFeedJob.mjs'
4+
import { CalculatePostScoreJob } from '@jobs/CalculatePostScoreJob.mjs'
5+
import { GenerateTrendingWritersJob } from '@jobs/GenerateTrendingWritersJob.mjs'
6+
import { DeleteFeedJob } from '@jobs/DeleteFeedJob.mjs'
7+
import { FastifyPluginAsync } from 'fastify'
78
import { container } from 'tsyringe'
89
import { ENV } from '@env'
9-
import { CheckSpamPostJob } from '@jobs/CheckSpamPostJob.js'
10-
import fp from 'fastify-plugin'
10+
import { CheckSpamPostJob } from '@jobs/CheckSpamPostJob.mjs'
11+
import { DeletePostReadJob } from '@jobs/DeletePostReadJob.mjs'
1112

12-
const cronPlugin: FastifyPluginCallback = async (fastfiy, opts, done) => {
13+
const cronPlugin: FastifyPluginAsync = async (fastfiy) => {
1314
const calculatePostScoreJob = container.resolve(CalculatePostScoreJob)
1415
const generateFeedJob = container.resolve(GenerateFeedJob)
1516
const generateTrendingWritersJob = container.resolve(GenerateTrendingWritersJob)
@@ -18,6 +19,7 @@ const cronPlugin: FastifyPluginCallback = async (fastfiy, opts, done) => {
1819
const statsWeeklyJob = container.resolve(StatsWeekly)
1920
const statsMonthlyJob = container.resolve(StatsMonthly)
2021
const checkSpamPostJob = container.resolve(CheckSpamPostJob)
22+
const deleteReadPostJob = container.resolve(DeletePostReadJob)
2123

2224
// 덜 실행하면서, 실행되는 순서로 정렬
2325
// crontime은 UTC 기준으로 작성되기 때문에 KST에서 9시간을 빼줘야함
@@ -54,6 +56,11 @@ const cronPlugin: FastifyPluginCallback = async (fastfiy, opts, done) => {
5456
cronTime: '*/2 * * * *', // every 2 minutes
5557
jobService: checkSpamPostJob,
5658
},
59+
{
60+
name: 'delete post read in every 2 minutes',
61+
cronTime: '*/2 * * * *', // every 2 minutes
62+
jobService: deleteReadPostJob,
63+
},
5764
// Stats Start
5865
{
5966
name: 'providing a count of new users and posts from the past 1 day',
@@ -82,33 +89,30 @@ const cronPlugin: FastifyPluginCallback = async (fastfiy, opts, done) => {
8289
})
8390
}
8491

85-
try {
86-
// for Test
92+
const initializeJobs = async () => {
93+
console.log('Initializing cron jobs...')
8794
if (ENV.appEnv !== 'production') {
88-
const immediateRunJobs = jobDescription.filter((job) => !!job.isImmediateExecute)
89-
await Promise.all(immediateRunJobs.map(createTick))
90-
const crons = immediateRunJobs.map(createJob)
91-
await Promise.all(
92-
crons.map((cron) => {
93-
console.log(`${cron.name} is registered`)
94-
cron.start()
95-
}),
96-
)
95+
const immediateRunJobs = jobDescription.filter((job) => job.isImmediateExecute)
96+
const crons = await Promise.all(immediateRunJobs.map(createJob))
97+
for (const cron of crons) {
98+
cron.start()
99+
console.log(`${cron.name} is registered`)
100+
}
97101
}
98102

99-
if (ENV.dockerEnv === 'production') {
100-
const crons = jobDescription.map(createJob)
101-
await Promise.all(
102-
crons.map((cron) => {
103-
console.log(`${cron.name} is registered`)
104-
cron.start()
105-
}),
106-
)
103+
if (['production', 'stage'].includes(ENV.dockerEnv)) {
104+
const crons = await Promise.all(jobDescription.map(createJob))
105+
for (const cron of crons) {
106+
cron.start()
107+
console.log(`${cron.name} is registered`)
108+
}
107109
}
110+
}
111+
112+
try {
113+
await initializeJobs()
108114
} catch (error) {
109115
console.error('Error initializing cron jobs:', error)
110-
} finally {
111-
done()
112116
}
113117
}
114118

@@ -148,6 +152,7 @@ type JobService =
148152
| StatsWeekly
149153
| StatsMonthly
150154
| CheckSpamPostJob
155+
| DeletePostReadJob
151156

152157
type BaseJobService = {
153158
name: string
@@ -164,4 +169,10 @@ type NotNeedParamJobService = Omit<BaseJobService, 'param'> & {
164169
jobService: Exclude<JobService, CalculatePostScoreJob>
165170
}
166171

167-
export default fp(cronPlugin)
172+
export default fp(cronPlugin, {
173+
name: 'cronPlugin',
174+
fastify: '4.x',
175+
decorators: {
176+
fastify: ['cron'],
177+
},
178+
})

apps/cron/src/common/plugins/global/errorHandlerPlugin.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ENV } from '@env'
2-
import { isHttpError } from '@errors/HttpError.js'
2+
import { isHttpError } from '@errors/HttpError.mjs'
33
import { FastifyPluginCallback } from 'fastify'
44
import fp from 'fastify-plugin'
55

apps/cron/src/env.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import dotenv from 'dotenv'
22
import { container } from 'tsyringe'
3-
import { UtilsService } from '@lib/utils/UtilsService.js'
3+
import { UtilsService } from '@lib/utils/UtilsService.mjs'
44
import { existsSync } from 'fs'
55
import { z } from 'zod'
66

apps/cron/src/jobs/CalculatePostScoreJob.ts renamed to apps/cron/src/jobs/CalculatePostScoreJob.mts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { DbService } from '@lib/db/DbService.js'
2-
import { PostService } from '@services/PostService/index.js'
1+
import { DbService } from '@lib/db/DbService.mjs'
2+
import { PostService } from '@services/PostService/index.mjs'
33
import { injectable, singleton } from 'tsyringe'
4-
import { Job, JobProgress } from '@jobs/JobProgress.js'
4+
import { Job, JobProgress } from '@jobs/JobProgress.mjs'
55

66
@singleton()
77
@injectable()
88
export class CalculatePostScoreJob extends JobProgress implements Job {
9-
constructor(
10-
private readonly postService: PostService,
11-
private readonly db: DbService,
12-
) {
9+
constructor(private readonly postService: PostService, private readonly db: DbService) {
1310
super()
1411
}
1512
public async runner(score: number) {

apps/cron/src/jobs/CheckSpamPostJob.ts renamed to apps/cron/src/jobs/CheckSpamPostJob.mts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { injectable, singleton } from 'tsyringe'
2-
import { Job, JobProgress } from './JobProgress.js'
3-
import { PostService } from '@services/PostService/index.js'
4-
import { CheckPostSpamArgs, RedisService } from '@lib/redis/RedisService.js'
5-
import { DiscordService } from '@lib/discord/DiscordService.js'
2+
import { Job, JobProgress } from './JobProgress.mjs'
3+
import { PostService } from '@services/PostService/index.mjs'
4+
import { CheckPostSpamArgs, RedisService } from '@lib/redis/RedisService.mjs'
5+
import { DiscordService } from '@lib/discord/DiscordService.mjs'
66

77
@injectable()
88
@singleton()

apps/cron/src/jobs/DeleteFeedJob.ts renamed to apps/cron/src/jobs/DeleteFeedJob.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { injectable, singleton } from 'tsyringe'
2-
import { Job, JobProgress } from './JobProgress.js'
3-
import { FeedService } from '@services/FeedService/index.js'
2+
import { Job, JobProgress } from './JobProgress.mjs'
3+
import { FeedService } from '@services/FeedService/index.mjs'
44

55
@singleton()
66
@injectable()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { injectable, singleton } from 'tsyringe'
2+
import { Job, JobProgress } from './JobProgress.mjs'
3+
import { PostReadService } from '@services/PostReadService/index.mjs'
4+
5+
@injectable()
6+
@singleton()
7+
export class DeletePostReadJob extends JobProgress implements Job {
8+
constructor(private readonly postReadService: PostReadService) {
9+
super()
10+
}
11+
public async runner() {
12+
console.log('Delete post read job start...')
13+
console.time('delete post read')
14+
15+
const count = 1000
16+
const postReads = await this.postReadService.findMany(count)
17+
18+
const postReadIds = postReads.map((postRead) => postRead.id)
19+
20+
for (const postReadId of postReadIds) {
21+
await this.postReadService.deleteById(postReadId)
22+
}
23+
24+
console.log(`Deleted PostRead count: ${count}`)
25+
console.timeEnd('delete post read')
26+
}
27+
}

apps/cron/src/jobs/GenerateFeedJob.ts renamed to apps/cron/src/jobs/GenerateFeedJob.mts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
import { Job, JobProgress } from '@jobs/JobProgress.js'
2-
import { RedisService } from '@lib/redis/RedisService.js'
3-
import { FeedService } from '@services/FeedService/index.js'
1+
import { Job, JobProgress } from '@jobs/JobProgress.mjs'
2+
import { RedisService } from '@lib/redis/RedisService.mjs'
3+
import { FeedService } from '@services/FeedService/index.mjs'
44
import { injectable, singleton } from 'tsyringe'
55

66
@singleton()
77
@injectable()
88
export class GenerateFeedJob extends JobProgress implements Job {
9-
constructor(
10-
private readonly redis: RedisService,
11-
private readonly feedService: FeedService,
12-
) {
9+
constructor(private readonly redis: RedisService, private readonly feedService: FeedService) {
1310
super()
1411
}
1512
public async runner() {

apps/cron/src/jobs/GenerateTrendingWritersJob.ts renamed to apps/cron/src/jobs/GenerateTrendingWritersJob.mts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import { injectable, singleton } from 'tsyringe'
2-
import { Job, JobProgress } from './JobProgress.js'
3-
import { RedisService } from '@lib/redis/RedisService.js'
4-
import { WriterService } from '@services/WriterService/index.js'
2+
import { Job, JobProgress } from './JobProgress.mjs'
3+
import { RedisService } from '@lib/redis/RedisService.mjs'
4+
import { WriterService } from '@services/WriterService/index.mjs'
55

66
@singleton()
77
@injectable()
88
export class GenerateTrendingWritersJob extends JobProgress implements Job {
9-
constructor(
10-
private readonly redis: RedisService,
11-
private readonly writerService: WriterService,
12-
) {
9+
constructor(private readonly redis: RedisService, private readonly writerService: WriterService) {
1310
super()
1411
}
1512
public async runner(): Promise<void> {

apps/cron/src/jobs/stats/StatsDaily.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { injectable, singleton } from 'tsyringe'
2-
import { Job, JobProgress } from '../JobProgress.js'
3-
import { StatsService } from '@services/StatsService/index.js'
2+
import { Job, JobProgress } from '../JobProgress.mjs'
3+
import { StatsService } from '@services/StatsService/index.mjs'
44

55
@singleton()
66
@injectable()

apps/cron/src/jobs/stats/StatsMonthly.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { injectable, singleton } from 'tsyringe'
2-
import { Job, JobProgress } from '../JobProgress.js'
3-
import { StatsService } from '@services/StatsService/index.js'
2+
import { Job, JobProgress } from '../JobProgress.mjs'
3+
import { StatsService } from '@services/StatsService/index.mjs'
44

55
@singleton()
66
@injectable()

apps/cron/src/jobs/stats/StatsWeekly.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { injectable, singleton } from 'tsyringe'
2-
import { Job, JobProgress } from '../JobProgress.js'
3-
import { StatsService } from '@services/StatsService/index.js'
2+
import { Job, JobProgress } from '../JobProgress.mjs'
3+
import { StatsService } from '@services/StatsService/index.mjs'
44

55
@singleton()
66
@injectable()

apps/cron/src/lib/discord/Discord.test.ts renamed to apps/cron/src/lib/discord/Discord.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { container } from 'tsyringe'
2-
import { DiscordService } from './DiscordService.js'
2+
import { DiscordService } from './DiscordService.mjs'
33

44
describe('DiscordService', () => {
55
const service = container.resolve(DiscordService)

apps/cron/src/lib/redis/Redis.test.ts renamed to apps/cron/src/lib/redis/Redis.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { container } from 'tsyringe'
2-
import { RedisService } from './RedisService.js'
2+
import { RedisService } from './RedisService.mjs'
33

44
describe('RedisService', () => {
55
const service = container.resolve(RedisService)

apps/cron/src/lib/utils/Utils.test.ts renamed to apps/cron/src/lib/utils/Utils.test.mts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { container } from 'tsyringe'
2-
import { UtilsService } from './UtilsService.js'
2+
import { UtilsService } from './UtilsService.mjs'
33

44
describe('UtilsService', () => {
55
const service = container.resolve(UtilsService)

0 commit comments

Comments
 (0)