Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,103 @@
|
||||
//! System Job 的完整持久合同参考;共用基础队列操作,不链接 Dever 运行库。
|
||||
use crate::base_queue;
|
||||
pub(crate) use base_queue::{Batch, Payload, Submit, enqueue, insert, transition};
|
||||
use base_queue::{Result, SCHEMA, TARGET, identifier, now};
|
||||
use deadpool_sqlite::rusqlite::{Connection, OptionalExtension, TransactionBehavior, params};
|
||||
use serde::Deserialize;
|
||||
|
||||
pub(crate) struct Claim {
|
||||
value: base_queue::Claim,
|
||||
target: String,
|
||||
schema: String,
|
||||
execution: String,
|
||||
tenant: i64,
|
||||
retries: i64,
|
||||
timeout_ms: i64,
|
||||
}
|
||||
impl std::ops::Deref for Claim {
|
||||
type Target = base_queue::Claim;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.value
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct Rejected(String);
|
||||
impl std::fmt::Display for Rejected {
|
||||
fn fmt(&self, out: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
out.write_str(&self.0)
|
||||
}
|
||||
}
|
||||
impl std::error::Error for Rejected {}
|
||||
fn rejected(cause: impl std::fmt::Display) -> Box<dyn std::error::Error + Send + Sync> {
|
||||
Box::new(Rejected(cause.to_string()))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(tag = "kind", deny_unknown_fields)]
|
||||
enum Execution {
|
||||
System { tenant: Option<i64> },
|
||||
}
|
||||
|
||||
pub(crate) fn decode(claim: &Claim) -> Result<Payload> {
|
||||
if claim.target != TARGET
|
||||
|| claim.schema != SCHEMA
|
||||
|| claim.tenant != 0
|
||||
|| claim.retries != 2
|
||||
|| claim.timeout_ms != 1000
|
||||
{
|
||||
return Err(rejected(
|
||||
"queued Job differs from its declaration or physical queue",
|
||||
));
|
||||
}
|
||||
if claim.execution.len() > 16 * 1024 * 1024 || claim.payload.len() > 16 * 1024 * 1024 {
|
||||
return Err(rejected("Job JSON exceeds 16 MiB"));
|
||||
}
|
||||
let Execution::System { tenant } = serde_json::from_str(&claim.execution).map_err(rejected)?;
|
||||
if tenant.is_some() {
|
||||
return Err(rejected(
|
||||
"Job execution tenant differs from its physical queue",
|
||||
));
|
||||
}
|
||||
// Serde 的结构体默认也接受位置数组;Dever 的具名载荷合同只接受对象。
|
||||
struct Object;
|
||||
impl<'de> serde::de::Visitor<'de> for Object {
|
||||
type Value = Payload;
|
||||
fn expecting(&self, out: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
out.write_str("Job payload object")
|
||||
}
|
||||
fn visit_map<A: serde::de::MapAccess<'de>>(self, fields: A) -> std::result::Result<Payload, A::Error> {
|
||||
Payload::deserialize(serde::de::value::MapAccessDeserializer::new(fields))
|
||||
}
|
||||
}
|
||||
let mut decoder = serde_json::Deserializer::from_str(&claim.payload);
|
||||
let value = serde::Deserializer::deserialize_map(&mut decoder, Object).map_err(rejected)?;
|
||||
decoder.end().map_err(rejected)?;
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
pub(crate) fn claim(connection: &mut Connection) -> Result<Option<Claim>> {
|
||||
let tx = connection.transaction_with_behavior(TransactionBehavior::Immediate)?;
|
||||
let clock = now();
|
||||
tx.prepare_cached("UPDATE _dever_job SET state=CASE WHEN attempt >= retries THEN 'dead' ELSE 'pending' END,token=NULL,lease_until=NULL,error='lease expired' WHERE id IN (SELECT id FROM _dever_job WHERE state='running' AND lease_until<=?1 ORDER BY lease_until,id LIMIT 256)")?.execute([clock])?;
|
||||
let token = identifier()?;
|
||||
let value = tx.prepare_cached(
|
||||
"SELECT id,target,substr(payload,1,16777217),schema,substr(execution,1,16777217),retries,timeout_ms,attempt,execution_tenant FROM _dever_job
|
||||
WHERE id=(SELECT id FROM (
|
||||
SELECT id,available_at FROM _dever_job WHERE state='pending' AND attempt < retries AND available_at <= ?1
|
||||
UNION ALL
|
||||
SELECT id,available_at FROM _dever_job WHERE state='running' AND attempt < retries AND lease_until <= ?1 AND available_at <= ?1
|
||||
ORDER BY available_at,id LIMIT 1
|
||||
))"
|
||||
)?.query_row([clock], |row| Ok(Claim {
|
||||
value: base_queue::Claim { id: row.get(0)?, payload: row.get(2)?, attempt: row.get::<_,i64>(7)?+1, token },
|
||||
target: row.get(1)?, schema: row.get(3)?, execution: row.get(4)?,
|
||||
retries: row.get(5)?, timeout_ms: row.get(6)?, tenant: row.get(8)?,
|
||||
})).optional()?;
|
||||
if let Some(value) = &value {
|
||||
tx.prepare_cached("UPDATE _dever_job SET state='running',attempt=?1,token=?2,lease_until=?3,error=CASE WHEN state='running' THEN 'lease expired' ELSE error END WHERE id=?4")?
|
||||
.execute(params![value.attempt,value.token,clock+10000,value.id])?;
|
||||
}
|
||||
tx.commit()?;
|
||||
Ok(value)
|
||||
}
|
||||
Reference in new issue
Block a user