2024-03-25 05:07:58 +01:00
|
|
|
use sea_orm::entity::prelude::*;
|
|
|
|
|
2024-06-06 02:20:43 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
|
2024-06-07 07:12:48 +02:00
|
|
|
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
|
2024-06-06 02:20:43 +02:00
|
|
|
pub enum JobType {
|
|
|
|
Inbound = 1,
|
|
|
|
Outbound = 2,
|
2024-06-06 19:04:08 +02:00
|
|
|
Delivery = 3,
|
2024-06-06 02:20:43 +02:00
|
|
|
}
|
|
|
|
|
2024-03-25 05:07:58 +01:00
|
|
|
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
2024-06-06 02:20:43 +02:00
|
|
|
#[sea_orm(table_name = "jobs")]
|
2024-03-25 05:07:58 +01:00
|
|
|
pub struct Model {
|
|
|
|
#[sea_orm(primary_key)]
|
2024-05-25 04:37:17 +02:00
|
|
|
pub internal: i64,
|
2024-06-06 02:20:43 +02:00
|
|
|
pub job_type: JobType,
|
2024-05-25 04:37:17 +02:00
|
|
|
pub actor: String,
|
2024-06-06 02:20:43 +02:00
|
|
|
pub target: Option<String>,
|
2024-05-25 04:37:17 +02:00
|
|
|
pub activity: String,
|
2024-06-08 16:58:16 +02:00
|
|
|
pub payload: Option<serde_json::Value>,
|
2024-05-25 07:00:03 +02:00
|
|
|
pub published: ChronoDateTimeUtc,
|
2024-03-25 05:07:58 +01:00
|
|
|
pub not_before: ChronoDateTimeUtc,
|
2024-06-07 07:15:47 +02:00
|
|
|
pub attempt: i16,
|
2024-07-06 06:03:26 +02:00
|
|
|
pub error: Option<String>,
|
2024-03-25 05:07:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
2024-06-06 02:20:43 +02:00
|
|
|
pub enum Relation {}
|
2024-03-25 05:07:58 +01:00
|
|
|
|
|
|
|
impl ActiveModelBehavior for ActiveModel {}
|
|
|
|
|
|
|
|
impl Model {
|
2024-06-06 02:20:43 +02:00
|
|
|
pub fn next_attempt(&self) -> ChronoDateTimeUtc {
|
2024-03-25 05:07:58 +01:00
|
|
|
match self.attempt {
|
|
|
|
0 => chrono::Utc::now() + std::time::Duration::from_secs(10),
|
|
|
|
1 => chrono::Utc::now() + std::time::Duration::from_secs(60),
|
|
|
|
2 => chrono::Utc::now() + std::time::Duration::from_secs(5 * 60),
|
|
|
|
3 => chrono::Utc::now() + std::time::Duration::from_secs(20 * 60),
|
|
|
|
4 => chrono::Utc::now() + std::time::Duration::from_secs(60 * 60),
|
|
|
|
5 => chrono::Utc::now() + std::time::Duration::from_secs(12 * 60 * 60),
|
|
|
|
_ => chrono::Utc::now() + std::time::Duration::from_secs(24 * 60 * 60),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-06 06:03:26 +02:00
|
|
|
pub fn repeat(self, error: Option<String>) -> ActiveModel {
|
2024-06-06 02:20:43 +02:00
|
|
|
ActiveModel {
|
|
|
|
internal: sea_orm::ActiveValue::NotSet,
|
|
|
|
job_type: sea_orm::ActiveValue::Set(self.job_type),
|
|
|
|
not_before: sea_orm::ActiveValue::Set(self.next_attempt()),
|
|
|
|
actor: sea_orm::ActiveValue::Set(self.actor),
|
|
|
|
target: sea_orm::ActiveValue::Set(self.target),
|
|
|
|
payload: sea_orm::ActiveValue::Set(self.payload),
|
|
|
|
activity: sea_orm::ActiveValue::Set(self.activity),
|
|
|
|
published: sea_orm::ActiveValue::Set(self.published),
|
|
|
|
attempt: sea_orm::ActiveValue::Set(self.attempt + 1),
|
2024-07-06 06:03:26 +02:00
|
|
|
error: sea_orm::ActiveValue::Set(error),
|
2024-06-06 02:20:43 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-25 05:07:58 +01:00
|
|
|
}
|