Repository navigation
feat: discard ready jobs from the ready jobs page #51
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/services/solid_queue_monitor/discard_ready_job_service.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SolidQueueMonitor | ||
| class DiscardReadyJobService | ||
| def call(id) | ||
| discard_job(id) | ||
| end | ||
|
|
||
| def discard_many(ids) | ||
| return { success: false, message: 'No jobs selected' } if ids.blank? | ||
|
|
||
| success_count = 0 | ||
| failed_count = 0 | ||
|
|
||
| Array(ids).each do |id| | ||
| if discard_job(id) | ||
| success_count += 1 | ||
| else | ||
| failed_count += 1 | ||
| end | ||
| rescue StandardError | ||
| failed_count += 1 | ||
| end | ||
|
|
||
| if success_count.positive? && failed_count.zero? | ||
| { success: true, message: 'All selected jobs have been discarded' } | ||
| elsif success_count.positive? && failed_count.positive? | ||
| { success: true, message: "#{success_count} jobs discarded, #{failed_count} failed" } | ||
| else | ||
| { success: false, message: 'Failed to discard jobs' } | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Deleting the ready execution is what claims the job, as it is for a | ||
| # worker: whoever deletes the row first wins. This holds on SQLite too, | ||
| # where the row lock is a no-op, so a job a worker already claimed counts | ||
| # as failed instead of being marked finished while it runs. | ||
| def discard_job(id) | ||
| SolidQueue::ReadyExecution.transaction do | ||
| execution = SolidQueue::ReadyExecution.lock.find_by(id: id) | ||
| next false unless execution | ||
| next false unless SolidQueue::ReadyExecution.where(id: execution.id).delete_all == 1 | ||
|
|
||
| # An execution whose job row is gone is just removed. | ||
| job = SolidQueue::Job.find_by(id: execution.job_id) | ||
| next true unless job | ||
|
|
||
| release_concurrency_slot(job) | ||
| job.update!(finished_at: Time.current) | ||
| true | ||
| end | ||
| end | ||
|
|
||
| # A ready job holds its concurrency slot; give it back so blocked jobs | ||
| # with the same key can run. `try` because Solid Queue versions without | ||
| # concurrency controls lack these methods. | ||
| def release_concurrency_slot(job) | ||
| return unless job.try(:concurrency_limited?) | ||
|
|
||
| if job.class_name.safe_constantize | ||
| job.unblock_next_blocked_job | ||
| else | ||
| # The limit comes from the job class, so without it the next blocked | ||
| # job can't be released here. Free the slot by key, as Solid Queue's | ||
| # own bulk discard does; its dispatcher then unblocks waiting jobs. | ||
| SolidQueue::Job.release_all_concurrency_locks([job]) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,19 @@ | ||
| <tr> | ||
| <td><input type="checkbox" class="ready-job-checkbox" value="<%= job.id %>"></td> | ||
| <td><%= link_to job.job.class_name, job_path(job.job), class: 'job-class-link' %></td> | ||
| <td><%= queue_link(job.queue_name) %></td> | ||
| <td><%= job.priority %></td> | ||
| <td><%= format_arguments(job.job.arguments) %></td> | ||
| <td><%= format_datetime(job.created_at) %></td> | ||
| <td class="actions-cell"> | ||
| <div class="job-actions"> | ||
| <form method="post" | ||
| action="<%= discard_ready_job_path(id: job.id) %>" | ||
| class="inline-form" | ||
| data-confirm="Are you sure you want to discard this job? It will not run."> | ||
| <%= csrf_token_field_if_enabled %> | ||
| <button type="submit" class="action-button discard-button">Discard</button> | ||
| </form> | ||
| </div> | ||
| </td> | ||
| </tr> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.