-
Notifications
You must be signed in to change notification settings - Fork 3
실행단계 최초 구현 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Gobukgol
wants to merge
3
commits into
team2
Choose a base branch
from
lmh
base: team2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
실행단계 최초 구현 #12
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
10 changes: 9 additions & 1 deletion
10
src/main/kotlin/org/javabom/bomscheduler/job/JobCollection.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -1,8 +1,16 @@ | ||
| package org.javabom.bomscheduler.job | ||
|
|
||
| class JobCollection(definedJobs : List<Job> = emptyList()) { | ||
| class JobCollection( | ||
| private var definedJobs: List<Job> = emptyList() | ||
| ) { | ||
|
|
||
| init { | ||
| definedJobs.forEach{ println("Register Job ${it.name}")} | ||
| } | ||
|
|
||
| fun getDefinedJobs(): List<Job> = definedJobs | ||
|
|
||
| fun replaceJobs(jobs: List<Job>) { | ||
| definedJobs = jobs | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
src/main/kotlin/org/javabom/bomscheduler/job/JobManager.kt
This file contains hidden or 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 org.javabom.bomscheduler.job | ||
|
|
||
| import org.javabom.bomscheduler.config.ScheduleConfig | ||
| import org.springframework.context.SmartLifecycle | ||
| import java.time.LocalDateTime | ||
|
|
||
| class JobManager( | ||
| private val jobCoordinator: JobCoordinator, | ||
| private val jobCollection: JobCollection | ||
| ): SmartLifecycle { | ||
|
|
||
| private var running = false | ||
| private var keepGoing = false | ||
|
|
||
| override fun start() { | ||
| running = true | ||
| keepGoing = true | ||
|
|
||
| Thread { | ||
| updateJob() | ||
| }.start() | ||
| } | ||
|
|
||
| override fun stop() { | ||
| this.keepGoing = false | ||
| } | ||
|
|
||
| override fun isRunning(): Boolean { | ||
| return running | ||
| } | ||
|
|
||
| private fun updateJob() { | ||
| while (keepGoing) { | ||
| val (currentJobs, anotherJobs) = jobCoordinator.getDefinedJob() | ||
| .partition { it.instanceName == ScheduleConfig.HOST_NAME } | ||
|
|
||
| val now = LocalDateTime.now() | ||
|
|
||
| val exchangedJobs = anotherJobs.filter { it.isExpired() } | ||
| .map { | ||
| val ttl = it.ttl | ||
|
|
||
| it.copy( | ||
| instanceName = ScheduleConfig.HOST_NAME, | ||
| instanceExpiredTime = now.plusNanos(ttl) | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| if (exchangedJobs.isNotEmpty()) { | ||
| jobCoordinator.updateJobs(exchangedJobs) | ||
| } | ||
|
|
||
| jobCollection.replaceJobs(currentJobs + exchangedJobs) | ||
| } | ||
|
|
||
| running = false | ||
| } | ||
|
|
||
| fun updateJobExecutionTime(job: Job) { | ||
| jobCoordinator.updateJobs(listOf(job)) | ||
| } | ||
| } | ||
This file contains hidden or 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,9 @@ | ||
| package org.javabom.bomscheduler.job | ||
|
|
||
| import java.time.LocalDateTime | ||
|
|
||
| data class JobMetadata( | ||
| val jobName: String, | ||
| val lastExecutionTime: LocalDateTime, | ||
| val ttl: Long | ||
| ) |
23 changes: 23 additions & 0 deletions
23
src/main/kotlin/org/javabom/bomscheduler/schedule/BomScheduleInterceptor.kt
This file contains hidden or 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,23 @@ | ||
| package org.javabom.bomscheduler.schedule | ||
|
|
||
| import org.aopalliance.intercept.MethodInterceptor | ||
| import org.aopalliance.intercept.MethodInvocation | ||
| import org.javabom.bomscheduler.task.TaskManager | ||
| import org.javabom.bomscheduler.task.TaskRunner | ||
|
|
||
| class BomScheduleInterceptor( | ||
| private val taskRunner: TaskRunner | ||
| ) : MethodInterceptor { | ||
| override fun invoke(invocation: MethodInvocation): Any? { | ||
| val jobName = invocation.method.getAnnotation(BomSchedule::class.java).jobName | ||
|
|
||
| val task = taskRunner.findTaskByJobName(jobName) | ||
|
|
||
| if (task != null) { | ||
| task.consuming() | ||
| return invocation.proceed() | ||
| } | ||
|
|
||
| return null | ||
| } | ||
| } |
This file contains hidden or 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,11 @@ | ||
| package org.javabom.bomscheduler.task | ||
|
|
||
| data class Task ( | ||
| val jobName: String, | ||
| val callback: () -> Unit | ||
| ) { | ||
|
|
||
| fun consuming() { | ||
| callback.invoke() | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
src/main/kotlin/org/javabom/bomscheduler/task/TaskManager.kt
This file contains hidden or 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,62 @@ | ||
| package org.javabom.bomscheduler.task | ||
|
|
||
| import org.javabom.bomscheduler.config.ScheduleConfig | ||
| import org.javabom.bomscheduler.job.JobCollection | ||
| import org.javabom.bomscheduler.job.JobManager | ||
| import org.springframework.context.SmartLifecycle | ||
| import java.time.LocalDateTime | ||
|
|
||
| class TaskManager( | ||
| private val jobManager: JobManager, | ||
| private val jobCollection: JobCollection, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요거 잡 매니저 통해서 사용하면 이상한가????? |
||
| private val taskRunner: TaskRunner | ||
| ): SmartLifecycle { | ||
| private var running = false | ||
| private var keepGoing = false | ||
|
|
||
|
|
||
| override fun start() { | ||
| running = true | ||
| keepGoing = true | ||
|
|
||
| Thread { | ||
| createExecutableTasks() | ||
| }.start() | ||
| } | ||
|
|
||
| override fun stop() { | ||
| keepGoing = false | ||
| } | ||
|
|
||
| override fun isRunning(): Boolean { | ||
| return running | ||
| } | ||
|
|
||
| private fun createExecutableTasks() { | ||
| val executableTasks = jobCollection.getDefinedJobs() | ||
| .filter { it.instanceName == ScheduleConfig.HOST_NAME } | ||
| .map { | ||
| Task( | ||
| jobName = it.name, | ||
| callback = { | ||
| val executionTime = LocalDateTime.now() | ||
| val ttl = it.ttl | ||
|
|
||
| taskRunner.consumeTask(it.name) | ||
| jobManager.updateJobExecutionTime( | ||
| it.copy( | ||
| lastExecutionTime = executionTime, | ||
| instanceExpiredTime = executionTime.plusNanos(ttl) | ||
| ) | ||
| ) | ||
| } | ||
| ) | ||
| } | ||
|
|
||
| while (keepGoing) { | ||
| taskRunner.putTasks(executableTasks) | ||
| } | ||
|
|
||
| running = false | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/org/javabom/bomscheduler/task/TaskRunner.kt
This file contains hidden or 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 org.javabom.bomscheduler.task | ||
|
|
||
| import org.javabom.bomscheduler.job.Job | ||
| import org.javabom.bomscheduler.job.JobMetadata | ||
| import org.springframework.stereotype.Component | ||
|
|
||
| class TaskRunner { | ||
| private val executableTasks: MutableMap<String, Task> = mutableMapOf() | ||
|
|
||
| fun putTasks(tasks: List<Task>) { | ||
| tasks.forEach { | ||
| executableTasks.putIfAbsent(it.jobName, it) | ||
| } | ||
| } | ||
|
|
||
| fun findTaskByJobName(jobName: String) = executableTasks[jobName] | ||
|
|
||
| fun consumeTask(jobName: String) = executableTasks.remove(jobName) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이조건문 탈출하려면 어떻게 해야하는거야?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요거 스프링이 종료될 때 SmartLifeCycle 구현한 클래스의 stop 메소드 호출하면서 while 문 탈출 할 수 있도록 돌아가는걸로 알고있어!