1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
pub use celery_codegen::task;
#[doc(hidden)]
#[macro_export]
macro_rules! __app_internal {
(
$broker_type:ty { $broker_url:expr },
[ $( $t:ty ),* ],
[ $( $pattern:expr => $queue:expr ),* ],
$( $x:ident = $y:expr, )*
) => {{
async fn _build_app(mut builder: $crate::CeleryBuilder) ->
$crate::export::Result<$crate::export::Arc<$crate::Celery>> {
let celery: $crate::Celery = builder.build().await?;
$(
celery.register_task::<$t>().await?;
)*
Ok($crate::export::Arc::new(celery))
}
let broker_url = $broker_url;
let mut builder = $crate::CeleryBuilder::new("celery", &broker_url);
$(
builder = builder.$x($y);
)*
$(
builder = builder.task_route($pattern, $queue);
)*
_build_app(builder)
}};
}
#[doc(hidden)]
#[macro_export]
macro_rules! __beat_internal {
(
$broker_type:ty { $broker_url:expr },
$scheduler_backend_type:ty { $scheduler_backend:expr },
[
$( $task_name:expr => {
$task_type:ty,
$schedule:expr,
( $( $task_arg:expr ),* $(,)?)
} ),*
],
[ $( $pattern:expr => $queue:expr ),* ],
$( $x:ident = $y:expr, )*
) => {{
async fn _build_beat(mut builder: $crate::beat::BeatBuilder::<$scheduler_backend_type>) ->
$crate::export::BeatResult<$crate::beat::Beat::<$scheduler_backend_type>> {
let mut beat = builder.build().await?;
$(
beat.schedule_named_task($task_name.to_string(), <$task_type>::new( $( $task_arg ),* ), $schedule);
)*
Ok(beat)
}
let broker_url = $broker_url;
let mut builder = $crate::beat::Beat::<$scheduler_backend_type>::custom_builder("beat", &broker_url, $scheduler_backend);
$(
builder = builder.$x($y);
)*
$(
builder = builder.task_route($pattern, $queue);
)*
_build_beat(builder)
}};
}
/// A macro for creating a [`Celery`](struct.Celery.html) app.
///
/// At a minimum the `app!` macro requires these 3 arguments (in order):
/// - `broker`: a broker type (currently only AMQP is supported) with an expression for the broker URL in brackets,
/// - `tasks`: a list of tasks to register, and
/// - `task_routes`: a list of routing rules in the form of `pattern => queue`.
///
/// # Optional parameters
///
/// Following the task routing rules there are a number of other optional parameters that
/// may appear in arbitrary order (all of which correspond to a method on the
/// [`CeleryBuilder`](struct.CeleryBuilder.html) struct):
///
/// - `default_queue`: Set the
/// [`CeleryBuilder::default_queue`](struct.CeleryBuilder.html#method.default_queue).
/// - `prefetch_count`: Set the [`CeleryBuilder::prefect_count`](struct.CeleryBuilder.html#method.prefect_count).
/// - `heartbeat`: Set the [`CeleryBuilder::heartbeat`](struct.CeleryBuilder.html#method.heartbeat).
/// - `task_time_limit`: Set an app-level [`TaskOptions::time_limit`](task/struct.TaskOptions.html#structfield.time_limit).
/// - `task_hard_time_limit`: Set an app-level [`TaskOptions::hard_time_limit`](task/struct.TaskOptions.html#structfield.hard_time_limit).
/// - `task_max_retries`: Set an app-level [`TaskOptions::max_retries`](task/struct.TaskOptions.html#structfield.max_retries).
/// - `task_min_retry_delay`: Set an app-level [`TaskOptions::min_retry_delay`](task/struct.TaskOptions.html#structfield.min_retry_delay).
/// - `task_max_retry_delay`: Set an app-level [`TaskOptions::max_retry_delay`](task/struct.TaskOptions.html#structfield.max_retry_delay).
/// - `task_retry_for_unexpected`: Set an app-level [`TaskOptions::retry_for_unexpected`](task/struct.TaskOptions.html#structfield.retry_for_unexpected).
/// - `acks_late`: Set an app-level [`TaskOptions::acks_late`](task/struct.TaskOptions.html#structfield.acks_late).
/// - `broker_connection_timeout`: Set the
/// [`CeleryBuilder::broker_connection_timeout`](struct.CeleryBuilder.html#method.broker_connection_timeout).
/// - `broker_connection_retry`: Set the
/// [`CeleryBuilder::broker_connection_retry`](struct.CeleryBuilder.html#method.broker_connection_retry).
/// - `broker_connection_max_retries`: Set the
/// [`CeleryBuilder::broker_connection_max_retries`](struct.CeleryBuilder.html#method.broker_connection_max_retries).
///
/// # Examples
///
/// ```rust,no_run
/// # #[macro_use] extern crate celery;
/// # use anyhow::Result;
/// use celery::prelude::*;
///
/// #[celery::task]
/// fn add(x: i32, y: i32) -> TaskResult<i32> {
/// Ok(x + y)
/// }
///
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let app = celery::app!(
/// broker = AMQPBroker { std::env::var("AMQP_ADDR").unwrap() },
/// tasks = [ add ],
/// task_routes = [ "*" => "celery" ],
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// ```rust,no_run
/// # #[macro_use] extern crate celery;
/// # use anyhow::Result;
/// # use celery::prelude::*;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let app = celery::app!(
/// broker = AMQPBroker { std::env::var("AMQP_ADDR").unwrap() },
/// tasks = [],
/// task_routes = [],
/// task_time_limit = 2
/// ).await?;
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! app {
(
broker = $broker_type:ty { $broker_url:expr },
tasks = [ $( $t:ty ),* $(,)? ],
task_routes = [ $( $pattern:expr => $queue:expr ),* $(,)? ]
$(, $x:ident = $y:expr )* $(,)?
) => {
$crate::__app_internal!(
$broker_type { $broker_url },
[ $( $t ),* ],
[ $( $pattern => $queue ),* ],
$( $x = $y, )*
);
};
}
/// A macro for creating a [`Beat`](beat/struct.Beat.html) app.
///
/// At a minimum the `beat!` macro requires these 3 arguments (in order):
/// - `broker`: a broker type (currently only AMQP is supported) with an expression for the broker URL in brackets,
/// - `tasks`: a list of tasks together with their relative schedules (can be empty),
/// - `task_routes`: a list of routing rules in the form of `pattern => queue`.
///
/// # Tasks
///
/// An entry in the task list has the following components:
/// - The name of the task,
/// - The instance of the task to execute,
/// - The task schedule, which can be one of the provided schedules (e.g., [`CronSchedule`](crate::beat::CronSchedule))
/// or any other struct that implements [`Schedule`](crate::beat::Schedule),
/// - A list of arguments for the task in the form of a comma-separated list surrounded by parenthesis.
///
/// # Custom scheduler backend
///
/// A custom scheduler backend can be given as the second argument.
/// If not given, the default [`LocalSchedulerBackend`](struct.LocalSchedulerBackend.html) will be used.
///
/// # Optional parameters
///
/// A number of other optional parameters can be passed as last arguments and in arbitrary order
/// (all of which correspond to a method on the [`BeatBuilder`](beat/struct.BeatBuilder.html) struct):
///
/// - `default_queue`: Set the
/// [`BeatBuilder::default_queue`](beat/struct.BeatBuilder.html#method.default_queue).
/// - `heartbeat`: Set the [`BeatBuilder::heartbeat`](beat/struct.BeatBuilder.html#method.heartbeat).
/// - `broker_connection_timeout`: Set the
/// [`BeatBuilder::broker_connection_timeout`](beat/struct.BeatBuilder.html#method.broker_connection_timeout).
/// - `broker_connection_retry`: Set the
/// [`BeatBuilder::broker_connection_retry`](beat/struct.BeatBuilder.html#method.broker_connection_retry).
/// - `broker_connection_max_retries`: Set the
/// [`BeatBuilder::broker_connection_max_retries`](beat/struct.BeatBuilder.html#method.broker_connection_max_retries).
///
/// # Examples
///
/// Create a `beat` which will send all messages to the `celery` queue:
///
/// ```rust,no_run
/// # #[macro_use] extern crate celery;
/// # use anyhow::Result;
/// # use celery::prelude::*;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let beat = celery::beat!(
/// broker = AMQPBroker{ std::env::var("AMQP_ADDR").unwrap() },
/// tasks = [],
/// task_routes = [ "*" => "celery" ],
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// Create a `beat` with a scheduled task:
///
/// ```rust,no_run
/// # #[macro_use] extern crate celery;
/// # use anyhow::Result;
/// # use celery::prelude::*;
/// # use celery::beat::CronSchedule;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// #[celery::task]
/// fn add(x: i32, y: i32) -> TaskResult<i32> {
/// // It is enough to provide the implementation to the worker,
/// // the beat does not need it.
/// unimplemented!()
/// }
///
/// let beat = celery::beat!(
/// broker = AMQPBroker{ std::env::var("AMQP_ADDR").unwrap() },
/// tasks = [
/// "add_task" => {
/// add,
/// schedule = CronSchedule::from_string("*/3 * * * mon-fri")?,
/// args = (1, 2)
/// }
/// ],
/// task_routes = [ "*" => "celery" ],
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// Create a `beat` with optional parameters:
///
/// ```rust,no_run
/// # #[macro_use] extern crate celery;
/// # use anyhow::Result;
/// # use celery::prelude::*;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let beat = celery::beat!(
/// broker = AMQPBroker { std::env::var("AMQP_ADDR").unwrap() },
/// tasks = [],
/// task_routes = [],
/// default_queue = "beat_queue"
/// ).await?;
/// # Ok(())
/// # }
/// ```
///
/// Create a `beat` with a custom scheduler backend:
///
/// ```rust,no_run
/// # #[macro_use] extern crate celery;
/// # use anyhow::Result;
/// use celery::prelude::*;
/// use celery::beat::*;
/// use std::collections::BinaryHeap;
///
/// struct CustomSchedulerBackend {}
///
/// impl SchedulerBackend for CustomSchedulerBackend {
/// fn should_sync(&self) -> bool {
/// unimplemented!()
/// }
///
/// fn sync(&mut self, scheduled_tasks: &mut BinaryHeap<ScheduledTask>) -> Result<(), BeatError> {
/// unimplemented!()
/// }
/// }
///
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let beat = celery::beat!(
/// broker = AMQPBroker { std::env::var("AMQP_ADDR").unwrap() },
/// scheduler_backend = CustomSchedulerBackend { CustomSchedulerBackend {} },
/// tasks = [],
/// task_routes = [
/// "*" => "beat_queue",
/// ],
/// ).await?;
/// # Ok(())
/// # }
/// ```
#[macro_export]
macro_rules! beat {
(
broker = $broker_type:ty { $broker_url:expr },
tasks = [
$( $task_name:expr => {
$task_type:ty,
schedule = $schedule:expr,
args = $args:tt $(,)?
} ),* $(,)?
],
task_routes = [ $( $pattern:expr => $queue:expr ),* $(,)? ]
$(, $x:ident = $y:expr )* $(,)?
) => {
$crate::__beat_internal!(
$broker_type { $broker_url },
$crate::beat::LocalSchedulerBackend { $crate::beat::LocalSchedulerBackend::new() },
[ $ (
$task_name => {
$task_type,
$schedule,
$args
}
),* ],
[ $( $pattern => $queue ),* ],
$( $x = $y, )*
);
};
(
broker = $broker_type:ty { $broker_url:expr },
scheduler_backend = $scheduler_backend_type:ty { $scheduler_backend:expr },
tasks = [
$( $task_name:expr => {
$task_type:ty,
schedule = $schedule:expr,
args = $args:tt $(,)?
} ),* $(,)?
],
task_routes = [ $( $pattern:expr => $queue:expr ),* $(,)? ]
$(, $x:ident = $y:expr )* $(,)?
) => {
$crate::__beat_internal!(
$broker_type { $broker_url },
$scheduler_backend_type { $scheduler_backend },
[ $ (
$task_name => {
$task_type,
$schedule,
$args
}
),* ],
[ $( $pattern => $queue ),* ],
$( $x = $y, )*
);
};
}