diff options
Diffstat (limited to 'kvision-modules/kvision-server-jooby/src/main/kotlin/pl')
3 files changed, 95 insertions, 104 deletions
diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt index 1d5cd926..17f51443 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/JoobyServiceManager.kt @@ -22,8 +22,10 @@ package pl.treksoft.kvision.remote import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.CoroutineStart +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.launch import org.jooby.Response import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -32,6 +34,7 @@ import java.text.SimpleDateFormat /** * Multiplatform service manager for Jooby. */ +@UseExperimental(ExperimentalCoroutinesApi::class) actual open class JoobyServiceManager<T : Any> actual constructor(val service: T) : ServiceManager { companion object { @@ -52,24 +55,26 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified RET> bind( - noinline function: T.(Request?) -> Deferred<RET>, + noinline function: suspend T.(Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" routes.add { call(method, "/kv/$routeDef") { req, res -> val jsonRpcRequest = req.body(JsonRpcRequest::class.java) - try { - val result = runBlocking { function.invoke(service, req).await() } - res.send( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, req) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } catch (e: Exception) { + LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } } }.invoke(this) } @@ -83,7 +88,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR, reified RET> bind( - noinline function: T.(PAR, Request?) -> Deferred<RET>, + noinline function: suspend T.(PAR, Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -92,17 +97,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T val jsonRpcRequest = req.body(JsonRpcRequest::class.java) if (jsonRpcRequest.params.size == 1) { val param = getParameter<PAR>(jsonRpcRequest.params[0]) - try { - val result = runBlocking { function.invoke(service, param, req).await() } - res.send( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param, req) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } catch (e: Exception) { + LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } } } else { res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters")) @@ -119,7 +126,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind( - noinline function: T.(PAR1, PAR2, Request?) -> Deferred<RET>, + noinline function: suspend T.(PAR1, PAR2, Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -129,17 +136,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T if (jsonRpcRequest.params.size == 2) { val param1 = getParameter<PAR1>(jsonRpcRequest.params[0]) val param2 = getParameter<PAR2>(jsonRpcRequest.params[1]) - try { - val result = runBlocking { function.invoke(service, param1, param2, req).await() } - res.send( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param1, param2, req) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } catch (e: Exception) { + LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } } } else { res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters")) @@ -156,7 +165,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( - noinline function: T.(PAR1, PAR2, PAR3, Request?) -> Deferred<RET>, + noinline function: suspend T.(PAR1, PAR2, PAR3, Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -168,17 +177,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T val param1 = getParameter<PAR1>(jsonRpcRequest.params[0]) val param2 = getParameter<PAR2>(jsonRpcRequest.params[1]) val param3 = getParameter<PAR3>(jsonRpcRequest.params[2]) - try { - val result = runBlocking { function.invoke(service, param1, param2, param3, req).await() } - res.send( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param1, param2, param3, req) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } catch (e: Exception) { + LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } } } else { res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters")) @@ -195,7 +206,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T */ @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, Request?) -> Deferred<RET>, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, Request?) -> RET, route: String?, method: RpcHttpMethod ) { val routeDef = route ?: "route${this::class.simpleName}${counter++}" @@ -208,18 +219,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T val param2 = getParameter<PAR2>(jsonRpcRequest.params[1]) val param3 = getParameter<PAR3>(jsonRpcRequest.params[2]) val param4 = getParameter<PAR4>(jsonRpcRequest.params[3]) - try { - val result = - runBlocking { function.invoke(service, param1, param2, param3, param4, req).await() } - res.send( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param1, param2, param3, param4, req) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } catch (e: Exception) { + LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } } } else { res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters")) @@ -237,7 +249,7 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T @Suppress("TooGenericExceptionCaught") protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> Deferred<RET>, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5, Request?) -> RET, route: String?, method: RpcHttpMethod ) { @@ -252,20 +264,19 @@ actual open class JoobyServiceManager<T : Any> actual constructor(val service: T val param3 = getParameter<PAR3>(jsonRpcRequest.params[2]) val param4 = getParameter<PAR4>(jsonRpcRequest.params[3]) val param5 = getParameter<PAR5>(jsonRpcRequest.params[4]) - try { - val result = - runBlocking { - function.invoke(service, param1, param2, param3, param4, param5, req).await() - } - res.send( - JsonRpcResponse( - id = jsonRpcRequest.id, - result = mapper.writeValueAsString(result) + GlobalScope.launch(start = CoroutineStart.UNDISPATCHED) { + try { + val result = function.invoke(service, param1, param2, param3, param4, param5, req) + res.send( + JsonRpcResponse( + id = jsonRpcRequest.id, + result = mapper.writeValueAsString(result) + ) ) - ) - } catch (e: Exception) { - LOG.error(e.message, e) - res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } catch (e: Exception) { + LOG.error(e.message, e) + res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = e.message ?: "Error")) + } } } else { res.send(JsonRpcResponse(id = jsonRpcRequest.id, error = "Invalid parameters")) diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt index bc182467..c2321911 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/KVServer.kt @@ -22,10 +22,6 @@ package pl.treksoft.kvision.remote import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper -import kotlinx.coroutines.CoroutineStart -import kotlinx.coroutines.Deferred -import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.GlobalScope import org.jooby.Kooby import org.jooby.Session import org.jooby.json.Jackson @@ -63,36 +59,20 @@ actual typealias Request = org.jooby.Request actual typealias Profile = CommonProfile /** - * A helper extension function for asynchronous request processing. - */ -@UseExperimental(ExperimentalCoroutinesApi::class) -fun <RESP> Request?.async(block: (Request) -> RESP): Deferred<RESP> = this?.let { req -> - GlobalScope.coroutinesAsync(start = CoroutineStart.UNDISPATCHED) { - block(req) - } -} ?: throw IllegalStateException("Request not set!") - -/** * A helper extension function for asynchronous request processing with session. */ -@UseExperimental(ExperimentalCoroutinesApi::class) -fun <RESP> Request?.asyncSession(block: (Request, Session) -> RESP): Deferred<RESP> = this?.let { req -> +fun <RESP> Request?.withSession(block: (Request, Session) -> RESP): RESP = this?.let { req -> val session = req.session() - GlobalScope.coroutinesAsync(start = CoroutineStart.UNDISPATCHED) { - block(req, session) - } + block(req, session) } ?: throw IllegalStateException("Request not set!") /** * A helper extension function for asynchronous request processing with session and user profile. */ -@UseExperimental(ExperimentalCoroutinesApi::class) -fun <RESP> Request?.asyncAuth(block: (Request, Session, Profile) -> RESP): Deferred<RESP> = this?.let { req -> +fun <RESP> Request?.withProfile(block: (Request, Session, Profile) -> RESP): RESP = this?.let { req -> val session = req.session() val profile = req.require(CommonProfile::class.java) as CommonProfile? profile?.let { - GlobalScope.coroutinesAsync(start = CoroutineStart.UNDISPATCHED) { - block(req, session, profile) - } + block(req, session, profile) } } ?: throw IllegalStateException("Request or profile not set!") diff --git a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt index 462b3777..5b566423 100644 --- a/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt +++ b/kvision-modules/kvision-server-jooby/src/main/kotlin/pl/treksoft/kvision/remote/SpringServiceManager.kt @@ -21,7 +21,6 @@ */ package pl.treksoft.kvision.remote -import kotlinx.coroutines.Deferred import kotlin.reflect.KClass /** @@ -36,7 +35,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl * @param method a HTTP method */ protected actual inline fun <reified RET> bind( - noinline function: T.() -> Deferred<RET>, + noinline function: suspend T.() -> RET, route: String?, method: RpcHttpMethod ) { @@ -50,7 +49,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl * @param method a HTTP method */ protected actual inline fun <reified PAR, reified RET> bind( - noinline function: T.(PAR) -> Deferred<RET>, + noinline function: suspend T.(PAR) -> RET, route: String?, method: RpcHttpMethod ) { @@ -64,7 +63,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl * @param method a HTTP method */ protected actual inline fun <reified PAR1, reified PAR2, reified RET> bind( - noinline function: T.(PAR1, PAR2) -> Deferred<RET>, + noinline function: suspend T.(PAR1, PAR2) -> RET, route: String?, method: RpcHttpMethod ) { @@ -78,7 +77,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl * @param method a HTTP method */ protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified RET> bind( - noinline function: T.(PAR1, PAR2, PAR3) -> Deferred<RET>, + noinline function: suspend T.(PAR1, PAR2, PAR3) -> RET, route: String?, method: RpcHttpMethod ) { @@ -92,7 +91,7 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl * @param method a HTTP method */ protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified RET> bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4) -> Deferred<RET>, + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4) -> RET, route: String?, method: RpcHttpMethod ) { @@ -105,8 +104,9 @@ actual open class SpringServiceManager<T : Any> actual constructor(val serviceCl * @param route a route * @param method a HTTP method */ - protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, reified PAR4, reified PAR5, reified RET> bind( - noinline function: T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> Deferred<RET>, + protected actual inline fun <reified PAR1, reified PAR2, reified PAR3, + reified PAR4, reified PAR5, reified RET> bind( + noinline function: suspend T.(PAR1, PAR2, PAR3, PAR4, PAR5) -> RET, route: String?, method: RpcHttpMethod ) { |