Skip to content

Commit

Permalink
Now coroutine executor will return job on execution
Browse files Browse the repository at this point in the history
  • Loading branch information
this-Aditya committed Nov 3, 2024
1 parent 5f092fb commit 04cb6d1
Showing 1 changed file with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.joinAll
Expand All @@ -42,7 +43,7 @@ import kotlin.coroutines.suspendCoroutine
* @param coroutineDispatcher The coroutine dispatcher to use for execution.
*/
class CoroutineTaskExecutor(
private val invokingClassName: String
private val invokingClassName: String,
private val coroutineDispatcher: CoroutineDispatcher,
) {
private var job: Job? = null
Expand Down Expand Up @@ -127,6 +128,18 @@ class CoroutineTaskExecutor(
}
}

fun returnJobAndExecute(task: suspend () -> Unit): Job? {
val activeStatus: Boolean? = executorScope?.isActive
if (activeStatus == null || activeStatus == false) {
logger.warn("Can't execute task and return job, scope is already cancelled")
return null
}
checkExecutorStarted() ?: return null
return executorScope?.launch {
runTaskSafely(task)
}
}

// /**
// * Launches a suspendable task, optionally on a specified [CoroutineDispatcher].
// *
Expand Down Expand Up @@ -212,16 +225,18 @@ class CoroutineTaskExecutor(
* Cancel the coroutine, running [finalization], if any, as the last operation.
* None of the finalization will run if [CoroutineTaskExecutor.start] was not called.
*/
suspend fun stop(finalization: (suspend () -> Unit)? = null) {
suspend fun stop(finalization: (suspend () -> Unit)? = null) = coroutineScope {
finalization?.let {
executorScope?.let { execScope ->
checkExecutorStarted() ?: return
executorScope?.also { execScope ->
checkExecutorStarted() ?: return@let
joinAll(
execScope.launch {
finalization()
runTaskSafely(it)
logger.info("Executed the finalization task")
}
)
} ?: launch {
runTaskSafely(it)
}
}
cancelAllFutures()
Expand Down Expand Up @@ -315,7 +330,7 @@ class CoroutineTaskExecutor(
checkNullJob() ?: return
job = executorScope?.launch {
delay(delayMillis)
task()
runTaskSafely(task)
}
}

Expand All @@ -324,7 +339,7 @@ class CoroutineTaskExecutor(
job = executorScope?.launch {
while (isActive) {
delay(delayMillis)
task()
runTaskSafely(task)
}
}
}
Expand All @@ -338,7 +353,11 @@ class CoroutineTaskExecutor(
var lastResult = true
while (isActive && lastResult) {
delay(delayMillis)
lastResult = task()
lastResult = try {
task()
} catch (ex: Exception) {
false
}
}
}
}
Expand Down

0 comments on commit 04cb6d1

Please sign in to comment.