fix: fix cron webui

This commit is contained in:
2025-07-06 02:35:55 +08:00
parent 3aad31a36b
commit 4174cea728
22 changed files with 382 additions and 123 deletions

View File

@@ -31,7 +31,8 @@ impl MigrationTrait for Migration {
CronStatus::Pending,
CronStatus::Running,
CronStatus::Completed,
CronStatus::Failed
CronStatus::Failed,
CronStatus::Disabled
)
.await?;
@@ -49,7 +50,7 @@ impl MigrationTrait for Migration {
.col(boolean(Cron::Enabled).default(true))
.col(string_null(Cron::LockedBy))
.col(timestamp_with_time_zone_null(Cron::LockedAt))
.col(integer_null(Cron::TimeoutMs))
.col(integer_null(Cron::TimeoutMs).default(5000))
.col(integer(Cron::Attempts).default(0))
.col(integer(Cron::MaxAttempts).default(1))
.col(integer(Cron::Priority).default(0))
@@ -243,8 +244,8 @@ impl MigrationTrait for Migration {
.from_col(ApalisJobs::CronId)
.to_tbl(Cron::Table)
.to_col(Cron::Id)
.on_delete(ForeignKeyAction::NoAction)
.on_update(ForeignKeyAction::NoAction),
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Restrict),
)
.to_owned(),
)

View File

@@ -41,6 +41,8 @@ pub enum CronStatus {
Completed,
#[sea_orm(string_value = "failed")]
Failed,
#[sea_orm(string_value = "disabled")]
Disabled,
}
#[derive(Debug, Clone, DeriveEntityModel, PartialEq, Serialize, Deserialize)]
@@ -138,7 +140,7 @@ pub enum RelatedEntity {
#[async_trait]
impl ActiveModelBehavior for ActiveModel {
async fn before_save<C>(mut self, _db: &C, _insert: bool) -> Result<Self, DbErr>
async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, DbErr>
where
C: ConnectionTrait,
{
@@ -189,6 +191,15 @@ impl ActiveModelBehavior for ActiveModel {
"Cron subscriber_id does not match system_task_cron.subscriber_id".to_string(),
));
}
if let ActiveValue::Set(enabled) = self.enabled
&& !insert
{
if enabled {
self.status = Set(CronStatus::Pending)
} else {
self.status = Set(CronStatus::Disabled)
}
}
Ok(self)
}

View File

@@ -52,23 +52,23 @@ pub enum Relation {
from = "Column::SubscriberId",
to = "super::subscribers::Column::Id",
on_update = "Cascade",
on_delete = "NoAction"
on_delete = "Restrict"
)]
Subscriber,
#[sea_orm(
belongs_to = "super::subscriptions::Entity",
from = "Column::SubscriptionId",
to = "super::subscriptions::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
on_update = "Cascade",
on_delete = "Restrict"
)]
Subscription,
#[sea_orm(
belongs_to = "super::cron::Entity",
from = "Column::CronId",
to = "super::cron::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
on_update = "Cascade",
on_delete = "Restrict"
)]
Cron,
}

View File

@@ -57,8 +57,8 @@ pub enum Relation {
belongs_to = "super::cron::Entity",
from = "Column::CronId",
to = "super::cron::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
on_update = "Cascade",
on_delete = "Restrict"
)]
Cron,
}

View File

@@ -49,7 +49,7 @@ pub fn default_system_task_workers() -> u32 {
}
pub fn default_cron_interval_duration() -> Duration {
Duration::from_secs(60)
Duration::from_secs(30)
}
pub fn default_subscriber_task_reenqueue_orphaned_after() -> Duration {

View File

@@ -373,6 +373,7 @@ mod tests {
let echo_cron = cron::ActiveModel {
cron_expr: ActiveValue::Set("*/1 * * * * *".to_string()),
cron_timezone: ActiveValue::Set("Asia/Singapore".to_string()),
system_task_cron: ActiveValue::Set(Some(
EchoTask::builder().task_id(task_id.clone()).build().into(),
)),
@@ -406,6 +407,7 @@ mod tests {
let echo_cron = cron::ActiveModel {
cron_expr: ActiveValue::Set("* * * */1 * *".to_string()),
cron_timezone: ActiveValue::Set("Asia/Singapore".to_string()),
next_run: ActiveValue::Set(Some(Utc::now() + chrono::Duration::seconds(-10))),
system_task_cron: ActiveValue::Set(Some(
EchoTask::builder().task_id(task_id.clone()).build().into(),