Skip to content

Commit

Permalink
Implement adjusting absence scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
mkSpace committed Jul 29, 2023
1 parent 519ccaf commit 8800267
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
11 changes: 11 additions & 0 deletions moit-api/src/main/kotlin/com/mashup/moit/config/AsyncConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,15 @@ class AsyncConfig {
setWaitForTasksToCompleteOnShutdown(true)
}
}

@Bean(name = ["absenceSchedulerExecutor"])
fun absenceSchedulerExecutor(): Executor {
return ThreadPoolTaskExecutor().apply {
corePoolSize = 5
maxPoolSize = 5
queueCapacity = 20
setThreadNamePrefix("AbsenceSchedulerThread-")
setWaitForTasksToCompleteOnShutdown(true)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.mashup.moit.scheduler

import com.mashup.moit.domain.attendance.AttendanceService
import com.mashup.moit.domain.fine.FineService
import com.mashup.moit.domain.study.StudyService
import org.springframework.scheduling.annotation.Async
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.time.LocalDateTime

@Component
class StudyAdjustAbsenceScheduler(
private val studyService: StudyService,
private val attendanceService: AttendanceService,
private val fineService: FineService
) {

@Scheduled(cron = "0 */5 * * * *")
@Async("asyncSchedulerExecutor")
@Transactional
fun adjustAbsenceStatusFromNow() {
// 결석 확정을 지을 때 endAt을 현재 시간보다 15초 정도 유예기간을 줌. 5분마다 배치가 돌기 때문에 95% 시간 내에 끝난 스터디를 반환함
val undecided = studyService.findUnfinalizedStudiesByEndAt(LocalDateTime.now().minusSeconds(DECIDE_ABSENCE_RANGE))
.map { it.id }
studyService.findByStudyIds(undecided)
.groupBy { study -> study.moitId }
.forEach { (moitId, studies) ->
studies.forEach { study ->
attendanceService.adjustUndecidedAttendancesByStudyId(study.id)
.forEach { attendanceId -> fineService.create(attendanceId, moitId) }
}
}
}

companion object {
private const val DECIDE_ABSENCE_RANGE = 15L
}
}

0 comments on commit 8800267

Please sign in to comment.