From 290945e258c5e4680f6e41de69e4af2cba740860 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Tue, 18 Oct 2022 13:57:58 +0200 Subject: [PATCH] Update the canceledBy and finishedAt fields --- dump/src/lib.rs | 7 +++++++ dump/src/reader/compat/v5_to_v6.rs | 1 + index-scheduler/src/batch.rs | 22 ++++++++++++++-------- index-scheduler/src/lib.rs | 2 ++ meilisearch-http/src/routes/tasks.rs | 4 ++++ meilisearch-types/src/tasks.rs | 1 + 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/dump/src/lib.rs b/dump/src/lib.rs index c7aed6280..dd3b90cad 100644 --- a/dump/src/lib.rs +++ b/dump/src/lib.rs @@ -62,6 +62,9 @@ pub struct TaskDump { #[serde(rename = "type")] pub kind: KindDump, + #[serde(skip_serializing_if = "Option::is_none")] + pub canceled_by: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub details: Option
, #[serde(skip_serializing_if = "Option::is_none")] @@ -136,6 +139,7 @@ impl From for TaskDump { index_uid: task.index_uid().map(|uid| uid.to_string()), status: task.status, kind: task.kind.into(), + canceled_by: task.canceled_by, details: task.details, error: task.error, enqueued_at: task.enqueued_at, @@ -289,6 +293,7 @@ pub(crate) mod test { primary_key: Some(S("bone")), documents_count: 12, }, + canceled_by: None, details: Some(Details::DocumentAddition { received_documents: 12, indexed_documents: Some(10), @@ -311,6 +316,7 @@ pub(crate) mod test { primary_key: None, documents_count: 2, }, + canceled_by: None, details: Some(Details::DocumentAddition { received_documents: 2, indexed_documents: None, @@ -337,6 +343,7 @@ pub(crate) mod test { index_uid: Some(S("catto")), status: Status::Enqueued, kind: KindDump::IndexDeletion, + canceled_by: None, details: None, error: None, enqueued_at: datetime!(2022-11-15 0:00 UTC), diff --git a/dump/src/reader/compat/v5_to_v6.rs b/dump/src/reader/compat/v5_to_v6.rs index aacdbd4e0..96d78a17a 100644 --- a/dump/src/reader/compat/v5_to_v6.rs +++ b/dump/src/reader/compat/v5_to_v6.rs @@ -125,6 +125,7 @@ impl CompatV5ToV6 { instance_uid: instance_uid.clone(), }, }, + canceled_by: None, details: task_view.details.map(|details| match details { v5::Details::DocumentAddition { received_documents, diff --git a/index-scheduler/src/batch.rs b/index-scheduler/src/batch.rs index 03fecea9b..dedc0a387 100644 --- a/index-scheduler/src/batch.rs +++ b/index-scheduler/src/batch.rs @@ -468,7 +468,8 @@ impl IndexScheduler { }; let mut wtxn = self.env.write_txn()?; - let nbr_canceled_tasks = self.cancel_matched_tasks(&mut wtxn, matched_tasks)?; + let canceled_tasks_count = + self.cancel_matched_tasks(&mut wtxn, task.uid, matched_tasks)?; task.status = Status::Succeeded; match &mut task.details { @@ -477,7 +478,7 @@ impl IndexScheduler { canceled_tasks, original_query: _, }) => { - *canceled_tasks = Some(nbr_canceled_tasks); + *canceled_tasks = Some(canceled_tasks_count); } _ => unreachable!(), } @@ -1029,20 +1030,25 @@ impl IndexScheduler { fn cancel_matched_tasks( &self, wtxn: &mut RwTxn, + cancel_task_id: TaskId, matched_tasks: &RoaringBitmap, ) -> Result { + let now = OffsetDateTime::now_utc(); + // 1. Remove from this list the tasks that we are not allowed to cancel // Notice that only the _enqueued_ ones are cancelable and we should // have already aborted the indexation of the _processing_ ones - let cancelable_tasks = self.get_status(&wtxn, Status::Enqueued)?; + let cancelable_tasks = self.get_status(wtxn, Status::Enqueued)?; let tasks_to_cancel = cancelable_tasks & matched_tasks; // 2. We now have a list of tasks to cancel, cancel them - self.update_status(wtxn, Status::Enqueued, |bitmap| *bitmap -= &tasks_to_cancel)?; - self.update_status(wtxn, Status::Canceled, |bitmap| *bitmap |= &tasks_to_cancel)?; - - // TODO update the content of the tasks i.e. canceled_by and finished_at - // TODO delete the content uuid of the tasks + for mut task in self.get_existing_tasks(wtxn, tasks_to_cancel.iter())? { + // TODO delete the content uuid of the task + task.status = Status::Canceled; + task.canceled_by = Some(cancel_task_id); + task.finished_at = Some(now); + self.update_task(wtxn, &task)?; + } Ok(tasks_to_cancel.len() as usize) } diff --git a/index-scheduler/src/lib.rs b/index-scheduler/src/lib.rs index ba4ad24d4..e9300d8e8 100644 --- a/index-scheduler/src/lib.rs +++ b/index-scheduler/src/lib.rs @@ -397,6 +397,7 @@ impl IndexScheduler { started_at: None, finished_at: None, error: None, + canceled_by: None, details: kind.default_details(), status: Status::Enqueued, kind: kind.clone(), @@ -478,6 +479,7 @@ impl IndexScheduler { started_at: task.started_at, finished_at: task.finished_at, error: task.error, + canceled_by: task.canceled_by, details: task.details, status: task.status, kind: match task.kind { diff --git a/meilisearch-http/src/routes/tasks.rs b/meilisearch-http/src/routes/tasks.rs index 7a3289e24..61ead0bd5 100644 --- a/meilisearch-http/src/routes/tasks.rs +++ b/meilisearch-http/src/routes/tasks.rs @@ -38,6 +38,9 @@ pub struct TaskView { #[serde(rename = "type")] pub kind: Kind, + #[serde(skip_serializing_if = "Option::is_none")] + pub canceled_by: Option, + #[serde(skip_serializing_if = "Option::is_none")] pub details: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -74,6 +77,7 @@ impl TaskView { .and_then(|vec| vec.first().map(|i| i.to_string())), status: task.status, kind: task.kind.as_kind(), + canceled_by: task.canceled_by, details: task.details.clone().map(DetailsView::from), error: task.error.clone(), duration: task diff --git a/meilisearch-types/src/tasks.rs b/meilisearch-types/src/tasks.rs index db334df65..5eda0b289 100644 --- a/meilisearch-types/src/tasks.rs +++ b/meilisearch-types/src/tasks.rs @@ -30,6 +30,7 @@ pub struct Task { pub finished_at: Option, pub error: Option, + pub canceled_by: Option, pub details: Option
, pub status: Status,