Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions src/scheduler/run_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,37 @@ impl Scheduler {
Ok(true)
}

/// Dispatch pending tasks using batch pop on the fast path (no gate
/// checks), falling back to one-at-a-time dispatch on the slow path.
async fn dispatch_pending(&self) -> Result<(), StoreError> {
if self.inner.fast_dispatch.load(AtomicOrdering::Relaxed) {
loop {
let active_count = self.inner.active.count();
let max = self.inner.max_concurrency.load(AtomicOrdering::Relaxed);
if active_count >= max {
break;
}
let available = max - active_count;
let tasks = self.inner.store.pop_next_batch(available).await?;
if tasks.is_empty() {
break;
}
for task in tasks {
self.spawn_dispatched_task(task).await?;
}
}
} else {
loop {
match self.try_dispatch().await {
Ok(true) => continue,
Ok(false) => break,
Err(e) => return Err(e),
}
}
}
Ok(())
}

/// Run the scheduler loop until the cancellation token is triggered.
///
/// This is the main entry point. The loop wakes on three conditions:
Expand Down Expand Up @@ -385,16 +416,10 @@ impl Scheduler {
}
}

// Try to dispatch tasks until we can't.
loop {
match self.try_dispatch().await {
Ok(true) => continue,
Ok(false) => break,
Err(e) => {
tracing::error!(error = %e, "scheduler dispatch error");
break;
}
}
// Dispatch pending tasks — batch pop on the fast path, one-at-a-time
// with gate checks on the slow path.
if let Err(e) = self.dispatch_pending().await {
tracing::error!(error = %e, "scheduler dispatch error");
}
}

Expand Down
1 change: 1 addition & 0 deletions src/store/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ impl TaskStore {
&IoBudget::default(),
None,
Some(&format!("dependency task {} failed", failed_task_id)),
false,
)
.await?;

Expand Down
1 change: 1 addition & 0 deletions src/store/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl TaskStore {
&crate::task::IoBudget::default(),
None,
None,
false,
)
.await?;
}
Expand Down
5 changes: 5 additions & 0 deletions src/store/lifecycle/cancel_expire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl TaskStore {
&IoBudget::default(),
duration_ms,
None,
false,
)
.await?;

Expand Down Expand Up @@ -99,6 +100,7 @@ impl TaskStore {
&IoBudget::default(),
duration_ms,
None,
false,
)
.await?;

Expand Down Expand Up @@ -193,6 +195,7 @@ impl TaskStore {
&IoBudget::default(),
None,
None,
false,
)
.await?;

Expand All @@ -215,6 +218,7 @@ impl TaskStore {
&IoBudget::default(),
None,
None,
false,
)
.await?;
crate::store::delete_task_tags(&mut conn, child.id).await?;
Expand Down Expand Up @@ -286,6 +290,7 @@ impl TaskStore {
&IoBudget::default(),
None,
None,
false,
)
.await?;

Expand Down
26 changes: 17 additions & 9 deletions src/store/lifecycle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,19 @@ impl HistoryStatus {
///
/// Shared by `complete()`, `fail()`, and `cancel_to_history()` to eliminate
/// the duplicated 22-column INSERT statement.
///
/// When `skip_tags` is `true` the history-tag copy (`INSERT INTO
/// task_history_tags … SELECT FROM task_tags`) is skipped. Callers that
/// know no tags have been inserted (e.g. via the `has_tags` fast-path
/// flag) can set this to avoid an unnecessary SQL round-trip.
pub(crate) async fn insert_history(
conn: &mut sqlx::pool::PoolConnection<sqlx::Sqlite>,
task: &TaskRecord,
status: HistoryStatus,
metrics: &IoBudget,
duration_ms: Option<i64>,
last_error: Option<&str>,
skip_tags: bool,
) -> Result<(), StoreError> {
let fail_fast_val: i32 = if task.fail_fast { 1 } else { 0 };
let retry_count = if status.increments_retries() {
Expand Down Expand Up @@ -110,15 +116,17 @@ pub(crate) async fn insert_history(
.await?;

// Copy tags from task_tags to task_history_tags.
let history_rowid = result.last_insert_rowid();
sqlx::query(
"INSERT INTO task_history_tags (history_rowid, key, value)
SELECT ?, key, value FROM task_tags WHERE task_id = ?",
)
.bind(history_rowid)
.bind(task.id)
.execute(&mut **conn)
.await?;
if !skip_tags {
let history_rowid = result.last_insert_rowid();
sqlx::query(
"INSERT INTO task_history_tags (history_rowid, key, value)
SELECT ?, key, value FROM task_tags WHERE task_id = ?",
)
.bind(history_rowid)
.bind(task.id)
.execute(&mut **conn)
.await?;
}

Ok(())
}
Expand Down
Loading
Loading