Skip to content

Commit ba6e4f8

Browse files
committed
refactor(suite): use orm-style database manipulation
1 parent b170034 commit ba6e4f8

3 files changed

Lines changed: 13 additions & 53 deletions

File tree

netmito/src/client/http.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,11 +1391,7 @@ impl MitoHttpClient {
13911391
}
13921392
}
13931393

1394-
pub async fn cancel_task_suite(
1395-
&mut self,
1396-
uuid: Uuid,
1397-
force: bool,
1398-
) -> crate::error::Result<()> {
1394+
pub async fn cancel_task_suite(&mut self, uuid: Uuid, force: bool) -> crate::error::Result<()> {
13991395
self.url.set_path(&format!("suites/{uuid}"));
14001396
if force {
14011397
self.url.set_query(Some("op=force"));

netmito/src/service/suite.rs

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -540,34 +540,6 @@ pub async fn user_get_task_suite_by_uuid(
540540
})
541541
}
542542

543-
/// Set a suite `Open → Closed`. Idempotency is enforced by the `state = Open` filter.
544-
pub(crate) async fn close_task_suite<C>(
545-
db: &C,
546-
task_suite_id: i64,
547-
now: TimeDateTimeWithTimeZone,
548-
) -> Result<()>
549-
where
550-
C: ConnectionTrait,
551-
{
552-
let updated = TaskSuites::Entity::update_many()
553-
.col_expr(
554-
TaskSuites::Column::State,
555-
Expr::value(TaskSuiteState::Closed),
556-
)
557-
.col_expr(TaskSuites::Column::UpdatedAt, Expr::value(now))
558-
.filter(TaskSuites::Column::Id.eq(task_suite_id))
559-
.filter(TaskSuites::Column::State.eq(TaskSuiteState::Open))
560-
.exec(db)
561-
.await?;
562-
563-
if updated.rows_affected != 1 {
564-
return Err(Error::ApiError(ApiError::NotFound(
565-
"Task suite not found or already closed".to_string(),
566-
)));
567-
}
568-
Ok(())
569-
}
570-
571543
/// Close a suite (`Open → Closed`). `Closed` is only an idle marker — the suite
572544
/// still accepts and runs tasks, and a new task reopens it. Requires Write/Admin
573545
/// in the suite's group.
@@ -600,7 +572,11 @@ pub async fn user_close_task_suite(user_id: i64, pool: &InfraPool, suite_uuid: U
600572
))));
601573
}
602574

603-
close_task_suite(txn, suite.id, now).await
575+
let mut suite: TaskSuites::ActiveModel = suite.into();
576+
suite.state = Set(TaskSuiteState::Closed);
577+
suite.updated_at = Set(now);
578+
suite.update(txn).await?;
579+
Ok(())
604580
})
605581
})
606582
.await?;
@@ -631,7 +607,7 @@ pub async fn user_cancel_task_suite(
631607
.one(txn)
632608
.await?
633609
.ok_or(Error::ApiError(ApiError::NotFound(format!(
634-
"Task suite with uuid {suite_uuid}"
610+
"Task suite with uuid {suite_uuid} not found"
635611
))))?;
636612

637613
UserGroup::Entity::find()
@@ -718,23 +694,11 @@ pub async fn user_cancel_task_suite(
718694
.await?;
719695
}
720696

721-
let updated = TaskSuites::Entity::update_many()
722-
.col_expr(
723-
TaskSuites::Column::State,
724-
Expr::value(TaskSuiteState::Cancelled),
725-
)
726-
.col_expr(TaskSuites::Column::UpdatedAt, Expr::value(now))
727-
.col_expr(TaskSuites::Column::CompletedAt, Expr::value(now))
728-
.filter(TaskSuites::Column::Id.eq(suite.id))
729-
.exec(txn)
730-
.await?;
731-
732-
if updated.rows_affected != 1 {
733-
return Err(Error::ApiError(ApiError::InvalidRequest(
734-
"Failed to update task suite state. Maybe due to concurrent state update"
735-
.to_string(),
736-
)));
737-
}
697+
let mut suite: TaskSuites::ActiveModel = suite.into();
698+
suite.state = Set(TaskSuiteState::Cancelled);
699+
suite.updated_at = Set(now);
700+
suite.completed_at = Set(Some(now));
701+
suite.update(txn).await?;
738702

739703
Ok(cancelled_count)
740704
})

netmito/src/service/task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ async fn internal_submit_task(
212212

213213
// Not pending, notify agent/worker depending on whether it belongs to a suite
214214
match suite {
215-
Some(suite) => {
215+
Some(_suite) => {
216216
// TODO: If this task belongs to a suite, notify agents
217217
}
218218
None => {

0 commit comments

Comments
 (0)