Skip to content

Commit 5220e78

Browse files
committed
feat: Add ScorePostJob to cron plugin
1 parent e8ccd47 commit 5220e78

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import { GenerateFeedJob } from '@jobs/GenerateFeedJob.mjs'
44
import { CalculatePostScoreJob } from '@jobs/CalculatePostScoreJob.mjs'
55
import { GenerateTrendingWritersJob } from '@jobs/GenerateTrendingWritersJob.mjs'
66
import { DeleteFeedJob } from '@jobs/DeleteFeedJob.mjs'
7-
import { FastifyPluginAsync } from 'fastify'
7+
import type { FastifyPluginAsync } from 'fastify'
88
import { container } from 'tsyringe'
99
import { ENV } from '@env'
1010
import { CheckPostSpamJob } from '@jobs/CheckPostSpamJob.mjs'
1111
import { DeletePostReadJob } from '@jobs/DeletePostReadJob.mjs'
12+
import { ScorePostJob } from '@jobs/ScorePostJob.mjs'
1213

1314
const cronPlugin: FastifyPluginAsync = async (fastfiy) => {
1415
const calculatePostScoreJob = container.resolve(CalculatePostScoreJob)
@@ -20,6 +21,7 @@ const cronPlugin: FastifyPluginAsync = async (fastfiy) => {
2021
const statsMonthlyJob = container.resolve(StatsMonthly)
2122
const checkPostSpamJob = container.resolve(CheckPostSpamJob)
2223
const deleteReadPostJob = container.resolve(DeletePostReadJob)
24+
const scorePostJob = container.resolve(ScorePostJob)
2325

2426
// 덜 실행하면서, 실행되는 순서로 정렬
2527
// crontime은 UTC 기준으로 작성되기 때문에 KST에서 9시간을 빼줘야함
@@ -51,6 +53,11 @@ const cronPlugin: FastifyPluginAsync = async (fastfiy) => {
5153
jobService: calculatePostScoreJob,
5254
param: 0.5,
5355
},
56+
{
57+
name: 'score post in every 1 minutes',
58+
cronTime: '*/1 * * * *', // every 1 minutes
59+
jobService: scorePostJob,
60+
},
5461
{
5562
name: 'check post spam in every 2 minutes',
5663
cronTime: '*/2 * * * *', // every 2 minutes
@@ -153,6 +160,7 @@ type JobService =
153160
| StatsMonthly
154161
| CheckPostSpamJob
155162
| DeletePostReadJob
163+
| ScorePostJob
156164

157165
type BaseJobService = {
158166
name: string

apps/cron/src/jobs/ScorePostJob.mts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { injectable, singleton } from 'tsyringe'
2+
import { Job, JobProgress } from './JobProgress.mjs'
3+
import { RedisService } from '@lib/redis/RedisService.mjs'
4+
import { PostService } from '@services/PostService/index.mjs'
5+
import { ScorePostQueueData } from '@packages/database/velog-redis'
6+
import { DiscordService } from '@lib/discord/DiscordService.mjs'
7+
8+
@singleton()
9+
@injectable()
10+
export class ScorePostJob extends JobProgress implements Job {
11+
constructor(
12+
private readonly postService: PostService,
13+
private readonly redis: RedisService,
14+
private readonly discord: DiscordService,
15+
) {
16+
super()
17+
}
18+
public async runner(): Promise<void> {
19+
console.log('ScorePostJob start...')
20+
console.time('ScorePostJob')
21+
22+
const scorePostQueueName = this.redis.queueName.scorePost
23+
let handledQueueCount = 0
24+
25+
while (true) {
26+
const item = await this.redis.lindex(scorePostQueueName, 0)
27+
if (!item) break
28+
const data: ScorePostQueueData = JSON.parse(item)
29+
try {
30+
await this.postService.scoreCalculator(data.post_id)
31+
} catch (error) {
32+
console.log('ScorePostJob error', error)
33+
const message = { message: 'ScorePostJob error', payload: item, error: error }
34+
this.discord.sendMessage('error', JSON.stringify(message))
35+
} finally {
36+
await this.redis.lpop(scorePostQueueName)
37+
handledQueueCount++
38+
}
39+
}
40+
41+
console.log(`handled Queue count: ${handledQueueCount}`)
42+
console.timeEnd('ScorePostJob')
43+
}
44+
}

apps/cron/src/routes/posts/v1/index.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ const v1: FastifyPluginCallback = (fastify, opts, done) => {
1919
},
2020
)
2121

22+
// dev 환경에서만 사용 가능
2223
fastify.patch('/score', async (_, reply) => {
2324
const processedPostsCount = await postController.calculateRecentPostScore()
2425
reply.status(HttpStatus.OK).send({ processedPostsCount })
2526
})
2627

28+
// dev 환경에서만 사용 가능
2729
fastify.post('/test/spam-filter', async (_, reply) => {
2830
await postController.spamFilterTestRunner()
2931
reply.status(HttpStatus.OK).send(HttpStatusMessage.Ok)

0 commit comments

Comments
 (0)