pub struct Job<T> { /* private fields */ }Expand description
Job.
An object that provides asynchronous response data, and it may be canceled before the underlying task was done.
thread unsafe
§Examples
use tsubakuro_rust_core::prelude::*;
async fn example(client: &SqlClient, transaction: &Transaction) -> Result<(), TgError> {
let sql = "insert into tb values(1, 'abc')";
let mut job = client.execute_async(transaction, sql).await?;
let execute_result = job.take_for(std::time::Duration::from_secs(10)).await?;
Ok(())
}Implementations§
Source§impl<T> Job<T>
impl<T> Job<T>
Sourcepub fn set_default_timeout(&mut self, timeout: Duration)
pub fn set_default_timeout(&mut self, timeout: Duration)
Set default timeout.
Sourcepub async fn wait(&mut self, timeout: Duration) -> Result<bool, TgError>
pub async fn wait(&mut self, timeout: Duration) -> Result<bool, TgError>
Wait for response.
§Returns
Ok(true)- Response received.Ok(false)- Timed out.
§Examples
use tsubakuro_rust_core::prelude::*;
async fn example(mut job: Job<SqlExecuteResult>) -> Result<(), TgError> {
let done = job.wait(std::time::Duration::from_secs(10)).await?;
if done {
let execute_result = job.take().await?;
}
Ok(())
}Sourcepub async fn is_done(&mut self) -> Result<bool, TgError>
pub async fn is_done(&mut self) -> Result<bool, TgError>
Whether a response has been received.
§Returns
Ok(true)- Response received.Ok(false)- No response received.
§Examples
use tsubakuro_rust_core::prelude::*;
async fn example(mut job: Job<SqlExecuteResult>) -> Result<(), TgError> {
loop {
let done = job.is_done().await?;
if done {
let execute_result = job.take().await?;
break;
}
}
Ok(())
}Sourcepub async fn take(&mut self) -> Result<T, TgError>
pub async fn take(&mut self) -> Result<T, TgError>
Retrieves the result value, or wait until response has been received.
You can only take once to retrieve the value.
§Examples
use tsubakuro_rust_core::prelude::*;
async fn example(mut job: Job<SqlExecuteResult>) -> Result<(), TgError> {
let execute_result = job.take().await?;
Ok(())
}Sourcepub async fn take_for(&mut self, timeout: Duration) -> Result<T, TgError>
pub async fn take_for(&mut self, timeout: Duration) -> Result<T, TgError>
Retrieves the result value, or wait until response has been received.
You can only take once to retrieve the value.
§Examples
use tsubakuro_rust_core::prelude::*;
async fn example(mut job: Job<SqlExecuteResult>) -> Result<(), TgError> {
let execute_result = job.take_for(std::time::Duration::from_secs(10)).await?;
Ok(())
}Sourcepub async fn take_if_ready(&mut self) -> Result<Option<T>, TgError>
pub async fn take_if_ready(&mut self) -> Result<Option<T>, TgError>
Retrieves the result value if a response has been received.
You can only take once to retrieve the value.
§Returns
Ok(Some(value))- result valueOk(None)- No response received.
§Examples
use tsubakuro_rust_core::prelude::*;
async fn example(mut job: Job<SqlExecuteResult>) -> Result<(), TgError> {
loop {
if let Some(execute_result) = job.take_if_ready().await? {
break;
}
}
Ok(())
}Sourcepub async fn cancel(self) -> Result<bool, TgError>
pub async fn cancel(self) -> Result<bool, TgError>
Cancel job.
§Returns
Ok(true)- Response received.Ok(false)- Timed out.
The response is not necessarily OPERATION_CANCELED. Depending on the timing, it may be a normal processing result.
§Examples
use tsubakuro_rust_core::prelude::*;
async fn example(mut job: Job<SqlExecuteResult>) -> Result<(), TgError> {
job.cancel().await?;
Ok(())
}Sourcepub async fn cancel_for(self, timeout: Duration) -> Result<bool, TgError>
pub async fn cancel_for(self, timeout: Duration) -> Result<bool, TgError>
Cancel job.
§Returns
Ok(true)- Response already received, or cancel already started.Ok(false)- Timed out.
The response is not necessarily OPERATION_CANCELED. Depending on the timing, it may be a normal processing result.
Sourcepub async fn cancel_async(self) -> Result<Option<CancelJob>, TgError>
pub async fn cancel_async(self) -> Result<Option<CancelJob>, TgError>
Cancel job.
§Returns
Ok(Some(CancelJob))- Cancellation started.Ok(None)- Not canceled. (response already received, or cancel already started)
§Examples
use tsubakuro_rust_core::prelude::*;
async fn example(mut job: Job<SqlExecuteResult>) -> Result<(), TgError> {
if let Some(mut cancel_job) = job.cancel_async().await? {
cancel_job.wait(std::time::Duration::from_secs(10)).await?;
}
Ok(())
}