mirror of
https://github.com/TheFunny/TelegramTwitterMediaBot.git
synced 2026-09-23 23:32:05 +00:00
send: reuse one process-wide Bot for queue workers
handle_task and dead_letter_notify built a fresh Bot (env parse + HTTP client) per queue item. A single LazyLock<Bot> is forced at startup so a missing TELOXIDE_TOKEN fails fast instead of on the first task.
This commit is contained in:
@@ -43,6 +43,9 @@ async fn main() {
|
|||||||
log::info!("Starting bot");
|
log::info!("Starting bot");
|
||||||
|
|
||||||
let bot = Bot::from_env();
|
let bot = Bot::from_env();
|
||||||
|
// Force the queue workers' shared Bot to initialize now so a missing
|
||||||
|
// token fails at startup, not on the first queued task.
|
||||||
|
let _ = &*send::BOT;
|
||||||
|
|
||||||
// Register the command list with Telegram (client `/` menu).
|
// Register the command list with Telegram (client `/` menu).
|
||||||
if let Err(e) = handlers::register_commands(&bot).await {
|
if let Err(e) = handlers::register_commands(&bot).await {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use crate::state::{EditMessage, unix_now};
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::sync::LazyLock;
|
||||||
use teloxide::prelude::*;
|
use teloxide::prelude::*;
|
||||||
use teloxide::types::{
|
use teloxide::types::{
|
||||||
ChatId, InlineKeyboardButton, InlineKeyboardMarkup, InputFile, InputMedia, InputMediaAnimation,
|
ChatId, InlineKeyboardButton, InlineKeyboardMarkup, InputFile, InputMedia, InputMediaAnimation,
|
||||||
@@ -20,6 +21,11 @@ use teloxide::{ApiError, RequestError};
|
|||||||
use tempfile::NamedTempFile;
|
use tempfile::NamedTempFile;
|
||||||
use x_media::site::FetchError;
|
use x_media::site::FetchError;
|
||||||
|
|
||||||
|
/// One process-wide Bot for queue workers. Building a fresh Bot (and its HTTP
|
||||||
|
/// client) per queue task was pure waste; forced at startup in main so a
|
||||||
|
/// missing token fails fast instead of on the first task.
|
||||||
|
pub static BOT: LazyLock<Bot> = LazyLock::new(Bot::from_env);
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||||
pub enum MediaItemPayload {
|
pub enum MediaItemPayload {
|
||||||
@@ -1158,7 +1164,7 @@ pub async fn handle_task(payload: serde_json::Value) -> Result<(), QueueError> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
let bot = Bot::from_env();
|
let bot = BOT.clone();
|
||||||
// A resumed multi-batch send already ran post_send_actions (edit prompt /
|
// A resumed multi-batch send already ran post_send_actions (edit prompt /
|
||||||
// forward) when it first started; running them again on the resume would
|
// forward) when it first started; running them again on the resume would
|
||||||
// open a duplicate edit prompt and double-forward. SendAnimation is
|
// open a duplicate edit prompt and double-forward. SendAnimation is
|
||||||
@@ -1227,7 +1233,7 @@ pub async fn dead_letter_notify(payload: serde_json::Value, message: String) {
|
|||||||
let notify_chat_id = payload.get("notify_chat_id").and_then(|v| v.as_i64());
|
let notify_chat_id = payload.get("notify_chat_id").and_then(|v| v.as_i64());
|
||||||
let notify_message_id = payload.get("notify_message_id").and_then(|v| v.as_i64());
|
let notify_message_id = payload.get("notify_message_id").and_then(|v| v.as_i64());
|
||||||
if notify_chat_id.is_some() {
|
if notify_chat_id.is_some() {
|
||||||
let bot = Bot::from_env();
|
let bot = BOT.clone();
|
||||||
notify_failure(
|
notify_failure(
|
||||||
&bot,
|
&bot,
|
||||||
notify_chat_id,
|
notify_chat_id,
|
||||||
|
|||||||
Reference in New Issue
Block a user