Skip to content
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

Add ability to pause jobs by queue, job class, or label (experimental) #1575

Merged
merged 8 commits into from
Jan 22, 2025

Conversation

bensheldon
Copy link
Owner

@bensheldon bensheldon commented Jan 18, 2025

Technical Description

Paused values are stored in the GoodJob::Setting table. During the fetch-and-lock query, this record is loaded as sub=selects that add additional scopes to the query that exclude them. Paused jobs will appear as "queued" (a visible "paused" status in the Dashboard is tbd).

I'm unsure of the performance impact of the query scopes, so I've made this feature "opt-in".

Query right now looks like this:

WHERE "good_jobs"."queue_name" NOT IN (SELECT jsonb_array_elements_text(value->'queues') FROM "good_job_settings" WHERE "good_job_settings"."key" = 'pauses') 
  AND "good_jobs"."job_class" NOT IN (SELECT jsonb_array_elements_text(value->'job_classes') FROM "good_job_settings" WHERE "good_job_settings"."key" = 'pauses') 
  AND NOT ("good_jobs"."labels" && ARRAY((SELECT jsonb_array_elements_text(value->'labels') FROM "good_job_settings" WHERE "good_job_settings"."key" = 'pauses')))

Connects to #919, #1574

I put the pause/unpause buttons on the Performance page. I think that could be problematic, though I'm not sure if it will be more or less so than how it affects the fetch-and-lock query to add new conditions.

Future thoughts:

  • Should probably also allow pausing based on label
  • I think Pausing may deserve it's own page. The Performance page already has performance problems (the irony!) so this should probably simply be a "type in the thing you want to pause"
  • The views should be updated to show a "Paused" status if the individual job matches the conditions. Paused jobs will still show up on "Queued" page.

@bensheldon bensheldon force-pushed the enable-pause branch 5 times, most recently from 736b14e to 240888b Compare January 19, 2025 00:16
@jnunemaker
Copy link

Exciting!

@bensheldon
Copy link
Owner Author

It's happening!

Comment on lines 99 to 125
# Exclude jobs that are paused via queue_name or job_class.
# Only applies when enable_pause configuration is true.
# @!method exclude_paused
# @!scope class
# @return [ActiveRecord::Relation]
scope :exclude_paused, lambda {
return all unless GoodJob.configuration.enable_pauses

paused_query = GoodJob::Setting.where(key: GoodJob::Setting::PAUSES)
paused_queues_query = paused_query.select("jsonb_array_elements_text(value->'queues')")
paused_job_classes_query = paused_query.select("jsonb_array_elements_text(value->'job_classes')")
paused_labels_query = paused_query.select("jsonb_array_elements_text(value->'labels')")

where.not(queue_name: paused_queues_query)
.where.not(job_class: paused_job_classes_query)
.where(
Arel::Nodes::Not.new(
Arel::Nodes::NamedFunction.new(
"COALESCE", [
Arel::Nodes::InfixOperation.new('&&', arel_table['labels'], Arel::Nodes::NamedFunction.new('ARRAY', [paused_labels_query.arel])),
Arel::Nodes::SqlLiteral.new('FALSE'),
]
)
)
)
}

Copy link
Owner Author

@bensheldon bensheldon Jan 19, 2025

Choose a reason for hiding this comment

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

This is the hot query, now with labels, which makes it slightly more complicated.

@bensheldon bensheldon force-pushed the enable-pause branch 6 times, most recently from 43e560a to d6f3d73 Compare January 19, 2025 20:46
README.md Outdated Show resolved Hide resolved
app/controllers/good_job/performance_controller.rb Outdated Show resolved Hide resolved
config/routes.rb Outdated Show resolved Hide resolved
lib/good_job/configuration.rb Outdated Show resolved Hide resolved
@bensheldon bensheldon changed the title Add ability to pause jobs by queue or job class (experimental) Add ability to pause jobs by queue, job class, or label (experimental) Jan 19, 2025
@bensheldon bensheldon added the enhancement New feature or request label Jan 19, 2025
@bensheldon bensheldon merged commit 19234ee into main Jan 22, 2025
31 checks passed
@bensheldon bensheldon deleted the enable-pause branch January 22, 2025 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging this pull request may close these issues.

2 participants