From 3f1539913a028c82f48557fedbec9baab4039590 Mon Sep 17 00:00:00 2001 From: yang Date: Tue, 29 Oct 2024 04:51:07 +0900 Subject: [PATCH] =?UTF-8?q?RAC-451=20fix=20:=20=EC=A0=95=EC=82=B0=20?= =?UTF-8?q?=EC=9E=90=EB=8F=99=20=EC=83=9D=EC=84=B1=EC=8B=9C=20=EC=84=B1?= =?UTF-8?q?=EA=B3=B5=20=EC=97=AC=EB=B6=80=20=ED=99=95=EC=9D=B8=20=ED=9B=84?= =?UTF-8?q?=20=EC=9E=AC=EC=8B=9C=EB=8F=84=20=ED=95=98=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../batch/scheduler/JobSchedulerConfig.java | 31 ++++++++++++++++++- .../domain/repository/SalaryRepository.java | 1 + .../domain/service/SalaryGetService.java | 6 ++++ .../global/slack/SlackErrorMessage.java | 25 +++++++++++++++ .../postgraduate/global/slack/SlackUtils.java | 7 +++++ .../resources/templates/adminPayment.html | 2 +- 6 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/postgraduate/batch/scheduler/JobSchedulerConfig.java b/src/main/java/com/postgraduate/batch/scheduler/JobSchedulerConfig.java index 8d1bb716b..1b812b860 100644 --- a/src/main/java/com/postgraduate/batch/scheduler/JobSchedulerConfig.java +++ b/src/main/java/com/postgraduate/batch/scheduler/JobSchedulerConfig.java @@ -1,5 +1,8 @@ package com.postgraduate.batch.scheduler; +import com.postgraduate.domain.member.senior.domain.service.SeniorGetService; +import com.postgraduate.domain.salary.domain.service.SalaryGetService; +import com.postgraduate.global.slack.SlackErrorMessage; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.batch.core.Job; @@ -20,6 +23,8 @@ @Slf4j @RequiredArgsConstructor public class JobSchedulerConfig { + private static final int MAX_RETRIES = 5; + private final JobLauncher jobLauncher; @Qualifier("cancelJob") private final Job cancelJob; @@ -30,6 +35,10 @@ public class JobSchedulerConfig { @Qualifier("salaryJobWithAdmin") private final Job salaryJobWithAdmin; + private final SeniorGetService seniorGetService; + private final SalaryGetService salaryGetService; + private final SlackErrorMessage slackErrorMessage; + @Scheduled(cron = "0 59 23 * * *", zone = "Asia/Seoul") public void launchCancelJob() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException { JobParameters jobParameters = new JobParametersBuilder() @@ -52,12 +61,32 @@ public void launchSalaryJob() throws JobInstanceAlreadyCompleteException, JobExe .addLocalDateTime("date", LocalDateTime.now()) .toJobParameters(); jobLauncher.run(salaryJob, jobParameters); + checkSalaryJobSuccess(jobParameters); } public void launchSalaryJobWithAdmin() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException { JobParameters jobParameters = new JobParametersBuilder() .addLocalDateTime("date", LocalDateTime.now()) .toJobParameters(); - jobLauncher.run(salaryJobWithAdmin, jobParameters); + checkSalaryJobSuccess(jobParameters); + } + + private void checkSalaryJobSuccess(JobParameters jobParameters) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException { + int retries = 0; + boolean success = false; + int seniorSize = seniorGetService.allSeniorId() + .size(); + while (retries < MAX_RETRIES){ + int salarySize = salaryGetService.findAllNext() + .size(); + if (salarySize == seniorSize) { + success = true; + break; + } + jobLauncher.run(salaryJobWithAdmin, jobParameters); + } + if (!success) { + slackErrorMessage.sendSlackSalaryError(); + } } } diff --git a/src/main/java/com/postgraduate/domain/salary/domain/repository/SalaryRepository.java b/src/main/java/com/postgraduate/domain/salary/domain/repository/SalaryRepository.java index 3f1a476f8..3247442b5 100644 --- a/src/main/java/com/postgraduate/domain/salary/domain/repository/SalaryRepository.java +++ b/src/main/java/com/postgraduate/domain/salary/domain/repository/SalaryRepository.java @@ -11,4 +11,5 @@ public interface SalaryRepository extends JpaRepository, SalaryDslRepository { Optional findBySeniorAndSalaryDateAndSenior_User_IsDeleteIsFalse(Senior senior, LocalDate salaryDate); List findAllBySenior(Senior senior); + List findAllBySalaryDate(LocalDate salaryDate); } diff --git a/src/main/java/com/postgraduate/domain/salary/domain/service/SalaryGetService.java b/src/main/java/com/postgraduate/domain/salary/domain/service/SalaryGetService.java index 7e2c9de05..03afdff57 100644 --- a/src/main/java/com/postgraduate/domain/salary/domain/service/SalaryGetService.java +++ b/src/main/java/com/postgraduate/domain/salary/domain/service/SalaryGetService.java @@ -42,4 +42,10 @@ public List findAllLast() { log.info("salaryDate : {}", salaryDate); return salaryRepository.findAllLastSalary(salaryDate); } + + public List findAllNext() { + LocalDate salaryDate = SalaryUtil.getSalaryDate() + .plusDays(7); + return salaryRepository.findAllBySalaryDate(salaryDate); + } } diff --git a/src/main/java/com/postgraduate/global/slack/SlackErrorMessage.java b/src/main/java/com/postgraduate/global/slack/SlackErrorMessage.java index aa3e52eb6..de293fa29 100644 --- a/src/main/java/com/postgraduate/global/slack/SlackErrorMessage.java +++ b/src/main/java/com/postgraduate/global/slack/SlackErrorMessage.java @@ -51,6 +51,19 @@ public void sendSlackSalaryError(Long seniorId, Throwable ex) { } } + public void sendSlackSalaryError() { + try { + slackClient.send(logWebHookUrl, Payload.builder() + .text("정산 자동 생성 에러 발생!! 백엔드팀 확인 요망!!") + .attachments( + List.of(generateSalaryErrorSlackAttachment()) + ) + .build()); + } catch (IOException e) { + log.error("slack 전송 오류"); + } + } + public void sendSlackBizppurioError(String phoneNumber) { try { slackClient.send(logWebHookUrl, Payload.builder() @@ -99,6 +112,18 @@ private Attachment generateSalaryErrorSlackAttachment(Long seniorId, Throwable e )) .build(); } + + private Attachment generateSalaryErrorSlackAttachment() { + String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now()); + return Attachment.builder() + .color("ff0000") + .title(requestTime + "에 발생한 에러 로그") + .fields(List.of( + generateSlackField("정산 자동 생성 실패") + )) + .build(); + } + private Attachment generateBizppurioErrorSlackAttachment(String phoneNumber) { String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now()); return Attachment.builder() diff --git a/src/main/java/com/postgraduate/global/slack/SlackUtils.java b/src/main/java/com/postgraduate/global/slack/SlackUtils.java index 5bdeeb01c..2f2c062b9 100644 --- a/src/main/java/com/postgraduate/global/slack/SlackUtils.java +++ b/src/main/java/com/postgraduate/global/slack/SlackUtils.java @@ -13,4 +13,11 @@ public static Field generateSlackField(String title, String value) { .valueShortEnough(false) .build(); } + + public static Field generateSlackField(String title) { + return Field.builder() + .title(title) + .valueShortEnough(false) + .build(); + } } diff --git a/src/main/resources/templates/adminPayment.html b/src/main/resources/templates/adminPayment.html index b24712364..9a1f43a28 100644 --- a/src/main/resources/templates/adminPayment.html +++ b/src/main/resources/templates/adminPayment.html @@ -42,7 +42,7 @@ - mentoringStatus + mentoringStatus