Skip to content

Added: Support for Pause and Resume #3669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft

Conversation

Sewer56
Copy link
Member

@Sewer56 Sewer56 commented Jul 30, 2025

Job Pause/Resume Implementation

Spent a bit thinking about the problem; including how to best handle
requested upcoming features such as downloads pausing/resuming.

I think the solution here is decent; it's a bit different than originally proposed, which was a cancel/restart.
This is more of a true pause/resume, by stalling existing jobs without hogging the threadpool.

Summary

Adds pause/resume functionality to the job system using a cooperative model, with both standard and pre-emptive pause modes.

Jobs can be paused using existing YieldAsync() yield points and resumed later,
with cancellation taking precedence over pause.

Main Points

  • JobCancellationToken: New token combining cancellation + pause/resume using AsyncManualResetEvent
  • YieldAsync(): Now handles both pause detection and waiting for resume
    • Many of our existing jobs are now pausable without requiring code changes.
  • API: Added Pause()/Resume() methods to IJob, IJobGroup, IJobMonitor
  • Force Pause: For jobs with long-running nested operations a 'pre-emptive' pause mode is available:
    • Set SupportsForcePause => true and handle OperationCanceledException with HandlePauseExceptionAsync()
    • See example.

Tests & Examples

  • Example (co-operative) usage in Examples/BestPractices/PauseResumeExample.cs
  • Force pause (pre-emptive) example in Examples/BestPractices/ForcePauseExample.cs
  • Test for new JobCancellationToken (e.g. Pausing, Waiting, etc.) in NexusMods.Jobs.Tests/Unit/JobCancellationTokenTests.cs
  • Test for pausing in jobs in NexusMods.Jobs.Tests/Unit/JobPauseResumeTests.cs
  • Test for force pause functionality in NexusMods.Jobs.Tests/Unit/JobForcePauseResumeTests.cs

Misc

  • Added AsyncEx package rather than implementing own AsyncManualResetEvent by hand.

Tests were written with extra care to prevent flakiness in CI.
As we know, the Windows GitHub Actions test runner can be unreliable and stall several seconds at a time. So I've been very careful with how we wait.

fixes #3604

@Sewer56 Sewer56 marked this pull request as ready for review July 30, 2025 23:21
@Sewer56 Sewer56 requested a review from a team July 30, 2025 23:21
@Sewer56 Sewer56 self-assigned this Jul 30, 2025
@Sewer56 Sewer56 added this to MVP Jul 30, 2025
@Sewer56 Sewer56 moved this to Review in MVP Jul 30, 2025
@erri120 erri120 changed the title Added: Native Support for Pause and Resume Added: Support for Pause and Resume Jul 31, 2025
Copy link
Member

@erri120 erri120 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't follow the requirements set out by #3604.

If you think a different approach to #3604 is more appropriate, you should've commented on #3604, explained your approach and allowed for others to comment on it as well.

@Al12rs
Copy link
Contributor

Al12rs commented Jul 31, 2025

From looking at the code, the main reason this alternative approach was implemented was to:

  • Support cancellation of a paused job

But the pausing approach proposed doesn't work well for our purposes because it

  • requires cooperative YieldAsync calls rather than Token cancellation, which means that we can't pause external code that takes tokens.
  • We want pause to stop operation even if inside nested async calls that supports token cancellation.

@Sewer56 Sewer56 marked this pull request as draft July 31, 2025 09:21
Added: ForcePauseExample
Updated: README Pointing to Force-Pause Feature
Updated: ForcePauseExample with further clarification
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Review
Development

Successfully merging this pull request may close these issues.

Add pause and resume support to the job system
3 participants