mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-22 22:46:39 +01:00
Move ctrlc handler enable function to DispatcherBuilder
This helps with consistency -- every setting is changed in builder. Also `Self -> Self` function sometimes plays more nicely with borrowck.
This commit is contained in:
parent
92abc22613
commit
dd4af30727
15 changed files with 76 additions and 43 deletions
|
@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
- Security checks based on `secret_token` param of `set_webhook` to built-in webhooks.
|
- Security checks based on `secret_token` param of `set_webhook` to built-in webhooks.
|
||||||
- `dispatching::update_listeners::{PollingBuilder, Polling, PollingStream}`.
|
- `dispatching::update_listeners::{PollingBuilder, Polling, PollingStream}`.
|
||||||
|
- `DispatcherBuilder::enable_ctrlc_handler` method.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
@ -27,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
### Deprecated
|
### Deprecated
|
||||||
|
|
||||||
- The `dispatching::update_listeners::polling` function.
|
- The `dispatching::update_listeners::polling` function.
|
||||||
|
- `Dispatcher::setup_ctrlc_handler` method.
|
||||||
|
|
||||||
## 0.9.2 - 2022-06-07
|
## 0.9.2 - 2022-06-07
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,9 @@ Some places now use `FileMeta` instead of `File`, you may need to change types.
|
||||||
### teloxide
|
### teloxide
|
||||||
|
|
||||||
Teloxide itself doesn't have any major API changes.
|
Teloxide itself doesn't have any major API changes.
|
||||||
Note however that `dispatching::update_listeners::polling` function was deprecated, use `polling_builder` instead.
|
Note however that some function were deprecated:
|
||||||
|
- Instead of `dispatching::update_listeners::polling` use `polling_builder`
|
||||||
|
- Instead of `Dispatcher::setup_ctrlc_handler` use `DispatcherBuilder::enable_ctrlc_handler`
|
||||||
|
|
||||||
## 0.7 -> 0.8
|
## 0.7 -> 0.8
|
||||||
|
|
||||||
|
|
|
@ -223,8 +223,8 @@ async fn main() {
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
||||||
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch()
|
.dispatch()
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
.branch(Update::filter_callback_query().endpoint(callback_handler))
|
.branch(Update::filter_callback_query().endpoint(callback_handler))
|
||||||
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
|
.branch(Update::filter_inline_query().endpoint(inline_query_handler));
|
||||||
|
|
||||||
Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
|
Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -54,8 +54,8 @@ async fn main() {
|
||||||
|
|
||||||
Dispatcher::builder(bot, handler)
|
Dispatcher::builder(bot, handler)
|
||||||
.dependencies(dptree::deps![storage])
|
.dependencies(dptree::deps![storage])
|
||||||
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch()
|
.dispatch()
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,8 +51,8 @@ async fn main() {
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
||||||
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch()
|
.dispatch()
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -87,8 +87,8 @@ async fn main() {
|
||||||
.error_handler(LoggingErrorHandler::with_custom_text(
|
.error_handler(LoggingErrorHandler::with_custom_text(
|
||||||
"An error has occurred in the dispatcher",
|
"An error has occurred in the dispatcher",
|
||||||
))
|
))
|
||||||
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch()
|
.dispatch()
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,5 +60,5 @@ async fn main() {
|
||||||
},
|
},
|
||||||
));
|
));
|
||||||
|
|
||||||
Dispatcher::builder(bot, handler).build().setup_ctrlc_handler().dispatch().await;
|
Dispatcher::builder(bot, handler).enable_ctrlc_handler().build().dispatch().await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,8 +52,8 @@ async fn main() {
|
||||||
|
|
||||||
Dispatcher::builder(bot, schema())
|
Dispatcher::builder(bot, schema())
|
||||||
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
.dependencies(dptree::deps![InMemStorage::<State>::new()])
|
||||||
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch()
|
.dispatch()
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,8 @@ async fn main() {
|
||||||
Dispatcher::builder(bot, handler)
|
Dispatcher::builder(bot, handler)
|
||||||
// Pass the shared state to the handler as a dependency.
|
// Pass the shared state to the handler as a dependency.
|
||||||
.dependencies(dptree::deps![messages_total])
|
.dependencies(dptree::deps![messages_total])
|
||||||
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch()
|
.dispatch()
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
|
|
@ -140,8 +140,8 @@
|
||||||
//!
|
//!
|
||||||
//! Dispatcher::builder(bot, schema())
|
//! Dispatcher::builder(bot, schema())
|
||||||
//! .dependencies(dptree::deps![InMemStorage::<State>::new()])
|
//! .dependencies(dptree::deps![InMemStorage::<State>::new()])
|
||||||
|
//! .enable_ctrlc_handler()
|
||||||
//! .build()
|
//! .build()
|
||||||
//! .setup_ctrlc_handler()
|
|
||||||
//! .dispatch()
|
//! .dispatch()
|
||||||
//! .await;
|
//! .await;
|
||||||
//! }
|
//! }
|
||||||
|
|
|
@ -33,6 +33,7 @@ pub struct DispatcherBuilder<R, Err, Key> {
|
||||||
handler: Arc<UpdateHandler<Err>>,
|
handler: Arc<UpdateHandler<Err>>,
|
||||||
default_handler: DefaultHandler,
|
default_handler: DefaultHandler,
|
||||||
error_handler: Arc<dyn ErrorHandler<Err> + Send + Sync>,
|
error_handler: Arc<dyn ErrorHandler<Err> + Send + Sync>,
|
||||||
|
ctrlc_handler: bool,
|
||||||
distribution_f: fn(&Update) -> Option<Key>,
|
distribution_f: fn(&Update) -> Option<Key>,
|
||||||
worker_queue_size: usize,
|
worker_queue_size: usize,
|
||||||
}
|
}
|
||||||
|
@ -78,6 +79,14 @@ where
|
||||||
Self { dependencies, ..self }
|
Self { dependencies, ..self }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Enables the `^C` handler that [`shutdown`]s dispatching.
|
||||||
|
///
|
||||||
|
/// [`shutdown`]: ShutdownToken::shutdown
|
||||||
|
#[cfg(feature = "ctrlc_handler")]
|
||||||
|
pub fn enable_ctrlc_handler(self) -> Self {
|
||||||
|
Self { ctrlc_handler: true, ..self }
|
||||||
|
}
|
||||||
|
|
||||||
/// Specifies size of the queue for workers.
|
/// Specifies size of the queue for workers.
|
||||||
///
|
///
|
||||||
/// By default it's 64.
|
/// By default it's 64.
|
||||||
|
@ -101,6 +110,7 @@ where
|
||||||
handler,
|
handler,
|
||||||
default_handler,
|
default_handler,
|
||||||
error_handler,
|
error_handler,
|
||||||
|
ctrlc_handler,
|
||||||
distribution_f: _,
|
distribution_f: _,
|
||||||
worker_queue_size,
|
worker_queue_size,
|
||||||
} = self;
|
} = self;
|
||||||
|
@ -111,6 +121,7 @@ where
|
||||||
handler,
|
handler,
|
||||||
default_handler,
|
default_handler,
|
||||||
error_handler,
|
error_handler,
|
||||||
|
ctrlc_handler,
|
||||||
distribution_f: f,
|
distribution_f: f,
|
||||||
worker_queue_size,
|
worker_queue_size,
|
||||||
}
|
}
|
||||||
|
@ -127,9 +138,10 @@ where
|
||||||
error_handler,
|
error_handler,
|
||||||
distribution_f,
|
distribution_f,
|
||||||
worker_queue_size,
|
worker_queue_size,
|
||||||
|
ctrlc_handler,
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
Dispatcher {
|
let dp = Dispatcher {
|
||||||
bot,
|
bot,
|
||||||
dependencies,
|
dependencies,
|
||||||
handler,
|
handler,
|
||||||
|
@ -142,8 +154,19 @@ where
|
||||||
default_worker: None,
|
default_worker: None,
|
||||||
current_number_of_active_workers: Default::default(),
|
current_number_of_active_workers: Default::default(),
|
||||||
max_number_of_active_workers: Default::default(),
|
max_number_of_active_workers: Default::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
#[cfg(feature = "ctrlc_handler")]
|
||||||
|
{
|
||||||
|
if ctrlc_handler {
|
||||||
|
let mut dp = dp;
|
||||||
|
dp.setup_ctrlc_handler_inner();
|
||||||
|
return dp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dp
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The base for update dispatching.
|
/// The base for update dispatching.
|
||||||
|
@ -212,6 +235,7 @@ where
|
||||||
Box::pin(async {})
|
Box::pin(async {})
|
||||||
}),
|
}),
|
||||||
error_handler: LoggingErrorHandler::new(),
|
error_handler: LoggingErrorHandler::new(),
|
||||||
|
ctrlc_handler: false,
|
||||||
worker_queue_size: DEFAULT_WORKER_QUEUE_SIZE,
|
worker_queue_size: DEFAULT_WORKER_QUEUE_SIZE,
|
||||||
distribution_f: default_distribution_function,
|
distribution_f: default_distribution_function,
|
||||||
}
|
}
|
||||||
|
@ -238,7 +262,6 @@ where
|
||||||
/// - [`crate::types::Me`] (can be used in [`HandlerExt::filter_command`]).
|
/// - [`crate::types::Me`] (can be used in [`HandlerExt::filter_command`]).
|
||||||
///
|
///
|
||||||
/// [`shutdown`]: ShutdownToken::shutdown
|
/// [`shutdown`]: ShutdownToken::shutdown
|
||||||
/// [a ctrlc signal]: Dispatcher::setup_ctrlc_handler
|
|
||||||
/// [`HandlerExt::filter_command`]: crate::dispatching::HandlerExt::filter_command
|
/// [`HandlerExt::filter_command`]: crate::dispatching::HandlerExt::filter_command
|
||||||
pub async fn dispatch(&mut self)
|
pub async fn dispatch(&mut self)
|
||||||
where
|
where
|
||||||
|
@ -258,7 +281,6 @@ where
|
||||||
/// This method adds the same dependencies as [`Dispatcher::dispatch`].
|
/// This method adds the same dependencies as [`Dispatcher::dispatch`].
|
||||||
///
|
///
|
||||||
/// [`shutdown`]: ShutdownToken::shutdown
|
/// [`shutdown`]: ShutdownToken::shutdown
|
||||||
/// [a ctrlc signal]: Dispatcher::setup_ctrlc_handler
|
|
||||||
pub async fn dispatch_with_listener<'a, UListener, ListenerE, Eh>(
|
pub async fn dispatch_with_listener<'a, UListener, ListenerE, Eh>(
|
||||||
&'a mut self,
|
&'a mut self,
|
||||||
mut update_listener: UListener,
|
mut update_listener: UListener,
|
||||||
|
@ -425,7 +447,22 @@ where
|
||||||
///
|
///
|
||||||
/// [`shutdown`]: ShutdownToken::shutdown
|
/// [`shutdown`]: ShutdownToken::shutdown
|
||||||
#[cfg(feature = "ctrlc_handler")]
|
#[cfg(feature = "ctrlc_handler")]
|
||||||
|
#[deprecated(since = "0.10", note = "use `enable_ctrlc_handler` on builder instead")]
|
||||||
pub fn setup_ctrlc_handler(&mut self) -> &mut Self {
|
pub fn setup_ctrlc_handler(&mut self) -> &mut Self {
|
||||||
|
self.setup_ctrlc_handler_inner();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a shutdown token, which can later be used to shutdown
|
||||||
|
/// dispatching.
|
||||||
|
pub fn shutdown_token(&self) -> ShutdownToken {
|
||||||
|
self.state.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R, Err, Key> Dispatcher<R, Err, Key> {
|
||||||
|
#[cfg(feature = "ctrlc_handler")]
|
||||||
|
fn setup_ctrlc_handler_inner(&mut self) {
|
||||||
let token = self.state.clone();
|
let token = self.state.clone();
|
||||||
tokio::spawn(async move {
|
tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
|
@ -443,14 +480,6 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns a shutdown token, which can later be used to shutdown
|
|
||||||
/// dispatching.
|
|
||||||
pub fn shutdown_token(&self) -> ShutdownToken {
|
|
||||||
self.state.clone()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -96,8 +96,8 @@ pub async fn commands_repl_with_listener<'a, R, Cmd, H, L, ListenerE, E, Args>(
|
||||||
Update::filter_message().filter_command::<Cmd>().chain(dptree::endpoint(handler)),
|
Update::filter_message().filter_command::<Cmd>().chain(dptree::endpoint(handler)),
|
||||||
)
|
)
|
||||||
.default_handler(ignore_update)
|
.default_handler(ignore_update)
|
||||||
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch_with_listener(
|
.dispatch_with_listener(
|
||||||
listener,
|
listener,
|
||||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
||||||
|
|
|
@ -71,8 +71,8 @@ where
|
||||||
|
|
||||||
Dispatcher::builder(bot, Update::filter_message().chain(dptree::endpoint(handler)))
|
Dispatcher::builder(bot, Update::filter_message().chain(dptree::endpoint(handler)))
|
||||||
.default_handler(ignore_update)
|
.default_handler(ignore_update)
|
||||||
|
.enable_ctrlc_handler()
|
||||||
.build()
|
.build()
|
||||||
.setup_ctrlc_handler()
|
|
||||||
.dispatch_with_listener(
|
.dispatch_with_listener(
|
||||||
listener,
|
listener,
|
||||||
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
LoggingErrorHandler::with_custom_text("An error from the update listener"),
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
## Cargo features
|
## Cargo features
|
||||||
|
|
||||||
| Feature | Description |
|
| Feature | Description |
|
||||||
|----------------------|------------------------------------------------------------------------------------|
|
|----------------------|--------------------------------------------------------------------------------------------|
|
||||||
| `webhooks` | Enables general webhook utilities (almost useless on its own) |
|
| `webhooks` | Enables general webhook utilities (almost useless on its own) |
|
||||||
| `webhooks-axum` | Enables webhook implementation based on axum framework |
|
| `webhooks-axum` | Enables webhook implementation based on axum framework |
|
||||||
| `macros` | Re-exports macros from [`teloxide-macros`]. |
|
| `macros` | Re-exports macros from [`teloxide-macros`]. |
|
||||||
| `ctrlc_handler` | Enables the [`Dispatcher::setup_ctrlc_handler`] function (**enabled by default**). |
|
| `ctrlc_handler` | Enables the [`DispatcherBuilder::enable_ctrlc_handler`] function (**enabled by default**). |
|
||||||
| `auto-send` | Enables the [`AutoSend`](adaptors::AutoSend) bot adaptor (**enabled by default**). |
|
| `auto-send` | Enables the [`AutoSend`](adaptors::AutoSend) bot adaptor (**enabled by default**). |
|
||||||
| `throttle` | Enables the [`Throttle`](adaptors::Throttle) bot adaptor. |
|
| `throttle` | Enables the [`Throttle`](adaptors::Throttle) bot adaptor. |
|
||||||
| `cache-me` | Enables the [`CacheMe`](adaptors::CacheMe) bot adaptor. |
|
| `cache-me` | Enables the [`CacheMe`](adaptors::CacheMe) bot adaptor. |
|
||||||
|
@ -31,4 +31,4 @@
|
||||||
[`teloxide::utils::UpState`]: utils::UpState
|
[`teloxide::utils::UpState`]: utils::UpState
|
||||||
[teloxide-core features]: https://docs.rs/teloxide-core/latest/teloxide_core/#cargo-features
|
[teloxide-core features]: https://docs.rs/teloxide-core/latest/teloxide_core/#cargo-features
|
||||||
|
|
||||||
[`Dispatcher::setup_ctrlc_handler`]: dispatching::Dispatcher::setup_ctrlc_handler
|
[`DispatcherBuilder::enable_ctrlc_handler`]: dispatching::DispatcherBuilder::enable_ctrlc_handler
|
Loading…
Reference in a new issue