mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: add non critical spawn with graceful shutdown signal fn (#5962)
This commit is contained in:
@ -507,6 +507,41 @@ impl TaskExecutor {
|
|||||||
|
|
||||||
self.handle.spawn(task)
|
self.handle.spawn(task)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This spawns a regular task onto the runtime.
|
||||||
|
///
|
||||||
|
/// The [TaskManager] will wait until the given future has completed before shutting down.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```no_run
|
||||||
|
/// # async fn t(executor: reth_tasks::TaskExecutor) {
|
||||||
|
///
|
||||||
|
/// executor.spawn_with_graceful_shutdown_signal(|shutdown| async move {
|
||||||
|
/// // await the shutdown signal
|
||||||
|
/// let guard = shutdown.await;
|
||||||
|
/// // do work before exiting the program
|
||||||
|
/// tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||||
|
/// // allow graceful shutdown
|
||||||
|
/// drop(guard);
|
||||||
|
/// });
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
pub fn spawn_with_graceful_shutdown_signal<F>(
|
||||||
|
&self,
|
||||||
|
f: impl FnOnce(GracefulShutdown) -> F,
|
||||||
|
) -> JoinHandle<()>
|
||||||
|
where
|
||||||
|
F: Future<Output = ()> + Send + 'static,
|
||||||
|
{
|
||||||
|
let on_shutdown = GracefulShutdown::new(
|
||||||
|
self.on_shutdown.clone(),
|
||||||
|
GracefulShutdownGuard::new(Arc::clone(&self.graceful_tasks)),
|
||||||
|
);
|
||||||
|
let fut = f(on_shutdown);
|
||||||
|
|
||||||
|
self.handle.spawn(fut)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TaskSpawner for TaskExecutor {
|
impl TaskSpawner for TaskExecutor {
|
||||||
@ -546,6 +581,16 @@ pub trait TaskSpawnerExt: Send + Sync + Unpin + std::fmt::Debug + DynClone {
|
|||||||
) -> JoinHandle<()>
|
) -> JoinHandle<()>
|
||||||
where
|
where
|
||||||
F: Future<Output = ()> + Send + 'static;
|
F: Future<Output = ()> + Send + 'static;
|
||||||
|
|
||||||
|
/// This spawns a regular task onto the runtime.
|
||||||
|
///
|
||||||
|
/// The [TaskManager] will wait until the given future has completed before shutting down.
|
||||||
|
fn spawn_with_graceful_shutdown_signal<F>(
|
||||||
|
&self,
|
||||||
|
f: impl FnOnce(GracefulShutdown) -> F,
|
||||||
|
) -> JoinHandle<()>
|
||||||
|
where
|
||||||
|
F: Future<Output = ()> + Send + 'static;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TaskSpawnerExt for TaskExecutor {
|
impl TaskSpawnerExt for TaskExecutor {
|
||||||
@ -559,6 +604,16 @@ impl TaskSpawnerExt for TaskExecutor {
|
|||||||
{
|
{
|
||||||
TaskExecutor::spawn_critical_with_graceful_shutdown_signal(self, name, f)
|
TaskExecutor::spawn_critical_with_graceful_shutdown_signal(self, name, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_with_graceful_shutdown_signal<F>(
|
||||||
|
&self,
|
||||||
|
f: impl FnOnce(GracefulShutdown) -> F,
|
||||||
|
) -> JoinHandle<()>
|
||||||
|
where
|
||||||
|
F: Future<Output = ()> + Send + 'static,
|
||||||
|
{
|
||||||
|
TaskExecutor::spawn_with_graceful_shutdown_signal(self, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Determines how a task is spawned
|
/// Determines how a task is spawned
|
||||||
|
|||||||
Reference in New Issue
Block a user