#[task]Expand description
A procedural macro for generating a Task from a function.
If the annotated function has a return value, the return value must be a
TaskResult<R>.
§Parameters
name: The name to use when registering the task. Should be unique. If not given the name will be set to the name of the function being decorated.time_limit: Set a task-levelTaskOptions::time_limit.hard_time_limit: Set a task-levelTaskOptions::hard_time_limit.max_retries: Set a task-levelTaskOptions::max_retries.min_retry_delay: Set a task-levelTaskOptions::min_retry_delay.max_retry_delay: Set a task-levelTaskOptions::max_retry_delay.retry_for_unexpected: Set a task-levelTaskOptions::retry_for_unexpected.acks_late: Set a task-levelTaskOptions::acks_late.content_type: Set a task-levelTaskOptions::content_type.bind: A bool. If true, the task will be run like an instance method and so the function’s first argument should be a reference toSelf. Note however that Rust won’t allow you to call the argumentself. Instead, you could usetaskor justt.on_failure: An async callback function to run when the task fails. Should accept a reference to a task instance and a reference to aTaskError.on_success: An async callback function to run when the task succeeds. Should accept a reference to a task instance and a reference to the value returned by the task.
For more information see the tasks chapter in the Rusty Celery Book.
§Examples
Create a task named add with all of the default options:
use celery::prelude::*;
#[celery::task]
fn add(x: i32, y: i32) -> TaskResult<i32> {
Ok(x + y)
}Use a name different from the function name:
#[celery::task(name = "sum")]
fn add(x: i32, y: i32) -> TaskResult<i32> {
Ok(x + y)
}Customize the default retry behavior:
#[celery::task(
time_limit = 3,
max_retries = 100,
min_retry_delay = 1,
max_retry_delay = 60,
)]
async fn io_task() -> TaskResult<()> {
// Do some async IO work that could possible fail, such as an HTTP request...
Ok(())
}Bind the function to the task instance so it runs like an instance method:
#[celery::task(bind = true)]
fn bound_task(task: &Self) {
println!("Hello, World! From {}", task.name());
}Run custom callbacks on failure and on success:
#[celery::task(on_failure = failure_callback, on_success = success_callback)]
fn task_with_callbacks() {}
async fn failure_callback<T: Task>(task: &T, err: &TaskError) {
println!("{} failed with {:?}", task.name(), err);
}
async fn success_callback<T: Task>(task: &T, ret: &T::Returns) {
println!("{} succeeded: {:?}", task.name(), ret);
}