2022-09-06 22:43:59 +08:00
|
|
|
use anyhow::Result;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use time::OffsetDateTime;
|
2022-09-08 04:16:49 +08:00
|
|
|
use uuid::Uuid;
|
2022-09-06 22:43:59 +08:00
|
|
|
|
|
|
|
use crate::TaskId;
|
|
|
|
|
2022-09-08 02:08:07 +08:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
2022-09-06 22:43:59 +08:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub enum Status {
|
|
|
|
Enqueued,
|
|
|
|
Processing,
|
|
|
|
Succeeded,
|
|
|
|
Failed,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Task {
|
2022-09-08 02:08:07 +08:00
|
|
|
pub uid: TaskId,
|
|
|
|
|
2022-09-06 22:43:59 +08:00
|
|
|
#[serde(with = "time::serde::rfc3339::option")]
|
|
|
|
pub enqueued_at: Option<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>,
|
|
|
|
|
2022-09-06 22:43:59 +08:00
|
|
|
pub status: Status,
|
2022-09-07 05:49:19 +08:00
|
|
|
pub kind: KindWithContent,
|
2022-09-06 22:43:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Task {
|
2022-09-07 06:22:58 +08:00
|
|
|
/// Persist all the temp files associated with the task.
|
2022-09-06 22:43:59 +08:00
|
|
|
pub fn persist(&self) -> Result<()> {
|
|
|
|
self.kind.persist()
|
|
|
|
}
|
|
|
|
|
2022-09-07 06:22:58 +08:00
|
|
|
/// Delete all the files associated with the task.
|
2022-09-06 22:43:59 +08:00
|
|
|
pub fn remove_data(&self) -> Result<()> {
|
|
|
|
self.kind.remove_data()
|
|
|
|
}
|
2022-09-07 06:22:58 +08:00
|
|
|
|
|
|
|
/// Return the list of indexes updated by this tasks.
|
|
|
|
pub fn indexes(&self) -> Option<Vec<&str>> {
|
|
|
|
self.kind.indexes()
|
|
|
|
}
|
2022-09-06 22:43:59 +08:00
|
|
|
}
|
|
|
|
|
2022-09-08 02:08:07 +08:00
|
|
|
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
|
2022-09-06 22:43:59 +08:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-09-07 05:49:19 +08:00
|
|
|
pub enum KindWithContent {
|
2022-09-06 22:43:59 +08:00
|
|
|
DumpExport {
|
|
|
|
output: PathBuf,
|
|
|
|
},
|
2022-09-07 05:49:19 +08:00
|
|
|
Snapshot,
|
2022-09-06 22:43:59 +08:00
|
|
|
DocumentAddition {
|
|
|
|
index_name: String,
|
2022-09-08 04:16:49 +08:00
|
|
|
content_file: Uuid,
|
2022-09-06 22:43:59 +08:00
|
|
|
},
|
|
|
|
DocumentDeletion {
|
|
|
|
index_name: String,
|
|
|
|
documents_ids: Vec<String>,
|
|
|
|
},
|
|
|
|
ClearAllDocuments {
|
|
|
|
index_name: String,
|
|
|
|
},
|
|
|
|
// TODO: TAMO: uncomment the settings
|
|
|
|
// Settings {
|
|
|
|
// index_name: String,
|
|
|
|
// new_settings: Settings,
|
|
|
|
// },
|
|
|
|
RenameIndex {
|
|
|
|
index_name: String,
|
|
|
|
new_name: String,
|
|
|
|
},
|
|
|
|
CreateIndex {
|
|
|
|
index_name: String,
|
|
|
|
primary_key: Option<String>,
|
|
|
|
},
|
|
|
|
DeleteIndex {
|
|
|
|
index_name: String,
|
|
|
|
},
|
|
|
|
SwapIndex {
|
|
|
|
lhs: String,
|
|
|
|
rhs: String,
|
|
|
|
},
|
|
|
|
CancelTask {
|
|
|
|
tasks: Vec<TaskId>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-09-07 05:49:19 +08:00
|
|
|
impl KindWithContent {
|
|
|
|
pub fn as_kind(&self) -> Kind {
|
|
|
|
match self {
|
|
|
|
KindWithContent::DumpExport { .. } => Kind::DumpExport,
|
|
|
|
KindWithContent::DocumentAddition { .. } => Kind::DocumentAddition,
|
|
|
|
KindWithContent::DocumentDeletion { .. } => Kind::DocumentDeletion,
|
|
|
|
KindWithContent::ClearAllDocuments { .. } => Kind::ClearAllDocuments,
|
|
|
|
KindWithContent::RenameIndex { .. } => Kind::RenameIndex,
|
|
|
|
KindWithContent::CreateIndex { .. } => Kind::CreateIndex,
|
|
|
|
KindWithContent::DeleteIndex { .. } => Kind::DeleteIndex,
|
|
|
|
KindWithContent::SwapIndex { .. } => Kind::SwapIndex,
|
|
|
|
KindWithContent::CancelTask { .. } => Kind::CancelTask,
|
|
|
|
KindWithContent::Snapshot => Kind::Snapshot,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-06 22:43:59 +08:00
|
|
|
pub fn persist(&self) -> Result<()> {
|
2022-09-07 06:22:58 +08:00
|
|
|
use KindWithContent::*;
|
|
|
|
|
2022-09-06 22:43:59 +08:00
|
|
|
match self {
|
2022-09-07 06:22:58 +08:00
|
|
|
DocumentAddition {
|
2022-09-07 05:49:19 +08:00
|
|
|
index_name: _,
|
|
|
|
content_file: _,
|
2022-09-06 22:43:59 +08:00
|
|
|
} => {
|
|
|
|
// TODO: TAMO: persist the file
|
|
|
|
// content_file.persist();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
// There is nothing to persist for all these tasks
|
2022-09-07 06:22:58 +08:00
|
|
|
DumpExport { .. }
|
|
|
|
| DocumentDeletion { .. }
|
|
|
|
| ClearAllDocuments { .. }
|
|
|
|
| RenameIndex { .. }
|
|
|
|
| CreateIndex { .. }
|
|
|
|
| DeleteIndex { .. }
|
|
|
|
| SwapIndex { .. }
|
|
|
|
| CancelTask { .. }
|
|
|
|
| Snapshot => Ok(()),
|
2022-09-06 22:43:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn remove_data(&self) -> Result<()> {
|
2022-09-07 06:22:58 +08:00
|
|
|
use KindWithContent::*;
|
|
|
|
|
2022-09-06 22:43:59 +08:00
|
|
|
match self {
|
2022-09-07 06:22:58 +08:00
|
|
|
DocumentAddition {
|
2022-09-07 05:49:19 +08:00
|
|
|
index_name: _,
|
|
|
|
content_file: _,
|
2022-09-06 22:43:59 +08:00
|
|
|
} => {
|
|
|
|
// TODO: TAMO: delete the file
|
|
|
|
// content_file.delete();
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
// There is no data associated with all these tasks
|
2022-09-07 06:22:58 +08:00
|
|
|
DumpExport { .. }
|
|
|
|
| DocumentDeletion { .. }
|
|
|
|
| ClearAllDocuments { .. }
|
|
|
|
| RenameIndex { .. }
|
|
|
|
| CreateIndex { .. }
|
|
|
|
| DeleteIndex { .. }
|
|
|
|
| SwapIndex { .. }
|
|
|
|
| CancelTask { .. }
|
|
|
|
| Snapshot => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn indexes(&self) -> Option<Vec<&str>> {
|
|
|
|
use KindWithContent::*;
|
|
|
|
|
|
|
|
match self {
|
|
|
|
DumpExport { .. } | Snapshot | CancelTask { .. } => None,
|
|
|
|
DocumentAddition { index_name, .. }
|
|
|
|
| DocumentDeletion { index_name, .. }
|
|
|
|
| ClearAllDocuments { index_name }
|
|
|
|
| CreateIndex { index_name, .. }
|
|
|
|
| DeleteIndex { index_name } => Some(vec![index_name]),
|
|
|
|
RenameIndex {
|
|
|
|
index_name: lhs,
|
|
|
|
new_name: rhs,
|
|
|
|
}
|
|
|
|
| SwapIndex { lhs, rhs } => Some(vec![lhs, rhs]),
|
2022-09-06 22:43:59 +08:00
|
|
|
}
|
|
|
|
}
|
2022-09-07 05:49:19 +08:00
|
|
|
}
|
2022-09-06 22:43:59 +08:00
|
|
|
|
2022-09-08 02:08:07 +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 {
|
|
|
|
CancelTask,
|
|
|
|
ClearAllDocuments,
|
|
|
|
CreateIndex,
|
|
|
|
DeleteIndex,
|
|
|
|
DocumentAddition,
|
|
|
|
DocumentDeletion,
|
|
|
|
DumpExport,
|
|
|
|
RenameIndex,
|
|
|
|
Settings,
|
|
|
|
Snapshot,
|
|
|
|
SwapIndex,
|
2022-09-06 22:43:59 +08:00
|
|
|
}
|