Skip to content

Commit

Permalink
[#140] refactor: SlackService DI로 변경, Job Success 시 슬랙 알림 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JuHyun419 committed Sep 10, 2022
1 parent 586d3dd commit 0cc9c38
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/main/kotlin/com/yapp/web2/batch/job/JobCompletionListener.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yapp.web2.batch.job

import com.yapp.web2.infra.slack.SlackServiceImpl
import com.yapp.web2.infra.slack.SlackService
import org.slf4j.LoggerFactory
import org.springframework.batch.core.BatchStatus
import org.springframework.batch.core.JobExecution
Expand All @@ -9,20 +9,27 @@ import org.springframework.stereotype.Component
import java.util.stream.Collectors

@Component
class JobCompletionListener : JobExecutionListenerSupport() {
class JobCompletionListener(
private val slackApi: SlackService
) : JobExecutionListenerSupport() {

private val log = LoggerFactory.getLogger(javaClass)
private val slackApi: SlackServiceImpl = SlackServiceImpl()

override fun afterJob(jobExecution: JobExecution) {
val jobStatus = jobExecution.status
log.info("Job {} - {}", jobStatus, jobExecution.jobInstance.jobName)

if (jobStatus == BatchStatus.COMPLETED) {
slackApi.sendSlackAlarm(
"*`${jobExecution.jobInstance.jobName}`* - executed"
)
}

if (jobStatus != BatchStatus.COMPLETED) {
val errorMessage = String.format(
"*`Batch Error`* - %s", getErrorMessage(jobExecution).replace("\"", "")
)
slackApi.sendMessage(errorMessage)
slackApi.sendSlackAlarm(errorMessage)
}
}

Expand Down

0 comments on commit 0cc9c38

Please sign in to comment.