1use std::future::Future;
2use std::pin::Pin;
3use worker::MessageBatch;
4
5use crate::job::{Job, JobExt};
6
7pub type QueueMessage = worker::Message<String>;
9
10pub trait QueueHandler: Send + Sync + 'static {
15 fn handle(
16 &self,
17 batch: MessageBatch<String>,
18 ) -> Pin<Box<dyn Future<Output = worker::Result<()>> + Send + 'static>>;
19}
20
21impl<F, Fut> QueueHandler for F
23where
24 F: Fn(MessageBatch<String>) -> Fut + Send + Sync + 'static,
25 Fut: Future<Output = worker::Result<()>> + Send + 'static,
26{
27 fn handle(
28 &self,
29 batch: MessageBatch<String>,
30 ) -> Pin<Box<dyn Future<Output = worker::Result<()>> + Send + 'static>> {
31 Box::pin((self)(batch))
32 }
33}
34
35#[derive(Clone)]
42pub struct Queue {
43 env: worker::Env,
44}
45
46impl Queue {
47 pub fn new(env: worker::Env) -> Self {
49 Self { env }
50 }
51
52 pub fn dispatch<'a, T: Job + serde::Serialize + 'a>(
59 &'a self,
60 job: T,
61 ) -> crate::send::SendFuture<impl std::future::Future<Output = crate::Result<()>> + 'a> {
62 crate::send::SendFuture::new(async move { job.dispatch(&self.env).await })
63 }
64
65 pub fn raw(&self, binding: &str) -> crate::Result<worker::Queue> {
69 let resolved = crate::resolve_binding(binding);
70 self.env.queue(&resolved).map_err(crate::Error)
71 }
72
73 pub fn env(&self) -> &worker::Env {
75 &self.env
76 }
77}