meilisearch/index-scheduler/src/task.rs

218 lines
6.0 KiB
Rust
Raw Normal View History

use anyhow::Result;
2022-09-14 04:38:43 +08:00
use index::{Settings, Unchecked};
2022-09-14 19:13:44 +08:00
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use time::OffsetDateTime;
2022-09-08 04:16:49 +08:00
use uuid::Uuid;
use crate::TaskId;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Status {
Enqueued,
Processing,
Succeeded,
Failed,
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Task {
pub uid: TaskId,
#[serde(with = "time::serde::rfc3339")]
pub enqueued_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339::option")]
pub started_at: Option<OffsetDateTime>,
#[serde(with = "time::serde::rfc3339::option")]
pub finished_at: Option<OffsetDateTime>,
2022-09-09 07:09:50 +08:00
pub error: Option<String>,
pub info: Option<String>,
pub status: Status,
2022-09-07 05:49:19 +08:00
pub kind: KindWithContent,
}
impl Task {
/// Persist all the temp files associated with the task.
pub fn persist(&self) -> Result<()> {
self.kind.persist()
}
/// Delete all the files associated with the task.
pub fn remove_data(&self) -> Result<()> {
self.kind.remove_data()
}
/// Return the list of indexes updated by this tasks.
pub fn indexes(&self) -> Option<Vec<&str>> {
self.kind.indexes()
}
}
2022-09-14 04:38:43 +08:00
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
2022-09-07 05:49:19 +08:00
pub enum KindWithContent {
DocumentAddition {
index_uid: String,
primary_key: Option<String>,
content_file: Uuid,
documents_count: usize,
allow_index_creation: bool,
},
DocumentUpdate {
2022-09-14 04:38:43 +08:00
index_uid: String,
primary_key: Option<String>,
2022-09-08 04:16:49 +08:00
content_file: Uuid,
2022-09-14 04:38:43 +08:00
documents_count: usize,
allow_index_creation: bool,
},
DocumentDeletion {
2022-09-14 04:38:43 +08:00
index_uid: String,
documents_ids: Vec<String>,
},
2022-09-14 04:38:43 +08:00
DocumentClear {
index_uid: String,
},
Settings {
2022-09-14 04:38:43 +08:00
index_uid: String,
new_settings: Settings<Unchecked>,
is_deletion: bool,
allow_index_creation: bool,
},
2022-09-14 04:38:43 +08:00
IndexDeletion {
index_uid: String,
},
2022-09-14 04:38:43 +08:00
IndexCreation {
index_uid: String,
primary_key: Option<String>,
},
2022-09-14 04:38:43 +08:00
IndexUpdate {
index_uid: String,
primary_key: Option<String>,
},
IndexRename {
index_uid: String,
new_name: String,
},
2022-09-14 04:38:43 +08:00
IndexSwap {
lhs: String,
rhs: String,
},
CancelTask {
tasks: Vec<TaskId>,
},
2022-09-14 04:38:43 +08:00
DumpExport {
output: PathBuf,
},
Snapshot,
}
2022-09-07 05:49:19 +08:00
impl KindWithContent {
pub fn as_kind(&self) -> Kind {
match self {
KindWithContent::DocumentAddition { .. } => Kind::DocumentAddition,
KindWithContent::DocumentUpdate { .. } => Kind::DocumentUpdate,
2022-09-07 05:49:19 +08:00
KindWithContent::DocumentDeletion { .. } => Kind::DocumentDeletion,
2022-09-14 04:38:43 +08:00
KindWithContent::DocumentClear { .. } => Kind::DocumentClear,
KindWithContent::Settings { .. } => Kind::Settings,
KindWithContent::IndexCreation { .. } => Kind::IndexCreation,
KindWithContent::IndexDeletion { .. } => Kind::IndexDeletion,
KindWithContent::IndexUpdate { .. } => Kind::IndexUpdate,
KindWithContent::IndexRename { .. } => Kind::IndexRename,
KindWithContent::IndexSwap { .. } => Kind::IndexSwap,
2022-09-07 05:49:19 +08:00
KindWithContent::CancelTask { .. } => Kind::CancelTask,
2022-09-14 04:38:43 +08:00
KindWithContent::DumpExport { .. } => Kind::DumpExport,
2022-09-07 05:49:19 +08:00
KindWithContent::Snapshot => Kind::Snapshot,
}
}
pub fn persist(&self) -> Result<()> {
use KindWithContent::*;
match self {
DocumentAddition { .. } | DocumentUpdate { .. } => {
// TODO: TAMO: persist the file
// content_file.persist();
Ok(())
}
2022-09-14 04:38:43 +08:00
DocumentDeletion { .. }
| DocumentClear { .. }
| Settings { .. }
2022-09-14 04:38:43 +08:00
| IndexCreation { .. }
| IndexDeletion { .. }
| IndexUpdate { .. }
| IndexRename { .. }
| IndexSwap { .. }
| CancelTask { .. }
2022-09-14 04:38:43 +08:00
| DumpExport { .. }
| Snapshot => Ok(()), // There is nothing to persist for all these tasks
}
}
pub fn remove_data(&self) -> Result<()> {
use KindWithContent::*;
match self {
DocumentAddition { .. } | DocumentUpdate { .. } => {
// TODO: TAMO: delete the file
// content_file.delete();
Ok(())
}
IndexCreation { .. }
| DocumentDeletion { .. }
2022-09-14 04:38:43 +08:00
| DocumentClear { .. }
| Settings { .. }
| IndexDeletion { .. }
| IndexUpdate { .. }
| IndexRename { .. }
| IndexSwap { .. }
| CancelTask { .. }
2022-09-14 04:38:43 +08:00
| DumpExport { .. }
| Snapshot => Ok(()), // There is no data associated with all these tasks
}
}
pub fn indexes(&self) -> Option<Vec<&str>> {
use KindWithContent::*;
match self {
DumpExport { .. } | Snapshot | CancelTask { .. } => None,
DocumentAddition { index_uid, .. }
| DocumentUpdate { index_uid, .. }
2022-09-14 04:38:43 +08:00
| DocumentDeletion { index_uid, .. }
| DocumentClear { index_uid }
| Settings { index_uid, .. }
| IndexCreation { index_uid, .. }
| IndexUpdate { index_uid, .. }
| IndexDeletion { index_uid } => Some(vec![index_uid]),
IndexRename {
index_uid: lhs,
new_name: rhs,
}
2022-09-14 04:38:43 +08:00
| IndexSwap { lhs, rhs } => Some(vec![lhs, rhs]),
}
}
2022-09-07 05:49:19 +08:00
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
2022-09-07 05:49:19 +08:00
#[serde(rename_all = "camelCase")]
pub enum Kind {
DocumentAddition,
DocumentUpdate,
2022-09-07 05:49:19 +08:00
DocumentDeletion,
2022-09-14 04:38:43 +08:00
DocumentClear,
2022-09-07 05:49:19 +08:00
Settings,
2022-09-14 04:38:43 +08:00
IndexCreation,
IndexDeletion,
IndexUpdate,
IndexRename,
IndexSwap,
CancelTask,
DumpExport,
2022-09-07 05:49:19 +08:00
Snapshot,
}