ferroq 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Cargo.toml +32 -0
- data/README.md +20 -0
- data/bin/ferroq +34 -0
- data/crates/ferroq/Cargo.toml +12 -0
- data/crates/ferroq/src/lib.rs +6 -0
- data/crates/ferroq-core/Cargo.toml +20 -0
- data/crates/ferroq-core/src/conformance.rs +186 -0
- data/crates/ferroq-core/src/crypto.rs +118 -0
- data/crates/ferroq-core/src/engine.rs +175 -0
- data/crates/ferroq-core/src/error.rs +21 -0
- data/crates/ferroq-core/src/job.rs +168 -0
- data/crates/ferroq-core/src/lib.rs +12 -0
- data/crates/ferroq-core/src/storage.rs +65 -0
- data/crates/ferroq-node/Cargo.toml +19 -0
- data/crates/ferroq-node/build.rs +3 -0
- data/crates/ferroq-node/src/lib.rs +215 -0
- data/crates/ferroq-storage-sqlite/Cargo.toml +14 -0
- data/crates/ferroq-storage-sqlite/src/lib.rs +508 -0
- data/ext/ferroq/Cargo.toml +17 -0
- data/ext/ferroq/extconf.rb +6 -0
- data/ext/ferroq/src/lib.rs +308 -0
- data/lib/active_job/queue_adapters/ferroq_adapter.rb +53 -0
- data/lib/ferroq/configuration.rb +32 -0
- data/lib/ferroq/job.rb +83 -0
- data/lib/ferroq/version.rb +5 -0
- data/lib/ferroq/worker.rb +127 -0
- data/lib/ferroq.rb +51 -0
- metadata +81 -0
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
use serde::{Deserialize, Serialize};
|
|
2
|
+
use std::time::{SystemTime, UNIX_EPOCH};
|
|
3
|
+
use uuid::Uuid;
|
|
4
|
+
|
|
5
|
+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
6
|
+
#[serde(rename_all = "snake_case")]
|
|
7
|
+
pub enum JobStatus {
|
|
8
|
+
Pending,
|
|
9
|
+
Running,
|
|
10
|
+
Completed,
|
|
11
|
+
Dead,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
impl JobStatus {
|
|
15
|
+
pub fn as_str(&self) -> &'static str {
|
|
16
|
+
match self {
|
|
17
|
+
JobStatus::Pending => "pending",
|
|
18
|
+
JobStatus::Running => "running",
|
|
19
|
+
JobStatus::Completed => "completed",
|
|
20
|
+
JobStatus::Dead => "dead",
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
pub fn from_str(s: &str) -> Option<Self> {
|
|
25
|
+
match s {
|
|
26
|
+
"pending" => Some(JobStatus::Pending),
|
|
27
|
+
"running" => Some(JobStatus::Running),
|
|
28
|
+
"completed" => Some(JobStatus::Completed),
|
|
29
|
+
"dead" => Some(JobStatus::Dead),
|
|
30
|
+
_ => None,
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// Core job representation designed from day one to support:
|
|
36
|
+
/// - Priority queues (`priority`)
|
|
37
|
+
/// - Multi-tenant fairness (`group_key`)
|
|
38
|
+
/// - Parent-Child DAG Flows (`parent_id`) and Batches (`batch_id`)
|
|
39
|
+
/// - Unique deduplication (`unique_key`)
|
|
40
|
+
/// - Strict lease-based reliable delivery (`lease_until_ms`, `worker_id`)
|
|
41
|
+
/// - Progress reporting & return values (`progress`, `result`)
|
|
42
|
+
/// - Transparent payload encryption (`encrypted`)
|
|
43
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
44
|
+
pub struct Job {
|
|
45
|
+
pub id: String,
|
|
46
|
+
pub queue: String,
|
|
47
|
+
pub job_class: String,
|
|
48
|
+
pub payload: String,
|
|
49
|
+
pub encrypted: bool,
|
|
50
|
+
pub priority: i32,
|
|
51
|
+
pub group_key: Option<String>,
|
|
52
|
+
pub parent_id: Option<String>,
|
|
53
|
+
pub batch_id: Option<String>,
|
|
54
|
+
pub unique_key: Option<String>,
|
|
55
|
+
pub status: JobStatus,
|
|
56
|
+
pub attempt: u32,
|
|
57
|
+
pub max_retries: u32,
|
|
58
|
+
pub run_at_ms: i64,
|
|
59
|
+
pub created_at_ms: i64,
|
|
60
|
+
pub updated_at_ms: i64,
|
|
61
|
+
pub lease_until_ms: Option<i64>,
|
|
62
|
+
pub worker_id: Option<String>,
|
|
63
|
+
pub progress: Option<u8>,
|
|
64
|
+
pub result: Option<String>,
|
|
65
|
+
pub last_error: Option<String>,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
69
|
+
pub struct EnqueueRequest {
|
|
70
|
+
pub queue: String,
|
|
71
|
+
pub job_class: String,
|
|
72
|
+
pub payload: String,
|
|
73
|
+
#[serde(default)]
|
|
74
|
+
pub encrypted: bool,
|
|
75
|
+
#[serde(default)]
|
|
76
|
+
pub priority: i32,
|
|
77
|
+
#[serde(default)]
|
|
78
|
+
pub group_key: Option<String>,
|
|
79
|
+
#[serde(default)]
|
|
80
|
+
pub parent_id: Option<String>,
|
|
81
|
+
#[serde(default)]
|
|
82
|
+
pub batch_id: Option<String>,
|
|
83
|
+
#[serde(default)]
|
|
84
|
+
pub unique_key: Option<String>,
|
|
85
|
+
#[serde(default = "default_max_retries")]
|
|
86
|
+
pub max_retries: u32,
|
|
87
|
+
#[serde(default)]
|
|
88
|
+
pub delay_ms: i64,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
fn default_max_retries() -> u32 {
|
|
92
|
+
5
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
impl EnqueueRequest {
|
|
96
|
+
pub fn new(
|
|
97
|
+
queue: impl Into<String>,
|
|
98
|
+
job_class: impl Into<String>,
|
|
99
|
+
payload: impl Into<String>,
|
|
100
|
+
) -> Self {
|
|
101
|
+
Self {
|
|
102
|
+
queue: queue.into(),
|
|
103
|
+
job_class: job_class.into(),
|
|
104
|
+
payload: payload.into(),
|
|
105
|
+
encrypted: false,
|
|
106
|
+
priority: 0,
|
|
107
|
+
group_key: None,
|
|
108
|
+
parent_id: None,
|
|
109
|
+
batch_id: None,
|
|
110
|
+
unique_key: None,
|
|
111
|
+
max_retries: default_max_retries(),
|
|
112
|
+
delay_ms: 0,
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
pub fn into_job(self, now_ms: i64) -> Job {
|
|
117
|
+
let id = Uuid::now_v7().to_string();
|
|
118
|
+
let run_at_ms = now_ms + self.delay_ms.max(0);
|
|
119
|
+
Job {
|
|
120
|
+
id,
|
|
121
|
+
queue: self.queue,
|
|
122
|
+
job_class: self.job_class,
|
|
123
|
+
payload: self.payload,
|
|
124
|
+
encrypted: self.encrypted,
|
|
125
|
+
priority: self.priority,
|
|
126
|
+
group_key: self.group_key,
|
|
127
|
+
parent_id: self.parent_id,
|
|
128
|
+
batch_id: self.batch_id,
|
|
129
|
+
unique_key: self.unique_key,
|
|
130
|
+
status: JobStatus::Pending,
|
|
131
|
+
attempt: 0,
|
|
132
|
+
max_retries: self.max_retries,
|
|
133
|
+
run_at_ms,
|
|
134
|
+
created_at_ms: now_ms,
|
|
135
|
+
updated_at_ms: now_ms,
|
|
136
|
+
lease_until_ms: None,
|
|
137
|
+
worker_id: None,
|
|
138
|
+
progress: None,
|
|
139
|
+
result: None,
|
|
140
|
+
last_error: None,
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
|
|
146
|
+
pub struct QueueStats {
|
|
147
|
+
pub pending: u64,
|
|
148
|
+
pub running: u64,
|
|
149
|
+
pub completed: u64,
|
|
150
|
+
pub dead: u64,
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
pub fn now_ms() -> i64 {
|
|
154
|
+
SystemTime::now()
|
|
155
|
+
.duration_since(UNIX_EPOCH)
|
|
156
|
+
.expect("system clock before UNIX_EPOCH")
|
|
157
|
+
.as_millis() as i64
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/// Exponential backoff with deterministic base + small jitter:
|
|
161
|
+
/// `base_ms * 2^(attempt - 1)` capped at 1 hour.
|
|
162
|
+
pub fn compute_retry_delay_ms(attempt: u32, base_ms: i64) -> i64 {
|
|
163
|
+
let exp = attempt.saturating_sub(1).min(12);
|
|
164
|
+
let delay = base_ms.saturating_mul(1_i64 << exp);
|
|
165
|
+
let capped = delay.min(3_600_000);
|
|
166
|
+
let jitter = (rand::random::<u32>() % 250) as i64;
|
|
167
|
+
capped + jitter
|
|
168
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
pub mod conformance;
|
|
2
|
+
pub mod crypto;
|
|
3
|
+
pub mod engine;
|
|
4
|
+
pub mod error;
|
|
5
|
+
pub mod job;
|
|
6
|
+
pub mod storage;
|
|
7
|
+
|
|
8
|
+
pub use crypto::{compute_unique_digest, CryptoEngine};
|
|
9
|
+
pub use engine::{EngineConfig, FerroqEngine};
|
|
10
|
+
pub use error::{FerroqError, Result};
|
|
11
|
+
pub use job::{EnqueueRequest, Job, JobStatus, QueueStats};
|
|
12
|
+
pub use storage::StorageBackend;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
use crate::error::Result;
|
|
2
|
+
use crate::job::{Job, JobStatus, QueueStats};
|
|
3
|
+
|
|
4
|
+
/// Unified storage contract implemented by all Ferroq backends:
|
|
5
|
+
/// - `ferroq-storage-sqlite` (Embedded SQLite WAL, zero external deps)
|
|
6
|
+
/// - `ferroq-storage-redis` (Redis / Valkey + Lua scripts)
|
|
7
|
+
/// - `ferroq-storage-postgres` (PostgreSQL LISTEN/NOTIFY + SKIP LOCKED)
|
|
8
|
+
///
|
|
9
|
+
/// Every implementation MUST pass `ferroq_core::conformance::run_storage_conformance_suite`.
|
|
10
|
+
pub trait StorageBackend: Send + Sync {
|
|
11
|
+
fn enqueue(&self, job: Job) -> Result<String>;
|
|
12
|
+
|
|
13
|
+
fn enqueue_batch(&self, jobs: Vec<Job>) -> Result<Vec<String>>;
|
|
14
|
+
|
|
15
|
+
/// Atomically claims up to `batch_size` due jobs from `queues` ordered by
|
|
16
|
+
/// `(priority DESC, run_at_ms ASC, id ASC)`, setting status = Running and
|
|
17
|
+
/// `lease_until_ms = now_ms + lease_ms`.
|
|
18
|
+
fn fetch(
|
|
19
|
+
&self,
|
|
20
|
+
queues: &[String],
|
|
21
|
+
batch_size: usize,
|
|
22
|
+
worker_id: &str,
|
|
23
|
+
lease_ms: i64,
|
|
24
|
+
now_ms: i64,
|
|
25
|
+
) -> Result<Vec<Job>>;
|
|
26
|
+
|
|
27
|
+
/// Marks a running job as completed and optionally persists its return value.
|
|
28
|
+
fn ack(&self, job_id: &str, result: Option<String>, now_ms: i64) -> Result<bool>;
|
|
29
|
+
|
|
30
|
+
/// Marks multiple running jobs as completed in a single atomic operation.
|
|
31
|
+
fn ack_batch(&self, job_ids: &[String], now_ms: i64) -> Result<usize>;
|
|
32
|
+
|
|
33
|
+
/// Records a job failure.
|
|
34
|
+
/// - If `next_run_at_ms` is `Some(ts)`, increments `attempt` and returns job to `Pending` at `ts`.
|
|
35
|
+
/// - If `next_run_at_ms` is `None`, moves job to `Dead` (DLQ).
|
|
36
|
+
fn fail(
|
|
37
|
+
&self,
|
|
38
|
+
job_id: &str,
|
|
39
|
+
error: &str,
|
|
40
|
+
next_run_at_ms: Option<i64>,
|
|
41
|
+
now_ms: i64,
|
|
42
|
+
) -> Result<JobStatus>;
|
|
43
|
+
|
|
44
|
+
/// Extends the lease of a running job owned by `worker_id`.
|
|
45
|
+
fn renew_lease(
|
|
46
|
+
&self,
|
|
47
|
+
job_id: &str,
|
|
48
|
+
worker_id: &str,
|
|
49
|
+
lease_until_ms: i64,
|
|
50
|
+
now_ms: i64,
|
|
51
|
+
) -> Result<bool>;
|
|
52
|
+
|
|
53
|
+
/// Reclaims running jobs whose `lease_until_ms <= now_ms` (e.g., after worker `kill -9`).
|
|
54
|
+
/// Jobs with remaining retries return to `Pending`; exhausted jobs move to `Dead`.
|
|
55
|
+
fn recover_stalled(&self, now_ms: i64) -> Result<usize>;
|
|
56
|
+
|
|
57
|
+
/// Updates job progress (0..=100).
|
|
58
|
+
fn update_progress(&self, job_id: &str, progress: u8, now_ms: i64) -> Result<bool>;
|
|
59
|
+
|
|
60
|
+
fn get_job(&self, job_id: &str) -> Result<Option<Job>>;
|
|
61
|
+
|
|
62
|
+
fn stats(&self) -> Result<QueueStats>;
|
|
63
|
+
|
|
64
|
+
fn clear(&self) -> Result<()>;
|
|
65
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "ferroq-node"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
license.workspace = true
|
|
6
|
+
|
|
7
|
+
[lib]
|
|
8
|
+
crate-type = ["cdylib"]
|
|
9
|
+
|
|
10
|
+
[dependencies]
|
|
11
|
+
ferroq-core = { path = "../ferroq-core" }
|
|
12
|
+
ferroq-storage-sqlite = { path = "../ferroq-storage-sqlite" }
|
|
13
|
+
napi = { version = "2", default-features = false, features = ["napi8", "serde-json"] }
|
|
14
|
+
napi-derive = "2"
|
|
15
|
+
serde.workspace = true
|
|
16
|
+
serde_json.workspace = true
|
|
17
|
+
|
|
18
|
+
[build-dependencies]
|
|
19
|
+
napi-build = "2"
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
use ferroq_core::{EngineConfig, EnqueueRequest, FerroqEngine, Job};
|
|
2
|
+
use ferroq_storage_sqlite::SqliteStorage;
|
|
3
|
+
use napi::bindgen_prelude::*;
|
|
4
|
+
use napi_derive::napi;
|
|
5
|
+
use serde::Deserialize;
|
|
6
|
+
use std::sync::Arc;
|
|
7
|
+
|
|
8
|
+
#[derive(Debug, Default, Deserialize)]
|
|
9
|
+
struct EngineInitOptions {
|
|
10
|
+
default_lease_ms: Option<i64>,
|
|
11
|
+
retry_base_delay_ms: Option<i64>,
|
|
12
|
+
encryption_secret: Option<String>,
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
#[derive(Debug, Default, Deserialize)]
|
|
16
|
+
struct EnqueueOpts {
|
|
17
|
+
#[serde(default)]
|
|
18
|
+
encrypted: bool,
|
|
19
|
+
#[serde(default)]
|
|
20
|
+
priority: i32,
|
|
21
|
+
group_key: Option<String>,
|
|
22
|
+
parent_id: Option<String>,
|
|
23
|
+
batch_id: Option<String>,
|
|
24
|
+
unique_key: Option<String>,
|
|
25
|
+
max_retries: Option<u32>,
|
|
26
|
+
#[serde(default)]
|
|
27
|
+
delay_ms: i64,
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#[napi(object)]
|
|
31
|
+
pub struct JsJob {
|
|
32
|
+
pub id: String,
|
|
33
|
+
pub queue: String,
|
|
34
|
+
pub name: String,
|
|
35
|
+
pub payload: String,
|
|
36
|
+
pub encrypted: bool,
|
|
37
|
+
pub priority: i32,
|
|
38
|
+
pub status: String,
|
|
39
|
+
pub attempt: u32,
|
|
40
|
+
pub max_retries: u32,
|
|
41
|
+
pub progress: Option<u32>,
|
|
42
|
+
pub result: Option<String>,
|
|
43
|
+
pub last_error: Option<String>,
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
impl From<Job> for JsJob {
|
|
47
|
+
fn from(j: Job) -> Self {
|
|
48
|
+
Self {
|
|
49
|
+
id: j.id,
|
|
50
|
+
queue: j.queue,
|
|
51
|
+
name: j.job_class,
|
|
52
|
+
payload: j.payload,
|
|
53
|
+
encrypted: j.encrypted,
|
|
54
|
+
priority: j.priority,
|
|
55
|
+
status: j.status.as_str().to_string(),
|
|
56
|
+
attempt: j.attempt,
|
|
57
|
+
max_retries: j.max_retries,
|
|
58
|
+
progress: j.progress.map(|p| p as u32),
|
|
59
|
+
result: j.result,
|
|
60
|
+
last_error: j.last_error,
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#[napi(object)]
|
|
66
|
+
pub struct JsQueueStats {
|
|
67
|
+
pub waiting: u32,
|
|
68
|
+
pub active: u32,
|
|
69
|
+
pub completed: u32,
|
|
70
|
+
pub failed: u32,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
fn map_err(e: ferroq_core::FerroqError) -> Error {
|
|
74
|
+
Error::from_reason(e.to_string())
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
#[napi]
|
|
78
|
+
pub struct NativeEngine {
|
|
79
|
+
inner: Arc<FerroqEngine>,
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#[napi]
|
|
83
|
+
impl NativeEngine {
|
|
84
|
+
#[napi(factory)]
|
|
85
|
+
pub fn open_sqlite(path: String, options_json: Option<String>) -> Result<Self> {
|
|
86
|
+
let init_opts: EngineInitOptions = match options_json {
|
|
87
|
+
Some(ref s) if !s.is_empty() => serde_json::from_str(s)
|
|
88
|
+
.map_err(|e| Error::from_reason(format!("invalid options_json: {e}")))?,
|
|
89
|
+
_ => EngineInitOptions::default(),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
let mut config = EngineConfig::default();
|
|
93
|
+
if let Some(lease) = init_opts.default_lease_ms {
|
|
94
|
+
config.default_lease_ms = lease;
|
|
95
|
+
}
|
|
96
|
+
if let Some(delay) = init_opts.retry_base_delay_ms {
|
|
97
|
+
config.retry_base_delay_ms = delay;
|
|
98
|
+
}
|
|
99
|
+
config.encryption_secret = init_opts.encryption_secret;
|
|
100
|
+
|
|
101
|
+
let storage = if path == ":memory:" {
|
|
102
|
+
SqliteStorage::open_in_memory().map_err(map_err)?
|
|
103
|
+
} else {
|
|
104
|
+
SqliteStorage::open(&path).map_err(map_err)?
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
let engine = FerroqEngine::new(Arc::new(storage), config).map_err(map_err)?;
|
|
108
|
+
Ok(Self {
|
|
109
|
+
inner: Arc::new(engine),
|
|
110
|
+
})
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#[napi]
|
|
114
|
+
pub fn enqueue(
|
|
115
|
+
&self,
|
|
116
|
+
queue: String,
|
|
117
|
+
name: String,
|
|
118
|
+
payload: String,
|
|
119
|
+
opts_json: Option<String>,
|
|
120
|
+
) -> Result<String> {
|
|
121
|
+
let opts: EnqueueOpts = match opts_json {
|
|
122
|
+
Some(ref s) if !s.is_empty() => serde_json::from_str(s)
|
|
123
|
+
.map_err(|e| Error::from_reason(format!("invalid opts_json: {e}")))?,
|
|
124
|
+
_ => EnqueueOpts::default(),
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
let mut req = EnqueueRequest::new(queue, name, payload);
|
|
128
|
+
req.encrypted = opts.encrypted;
|
|
129
|
+
req.priority = opts.priority;
|
|
130
|
+
req.group_key = opts.group_key;
|
|
131
|
+
req.parent_id = opts.parent_id;
|
|
132
|
+
req.batch_id = opts.batch_id;
|
|
133
|
+
req.unique_key = opts.unique_key;
|
|
134
|
+
if let Some(mr) = opts.max_retries {
|
|
135
|
+
req.max_retries = mr;
|
|
136
|
+
}
|
|
137
|
+
req.delay_ms = opts.delay_ms;
|
|
138
|
+
|
|
139
|
+
self.inner.enqueue(req).map_err(map_err)
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
#[napi]
|
|
143
|
+
pub fn enqueue_batch(&self, requests_json: String) -> Result<Vec<String>> {
|
|
144
|
+
let requests: Vec<EnqueueRequest> = serde_json::from_str(&requests_json)
|
|
145
|
+
.map_err(|e| Error::from_reason(format!("invalid requests_json: {e}")))?;
|
|
146
|
+
self.inner.enqueue_batch(requests).map_err(map_err)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
#[napi]
|
|
150
|
+
pub fn fetch(
|
|
151
|
+
&self,
|
|
152
|
+
queues: Vec<String>,
|
|
153
|
+
batch_size: u32,
|
|
154
|
+
worker_id: String,
|
|
155
|
+
lease_ms: Option<i64>,
|
|
156
|
+
) -> Result<Vec<JsJob>> {
|
|
157
|
+
let jobs = self
|
|
158
|
+
.inner
|
|
159
|
+
.fetch(&queues, batch_size as usize, &worker_id, lease_ms)
|
|
160
|
+
.map_err(map_err)?;
|
|
161
|
+
Ok(jobs.into_iter().map(JsJob::from).collect())
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
#[napi]
|
|
165
|
+
pub fn ack(&self, job_id: String, result: Option<String>) -> Result<bool> {
|
|
166
|
+
self.inner.ack(&job_id, result).map_err(map_err)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
#[napi]
|
|
170
|
+
pub fn ack_batch(&self, job_ids: Vec<String>) -> Result<u32> {
|
|
171
|
+
let n = self.inner.ack_batch(&job_ids).map_err(map_err)?;
|
|
172
|
+
Ok(n as u32)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
#[napi]
|
|
176
|
+
pub fn fail(&self, job_id: String, error_msg: String) -> Result<String> {
|
|
177
|
+
let status = self.inner.fail(&job_id, &error_msg).map_err(map_err)?;
|
|
178
|
+
Ok(status.as_str().to_string())
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
#[napi]
|
|
182
|
+
pub fn recover_stalled(&self) -> Result<u32> {
|
|
183
|
+
let n = self.inner.recover_stalled().map_err(map_err)?;
|
|
184
|
+
Ok(n as u32)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
#[napi]
|
|
188
|
+
pub fn update_progress(&self, job_id: String, progress: u32) -> Result<bool> {
|
|
189
|
+
self.inner
|
|
190
|
+
.update_progress(&job_id, progress.min(100) as u8)
|
|
191
|
+
.map_err(map_err)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
#[napi]
|
|
195
|
+
pub fn get_job(&self, job_id: String) -> Result<Option<JsJob>> {
|
|
196
|
+
let job = self.inner.get_job(&job_id).map_err(map_err)?;
|
|
197
|
+
Ok(job.map(JsJob::from))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#[napi]
|
|
201
|
+
pub fn stats(&self) -> Result<JsQueueStats> {
|
|
202
|
+
let s = self.inner.stats().map_err(map_err)?;
|
|
203
|
+
Ok(JsQueueStats {
|
|
204
|
+
waiting: s.pending as u32,
|
|
205
|
+
active: s.running as u32,
|
|
206
|
+
completed: s.completed as u32,
|
|
207
|
+
failed: s.dead as u32,
|
|
208
|
+
})
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
#[napi]
|
|
212
|
+
pub fn clear(&self) -> Result<()> {
|
|
213
|
+
self.inner.clear().map_err(map_err)
|
|
214
|
+
}
|
|
215
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "ferroq-storage-sqlite"
|
|
3
|
+
version.workspace = true
|
|
4
|
+
edition.workspace = true
|
|
5
|
+
license.workspace = true
|
|
6
|
+
|
|
7
|
+
[dependencies]
|
|
8
|
+
ferroq-core = { path = "../ferroq-core" }
|
|
9
|
+
parking_lot.workspace = true
|
|
10
|
+
rusqlite.workspace = true
|
|
11
|
+
serde_json.workspace = true
|
|
12
|
+
|
|
13
|
+
[dev-dependencies]
|
|
14
|
+
tempfile = "3"
|