-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Batch-v1.0.2
- Loading branch information
Showing
15 changed files
with
279 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
DuDoong-Batch/src/main/java/band/gosrock/job/EventExpiration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package band.gosrock.job; | ||
|
||
|
||
import band.gosrock.domain.domains.event.domain.Event; | ||
import band.gosrock.domain.domains.event.service.EventService; | ||
import band.gosrock.parameter.DateTimeJobParameter; | ||
import band.gosrock.slack.SlackEventExpirationSender; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.JobScope; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.repeat.RepeatStatus; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Configuration | ||
public class EventExpiration { | ||
|
||
private static final String JOB_NAME = "이벤트_자동만료"; | ||
private static final String BEAN_PREFIX = JOB_NAME + "_"; | ||
|
||
private final JobBuilderFactory jobBuilderFactory; | ||
private final StepBuilderFactory stepBuilderFactory; | ||
|
||
private final SlackEventExpirationSender slackEventExpirationSender; | ||
private final EventService eventService; | ||
|
||
@Bean(BEAN_PREFIX + "dateTimeJobParameter") | ||
@JobScope | ||
public DateTimeJobParameter dateTimeJobParameter() { | ||
return new DateTimeJobParameter(); | ||
} | ||
|
||
@Bean(JOB_NAME) | ||
public Job eventExpirationJob() { | ||
return jobBuilderFactory | ||
.get(JOB_NAME) | ||
.preventRestart() | ||
.start(eventExpirationStep()) | ||
.build(); | ||
} | ||
|
||
@Bean(BEAN_PREFIX + "step") | ||
@JobScope | ||
public Step eventExpirationStep() { | ||
return stepBuilderFactory | ||
.get(BEAN_PREFIX + "step") | ||
.tasklet( | ||
(contribution, chunkContext) -> { | ||
log.info(">>>>> 이벤트 자동 만료 작업 실행"); | ||
LocalDateTime time = dateTimeJobParameter().getTime(); | ||
List<Event> events = eventService.closeExpiredEventsEndAtBefore(time); | ||
slackEventExpirationSender.execute(time, events); | ||
return RepeatStatus.FINISHED; | ||
}) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
DuDoong-Batch/src/main/java/band/gosrock/parameter/DateTimeJobParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package band.gosrock.parameter; | ||
|
||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.Objects; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.batch.core.JobParametersInvalidException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class DateTimeJobParameter { | ||
private LocalDateTime time; | ||
|
||
@Value("#{jobParameters[dateTime]}") | ||
public void setDateTime(String dateTime) throws JobParametersInvalidException { | ||
// 인자가 없다면 now 로 설정 | ||
if (Objects.isNull(dateTime)) this.time = LocalDateTime.now(); | ||
else { | ||
try { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); | ||
this.time = LocalDateTime.parse(dateTime, formatter); | ||
} catch (Exception e) { | ||
throw new JobParametersInvalidException("올바르지 않은 시간 형식입니다"); | ||
} | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
DuDoong-Batch/src/main/java/band/gosrock/slack/SlackEventExpirationSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package band.gosrock.slack; | ||
|
||
import static com.slack.api.model.block.Blocks.divider; | ||
import static com.slack.api.model.block.Blocks.section; | ||
import static com.slack.api.model.block.composition.BlockCompositions.plainText; | ||
|
||
import band.gosrock.domain.domains.event.domain.Event; | ||
import band.gosrock.infrastructure.config.slack.SlackServiceNotificationProvider; | ||
import com.slack.api.model.block.Blocks; | ||
import com.slack.api.model.block.LayoutBlock; | ||
import com.slack.api.model.block.composition.MarkdownTextObject; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class SlackEventExpirationSender { | ||
private final SlackServiceNotificationProvider slackProvider; | ||
|
||
public void execute(LocalDateTime time, List<Event> events) { | ||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); | ||
List<LayoutBlock> layoutBlocks = new ArrayList<>(); | ||
|
||
layoutBlocks.add( | ||
Blocks.header( | ||
headerBlockBuilder -> headerBlockBuilder.text(plainText("공연 자동 만료 알림")))); | ||
layoutBlocks.add(divider()); | ||
MarkdownTextObject markdownDateTime = | ||
MarkdownTextObject.builder().text("* 기준 시간:*\n" + time.format(formatter)).build(); | ||
MarkdownTextObject markdownTotalEvents = | ||
MarkdownTextObject.builder().text("* 만료 공연 수:*\n" + events.size()).build(); | ||
layoutBlocks.add( | ||
section(section -> section.fields(List.of(markdownDateTime, markdownTotalEvents)))); | ||
layoutBlocks.add(divider()); | ||
|
||
MarkdownTextObject markdownEventIdTitle = | ||
MarkdownTextObject.builder().text("* 공연 ID:*\n").build(); | ||
MarkdownTextObject markdownEventNameTitle = | ||
MarkdownTextObject.builder().text("* 공연 이름:*\n").build(); | ||
layoutBlocks.add( | ||
section( | ||
section -> | ||
section.fields( | ||
List.of(markdownEventIdTitle, markdownEventNameTitle)))); | ||
|
||
events.forEach( | ||
event -> { | ||
MarkdownTextObject markdownEventId = | ||
MarkdownTextObject.builder().text(event.getId().toString()).build(); | ||
MarkdownTextObject markdownEventName = | ||
MarkdownTextObject.builder() | ||
.text(event.getEventBasic().getName()) | ||
.build(); | ||
layoutBlocks.add( | ||
section( | ||
section -> | ||
section.fields( | ||
List.of(markdownEventId, markdownEventName)))); | ||
}); | ||
|
||
slackProvider.sendNotification(layoutBlocks); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.