mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-03 23:29:33 +00:00
add RequestsExecutor.Log
This commit is contained in:
@@ -7,6 +7,8 @@
|
|||||||
* `Coroutines`: `1.10.1` -> `1.10.2`
|
* `Coroutines`: `1.10.1` -> `1.10.2`
|
||||||
* `Ktor`: `3.1.1` -> `3.1.3`
|
* `Ktor`: `3.1.1` -> `3.1.3`
|
||||||
* `MicroUtils`: `0.25.3` -> `0.25.7`
|
* `MicroUtils`: `0.25.3` -> `0.25.7`
|
||||||
|
* `Core`:
|
||||||
|
* `RequestsExecutor` got property `RequestsExecutor.Log: KSLog?`
|
||||||
|
|
||||||
## 25.0.1
|
## 25.0.1
|
||||||
|
|
||||||
|
@@ -102,6 +102,7 @@ public final class dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultBehavio
|
|||||||
public fun getEditedMessagesFlow ()Lkotlinx/coroutines/flow/Flow;
|
public fun getEditedMessagesFlow ()Lkotlinx/coroutines/flow/Flow;
|
||||||
public fun getFlowsUpdatesFilter ()Ldev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter;
|
public fun getFlowsUpdatesFilter ()Ldev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter;
|
||||||
public fun getInlineQueriesFlow ()Lkotlinx/coroutines/flow/Flow;
|
public fun getInlineQueriesFlow ()Lkotlinx/coroutines/flow/Flow;
|
||||||
|
public fun getLog ()Ldev/inmo/kslog/common/KSLog;
|
||||||
public fun getMessageMediaGroupsFlow ()Lkotlinx/coroutines/flow/Flow;
|
public fun getMessageMediaGroupsFlow ()Lkotlinx/coroutines/flow/Flow;
|
||||||
public fun getMessagesFlow ()Lkotlinx/coroutines/flow/Flow;
|
public fun getMessagesFlow ()Lkotlinx/coroutines/flow/Flow;
|
||||||
public fun getMyChatMemberUpdatesFlow ()Lkotlinx/coroutines/flow/Flow;
|
public fun getMyChatMemberUpdatesFlow ()Lkotlinx/coroutines/flow/Flow;
|
||||||
|
@@ -116,6 +116,7 @@ public final class dev/inmo/tgbotapi/extensions/behaviour_builder/DefaultBehavio
|
|||||||
public fun getCoroutineContext ()Lkotlin/coroutines/CoroutineContext;
|
public fun getCoroutineContext ()Lkotlin/coroutines/CoroutineContext;
|
||||||
public fun getData ()Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextData;
|
public fun getData ()Ldev/inmo/tgbotapi/extensions/behaviour_builder/BehaviourContextData;
|
||||||
public fun getFlowsUpdatesFilter ()Ldev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter;
|
public fun getFlowsUpdatesFilter ()Ldev/inmo/tgbotapi/updateshandlers/FlowsUpdatesFilter;
|
||||||
|
public fun getLog ()Ldev/inmo/kslog/common/KSLog;
|
||||||
public fun getScope ()Lkotlinx/coroutines/CoroutineScope;
|
public fun getScope ()Lkotlinx/coroutines/CoroutineScope;
|
||||||
public fun getSubcontextInitialAction ()Lkotlin/jvm/functions/Function3;
|
public fun getSubcontextInitialAction ()Lkotlin/jvm/functions/Function3;
|
||||||
public fun getTriggersHolder ()Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;
|
public fun getTriggersHolder ()Ldev/inmo/tgbotapi/extensions/behaviour_builder/utils/handlers_registrar/TriggersHolder;
|
||||||
|
@@ -40,7 +40,7 @@ class CombinedSubcontextInitialAction(
|
|||||||
runCatching {
|
runCatching {
|
||||||
invoke(update)
|
invoke(update)
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
logger.error(it) {
|
(Log ?: logger).error(it) {
|
||||||
"Unable to execute $subaction for update $update. Will try on next round"
|
"Unable to execute $subaction for update $update. Will try on next round"
|
||||||
}
|
}
|
||||||
}.onSuccess {
|
}.onSuccess {
|
||||||
@@ -50,7 +50,7 @@ class CombinedSubcontextInitialAction(
|
|||||||
}
|
}
|
||||||
leftSubActions.removeAll(successSubActions)
|
leftSubActions.removeAll(successSubActions)
|
||||||
if (successSubActions.isEmpty()) {
|
if (successSubActions.isEmpty()) {
|
||||||
logger.error {
|
(Log ?: logger).error {
|
||||||
"Some SubActions have been unable to complete successfully:${leftSubActions.joinToString("\n") { it.toString() }}"
|
"Some SubActions have been unable to complete successfully:${leftSubActions.joinToString("\n") { it.toString() }}"
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
@@ -246,6 +246,7 @@ public abstract class dev/inmo/tgbotapi/bot/BaseRequestsExecutor : dev/inmo/tgbo
|
|||||||
|
|
||||||
public abstract interface class dev/inmo/tgbotapi/bot/RequestsExecutor : java/io/Closeable {
|
public abstract interface class dev/inmo/tgbotapi/bot/RequestsExecutor : java/io/Closeable {
|
||||||
public abstract fun execute (Ldev/inmo/tgbotapi/requests/abstracts/Request;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public abstract fun execute (Ldev/inmo/tgbotapi/requests/abstracts/Request;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
|
public abstract fun getLog ()Ldev/inmo/kslog/common/KSLog;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract class dev/inmo/tgbotapi/bot/exceptions/BotException : java/io/IOException, kotlinx/coroutines/CopyableThrowable {
|
public abstract class dev/inmo/tgbotapi/bot/exceptions/BotException : java/io/IOException, kotlinx/coroutines/CopyableThrowable {
|
||||||
@@ -416,6 +417,7 @@ public final class dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactoryKt
|
|||||||
public final class dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor : dev/inmo/tgbotapi/bot/BaseRequestsExecutor {
|
public final class dev/inmo/tgbotapi/bot/ktor/base/DefaultKtorRequestsExecutor : dev/inmo/tgbotapi/bot/BaseRequestsExecutor {
|
||||||
public fun close ()V
|
public fun close ()V
|
||||||
public fun execute (Ldev/inmo/tgbotapi/requests/abstracts/Request;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun execute (Ldev/inmo/tgbotapi/requests/abstracts/Request;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
|
public fun getLog ()Ldev/inmo/kslog/common/KSLog;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory : dev/inmo/tgbotapi/bot/ktor/KtorCallFactory {
|
public final class dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory : dev/inmo/tgbotapi/bot/ktor/KtorCallFactory {
|
||||||
@@ -439,6 +441,7 @@ public final class dev/inmo/tgbotapi/bot/ktor/base/MultipleClientKtorRequestsExe
|
|||||||
public fun <init> (Ldev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper;Ljava/util/List;ZLdev/inmo/tgbotapi/bot/settings/limiters/RequestLimiter;Lkotlinx/serialization/json/Json;Ldev/inmo/tgbotapi/bot/ktor/TelegramBotPipelinesHandler;ILdev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function0;)V
|
public fun <init> (Ldev/inmo/tgbotapi/utils/TelegramAPIUrlsKeeper;Ljava/util/List;ZLdev/inmo/tgbotapi/bot/settings/limiters/RequestLimiter;Lkotlinx/serialization/json/Json;Ldev/inmo/tgbotapi/bot/ktor/TelegramBotPipelinesHandler;ILdev/inmo/kslog/common/KSLog;Lkotlin/jvm/functions/Function0;)V
|
||||||
public fun close ()V
|
public fun close ()V
|
||||||
public fun execute (Ldev/inmo/tgbotapi/requests/abstracts/Request;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun execute (Ldev/inmo/tgbotapi/requests/abstracts/Request;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
|
public fun getLog ()Ldev/inmo/kslog/common/KSLog;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class dev/inmo/tgbotapi/bot/ktor/base/SimpleRequestCallFactory : dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory {
|
public final class dev/inmo/tgbotapi/bot/ktor/base/SimpleRequestCallFactory : dev/inmo/tgbotapi/bot/ktor/base/AbstractRequestCallFactory {
|
||||||
@@ -541,6 +544,7 @@ public final class dev/inmo/tgbotapi/bot/multiserver/SimpleMultiServerRequestsEx
|
|||||||
public synthetic fun <init> (Ljava/util/List;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
public synthetic fun <init> (Ljava/util/List;Lkotlin/jvm/functions/Function4;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
|
||||||
public fun close ()V
|
public fun close ()V
|
||||||
public fun execute (Ldev/inmo/tgbotapi/requests/abstracts/Request;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
public fun execute (Ldev/inmo/tgbotapi/requests/abstracts/Request;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
|
||||||
|
public fun getLog ()Ldev/inmo/kslog/common/KSLog;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final class dev/inmo/tgbotapi/bot/multiserver/SimpleMultiServerRequestsExecutor$Companion {
|
public final class dev/inmo/tgbotapi/bot/multiserver/SimpleMultiServerRequestsExecutor$Companion {
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.bot
|
package dev.inmo.tgbotapi.bot
|
||||||
|
|
||||||
|
import dev.inmo.kslog.common.KSLog
|
||||||
import dev.inmo.tgbotapi.requests.abstracts.Request
|
import dev.inmo.tgbotapi.requests.abstracts.Request
|
||||||
import io.ktor.utils.io.core.Closeable
|
import io.ktor.utils.io.core.Closeable
|
||||||
|
|
||||||
@@ -11,6 +12,8 @@ import io.ktor.utils.io.core.Closeable
|
|||||||
* @see dev.inmo.tgbotapi.bot.Ktor.KtorRequestsExecutor
|
* @see dev.inmo.tgbotapi.bot.Ktor.KtorRequestsExecutor
|
||||||
*/
|
*/
|
||||||
interface RequestsExecutor : Closeable {
|
interface RequestsExecutor : Closeable {
|
||||||
|
val Log: KSLog?
|
||||||
|
get() = null
|
||||||
/**
|
/**
|
||||||
* Unsafe execution of incoming [request]. Can throw almost any exception. So, it is better to use
|
* Unsafe execution of incoming [request]. Can throw almost any exception. So, it is better to use
|
||||||
* something like [dev.inmo.tgbotapi.extensions.utils.shortcuts.executeAsync] or
|
* something like [dev.inmo.tgbotapi.extensions.utils.shortcuts.executeAsync] or
|
||||||
|
@@ -29,6 +29,7 @@ expect class KtorRequestsExecutor internal constructor(
|
|||||||
logger: KSLog,
|
logger: KSLog,
|
||||||
diff: Unit // just a diff property to know where constructor and where calling function with defaults
|
diff: Unit // just a diff property to know where constructor and where calling function with defaults
|
||||||
) : BaseRequestsExecutor {
|
) : BaseRequestsExecutor {
|
||||||
|
override val Log: KSLog?
|
||||||
override suspend fun <T : Any> execute(request: Request<T>): T
|
override suspend fun <T : Any> execute(request: Request<T>): T
|
||||||
override fun close()
|
override fun close()
|
||||||
}
|
}
|
||||||
|
@@ -30,6 +30,7 @@ class DefaultKtorRequestsExecutor internal constructor(
|
|||||||
private val logger: KSLog,
|
private val logger: KSLog,
|
||||||
diff: Unit
|
diff: Unit
|
||||||
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
||||||
|
override val Log: KSLog? = logger
|
||||||
private val callsFactories: List<KtorCallFactory> = callsFactories.run {
|
private val callsFactories: List<KtorCallFactory> = callsFactories.run {
|
||||||
if (!excludeDefaultFactories) {
|
if (!excludeDefaultFactories) {
|
||||||
this@DefaultKtorRequestsExecutor.logger.v { "Installing default factories" }
|
this@DefaultKtorRequestsExecutor.logger.v { "Installing default factories" }
|
||||||
|
@@ -46,7 +46,7 @@ class MultipleClientKtorRequestsExecutor(
|
|||||||
jsonFormatter: Json,
|
jsonFormatter: Json,
|
||||||
pipelineStepsHolder: TelegramBotPipelinesHandler,
|
pipelineStepsHolder: TelegramBotPipelinesHandler,
|
||||||
requestExecutorsCount: Int,
|
requestExecutorsCount: Int,
|
||||||
logger: KSLog,
|
override val Log: KSLog,
|
||||||
clientFactory: () -> HttpClient,
|
clientFactory: () -> HttpClient,
|
||||||
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
) : BaseRequestsExecutor(telegramAPIUrlsKeeper) {
|
||||||
private val requestExecutors = (0 until requestExecutorsCount).map {
|
private val requestExecutors = (0 until requestExecutorsCount).map {
|
||||||
@@ -58,7 +58,7 @@ class MultipleClientKtorRequestsExecutor(
|
|||||||
requestsLimiter,
|
requestsLimiter,
|
||||||
jsonFormatter,
|
jsonFormatter,
|
||||||
pipelineStepsHolder,
|
pipelineStepsHolder,
|
||||||
logger,
|
Log,
|
||||||
Unit
|
Unit
|
||||||
)
|
)
|
||||||
}.toSet()
|
}.toSet()
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.bot.multiserver
|
package dev.inmo.tgbotapi.bot.multiserver
|
||||||
|
|
||||||
|
import dev.inmo.kslog.common.KSLog
|
||||||
import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutorBuilder
|
import dev.inmo.tgbotapi.bot.ktor.KtorRequestsExecutorBuilder
|
||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
import dev.inmo.tgbotapi.bot.RequestsExecutor
|
||||||
@@ -29,6 +30,8 @@ class SimpleMultiServerRequestsExecutor(
|
|||||||
bots.forEach(TelegramBot::close)
|
bots.forEach(TelegramBot::close)
|
||||||
}
|
}
|
||||||
) : RequestsExecutor {
|
) : RequestsExecutor {
|
||||||
|
override val Log: KSLog?
|
||||||
|
get() = bots.firstNotNullOfOrNull { it.Log }
|
||||||
override suspend fun <T : Any> execute(request: Request<T>): T {
|
override suspend fun <T : Any> execute(request: Request<T>): T {
|
||||||
var currentBot = bots.botSelector(-1, null)
|
var currentBot = bots.botSelector(-1, null)
|
||||||
while (currentCoroutineContext().isActive) {
|
while (currentCoroutineContext().isActive) {
|
||||||
|
@@ -33,7 +33,7 @@ fun TelegramBot.longPollingFlow(
|
|||||||
mediaGroupsDebounceTimeMillis: Long? = 1000L,
|
mediaGroupsDebounceTimeMillis: Long? = 1000L,
|
||||||
): Flow<Update> = channelFlow {
|
): Flow<Update> = channelFlow {
|
||||||
if (autoDisableWebhooks) {
|
if (autoDisableWebhooks) {
|
||||||
runCatchingSafely {
|
runCatching {
|
||||||
execute(DeleteWebhook())
|
execute(DeleteWebhook())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -72,6 +72,13 @@ fun TelegramBot.longPollingFlow(
|
|||||||
} else {
|
} else {
|
||||||
{ originalUpdates: List<Update> ->
|
{ originalUpdates: List<Update> ->
|
||||||
val converted = originalUpdates.convertWithMediaGroupUpdates()
|
val converted = originalUpdates.convertWithMediaGroupUpdates()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dirty hack for cases when the media group was retrieved not fully:
|
||||||
|
*
|
||||||
|
* We are throw out the last media group and will reretrieve it again in the next get updates
|
||||||
|
* and it will guarantee that it is full
|
||||||
|
*/
|
||||||
/**
|
/**
|
||||||
* Dirty hack for cases when the media group was retrieved not fully:
|
* Dirty hack for cases when the media group was retrieved not fully:
|
||||||
*
|
*
|
||||||
@@ -80,14 +87,14 @@ fun TelegramBot.longPollingFlow(
|
|||||||
*/
|
*/
|
||||||
val updates = if (
|
val updates = if (
|
||||||
originalUpdates.size == getUpdatesLimit.last
|
originalUpdates.size == getUpdatesLimit.last
|
||||||
&& ((converted.last() as? BaseSentMessageUpdate) ?.data as? CommonMessage<*>) ?.content is MediaGroupContent<*>
|
&& ((converted.last() as? BaseSentMessageUpdate)?.data as? CommonMessage<*>)?.content is MediaGroupContent<*>
|
||||||
) {
|
) {
|
||||||
converted - converted.last()
|
converted - converted.last()
|
||||||
} else {
|
} else {
|
||||||
converted
|
converted
|
||||||
}
|
}
|
||||||
|
|
||||||
safelyWithResult {
|
runCatching {
|
||||||
for (update in updates) {
|
for (update in updates) {
|
||||||
send(update)
|
send(update)
|
||||||
|
|
||||||
@@ -96,25 +103,16 @@ fun TelegramBot.longPollingFlow(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
cancel(it as? CancellationException ?: return@onFailure)
|
if (it is CancellationException) {
|
||||||
|
cancel(it)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
withContext(contextToWork) {
|
withContext(contextToWork) {
|
||||||
while (isActive) {
|
while (isActive) {
|
||||||
safely(
|
runCatching {
|
||||||
{ e ->
|
|
||||||
val isHttpRequestTimeoutException = e is HttpRequestTimeoutException || (e is CommonBotException && e.cause is HttpRequestTimeoutException)
|
|
||||||
if (isHttpRequestTimeoutException && autoSkipTimeoutExceptions) {
|
|
||||||
return@safely
|
|
||||||
}
|
|
||||||
exceptionsHandler ?.invoke(e)
|
|
||||||
if (e is RequestException) {
|
|
||||||
delay(1000L)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
execute(
|
execute(
|
||||||
GetUpdates(
|
GetUpdates(
|
||||||
offset = lastUpdateIdentifier?.plus(1),
|
offset = lastUpdateIdentifier?.plus(1),
|
||||||
@@ -124,6 +122,16 @@ fun TelegramBot.longPollingFlow(
|
|||||||
).let { originalUpdates ->
|
).let { originalUpdates ->
|
||||||
updatesHandler(originalUpdates)
|
updatesHandler(originalUpdates)
|
||||||
}
|
}
|
||||||
|
}.onFailure { e ->
|
||||||
|
val isHttpRequestTimeoutException =
|
||||||
|
e is HttpRequestTimeoutException || (e is CommonBotException && e.cause is HttpRequestTimeoutException)
|
||||||
|
if (isHttpRequestTimeoutException && autoSkipTimeoutExceptions) {
|
||||||
|
return@onFailure
|
||||||
|
}
|
||||||
|
exceptionsHandler?.invoke(e)
|
||||||
|
if (e is RequestException) {
|
||||||
|
delay(1000L)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user