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

[BLOOM-134] 건강상태 알림 기능 추가 & Batch 스레드 풀 제한 #135

Merged
merged 7 commits into from
Oct 7, 2024
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
1 change: 1 addition & 0 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ tasks.jar { enabled = true }
tasks.bootJar { enabled = true }

dependencies {
implementation(project(":batch"))
implementation(project(":common"))
implementation(project(":client"))
implementation(project(":domain:core"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class GlobalExceptionHandler {

@ExceptionHandler(MethodArgumentTypeMismatchException::class)
fun handleArgumentTypeMismatchException(exception: MethodArgumentTypeMismatchException): ResponseEntity<ErrorResponse> {
val errorType = ErrorType.ARUGMENT_TYPE_MISMATCH
val errorType = ErrorType.ARGUMENT_TYPE_MISMATCH

logException(errorType, exception, ExceptionSource.HTTP)

Expand Down
2 changes: 2 additions & 0 deletions api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ spring:
activate:
on-profile: local
import:
- batch-local.yml
- client.yml
- redis-local.yml
- core-local.yml
Expand All @@ -39,6 +40,7 @@ spring:
activate:
on-profile: prod
import:
- batch-prod.yml
- client.yml
- redis-prod.yml
- core-prod.yml
Expand Down
6 changes: 6 additions & 0 deletions api/src/test/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ spring:
dialect: org.hibernate.dialect.H2Dialect
defer-datasource-initialization: true

batch:
jdbc:
initialize-schema: always
job:
enabled: false

data:
redis:
host: localhost
Expand Down
2 changes: 1 addition & 1 deletion batch/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tasks.jar { enabled = true }
tasks.bootJar { enabled = true }
tasks.bootJar { enabled = false }

dependencies {
implementation(project(":common"))
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dnd11th.blooming.batch
package dnd11th.blooming.batch.config

import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dnd11th.blooming.batch
package dnd11th.blooming.batch.notification

import dnd11th.blooming.common.util.Logger.Companion.log
import org.springframework.batch.core.Job
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dnd11th.blooming.batch
package dnd11th.blooming.batch.notification

import dnd11th.blooming.client.fcm.PushNotification
import dnd11th.blooming.core.entity.myplant.UserPlantDto
Expand All @@ -20,18 +20,22 @@ import org.springframework.transaction.PlatformTransactionManager
@Configuration
class PlantNotificationJobConfig {
companion object {
const val CHUNK_SIZE: Int = 100
const val CHUNK_SIZE: Int = 1000
}

@Bean
@Primary
fun notificationJob(
jobRepository: JobRepository,
waterNotificationStep: Step,
fertilizerNotificationStep: Step,
healthCheckNotificationStep: Step,
): Job {
return JobBuilder("notificationJob", jobRepository)
.incrementer(RunIdIncrementer())
.start(waterNotificationStep)
.next(fertilizerNotificationStep)
.next(healthCheckNotificationStep)
.build()
}

Expand All @@ -51,4 +55,38 @@ class PlantNotificationJobConfig {
.writer(waterNotificationItemWriter)
.build()
}

@Bean
@JobScope
fun fertilizerNotificationStep(
jobRepository: JobRepository,
transactionManager: PlatformTransactionManager,
fertilizerNotificationItemReader: ItemReader<UserPlantDto>,
waterNotificationItemProcessor: ItemProcessor<UserPlantDto, PushNotification>,
waterNotificationItemWriter: ItemWriter<PushNotification>,
): Step {
return StepBuilder("fertilizerNotificationStep", jobRepository)
.chunk<UserPlantDto, PushNotification>(CHUNK_SIZE, transactionManager)
.reader(fertilizerNotificationItemReader)
.processor(waterNotificationItemProcessor)
.writer(waterNotificationItemWriter)
.build()
}

@Bean
@JobScope
fun healthCheckNotificationStep(
jobRepository: JobRepository,
transactionManager: PlatformTransactionManager,
healthCheckNotificationItemReader: ItemReader<UserPlantDto>,
waterNotificationItemProcessor: ItemProcessor<UserPlantDto, PushNotification>,
waterNotificationItemWriter: ItemWriter<PushNotification>,
): Step {
return StepBuilder("healthCheckNotificationStep", jobRepository)
.chunk<UserPlantDto, PushNotification>(CHUNK_SIZE, transactionManager)
.reader(healthCheckNotificationItemReader)
.processor(waterNotificationItemProcessor)
.writer(waterNotificationItemWriter)
.build()
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dnd11th.blooming.batch
package dnd11th.blooming.batch.notification

import dnd11th.blooming.client.fcm.PushNotification
import dnd11th.blooming.core.entity.myplant.UserPlantDto
Expand All @@ -13,7 +13,7 @@ class PlantNotificationProcessor {
@StepScope
fun waterNotificationItemProcessor(): ItemProcessor<UserPlantDto, PushNotification> {
return ItemProcessor { userPlantDto ->
PushNotification.create(userPlantDto.myPlantId, "token", userPlantDto.plantNickname)
PushNotification.create(userPlantDto.myPlantId, userPlantDto.deviceToken, userPlantDto.plantNickname)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dnd11th.blooming.batch.notification

import dnd11th.blooming.core.entity.myplant.UserPlantDto
import dnd11th.blooming.core.entity.user.AlarmTime
import dnd11th.blooming.core.repository.myplant.MyPlantRepository
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.item.support.ListItemReader
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.time.LocalTime

@Configuration
class PlantNotificationReader(
private val myPlantRepository: MyPlantRepository,
) {
@Bean
@StepScope
fun waterNotificationItemReader(): ListItemReader<UserPlantDto> {
val now: LocalTime = LocalTime.now()
// val alarmTime = AlarmTime.fromHour(now)
val alarmTime = AlarmTime.TIME_10_11

val userPlantByAlarmTime: List<UserPlantDto> =
myPlantRepository.findNeedWaterPlantsByAlarmTimeInBatch(alarmTime)
return ListItemReader(userPlantByAlarmTime)
}

@Bean
@StepScope
fun fertilizerNotificationItemReader(): ListItemReader<UserPlantDto> {
val now: LocalTime = LocalTime.now()
val alarmTime = AlarmTime.fromHour(now)
val userPlantByAlarmTime: List<UserPlantDto> =
myPlantRepository.findNeedFertilizerPlantsByAlarmTimeInBatch(alarmTime)
return ListItemReader(userPlantByAlarmTime)
}

@Bean
@StepScope
fun healthCheckNotificationItemReader(): ListItemReader<UserPlantDto> {
val now: LocalTime = LocalTime.now()
val alarmTime = AlarmTime.fromHour(now)
val userPlantByAlarmTime: List<UserPlantDto> =
myPlantRepository.findNeedHealthCheckPlantsByAlarmTimeInBatch(alarmTime)
return ListItemReader(userPlantByAlarmTime)
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
package dnd11th.blooming.batch
package dnd11th.blooming.batch.notification

import dnd11th.blooming.client.fcm.FcmService
import dnd11th.blooming.client.fcm.PushNotification
import dnd11th.blooming.common.util.Logger.Companion.log
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.springframework.batch.core.configuration.annotation.StepScope
import org.springframework.batch.item.ItemWriter
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.util.concurrent.Executors
import java.util.concurrent.atomic.AtomicInteger

@Configuration
class PlantNotificationWriter(
private val fcmService: FcmService,
) {
private var counter = AtomicInteger(0)
private val customDispatcher = Executors.newFixedThreadPool(3).asCoroutineDispatcher()

@Bean
@StepScope
fun waterNotificationItemWriter(): ItemWriter<PushNotification> {
return ItemWriter { pushNotifications ->
runBlocking {
pushNotifications.forEach { pushNotification ->
launch {
fcmService.send(pushNotification) // 비동기 처리
launch(customDispatcher) {
val currentCount = counter.incrementAndGet()
val threadName = Thread.currentThread().name
fcmService.mock(pushNotification)
log.info { "Thread: $threadName, Count: $currentCount" }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import dnd11th.blooming.core.entity.region.Region
import dnd11th.blooming.redis.entity.weather.WeatherCareMessage
import dnd11th.blooming.redis.entity.weather.WeatherCareMessageType
import dnd11th.blooming.redis.repository.weather.WeatherCareMessageRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.runBlocking
Expand All @@ -20,7 +20,9 @@ import org.springframework.util.StopWatch
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Collections
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicInteger

@Configuration
class WeatherCareMessageWriter(
Expand All @@ -41,6 +43,9 @@ class WeatherCareMessageWriter(
private const val HIGH_TEMPERATURE_THRESHOLD = 30
}

private var counter = AtomicInteger(0)
private val customDispatcher = Executors.newFixedThreadPool(3).asCoroutineDispatcher()

@Bean
@StepScope
fun weatherCareMessageItemWriter(): ItemWriter<Region> {
Expand All @@ -53,7 +58,9 @@ class WeatherCareMessageWriter(
runBlocking {
val jobs =
regions.map { region ->
async(Dispatchers.IO) {
async(customDispatcher) {
val currentCount = counter.incrementAndGet()
val threadName = Thread.currentThread().name
try {
val weatherItems: List<WeatherItem> =
weatherInfoClient.getWeatherInfo(
Expand All @@ -68,18 +75,18 @@ class WeatherCareMessageWriter(
} catch (e: Exception) {
log.error(e) { "날씨 데이터를 불러오는데 실패했습니다: ${region.id}" }
}
log.info { "Thread: $threadName, Count: $currentCount" }
}
}
jobs.awaitAll()
runBlockingStopWatch.stop()
log.info { runBlockingStopWatch.getTotalTime(TimeUnit.MILLISECONDS) }

val saveStopWatch = StopWatch()
saveStopWatch.start()
weatherCareMessageRepository.saveAll(weatherCareMessages)
saveStopWatch.stop()
log.info { saveStopWatch.getTotalTime(TimeUnit.MILLISECONDS) }
}
val saveStopWatch = StopWatch()
saveStopWatch.start()
weatherCareMessageRepository.saveAll(weatherCareMessages)
saveStopWatch.stop()
log.info { saveStopWatch.getTotalTime(TimeUnit.MILLISECONDS) }
}
}

Expand Down
Loading
Loading