use std::time::SystemTime;
use rusqlite::{Connection, OptionalExtension, Row, params};
use serde::de::DeserializeOwned;
use super::rows::due_of;
use super::state::QueueState;
use super::values;
use crate::clock::from_unix_millis;
use crate::sqlite::{TextBytes, prefix_end, query_cached, sql_error};
use crate::{Error, Result};
const MAX_AHEAD: usize = 10_000;
pub const PAGE_JOBS: usize = 1000;
const DEFAULT_PAGE: usize = 100;
const PAGE_BYTES: usize = 4 << 20;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum State {
Scheduled,
Waiting,
Running,
Done,
Failed,
Cancelled,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Job<V> {
pub id: Option<String>,
pub value: V,
pub state: State,
pub at: SystemTime,
pub attempt: u32,
pub ahead: u32,
pub progress: Option<serde_json::Value>,
pub error: Option<String>,
pub group: Option<String>,
pub repeat: Option<String>,
pub last_run: Option<LastRun>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct LastRun {
pub started_at: SystemTime,
pub ended_at: SystemTime,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Filter {
prefix: String,
state: Option<State>,
limit: Option<usize>,
after: Option<String>,
}
impl Filter {
pub fn prefix(prefix: impl Into<String>) -> Filter {
Filter { prefix: prefix.into(), ..Filter::default() }
}
pub fn state(mut self, state: State) -> Filter {
self.state = Some(state);
self
}
pub fn limit(mut self, jobs: usize) -> Filter {
self.limit = Some(jobs);
self
}
pub fn after(mut self, next: impl Into<String>) -> Filter {
self.after = Some(next.into());
self
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Page<V> {
pub jobs: Vec<Job<V>>,
pub next: Option<String>,
}
#[derive(Debug)]
struct Found {
key: Option<String>,
time: i64,
id: i64,
at: i64,
attempt: i64,
leased: Option<i64>,
table: Table,
repeat: Option<String>,
error: Option<String>,
value: Option<Vec<u8>>,
spill: Option<i64>,
size: usize,
ran: Option<i64>,
took: Option<i64>,
group: Option<String>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Table {
Jobs,
Failed,
Done,
}
macro_rules! keyed_columns {
() => {
"j.key, j.next, j.id, j.at, j.attempt, l.attempt, 0, j.repeat, j.error, j.value, j.spill,
coalesce(length(j.value), (select length(s.value) from _tinystore_jobs_spilled s where s.id = j.spill), 0),
j.ran, j.took, j.grp
from _tinystore_jobs_keys k join _tinystore_jobs j on j.queue = k.queue and j.next = k.next and j.id = k.id
left join _tinystore_jobs_leases l on l.id = j.id and l.until > ?9"
};
}
macro_rules! failed_columns {
() => {
"f.key, f.failed, f.id, f.at, f.attempt, null, 1, null, f.error, f.value, f.spill,
coalesce(length(f.value), (select length(s.value) from _tinystore_jobs_spilled s where s.id = f.spill), 0),
f.ran, f.took, f.grp
from _tinystore_jobs_failed f"
};
}
const GET_JOB: &str = concat!("select ", keyed_columns!(), " where k.queue = ?1 and k.key = ?2");
const GET_FAILED: &str = concat!("select ", failed_columns!(), " where f.queue = ?1 and f.key = ?2");
const GET_DONE: &str = "select d.key, d.until, 0, d.at, d.attempt, null, 2, null, null, d.value, d.spill,
coalesce(length(d.value), (select length(s.value) from _tinystore_jobs_spilled s where s.id = d.spill), 0),
d.ran, d.took, d.grp
from _tinystore_jobs_done d where d.queue = ?1 and d.key = ?2 and d.until > ?3";
const SPILLED: &str = "select value from _tinystore_jobs_spilled where id = ?1";
const ROWS_BEFORE: &str = "select count(*) from (select 1 from _tinystore_jobs
where queue = ?1 and (next, id) < (?2, ?3) limit cast(?4 as integer))";
const PAGE_BY_KEY: &str = concat!(
"select * from (
select ",
keyed_columns!(),
" where k.queue = ?1 and k.key > ?2 and k.key >= ?3 and (?4 is null or k.key < ?4) and ?5 in (0, 1)
union all
select ",
failed_columns!(),
" where f.queue = ?1 and f.key > ?2 and f.key >= ?3 and (?4 is null or f.key < ?4) and ?5 in (0, 2)
) order by 1 limit cast(?6 as integer)"
);
const PAGE_FAILED: &str = concat!(
"select ",
failed_columns!(),
" where f.queue = ?1 and (f.failed, f.id) < (?7, ?8) order by f.failed desc, f.id desc limit cast(?6 as integer)"
);
pub(crate) fn get<V: DeserializeOwned>(
c: &Connection,
queue: &QueueState,
key: &str,
now: i64,
) -> Result<Option<Job<V>>> {
let no = Option::<i64>::None;
let mut found = one(c, GET_JOB, params![queue.id, key, no, no, no, no, no, no, now])?;
if found.is_none() {
found = one(c, GET_FAILED, params![queue.id, key])?;
}
if found.is_none() {
found = one(c, GET_DONE, params![queue.id, key, now])?;
}
let Some(found) = found else {
return Ok(None);
};
let mut job = job_of::<V>(c, queue, &found, now)?;
if job.state == State::Waiting {
job.ahead = ahead(c, queue, &found, now)?;
}
Ok(Some(job))
}
pub(crate) fn page<V: DeserializeOwned>(
c: &Connection,
queue: &QueueState,
filter: &Filter,
now: i64,
) -> Result<Page<V>> {
let limit = filter.limit.unwrap_or(DEFAULT_PAGE);
if limit == 0 || limit > PAGE_JOBS {
return Err(Error::invalid(format!("a page of {limit} jobs, not 1 to {PAGE_JOBS}")));
}
if filter.state.is_some_and(|state| matches!(state, State::Done | State::Cancelled)) {
return Err(Error::invalid("a page lists scheduled, waiting, running or failed jobs"));
}
let found = rows(c, queue, filter, limit + 1, now)?;
let mut page = Page { jobs: Vec::new(), next: None };
let (mut bytes, mut read, mut last) = (0, 0, None);
for (at, found) in found.iter().enumerate() {
if at == limit || at > 0 && bytes + found.size > PAGE_BYTES {
break;
}
read += 1;
bytes += found.size;
last = Some(cursor_of(filter, found));
let job = job_of::<V>(c, queue, found, now).map_err(|error| match &found.key {
Some(key) => error.within(format!("id {key:?}")),
None => error,
})?;
if filter.state.is_none_or(|state| state == job.state) {
page.jobs.push(job);
}
}
if read < found.len() {
page.next = last;
}
Ok(page)
}
fn rows(c: &Connection, queue: &QueueState, filter: &Filter, limit: usize, now: i64) -> Result<Vec<Found>> {
let limit = limit as i64;
if filter.prefix.is_empty() && filter.state == Some(State::Failed) {
let (before, id) = failed_cursor(filter.after.as_deref())?;
let no = Option::<i64>::None;
return all(c, PAGE_FAILED, params![queue.id, no, no, no, no, limit, before, id]);
}
let tables = match filter.state {
None => 0,
Some(State::Failed) => 2,
Some(_) => 1,
};
let after = filter.after.as_deref().unwrap_or("");
let end = prefix_end(&filter.prefix);
let end = end.as_deref().map(TextBytes);
all(c, PAGE_BY_KEY, params![queue.id, after, filter.prefix, end, tables, limit, None::<i64>, None::<i64>, now])
}
fn cursor_of(filter: &Filter, found: &Found) -> String {
if filter.prefix.is_empty() && filter.state == Some(State::Failed) {
return format!("{}.{}", found.time, found.id);
}
found.key.clone().unwrap_or_default()
}
fn failed_cursor(after: Option<&str>) -> Result<(i64, i64)> {
let Some(after) = after else {
return Ok((i64::MAX, i64::MAX));
};
let parsed = after.split_once('.').and_then(|(failed, id)| Some((failed.parse().ok()?, id.parse().ok()?)));
parsed.ok_or_else(|| Error::invalid(format!("a cursor of {after:?}")))
}
fn one(c: &Connection, sql: &str, params: impl rusqlite::Params) -> Result<Option<Found>> {
c.prepare_cached(sql)
.and_then(|mut select| select.query_row(params, found).optional())
.map_err(|error| sql_error("a job", error))
}
fn all(c: &Connection, sql: &str, params: impl rusqlite::Params) -> Result<Vec<Found>> {
query_cached(c, (sql, "a page of jobs"), params, found)
}
fn found(row: &Row<'_>) -> rusqlite::Result<Found> {
let table = match row.get::<_, i64>(6)? {
1 => Table::Failed,
2 => Table::Done,
_ => Table::Jobs,
};
Ok(Found {
key: row.get(0)?,
time: row.get(1)?,
id: row.get(2)?,
at: row.get(3)?,
attempt: row.get(4)?,
leased: row.get(5)?,
table,
repeat: row.get(7)?,
error: row.get(8)?,
value: row.get(9)?,
spill: row.get(10)?,
size: usize::try_from(row.get::<_, i64>(11)?).unwrap_or(0),
ran: row.get(12)?,
took: row.get(13)?,
group: row.get(14)?,
})
}
fn job_of<V: DeserializeOwned>(c: &Connection, queue: &QueueState, found: &Found, now: i64) -> Result<Job<V>> {
let held = found.leased.and_then(|_| queue.held(found.id)).filter(|lease| lease.started() && !lease.settled());
let (state, at) = match found.table {
Table::Failed => (State::Failed, found.at),
Table::Done => (State::Done, found.at),
Table::Jobs if held.is_some() => (State::Running, found.at),
Table::Jobs if due_of(found.time) > now => (State::Scheduled, due_of(found.time)),
Table::Jobs => (State::Waiting, due_of(found.time)),
};
let attempt = match (&held, found.leased) {
(Some(_), Some(leased)) => leased,
_ => found.attempt,
};
let encoded = match found.spill {
Some(spill) => c
.prepare_cached(SPILLED)
.and_then(|mut select| select.query_row([spill], |row| row.get::<_, Vec<u8>>(0)))
.map_err(|error| sql_error("the value a job spilled", error))?,
None => found.value.clone().unwrap_or_default(),
};
let value = values::decode::<V>(&encoded).map_err(|error| Error::corrupt("a job's value").with_source(error))?;
let last_run = match (found.ran, found.took) {
(Some(ran), Some(took)) => {
Some(LastRun { started_at: from_unix_millis(ran), ended_at: from_unix_millis(ran.saturating_add(took)) })
}
_ => None,
};
Ok(Job {
id: found.key.clone(),
value,
state,
at: from_unix_millis(at),
attempt: u32::try_from(attempt).unwrap_or(u32::MAX),
ahead: 0,
progress: held.and_then(|lease| lease.progress()),
error: found.error.clone(),
group: found.group.clone(),
repeat: found.repeat.clone(),
last_run,
})
}
fn ahead(c: &Connection, queue: &QueueState, found: &Found, now: i64) -> Result<u32> {
if found.leased.is_some() {
return Ok(0); }
let running = queue.running_before(found.time, found.id, now);
let rows: i64 = c
.prepare_cached(ROWS_BEFORE)
.and_then(|mut select| {
select.query_row(params![queue.id, found.time, found.id, (MAX_AHEAD + running) as i64], |row| row.get(0))
})
.map_err(|error| sql_error("the jobs ahead of a job", error))?;
let ahead = usize::try_from(rows).unwrap_or(0).saturating_sub(running).min(MAX_AHEAD);
Ok(u32::try_from(ahead).unwrap_or(u32::MAX))
}