mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-22 11:04:21 +00:00
Allow running multiple tasks at once, and bail as soon as the first one crashes
This commit is contained in:
parent
ade4973fc4
commit
4d7e196d13
3 changed files with 33 additions and 13 deletions
28
src/main.rs
28
src/main.rs
|
@ -1,4 +1,5 @@
|
||||||
use anyhow::{Result};
|
use anyhow::{Result};
|
||||||
|
use crate::telegram::DispatchWithResult;
|
||||||
|
|
||||||
pub(crate) mod database;
|
pub(crate) mod database;
|
||||||
mod telegram;
|
mod telegram;
|
||||||
|
@ -6,16 +7,21 @@ mod telegram;
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
// Logging setup
|
// Logging setup
|
||||||
{
|
pretty_env_logger::init();
|
||||||
pretty_env_logger::init();
|
log::debug!("Logging initialized successfully!");
|
||||||
log::info!("Logging initialized successfully!");
|
|
||||||
}
|
|
||||||
// Telegram setup
|
|
||||||
{
|
|
||||||
log::trace!("Setting up Telegram bot...");
|
|
||||||
let mut dispatcher = telegram::dispatcher();
|
|
||||||
dispatcher.dispatch().await
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
// Telegram setup
|
||||||
|
log::trace!("Setting up Telegram bot dispatcher...");
|
||||||
|
let mut telegram_dispatcher = telegram::dispatcher();
|
||||||
|
let telegram_awaitable = telegram_dispatcher.dispatch_with_result();
|
||||||
|
|
||||||
|
// Run all services concurrently
|
||||||
|
log::info!("Starting services...");
|
||||||
|
tokio::try_join![
|
||||||
|
telegram_awaitable,
|
||||||
|
];
|
||||||
|
|
||||||
|
// This should never happen, but just in case...
|
||||||
|
log::error!("A service has exited, bailing out...");
|
||||||
|
anyhow::bail!("A service has exited.")
|
||||||
}
|
}
|
||||||
|
|
|
@ -105,7 +105,6 @@ pub fn dispatcher(bot: Bot) -> Dispatcher<Bot, Error, DefaultKey> {
|
||||||
.dependencies(
|
.dependencies(
|
||||||
dptree::deps![] // No deps needed at the moment.
|
dptree::deps![] // No deps needed at the moment.
|
||||||
)
|
)
|
||||||
.enable_ctrlc_handler()
|
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
use anyhow::Error;
|
use std::convert::Infallible;
|
||||||
|
use anyhow::{Error, Result};
|
||||||
use teloxide::Bot;
|
use teloxide::Bot;
|
||||||
use teloxide::dispatching::{DefaultKey, Dispatcher};
|
use teloxide::dispatching::{DefaultKey, Dispatcher};
|
||||||
|
|
||||||
|
@ -12,3 +13,17 @@ pub fn dispatcher() -> Dispatcher<Bot, Error, DefaultKey> {
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait DispatchWithResult {
|
||||||
|
async fn dispatch_with_result(&mut self) -> Result<Infallible>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DispatchWithResult for Dispatcher<Bot, Error, DefaultKey> {
|
||||||
|
async fn dispatch_with_result(&mut self) -> Result<Infallible> {
|
||||||
|
log::info!("Starting Telegram dispatcher...");
|
||||||
|
self.dispatch().await;
|
||||||
|
log::error!("Telegram dispatcher has exited, bailing out...");
|
||||||
|
|
||||||
|
anyhow::bail!("Telegram dispatcher has exited.")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue