1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! Main Boson runtime type.
use std::sync::Arc;
use boson_core::{
BosonError, IdempotencyMode, Job, JobEnqueueDisposition, JobStatus, QueueBackend, Result, Run,
TaskConfig, TaskRunStats,
};
use chrono::{DateTime, Utc};
use crate::registry::TaskRegistry;
use crate::worker::WorkerSettings;
/// Boson work engine — enqueue, admin reads, and worker orchestration.
#[derive(Clone)]
pub struct Boson {
pub(crate) backend: Arc<dyn QueueBackend>,
pub(crate) registry: Arc<TaskRegistry>,
worker: WorkerSettings,
/// Runtime default when [`TaskConfig::idempotency_mode`] is unset.
idempotency_mode: IdempotencyMode,
}
impl Boson {
/// Construct from injected parts (used by builder and tests).
pub fn from_parts(
backend: Arc<dyn QueueBackend>,
registry: Arc<TaskRegistry>,
worker: WorkerSettings,
) -> Self {
Self::from_parts_with_idempotency(backend, registry, worker, IdempotencyMode::Lwt)
}
/// Construct with an explicit default idempotency mode.
pub fn from_parts_with_idempotency(
backend: Arc<dyn QueueBackend>,
registry: Arc<TaskRegistry>,
worker: WorkerSettings,
idempotency_mode: IdempotencyMode,
) -> Self {
Self {
backend,
registry,
worker,
idempotency_mode,
}
}
/// Runtime default idempotency mode (builder / task override may change per enqueue).
#[must_use]
pub const fn idempotency_mode(&self) -> IdempotencyMode {
self.idempotency_mode
}
/// Worker settings this instance was built with.
#[must_use]
pub const fn worker_settings(&self) -> &WorkerSettings {
&self.worker
}
/// Telemetry/runtime label (topology slug or `embedded`).
#[must_use]
pub fn runtime_label(&self) -> &str {
&self.worker.runtime_label
}
/// Queue backend handle.
#[must_use]
pub fn queue_backend(&self) -> Arc<dyn QueueBackend> {
Arc::clone(&self.backend)
}
/// Task registry.
#[must_use]
pub fn registry(&self) -> &TaskRegistry {
&self.registry
}
/// Resolve task config from backend or registry defaults.
///
/// Precedence: persisted backend config, else descriptor policy defaults, then runtime
/// idempotency fallback when the mode is unset.
///
/// # Errors
///
/// Returns an error if the task is unknown or the backend fails.
pub async fn resolve_task_config(&self, task_name: &str) -> Result<TaskConfig> {
let config = if let Some(c) = self.backend.get_task_config(task_name).await? {
c
} else {
self.registry.get_or_err(task_name)?.to_task_config()
};
Ok(config.with_runtime_idempotency_fallback(self.idempotency_mode()))
}
/// Resolve priority and pool for enqueue.
///
/// # Errors
///
/// Returns an error if the task is unknown or the backend fails.
pub async fn resolve_priority_pool(&self, task_name: &str) -> Result<(i32, String)> {
let config = self.resolve_task_config(task_name).await?;
Ok((config.priority, config.pool))
}
/// Enqueue helper used internally and by admin APIs.
pub(crate) async fn enqueue_internal(
&self,
task_name: &str,
actor_json: serde_json::Value,
params_json: serde_json::Value,
idempotency_key: Option<String>,
) -> Result<String> {
let descriptor = self.registry.get_or_err(task_name)?;
let task_config = self.resolve_task_config(task_name).await?;
let priority = task_config.priority;
let pool = task_config.pool.clone();
let job = Job::new(
task_name,
actor_json,
params_json,
priority,
&pool,
descriptor.signature_hash,
idempotency_key,
);
let (job_id, disposition) = self
.backend
.enqueue_with_policies(job, &task_config)
.await
.map_err(|e| {
if matches!(e, BosonError::RateLimited(_)) {
crate::telemetry::record_task_failed(
task_name,
"",
"",
&e.to_string(),
false,
);
}
e
})?;
if disposition == JobEnqueueDisposition::InsertedNew {
crate::telemetry::record_task_enqueued(task_name, self.runtime_label());
}
Ok(job_id)
}
/// Enqueue a job.
///
/// Priority and pool come from persisted [`TaskConfig`](boson_core::TaskConfig) merged with
/// [`TaskDescriptor`](crate::registry::TaskDescriptor) defaults. Optional `idempotency_key`
/// deduplicates non-terminal jobs. Rate limits may return
/// [`BosonError::RateLimited`](boson_core::BosonError::RateLimited).
///
/// # Errors
///
/// Returns an error if the task is unknown, rate limits apply, or the backend fails.
pub async fn enqueue(
&self,
task_name: &str,
actor_json: serde_json::Value,
params_json: serde_json::Value,
idempotency_key: Option<String>,
) -> Result<String> {
self.enqueue_internal(task_name, actor_json, params_json, idempotency_key)
.await
}
/// Get a job by id.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn get_job(&self, job_id: &str) -> Result<Option<Job>> {
self.backend.get_job(job_id).await
}
/// List jobs with optional status filter.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn list_jobs(
&self,
status_filter: Option<JobStatus>,
offset: usize,
limit: usize,
) -> Result<Vec<Job>> {
self.backend.list_jobs(status_filter, offset, limit).await
}
/// Cancel a job if still active.
///
/// # Errors
///
/// Returns an error if the job is not found or the backend fails.
pub async fn cancel_job(&self, job_id: &str) -> Result<()> {
self.backend.cancel_job_if_active(job_id).await
}
/// Get or default task config.
///
/// # Errors
///
/// Returns an error if the task is unknown or the backend fails.
pub async fn get_task_config(&self, task_name: &str) -> Result<TaskConfig> {
self.resolve_task_config(task_name).await
}
/// Upsert task config.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn upsert_task_config(&self, config: TaskConfig) -> Result<()> {
self.backend.upsert_task_config(&config).await
}
/// List runs.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn list_runs(
&self,
job_id_filter: Option<&str>,
offset: usize,
limit: usize,
) -> Result<Vec<Run>> {
self.backend.list_runs(job_id_filter, offset, limit).await
}
/// Get run by id.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn get_run(&self, run_id: &str) -> Result<Option<Run>> {
self.backend.get_run(run_id).await
}
/// Count jobs.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn count_jobs(&self, status_filter: Option<JobStatus>) -> Result<u64> {
self.backend.count_jobs(status_filter).await
}
/// Count runs.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn count_runs(&self, job_id_filter: Option<&str>) -> Result<u64> {
self.backend.count_runs(job_id_filter).await
}
/// Count runs since timestamp.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn count_runs_since(&self, since: DateTime<Utc>) -> Result<u64> {
self.backend.count_runs_since(since).await
}
/// Count jobs for one task.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn count_jobs_for_task(
&self,
task_name: &str,
status: Option<JobStatus>,
) -> Result<u64> {
self.backend.count_jobs_for_task(task_name, status).await
}
/// Aggregate run stats for one task.
///
/// # Errors
///
/// Returns an error if the backend fails.
pub async fn task_run_stats(&self, task_name: &str) -> Result<TaskRunStats> {
self.backend.task_run_stats(task_name).await
}
}