Skip to content

Commit

Permalink
Standardized callables execution.
Browse files Browse the repository at this point in the history
  • Loading branch information
milos85vasic committed Jan 13, 2024
1 parent 280ecfa commit 83b50b6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
28 changes: 24 additions & 4 deletions Main/src/main/java/com/redelf/commons/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,22 @@ fun yieldWhile(condition: () -> Boolean) {
}
}

fun exec(callable: Callable<Boolean>, logTag: String = "Bool exec ::"): Boolean {
fun exec(

val result = doExec(callable, logTag)
callable: Callable<Boolean>,
timeout: Long = 30L,
timeUnit: TimeUnit = TimeUnit.SECONDS,
logTag: String = "Bool exec ::"

): Boolean {

val result = doExec(

callable = callable,
timeout = timeout,
timeUnit = timeUnit,
logTag = logTag
)

result?.let {

Expand All @@ -59,7 +72,14 @@ fun exec(callable: Callable<Boolean>, logTag: String = "Bool exec ::"): Boolean
return false
}

fun <T> doExec(callable: Callable<T>, logTag: String = "Do exec ::"): T? {
fun <T> doExec(

callable: Callable<T>,
timeout: Long = 30L,
timeUnit: TimeUnit = TimeUnit.SECONDS,
logTag: String = "Do exec ::",

): T? {

var success: T? = null
val future = Executor.MAIN.execute(callable)
Expand All @@ -68,7 +88,7 @@ fun <T> doExec(callable: Callable<T>, logTag: String = "Do exec ::"): T? {

Timber.v("$logTag Callable: PRE-START")

success = future.get(30, TimeUnit.SECONDS)
success = future.get(timeout, timeUnit)

if (success != null) {

Expand Down
43 changes: 7 additions & 36 deletions Main/src/main/java/com/redelf/commons/lifecycle/Initialization.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.redelf.commons.lifecycle

import com.redelf.commons.exec
import com.redelf.commons.execution.Executor
import com.redelf.commons.lifecycle.exception.NotInitializedException
import timber.log.Timber
Expand Down Expand Up @@ -53,43 +54,13 @@ interface Initialization<T> : InitializationCondition {
}
}

val future = Executor.MAIN.execute(callable)
exec(

try {

val tag = "$initLogTag Init check ::"

Timber.v("$tag PRE-START")

val success = future.get(timeoutInSeconds, TimeUnit.SECONDS)

if (success) {

Timber.v("$tag Callable: RETURNED SUCCESS")

} else {

Timber.e("$tag Callable: RETURNED FAILURE")
}

Timber.v("$tag Callable: POST-END")

} catch (e: RejectedExecutionException) {

Timber.e(e)

} catch (e: InterruptedException) {

Timber.e(e)

} catch (e: ExecutionException) {

Timber.e(e)

} catch (e: TimeoutException) {

future.cancel(true)
}
callable = callable,
timeout = timeoutInSeconds,
timeUnit = TimeUnit.SECONDS,
logTag = initLogTag
)

if (!who.isInitialized()) {

Expand Down

0 comments on commit 83b50b6

Please sign in to comment.