-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: TimeOutEventTaskManager 구현 * feat: ScheduledEvent 구현 * feat: ScheduledEvent canceled 칼럼 추가 * feat: NotificationEmailSendTimeOutEventReplayer 반영 * feat: CrmEmailSendView 화면 구현
- Loading branch information
1 parent
b83eb8f
commit f7db0e0
Showing
7 changed files
with
214 additions
and
10 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
25 changes: 25 additions & 0 deletions
25
domain/crm/src/main/kotlin/com/few/crm/email/event/schedule/ScheduledEvent.kt
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,25 @@ | ||
package com.few.crm.email.event.schedule | ||
|
||
import event.Event | ||
import event.EventUtils | ||
import java.time.LocalDateTime | ||
|
||
abstract class ScheduledEvent( | ||
eventId: String, | ||
eventType: String, | ||
eventTime: LocalDateTime, | ||
) : Event( | ||
eventId, | ||
eventType, | ||
eventTime, | ||
) | ||
|
||
class CancelScheduledEvent( | ||
val targetEventId: String, | ||
eventId: String = EventUtils.generateEventId(), | ||
eventTime: LocalDateTime = LocalDateTime.now(), | ||
) : ScheduledEvent( | ||
eventId, | ||
"CancelScheduledEvent", | ||
eventTime, | ||
) |
20 changes: 20 additions & 0 deletions
20
domain/crm/src/main/kotlin/com/few/crm/email/event/schedule/ScheduledEventListener.kt
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 com.few.crm.email.event.schedule | ||
|
||
import com.few.crm.email.event.schedule.handler.CancelScheduledEventHandler | ||
import com.few.crm.support.jpa.CrmTransactional | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.scheduling.annotation.Async | ||
import org.springframework.stereotype.Component | ||
import org.springframework.transaction.annotation.Propagation | ||
|
||
@Component | ||
class ScheduledEventListener( | ||
private val cancelScheduledEventHandler: CancelScheduledEventHandler, | ||
) { | ||
@Async | ||
@EventListener | ||
@CrmTransactional(propagation = Propagation.REQUIRES_NEW) | ||
fun onCancelEvent(event: CancelScheduledEvent) { | ||
cancelScheduledEventHandler.handle(event) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...m/src/main/kotlin/com/few/crm/email/event/schedule/handler/CancelScheduledEventHandler.kt
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,19 @@ | ||
package com.few.crm.email.event.schedule.handler | ||
|
||
import com.few.crm.email.event.schedule.CancelScheduledEvent | ||
import com.few.crm.email.repository.ScheduledEventRepository | ||
import com.few.crm.support.jpa.CrmTransactional | ||
import event.EventHandler | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class CancelScheduledEventHandler( | ||
private val scheduledEventRepository: ScheduledEventRepository, | ||
) : EventHandler<CancelScheduledEvent> { | ||
@CrmTransactional | ||
override fun handle(event: CancelScheduledEvent) { | ||
scheduledEventRepository | ||
.findByEventId(event.targetEventId) | ||
?.cancel() ?: throw IllegalStateException("Scheduled event not found for event id: ${event.eventId}") | ||
} | ||
} |
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
86 changes: 86 additions & 0 deletions
86
domain/crm/src/main/kotlin/com/few/crm/support/schedule/TimeOutEventTaskManager.kt
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,86 @@ | ||
package com.few.crm.support.schedule | ||
|
||
import com.few.crm.email.event.schedule.CancelScheduledEvent | ||
import com.few.crm.email.event.send.NotificationEmailSendTimeOutEvent | ||
import com.few.crm.support.toScheduleTime | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import org.springframework.context.ApplicationEventPublisher | ||
import org.springframework.scheduling.TaskScheduler | ||
import org.springframework.stereotype.Component | ||
import java.util.concurrent.ScheduledFuture | ||
|
||
class ManagedTask( | ||
val taskName: String, | ||
val task: ScheduledFuture<*>, | ||
val taskView: TaskView, | ||
) | ||
|
||
data class TaskView( | ||
val taskName: String, | ||
val values: Map<String, Any>, | ||
) | ||
|
||
@Component | ||
class TimeOutEventTaskManager( | ||
private val taskScheduler: TaskScheduler, | ||
private val applicationEventPublisher: ApplicationEventPublisher, | ||
) { | ||
val log = KotlinLogging.logger {} | ||
|
||
companion object { | ||
val tasks = mutableMapOf<String, ManagedTask>() | ||
} | ||
|
||
fun schedule( | ||
taskName: String, | ||
task: ScheduledFuture<*>, | ||
taskView: TaskView, | ||
) { | ||
log.info { "Scheduling task $taskName" } | ||
tasks[taskName] = ManagedTask(taskName, task, taskView) | ||
} | ||
|
||
fun newSchedule(event: NotificationEmailSendTimeOutEvent) { | ||
schedule(event) | ||
applicationEventPublisher.publishEvent(event) | ||
} | ||
|
||
fun reSchedule(event: NotificationEmailSendTimeOutEvent) { | ||
schedule(event) | ||
} | ||
|
||
private fun schedule(event: NotificationEmailSendTimeOutEvent) { | ||
this.schedule( | ||
event.eventId, | ||
taskScheduler.schedule(event, event.expiredTime.toScheduleTime()), | ||
TaskView( | ||
taskName = event.eventId, | ||
values = | ||
mapOf( | ||
"templateId" to event.templateId, | ||
"userIds" to event.userIds, | ||
"eventId" to event.eventId, | ||
"eventType" to event.eventType, | ||
"eventTime" to event.eventTime, | ||
"expiredTime" to event.expiredTime, | ||
"completed" to event.completed, | ||
), | ||
), | ||
) | ||
} | ||
|
||
fun cancel(taskName: String) { | ||
tasks[taskName]?.let { | ||
it.task.cancel(false) | ||
tasks.remove(taskName) | ||
applicationEventPublisher.publishEvent( | ||
CancelScheduledEvent( | ||
targetEventId = taskName, | ||
), | ||
) | ||
log.info { "Task $taskName is cancelled" } | ||
} | ||
} | ||
|
||
fun scheduledTasksView(): List<TaskView> = tasks.values.map { it.taskView } | ||
} |
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