-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
518 additions
and
134 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "devolutions-gateway-task" | ||
version = "0.0.0" | ||
authors = ["Devolutions Inc. <infos@devolutions.net>"] | ||
edition = "2021" | ||
publish = false | ||
|
||
[features] | ||
default = [] | ||
named_tasks = ["tokio/tracing"] | ||
|
||
[dependencies] | ||
tokio = { version = "1.28.1", features = ["sync", "rt", "tracing"] } | ||
async-trait = "0.1.68" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use std::future::Future; | ||
|
||
use async_trait::async_trait; | ||
use tokio::task::JoinHandle; | ||
|
||
#[derive(Debug)] | ||
pub struct ShutdownHandle(tokio::sync::watch::Sender<()>); | ||
|
||
impl ShutdownHandle { | ||
pub fn new() -> (Self, ShutdownSignal) { | ||
let (sender, receiver) = tokio::sync::watch::channel(()); | ||
(Self(sender), ShutdownSignal(receiver)) | ||
} | ||
|
||
pub fn signal(&self) { | ||
let _ = self.0.send(()); | ||
} | ||
|
||
pub async fn all_closed(&self) { | ||
self.0.closed().await; | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct ShutdownSignal(tokio::sync::watch::Receiver<()>); | ||
|
||
impl ShutdownSignal { | ||
pub async fn wait(&mut self) { | ||
let _ = self.0.changed().await; | ||
} | ||
} | ||
|
||
/// Aborts the running task when dropped. | ||
/// Also see https://github.com/tokio-rs/tokio/issues/1830 for some background. | ||
#[must_use] | ||
pub struct ChildTask<T>(JoinHandle<T>); | ||
|
||
impl<T> ChildTask<T> { | ||
pub fn spawn<F>(future: F) -> Self | ||
where | ||
F: Future<Output = T> + Send + 'static, | ||
T: Send + 'static, | ||
{ | ||
ChildTask(tokio::task::spawn(future)) | ||
} | ||
|
||
pub async fn join(mut self) -> Result<T, tokio::task::JoinError> { | ||
(&mut self.0).await | ||
} | ||
|
||
/// Immediately abort the task | ||
pub fn abort(&self) { | ||
self.0.abort() | ||
} | ||
|
||
/// Drop without aborting the task | ||
pub fn detach(self) { | ||
core::mem::forget(self); | ||
} | ||
} | ||
|
||
impl<T> From<JoinHandle<T>> for ChildTask<T> { | ||
fn from(value: JoinHandle<T>) -> Self { | ||
Self(value) | ||
} | ||
} | ||
|
||
impl<T> Drop for ChildTask<T> { | ||
fn drop(&mut self) { | ||
self.abort(); | ||
} | ||
} | ||
|
||
#[async_trait] | ||
pub trait Task { | ||
type Output: Send; | ||
|
||
const NAME: &'static str; | ||
|
||
async fn run(self, shutdown_signal: ShutdownSignal) -> Self::Output; | ||
} | ||
|
||
pub fn spawn_task<T>(task: T, shutdown_signal: ShutdownSignal) -> ChildTask<T::Output> | ||
where | ||
T: Task + 'static, | ||
{ | ||
let task_fut = task.run(shutdown_signal); | ||
let handle = spawn_task_impl(task_fut, T::NAME); | ||
ChildTask(handle) | ||
} | ||
|
||
#[cfg(not(all(feature = "named_tasks", tokio_unstable)))] | ||
#[track_caller] | ||
fn spawn_task_impl<T>(future: T, _name: &str) -> JoinHandle<T::Output> | ||
where | ||
T: Future + Send + 'static, | ||
T::Output: Send + 'static, | ||
{ | ||
tokio::task::spawn(future) | ||
} | ||
|
||
#[cfg(all(feature = "named_tasks", tokio_unstable))] | ||
#[track_caller] | ||
fn spawn_task_impl<T>(future: T, name: &str) -> JoinHandle<T::Output> | ||
where | ||
T: Future + Send + 'static, | ||
T::Output: Send + 'static, | ||
{ | ||
// NOTE: To enable this code, this crate must be built using a command similar to: | ||
// > RUSTFLAGS="--cfg tokio_unstable" cargo check --features named_tasks | ||
|
||
// NOTE: unwrap because as of now (tokio 1.28), this never returns an error, | ||
// and production build never enable tokio-console instrumentation anyway (unstable). | ||
tokio::task::Builder::new().name(name).spawn(future).unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
// TODO: extract this module into another crate | ||
|
||
mod push_files; | ||
mod sogar_auth; | ||
mod sogar_token; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.