-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from GSM-GOGO/feature/163-match-before-ten-mi…
…nute 경기 10분 전 알림
- Loading branch information
Showing
9 changed files
with
318 additions
and
1 deletion.
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
20 changes: 20 additions & 0 deletions
20
gsmgogo-batch/src/main/java/team/gsmgogo/global/config/MessageConfig.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,20 @@ | ||
package team.gsmgogo.global.config; | ||
|
||
import net.nurigo.sdk.message.service.DefaultMessageService; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class MessageConfig { | ||
@Value("${message.apiKey}") | ||
private String apiKey; | ||
|
||
@Value("${message.apiSecretKey}") | ||
private String apiSecretKey; | ||
|
||
@Bean | ||
public DefaultMessageService defaultMessageService() { | ||
return new DefaultMessageService(apiKey, apiSecretKey, "https://api.coolsms.co.kr"); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
gsmgogo-batch/src/main/java/team/gsmgogo/global/config/SchedulerConfig.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,21 @@ | ||
package team.gsmgogo.global.config; | ||
|
||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.scheduling.annotation.SchedulingConfigurer; | ||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; | ||
import org.springframework.scheduling.config.ScheduledTaskRegistrar; | ||
|
||
@Configuration | ||
public class SchedulerConfig implements SchedulingConfigurer { | ||
|
||
@Override | ||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | ||
ThreadPoolTaskScheduler threadPool = new ThreadPoolTaskScheduler(); | ||
|
||
int processors = Runtime.getRuntime().availableProcessors(); | ||
threadPool.setPoolSize(processors); | ||
threadPool.initialize(); | ||
|
||
taskRegistrar.setTaskScheduler(threadPool); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
gsmgogo-batch/src/main/java/team/gsmgogo/job/AlertJob.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,93 @@ | ||
package team.gsmgogo.job; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.nurigo.sdk.message.model.Message; | ||
import net.nurigo.sdk.message.request.SingleMessageSendingRequest; | ||
import net.nurigo.sdk.message.service.DefaultMessageService; | ||
import org.quartz.Job; | ||
import org.quartz.JobDataMap; | ||
import org.quartz.JobExecutionContext; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Configuration; | ||
import team.gsmgogo.domain.match.entity.MatchEntity; | ||
import team.gsmgogo.domain.match.enums.MatchLevelType; | ||
import team.gsmgogo.domain.match.repository.MatchJpaRepository; | ||
import team.gsmgogo.domain.team.entity.TeamEntity; | ||
import team.gsmgogo.domain.user.entity.UserEntity; | ||
import team.gsmgogo.domain.user.repository.UserQueryDslRepository; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class AlertJob implements Job { | ||
private final MatchJpaRepository matchJpaRepository; | ||
private final UserQueryDslRepository userQueryDslRepository; | ||
private final DefaultMessageService messageService; | ||
|
||
@Value("${message.send}") | ||
private String sendNumber; | ||
|
||
@Override | ||
public void execute(JobExecutionContext context) { | ||
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); | ||
|
||
Long matchId = jobDataMap.getLong("matchId"); | ||
MatchEntity match = matchJpaRepository.findByMatchId(matchId) | ||
.orElseThrow(() -> new RuntimeException("배치에서 해당 매치를 찾을 수 없습니다.")); | ||
|
||
String matchLevel; | ||
if(match.getMatchLevel() == MatchLevelType.TRYOUT){ | ||
matchLevel = "예선"; | ||
} else if(match.getMatchLevel() == MatchLevelType.SEMI_FINAL){ | ||
matchLevel = "준결승"; | ||
} else { | ||
matchLevel = "결승"; | ||
} | ||
|
||
TeamEntity teamA = match.getTeamA(); | ||
TeamEntity teamB = match.getTeamB(); | ||
|
||
List<UserEntity> followAUsers = userQueryDslRepository.findByFollowsTeam(teamA); | ||
followAUsers.forEach(user -> { | ||
Message message = new Message(); | ||
message.setFrom(sendNumber); | ||
message.setTo(user.getPhoneNumber()); | ||
message.setText( | ||
""" | ||
GSM GOGO 경기 알림 | ||
곧 %s팀 VS %s팀 %s 경기가 시작됩니다! | ||
%s팀의 승리를 위해 힘찬 응원 부탁드립니다! | ||
잠시 후 10분 후 경기가 시작됩니다. 아직 배팅에 참여하지 않았다면 서비스에 접속하여 배팅을 진행해주세요! | ||
https://gsmgogo.kr | ||
""".formatted(teamA.getTeamName(), teamB.getTeamName(), matchLevel, teamA.getTeamName()) | ||
); | ||
messageService.sendOne(new SingleMessageSendingRequest(message)); | ||
}); | ||
|
||
List<UserEntity> followBUsers = userQueryDslRepository.findByFollowsTeam(teamB); | ||
followBUsers.forEach(user -> { | ||
Message message = new Message(); | ||
message.setFrom(sendNumber); | ||
message.setTo(user.getPhoneNumber()); | ||
message.setText( | ||
""" | ||
GSM GOGO 경기 알림 | ||
곧 %s팀 VS %s팀 %s 경기가 시작됩니다! | ||
%s팀의 승리를 위해 힘찬 응원 부탁드립니다! | ||
잠시 후 10분 후 경기가 시작됩니다. 아직 배팅에 참여하지 않았다면 서비스에 접속하여 배팅을 진행해주세요! | ||
https://gsmgogo.kr | ||
""".formatted(teamB.getTeamName(), teamA.getTeamName(), matchLevel, teamB.getTeamName()) | ||
); | ||
messageService.sendOne(new SingleMessageSendingRequest(message)); | ||
}); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
gsmgogo-batch/src/main/java/team/gsmgogo/job/DailyJob.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,40 @@ | ||
package team.gsmgogo.job; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.*; | ||
import org.springframework.batch.core.job.builder.JobBuilder; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.step.builder.StepBuilder; | ||
import org.springframework.batch.repeat.RepeatStatus; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import team.gsmgogo.domain.user.repository.UserQueryDslRepository; | ||
|
||
@Slf4j | ||
@Configuration | ||
@RequiredArgsConstructor | ||
public class DailyJob { | ||
private final JobRepository jobRepository; | ||
private final PlatformTransactionManager platformTransactionManager; | ||
private final UserQueryDslRepository userQueryDslRepository; | ||
|
||
@Bean(name = "resetCountJob") | ||
public Job resetCountJob(){ | ||
return new JobBuilder("reset-count-Job", jobRepository) | ||
.start(resetCountStep(jobRepository, platformTransactionManager)) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public Step resetCountStep(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager){ | ||
return new StepBuilder("reset-count-step", jobRepository) | ||
.tasklet((contribution, chunkContext) -> { | ||
userQueryDslRepository.bulkResetVerifyCount(); | ||
return RepeatStatus.FINISHED; | ||
}, | ||
platformTransactionManager) | ||
.build(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
gsmgogo-batch/src/main/java/team/gsmgogo/scheduler/AlertScheduler.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,63 @@ | ||
package team.gsmgogo.scheduler; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.quartz.*; | ||
import org.quartz.impl.JobDetailImpl; | ||
import org.quartz.impl.triggers.CronTriggerImpl; | ||
import org.springframework.batch.core.JobParameter; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import team.gsmgogo.domain.match.entity.MatchEntity; | ||
import team.gsmgogo.domain.match.repository.MatchQueryDslRepository; | ||
import team.gsmgogo.job.AlertJob; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class AlertScheduler { | ||
private final Scheduler scheduler; | ||
private final MatchQueryDslRepository matchQueryDslRepository; | ||
|
||
@Scheduled(cron = "0 0 0 * * *") | ||
public void start(){ | ||
LocalDate today = LocalDate.now(); | ||
List<MatchEntity> matches = matchQueryDslRepository.findByMonthAndDay( | ||
today.getMonthValue(), | ||
today.getDayOfMonth() | ||
); | ||
|
||
matches.forEach(match -> { | ||
try { | ||
LocalDateTime beforeMatch = match.getStartAt().minusMinutes(10); | ||
|
||
JobDataMap jobDataMap = new JobDataMap(); | ||
jobDataMap.put("matchId", match.getMatchId()); | ||
|
||
JobDetailImpl detail1 = new JobDetailImpl(); | ||
detail1.setName("alert-detail"); | ||
detail1.setGroup("alert"); | ||
detail1.setJobClass(AlertJob.class); | ||
detail1.setJobDataMap(jobDataMap); | ||
|
||
Trigger trigger1 = TriggerBuilder.newTrigger() | ||
.withSchedule(CronScheduleBuilder.cronSchedule("0 %d %d %d %d ? %d".formatted( | ||
beforeMatch.getMinute(), | ||
beforeMatch.getHour(), | ||
beforeMatch.getDayOfMonth(), | ||
beforeMatch.getMonthValue(), | ||
beforeMatch.getYear() | ||
))).build(); | ||
|
||
scheduler.scheduleJob(detail1, trigger1); | ||
} catch (SchedulerException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
gsmgogo-batch/src/main/java/team/gsmgogo/scheduler/DailyScheduler.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,54 @@ | ||
package team.gsmgogo.scheduler; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.batch.core.JobParameter; | ||
import org.springframework.batch.core.JobParameters; | ||
import org.springframework.batch.core.JobParametersInvalidException; | ||
import org.springframework.batch.core.launch.JobLauncher; | ||
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException; | ||
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException; | ||
import org.springframework.batch.core.repository.JobRepository; | ||
import org.springframework.batch.core.repository.JobRestartException; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
import team.gsmgogo.domain.user.repository.UserQueryDslRepository; | ||
import team.gsmgogo.job.DailyJob; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class DailyScheduler { | ||
private final JobLauncher jobLauncher; | ||
private final JobRepository jobRepository; | ||
private final PlatformTransactionManager platformTransactionManager; | ||
private final UserQueryDslRepository userQueryDslRepository; | ||
|
||
@Scheduled(cron = "0 5 1 * * *") | ||
public void resetLoginCount() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException { | ||
Map<String, JobParameter<?>> confMap = new HashMap<>(); | ||
confMap.put("time", new JobParameter(System.currentTimeMillis(), String.class)); | ||
JobParameters jobParameters = new JobParameters(confMap); | ||
|
||
jobLauncher.run( | ||
new DailyJob(jobRepository, platformTransactionManager, userQueryDslRepository).resetCountJob(), | ||
jobParameters | ||
); | ||
} | ||
|
||
@Scheduled(cron = "0 0 0 * * *") | ||
public void registerAlert() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException { | ||
Map<String, JobParameter<?>> confMap = new HashMap<>(); | ||
confMap.put("time", new JobParameter(System.currentTimeMillis(), String.class)); | ||
JobParameters jobParameters = new JobParameters(confMap); | ||
|
||
jobLauncher.run( | ||
new DailyJob(jobRepository, platformTransactionManager, userQueryDslRepository).resetCountJob(), | ||
jobParameters | ||
); | ||
} | ||
} |
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