93 lines
2 KiB
Rust
93 lines
2 KiB
Rust
use chrono::Utc;
|
|
use serde::de::DeserializeOwned;
|
|
use serde::Serialize;
|
|
use std::fmt::Display;
|
|
use tabled::Tabled;
|
|
|
|
mod manager;
|
|
|
|
#[derive(sqlx::Type, Debug, Clone)]
|
|
#[repr(u8)]
|
|
pub enum TaskStatus {
|
|
Pending = 1,
|
|
InProgress = 2,
|
|
Completed = 3,
|
|
Failed = 4,
|
|
}
|
|
|
|
impl Display for TaskStatus {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
TaskStatus::Pending => {
|
|
write!(f, "Pending")
|
|
}
|
|
TaskStatus::InProgress => {
|
|
write!(f, "In Progress")
|
|
}
|
|
TaskStatus::Completed => {
|
|
write!(f, "Completed")
|
|
}
|
|
TaskStatus::Failed => {
|
|
write!(f, "Failed")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait TaskPayload:
|
|
Serialize + DeserializeOwned + Send + Unpin + 'static
|
|
{
|
|
}
|
|
impl<T: Serialize + DeserializeOwned + Send + Unpin + 'static>
|
|
TaskPayload for T
|
|
{
|
|
}
|
|
|
|
pub type TaskJob<T> = fn(&Task<T>) -> TaskStatus;
|
|
|
|
#[derive(sqlx::FromRow, Tabled, Debug)]
|
|
pub struct Task<T: TaskPayload> {
|
|
id: u32,
|
|
payload_key: String,
|
|
#[sqlx(json)]
|
|
#[tabled(skip)]
|
|
payload: T,
|
|
#[sqlx(rename = "status_id")]
|
|
status: TaskStatus,
|
|
created_at: chrono::DateTime<Utc>,
|
|
#[tabled(display = "display_option_date")]
|
|
updated_at: Option<chrono::DateTime<Utc>>,
|
|
}
|
|
|
|
impl<T: TaskPayload> Task<T> {
|
|
pub fn new(payload_key: String, payload: T, status: TaskStatus) -> Self {
|
|
Self {
|
|
id: 0,
|
|
payload_key,
|
|
payload,
|
|
status,
|
|
created_at: Default::default(),
|
|
updated_at: None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: TaskPayload> Task<T> {
|
|
pub fn payload(&self) -> &T {
|
|
&self.payload
|
|
}
|
|
}
|
|
|
|
impl<T: TaskPayload> Task<T> {
|
|
pub fn get_key(&self) -> String {
|
|
self.payload_key.clone()
|
|
}
|
|
}
|
|
|
|
fn display_option_date(o: &Option<chrono::DateTime<Utc>>) -> String {
|
|
match o {
|
|
Some(s) => format!("{}", s),
|
|
None => String::from(""),
|
|
}
|
|
}
|
|
|