-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Internalizing jobs and renaming it to tasks
This commit is in preparation of the scheduled jobs system. My original goal was to reuse this code, but as I thought about how I was going to implement it, it will be built atop the higher-level features of PubSub and Collections/KeyValue to allow persistence. So this internalizes it, and implements a simple task system for handling incoming requests in the server separately.
- Loading branch information
Showing
17 changed files
with
123 additions
and
144 deletions.
There are no files selected for viewing
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 was deleted.
Oops, something went wrong.
This file was deleted.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::{fmt::Debug, sync::Arc}; | ||
|
||
use tokio::sync::oneshot; | ||
|
||
/// he `Id` of an executing task. | ||
#[derive(Debug, Hash, Eq, PartialEq, Clone, Copy)] | ||
pub struct Id(pub(crate) u64); | ||
|
||
/// References a background task. | ||
#[derive(Debug)] | ||
pub struct Handle<T, E> { | ||
/// The task's id. | ||
pub id: Id, | ||
|
||
pub(crate) receiver: oneshot::Receiver<Result<T, Arc<E>>>, | ||
} | ||
|
||
impl<T, E> Handle<T, E> | ||
where | ||
T: Send + Sync + 'static, | ||
E: Send + Sync + 'static, | ||
{ | ||
/// Waits for the job to complete and returns the result. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns an error if the job is cancelled. | ||
pub async fn receive( | ||
self, | ||
) -> Result<Result<T, Arc<E>>, tokio::sync::oneshot::error::RecvError> { | ||
self.receiver.await | ||
} | ||
|
||
// /// Tries to receive the status of the job. If available, it is returned. | ||
// /// This function will not block. | ||
// /// | ||
// /// # Errors | ||
// /// | ||
// /// Returns an error if the job isn't complete. | ||
// /// | ||
// /// * [`TryRecvError::Disconnected`](flume::TryRecvError::Disconnected): The job has been cancelled. | ||
// /// * [`TryRecvError::Empty`](flume::TryRecvError::Empty): The job has not completed yet. | ||
#[cfg(test)] | ||
pub fn try_receive( | ||
&mut self, | ||
) -> Result<Result<T, Arc<E>>, tokio::sync::oneshot::error::TryRecvError> { | ||
self.receiver.try_recv() | ||
} | ||
} |
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
File renamed without changes.
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.