diff --git a/.gitignore b/.gitignore index c14381a..2bf7d33 100644 --- a/.gitignore +++ b/.gitignore @@ -160,6 +160,7 @@ fabric.properties # Log file *.log +*.log.gz # BlueJ files *.ctxt diff --git a/src/main/kotlin/backend/itracker/alarm/service/AlarmService.kt b/src/main/kotlin/backend/itracker/alarm/service/AlarmService.kt index f98d8d4..74dd7a8 100644 --- a/src/main/kotlin/backend/itracker/alarm/service/AlarmService.kt +++ b/src/main/kotlin/backend/itracker/alarm/service/AlarmService.kt @@ -2,12 +2,13 @@ package backend.itracker.alarm.service import backend.itracker.alarm.infra.slack.Color import backend.itracker.alarm.infra.slack.SlackClient +import backend.itracker.alarm.service.event.FailedNotificationEvent import backend.itracker.crawl.service.CrawlResultService import backend.itracker.schedule.service.event.CrawlEndEvent import org.springframework.context.event.EventListener import org.springframework.stereotype.Component -private const val CRAWL_RESULT_TITLE = "[ 크롤링 결과 ]" +private const val CRAWL_RESULT_TITLE = "*[ 크롤링 결과 ]*" @Component class AlarmService ( @@ -31,4 +32,16 @@ class AlarmService ( false -> slackClient.sendNotification(CRAWL_RESULT_TITLE, Color.GREEN, messages) } } + + @EventListener(FailedNotificationEvent::class) + fun alarmNotificationFailEvent(event: FailedNotificationEvent) { + slackClient.sendNotification( + """ + |*[ 알림 전송 실패 ]* + |*실패 사유* : ${event.getCause()} + """.trimMargin(), + Color.RED, + event.getMessages() + ) + } } diff --git a/src/main/kotlin/backend/itracker/alarm/service/event/FailedNotificationEvent.kt b/src/main/kotlin/backend/itracker/alarm/service/event/FailedNotificationEvent.kt new file mode 100644 index 0000000..868b33b --- /dev/null +++ b/src/main/kotlin/backend/itracker/alarm/service/event/FailedNotificationEvent.kt @@ -0,0 +1,8 @@ +package backend.itracker.alarm.service.event + +interface FailedNotificationEvent { + + fun getMessages(): List + + fun getCause(): String? +} diff --git a/src/main/kotlin/backend/itracker/schedule/infra/notification/event/MessageSendFailEvent.kt b/src/main/kotlin/backend/itracker/schedule/infra/notification/event/MessageSendFailEvent.kt new file mode 100644 index 0000000..b08e641 --- /dev/null +++ b/src/main/kotlin/backend/itracker/schedule/infra/notification/event/MessageSendFailEvent.kt @@ -0,0 +1,65 @@ +package backend.itracker.schedule.infra.notification.event + +import backend.itracker.alarm.service.event.FailedNotificationEvent +import net.nurigo.sdk.message.model.FailedMessage + +data class MessageSendFailEvent( + val failedMessages: List, + val causeMessage: String?, +) : FailedNotificationEvent { + + companion object { + fun of(failedMessageList: List, causeMessage: String?): MessageSendFailEvent { + return MessageSendFailEvent( + failedMessages = failedMessageList.map { FailedMessageInfo.from(it) }, + causeMessage = causeMessage + ) + } + } + + override fun getMessages(): List { + return failedMessages.map { + """ + *to*: ${it.to} + *from*: ${it.from} + *type*: ${it.type} + *country*: ${it.country} + *messageId*: ${it.messageId} + *statusCode*: ${it.statusCode} + *statusMessage*: ${it.statusMessage} + *accountId*: ${it.accountId} + """.trimIndent() + } + } + + override fun getCause(): String? { + return causeMessage + } +} + +data class FailedMessageInfo( + var to: String? = null, + var from: String? = null, + var type: String? = null, + var country: String? = null, + var messageId: String? = null, + var statusCode: String? = null, + var statusMessage: String? = null, + var accountId: String? = null +) { + + companion object { + fun from(message: FailedMessage): FailedMessageInfo { + return FailedMessageInfo( + to = message.to, + from = message.from, + type = message.type, + country = message.country, + messageId = message.messageId, + statusCode = message.statusCode, + statusMessage = message.statusMessage, + accountId = message.accountId, + ) + } + } +} diff --git a/src/main/kotlin/backend/itracker/schedule/infra/notification/solapi/NotificationClient.kt b/src/main/kotlin/backend/itracker/schedule/infra/notification/solapi/NotificationClient.kt index 7401bf8..8e8c2c7 100644 --- a/src/main/kotlin/backend/itracker/schedule/infra/notification/solapi/NotificationClient.kt +++ b/src/main/kotlin/backend/itracker/schedule/infra/notification/solapi/NotificationClient.kt @@ -1,5 +1,6 @@ package backend.itracker.schedule.infra.notification.solapi +import backend.itracker.schedule.infra.notification.event.MessageSendFailEvent import backend.itracker.schedule.infra.notification.solapi.config.NurigoKakaoChannelConfig import backend.itracker.schedule.service.notification.NotificationSender import backend.itracker.schedule.service.notification.dto.PriceChangeNotificationInfo @@ -8,6 +9,7 @@ import net.nurigo.sdk.message.exception.NurigoMessageNotReceivedException import net.nurigo.sdk.message.model.KakaoOption import net.nurigo.sdk.message.model.Message import net.nurigo.sdk.message.service.DefaultMessageService +import org.springframework.context.ApplicationEventPublisher import org.springframework.context.annotation.Profile import org.springframework.stereotype.Component import java.time.LocalDate @@ -22,6 +24,7 @@ val logger = KotlinLogging.logger {} class NotificationClient( private val messageService: DefaultMessageService, private val nurigoKakaoChannelConfig: NurigoKakaoChannelConfig, + private val eventPublisher: ApplicationEventPublisher, ) : NotificationSender { override fun sendPriceChangeNotification( @@ -53,10 +56,16 @@ class NotificationClient( """ [ 메세지 전송 실패 ] 실패 리스트 : ${exception.failedMessageList} - 실패 메세지${exception.message} - """.trimIndent() + 실패 메세지 : ${exception.message} + """.trimIndent() } + + eventPublisher.publishEvent( + MessageSendFailEvent.of( + exception.failedMessageList, + exception.message + ) + ) } } - }