This commit is contained in:
Louis Dureuil 2025-04-02 15:29:47 +02:00
parent 19f4c1ac98
commit 31bda976f2
No known key found for this signature in database
5 changed files with 288 additions and 68 deletions

View file

@ -6,7 +6,7 @@ use time::OffsetDateTime;
use utoipa::ToSchema;
use crate::task_view::DetailsView;
use crate::tasks::{Kind, Status};
use crate::tasks::{BatchStopReason, Kind, Status};
pub type BatchId = u32;
@ -28,11 +28,26 @@ pub struct Batch {
// Enqueued at is never displayed and is only required when removing a batch.
// It's always some except when upgrading from a database pre v1.12
pub enqueued_at: Option<BatchEnqueuedAt>,
#[serde(default = "default_stop_reason")]
pub stop_reason: String,
}
fn default_stop_reason() -> String {
BatchStopReason::default().to_string()
}
impl PartialEq for Batch {
fn eq(&self, other: &Self) -> bool {
let Self { uid, progress, details, stats, started_at, finished_at, enqueued_at } = self;
let Self {
uid,
progress,
details,
stats,
started_at,
finished_at,
enqueued_at,
stop_reason,
} = self;
*uid == other.uid
&& progress.is_none() == other.progress.is_none()
@ -41,6 +56,7 @@ impl PartialEq for Batch {
&& started_at == &other.started_at
&& finished_at == &other.finished_at
&& enqueued_at == &other.enqueued_at
&& stop_reason == &other.stop_reason
}
}

View file

@ -675,6 +675,121 @@ impl Details {
}
}
#[derive(Default, Debug, Clone)]
pub enum BatchStopReason {
#[default]
Unspecified,
TaskCannotBeBatched {
kind: Kind,
id: TaskId,
},
ExhaustedEnqueuedTasks,
ExhaustedEnqueuedTasksForIndex {
index: String,
},
ReachedTaskLimit {
task_limit: usize,
},
ReachedSizeLimit {
size_limit: usize,
size: usize,
},
PrimaryKeyIndexMismatch {
id: TaskId,
in_index: String,
in_task: String,
},
IndexCreationMismatch {
id: TaskId,
},
PrimaryKeyMismatch {
id: TaskId,
batch_pk: Option<String>,
task_pk: Option<String>,
},
IndexDeletion {
id: TaskId,
},
DocumentOperationWithSettings {
id: TaskId,
},
DocumentOperationWithDeletionByFilter {
id: TaskId,
},
DeletionByFilterWithDocumentOperation {
id: TaskId,
},
SettingsWithDocumentOperation {
id: TaskId,
},
}
impl BatchStopReason {
pub fn replace_unspecified(&mut self, new: BatchStopReason) {
if let BatchStopReason::Unspecified = self {
*self = new;
}
}
}
pub enum PrimaryKeyMismatchReason {}
impl Display for BatchStopReason {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
BatchStopReason::Unspecified => f.write_str("unspecified"),
BatchStopReason::TaskCannotBeBatched { kind, id } => {
write!(f, "task with id {id} of type `{kind}` cannot be batched")
}
BatchStopReason::ExhaustedEnqueuedTasks => f.write_str("batched all enqueued tasks"),
BatchStopReason::ExhaustedEnqueuedTasksForIndex { index } => {
write!(f, "batched all enqueued tasks for index `{index}`")
}
BatchStopReason::ReachedTaskLimit { task_limit } => {
write!(f, "reached configured batch limit of {task_limit} tasks")
}
BatchStopReason::ReachedSizeLimit { size_limit, size } => write!(
f,
"reached configured batch size limit of {size_limit}B with a total of {size}B"
),
BatchStopReason::PrimaryKeyIndexMismatch { id, in_index, in_task } => {
write!(f, "primary key `{in_task}` in task with id {id} is different from the primary key of the index `{in_index}`")
}
BatchStopReason::IndexCreationMismatch { id } => {
write!(f, "task with id {id} has different index creation rules as in the batch")
}
BatchStopReason::PrimaryKeyMismatch { id, batch_pk, task_pk } => {}
BatchStopReason::IndexDeletion { id } => {
write!(f, "task with id {id} deletes the index")
}
BatchStopReason::DocumentOperationWithSettings { id } => {
write!(
f,
"task with id {id} is a settings change in a batch of document operations"
)
}
BatchStopReason::DocumentOperationWithDeletionByFilter { id } => {
write!(
f,
"task with id {id} is a deletion by filter in a batch of document operations"
)
}
BatchStopReason::DeletionByFilterWithDocumentOperation { id } => {
write!(
f,
"task with id {id} is a document operation in a batch of deletions by filter"
)
}
BatchStopReason::SettingsWithDocumentOperation { id } => {
write!(
f,
"task with id {id} is a document operation in a batch of settings changes"
)
}
}
}
}
/// Serialize a `time::Duration` as a best effort ISO 8601 while waiting for
/// https://github.com/time-rs/time/issues/378.
/// This code is a port of the old code of time that was removed in 0.2.