Skip to content

fix: batch-mode FailFast, start times and token refunds; live Running->Pending updates; CLI, web and test follow-ups (#94) - #96

Merged
CodingAnarchy merged 5 commits into
masterfrom
fix/issue-94
Oct 11, 2026
Merged

CodingAnarchy merged 5 commits into
masterfrom
fix/issue-94

Conversation

@CodingAnarchy

Copy link
Copy Markdown
Owner

Closes #94

Fixes the batch-mode and live-update follow-ups in the issue body and the CLI, web and test items in the comment. Everything is non-breaking for 2.1.1. The new trait methods all have default implementations, so no item had to be moved to 3.0.

Batch mode (src/worker.rs, src/queue/*)

  • FailFast batches with held jobs. run_batch_until_done already checks for a pause before starting a held job, at most once per poll interval. That same check now also reads the status of the batches the held jobs belong to (get_batch_status), one query per distinct batch, and remembers which ones have failed. A held job of a failed batch is failed the same way FailFast fails its Pending jobs: status Failed, no retry, error Batch failed (fail-fast mode) before this claimed job started. Before failing it, the worker sends a heartbeat (heartbeat_job) to confirm it still holds the run. It does not release the job, because a released job would be Pending in a failed batch and would run later. On a pause or shutdown, held jobs of a failed batch are failed instead of released.
  • Processing time excludes waiting time. A new defaulted DatabaseQueue::start_claimed_run(&mut Job, lease) sets started_at to the current database time and renews the lease, guarded by the run. It also updates the in-memory run, because started_at is part of what identifies a run. PostgreSQL uses RETURNING. MySQL reads UTC_TIMESTAMP(6) first and writes that value. TestQueue implements it too. The default implementation only sends a heartbeat. Held jobs call it when they start after the first fill, and it replaces the old "renew then run" heartbeat. Jobs that start straight after the claim keep the claim time and skip the extra write.
  • Lost-lease token refund. A held job skipped because its lease was lost now refunds its rate-limit token, whether the loss was detected while it waited or when it started.

Live updates (hammerwork-web)

recent_job_changes filters on timestamps, and a Running to Pending transition (a retry, a reaper requeue, a batch release) changes none of them. Detecting it without state would need a new column written on every transition, or a trigger. That is a schema change, and on MySQL a trigger can need extra privileges, which is too much for a patch release. Instead, LiveUpdates keeps track of the jobs it has seen running:

  • JobHistory::running_jobs(limit) returns the jobs currently Running. The tracked set is these plus any Running rows in the change feed.
  • A tracked job that appears in neither result has its current state read with JobHistory::job_states(ids) (at most max_jobs ids per poll). If it is no longer Running, a JobUpdate is pushed, stamped with the time of the poll that saw the change. If it is still running, it only fell out of the window and stays tracked.

Both methods have defaults (job_states falls back to get_job). The recent_job_changes docs now say this.

CLI (cargo-hammerwork)

  • backup create/restore and migration status connect with connect_with_config, so connect_timeout_secs applies.
  • maintenance vacuum reports the row counts the DELETEs returned, not the counts taken before them.
  • maintenance dead-jobs text and --stale-hours help say what is filtered: Running jobs that started more than N hours ago. README updated.
  • monitor's status icons cover Retrying and TimedOut, and tolerate JSON-quoted status values.
  • webhook update --name refuses a name another webhook already has. Keeping its own name is still allowed.

Web

  • SystemState::uptime_seconds is clamped at 0, and callers convert with u64::try_from, so a clock going backwards no longer wraps the value.
  • SortParams::is_ascending matches asc case-insensitively. The job list and get_order_by both use it.
  • An archive dry run fills stats.jobs_archived with the count a real run would archive, from the new defaulted JobHistory::count_archivable. It uses the archiver's conditions and the policy's batch_size cap.

Tests

  • batch_tests (large batch): checks every index comes back exactly once and that created_at never decreases. It no longer computes an index and ignores it.
  • test_batch_statistics_tracking: runs the worker through an Arc, so its statistics stay readable after it stops, and asserts processed, completed, failed, batches, success rates and lower bounds on processing time.
  • Leaky tests: the new test_utils::QueueCleanup drop guard deletes a queue's jobs on its own thread, runtime and connection. It works under any runtime and also runs when a test panics. It is used in result_storage_tests, result_config_test, jobrow_query_tests and test_mysql_concurrent_dequeue. Tests that shared the test_queue name now use unique queues.
  • VAULT_TOKEN: the test re-runs itself in a child process started with env_remove("VAULT_TOKEN"), so the test process's environment is never changed and no unsafe is needed. It now asserts that the error names VAULT_TOKEN.
  • New regression tests:
    • Batch mode, both backends: fail_fast_batch_fails_held_jobs, held_jobs_record_their_actual_start and lost_held_job_refunds_its_rate_limit_token. All three failed with the fixes reverted.
    • Live updates, both backends: clients_see_running_jobs_go_back_to_pending. It failed with the tracking turned off.
    • Unit tests: the live-update tracking helpers, start_claimed_run on TestQueue, the archivable-count SQL, sort order, uptime and status icons.
    • CLI: connect-timeout tests for backup and migration status, vacuum counts on a scratch database, and the webhook rename check.
  • Timing: new tests wait on conditions with generous deadlines and check individual rows. The start-time test compares database timestamps with each other (each started_at is at or after the previous job's completed_at), so it doesn't depend on elapsed time.

Verification

  • PostgreSQL run (--include-ignored --skip mysql): 1969 passed, 0 failed.
  • MySQL run (--include-ignored mysql, both URLs on MySQL): 202 passed, 0 failed.
  • cargo test -p hammerwork --doc: 186 passed.
  • cargo fmt --all --check and cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: clean.
  • cargo clippy -p hammerwork --lib --tests --no-default-features for every CI matrix entry: clean.

🤖 Generated with Claude Code

CodingAnarchy and others added 5 commits October 11, 2026 01:40
…n refund for held batch jobs

A batch-mode worker now checks, at most once per poll interval together with
the pause check, whether the batch of a held job has failed; held jobs of a
failed fail-fast batch are failed like its Pending jobs instead of run (or
released on pause/shutdown). Held jobs that waited record their actual start
through the new defaulted DatabaseQueue::start_claimed_run, so their wait is
not counted as processing time. A held job skipped because its lease was lost
refunds its rate limit token.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ates

Retries, reaper requeues and batch releases change none of the timestamps
recent_job_changes reads, so live-update clients never saw them. Without a
schema change there is no timestamp to query, so the poller now tracks the
running jobs (JobHistory::running_jobs) and reads the state of the ones that
stopped running unseen (JobHistory::job_states), reporting them at the poll
that observed the change. Both trait methods have defaults, so the change is
non-breaking.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…icons, webhook renames

- backup create/restore and migration status connect with connect_with_config,
  so connect_timeout_secs applies.
- maintenance vacuum reports the rows the deletes removed.
- maintenance dead-jobs says it selects jobs that started more than N hours ago.
- monitor's status icons cover Retrying and TimedOut.
- webhook update --name rejects a name another webhook already has.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-run counts

- uptime_seconds never goes negative, and callers convert without wrapping.
- The job list treats sort_order "asc" case-insensitively (SortParams::is_ascending).
- An archive dry run reports the number of jobs a real run would archive
  (JobHistory::count_archivable) instead of zeroed statistics.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… isolate VAULT_TOKEN

- batch_tests checks that every job of a large batch comes back once, in
  creation order, instead of computing an index it never compared.
- test_batch_statistics_tracking asserts the worker's batch statistics.
- result_storage_tests, result_config_test, jobrow_query_tests and
  test_mysql_concurrent_dequeue delete their jobs through a drop guard
  (test_utils::QueueCleanup), so they clean up even when they fail; tests that
  shared the "test_queue" name now use unique queues.
- The VAULT_TOKEN test re-runs itself in a child process without the variable
  instead of changing the environment of a process whose other tests run in
  parallel, and checks the error names VAULT_TOKEN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@CodingAnarchy
CodingAnarchy merged commit 4e85b16 into master Oct 11, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Batch mode and live update follow-ups from the 2.1 bug hunt

1 participant