use crate::{
durable::{DurableOrdered, BINDING_DAP_LEADER_AGG_JOB_QUEUE},
int_err,
};
use worker::*;
pub(crate) const DURABLE_LEADER_AGG_JOB_QUEUE_PUT: &str = "/internal/do/agg_job_queue/put";
pub(crate) const DURABLE_LEADER_AGG_JOB_QUEUE_GET: &str = "/internal/do/agg_job_queue/get";
pub(crate) const DURABLE_LEADER_AGG_JOB_QUEUE_FINISH: &str = "/internal/do/agg_job_queue/finish";
#[durable_object]
pub struct LeaderAggregationJobQueue {
#[allow(dead_code)]
state: State,
env: Env,
touched: bool,
}
#[durable_object]
impl DurableObject for LeaderAggregationJobQueue {
fn new(state: State, env: Env) -> Self {
Self {
state,
env,
touched: false,
}
}
async fn fetch(&mut self, mut req: Request) -> Result<Response> {
let id_hex = self.state.id().to_string();
ensure_garbage_collected!(req, self, id_hex, BINDING_DAP_LEADER_AGG_JOB_QUEUE);
match (req.path().as_ref(), req.method()) {
(DURABLE_LEADER_AGG_JOB_QUEUE_PUT, Method::Post) => {
let agg_job: DurableOrdered<String> = req.json().await?;
agg_job.put(&self.state).await?;
console_debug!(
"LeaderAggregationJobQueue: {} has been scheduled",
agg_job.as_ref(),
);
Response::from_json(&())
}
(DURABLE_LEADER_AGG_JOB_QUEUE_GET, Method::Post) => {
let max_agg_jobs: usize = req.json().await?;
let res: Vec<String> =
DurableOrdered::get_front(&self.state, "agg_job", max_agg_jobs)
.await?
.into_iter()
.map(|agg_job| agg_job.into_item())
.collect();
console_debug!("agg job queue: {:?}", res);
Response::from_json(&res)
}
(DURABLE_LEADER_AGG_JOB_QUEUE_FINISH, Method::Post) => {
let agg_job: DurableOrdered<String> = req.json().await?;
agg_job.delete(&self.state).await?;
Response::from_json(&())
}
_ => Err(int_err(format!(
"LeaderAggregationJobQueue: unexpected request: method={:?}; path={:?}",
req.method(),
req.path()
))),
}
}
}