diff --git a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt index ea95854ba7..ef379eb7be 100644 --- a/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt +++ b/TelegramBotAPI-extensions-utils/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/extensions/utils/updates/retrieving/Webhook.kt @@ -77,7 +77,6 @@ fun startListenWebhooks( scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), block: UpdateReceiver ): ApplicationEngine { - lateinit var engine: ApplicationEngine val env = applicationEngineEnvironment { module { @@ -103,10 +102,10 @@ fun startListenWebhooks( } } - engine = embeddedServer(engineFactory, env) - engine.start(false) - return engine + return embeddedServer(engineFactory, env).also { + it.start(false) + } } private suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhooks( @@ -119,14 +118,10 @@ private suspend fun RequestsExecutor.internalSetWebhookInfoAndStartListenWebhook privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), block: UpdateReceiver -): Job { +): ApplicationEngine { return try { execute(setWebhookRequest) - val engine = startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block) - scope.launch { - engine.environment.parentCoroutineContext[Job] ?.join() - engine.stop(1000, 5000) - } + startListenWebhooks(listenPort, engineFactory, exceptionsHandler, listenHost, listenRoute, privateKeyConfig, scope, block) } catch (e: Exception) { throw e } @@ -154,7 +149,7 @@ suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks( privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), block: UpdateReceiver -): Job = internalSetWebhookInfoAndStartListenWebhooks( +): ApplicationEngine = internalSetWebhookInfoAndStartListenWebhooks( listenPort, engineFactory, setWebhookRequest as Request, @@ -188,7 +183,7 @@ suspend fun RequestsExecutor.setWebhookInfoAndStartListenWebhooks( privateKeyConfig: WebhookPrivateKeyConfig? = null, scope: CoroutineScope = CoroutineScope(Executors.newFixedThreadPool(4).asCoroutineDispatcher()), block: UpdateReceiver -): Job = internalSetWebhookInfoAndStartListenWebhooks( +): ApplicationEngine = internalSetWebhookInfoAndStartListenWebhooks( listenPort, engineFactory, setWebhookRequest as Request,