Skip to content

Commit

Permalink
Only show Dashboard UI when configuration is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
bensheldon committed Jan 19, 2025
1 parent 240888b commit ed7eadc
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 36 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,7 @@ If WEBrick is configured to be used, but the dependency is not found, GoodJob wi
GoodJob allows for pausing jobs by queue or job class. This feature is currently opt-in because the performance impact of loading and filtering these attributes is not yet known. To enable this feature, add the following to your configuration:
> ```ruby
> config.good_job.enable_pause = true
> config.good_job.enable_pauses = true
> ```
Pausing can be done via the Dashboard's Performance page, or in Ruby
Expand Down
2 changes: 1 addition & 1 deletion app/models/good_job/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class Job < BaseRecord
# @!scope class
# @return [ActiveRecord::Relation]
scope :exclude_paused, lambda {
return all unless GoodJob.configuration.enable_pause
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')")
Expand Down
60 changes: 34 additions & 26 deletions app/views/good_job/pauses/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,39 @@
<div class="border-bottom">
<h1 class="h2 pt-3 pb-2"><%= t('good_job.pauses.index.title') %></h1>
</div>
<div class="card mb-4">
<div class="card-body">
<%= form_tag({ action: :destroy }, method: :post, class: 'row g-3 align-items-end') do %>
<div class="col-3">
<%= label_tag :type, t('good_job.pauses.index.type'), class: 'form-label' %>
<%= select_tag :type,
options_for_select([
[t('good_job.pauses.index.queue'), 'queue'],
[t('good_job.pauses.index.job_class'), 'job_class'],
[t('good_job.pauses.index.label'), 'label'],
]),
class: 'form-select',
required: true %>
</div>
<div class="col-6">
<%= label_tag :value, t('good_job.pauses.index.value'), class: 'form-label' %>
<%= text_field_tag :value, nil, class: 'form-control', autocomplete: "off", required: true %>
</div>
<div class="col-3">
<%= submit_tag t('good_job.pauses.index.pause', value: ''), class: 'btn btn-primary' %>
</div>
<% end %>

<% if GoodJob.configuration.enable_pauses %>
<div class="card mb-4">
<div class="card-body">
<%= form_tag({ action: :destroy }, method: :post, class: 'row g-3 align-items-end') do %>
<div class="col-3">
<%= label_tag :type, t('good_job.pauses.index.type'), class: 'form-label' %>
<%= select_tag :type,
options_for_select([
[t('good_job.pauses.index.queue'), 'queue'],
[t('good_job.pauses.index.job_class'), 'job_class'],
[t('good_job.pauses.index.label'), 'label'],
]),
class: 'form-select',
required: true %>
</div>
<div class="col-6">
<%= label_tag :value, t('good_job.pauses.index.value'), class: 'form-label' %>
<%= text_field_tag :value, nil, class: 'form-control', autocomplete: "off", required: true %>
</div>
<div class="col-3">
<%= submit_tag t('good_job.pauses.index.pause', value: ''), class: 'btn btn-primary' %>
</div>
<% end %>
</div>
</div>
</div>

<%= render 'group', paused: @paused, type: :queue %>
<%= render 'group', paused: @paused, type: :job_class %>
<%= render 'group', paused: @paused, type: :label %>
<%= render 'group', paused: @paused, type: :queue %>
<%= render 'group', paused: @paused, type: :job_class %>
<%= render 'group', paused: @paused, type: :label %>
<% else %>
<div class="alert alert-warning" role="alert">
<p><%= t('good_job.pauses.index.disabled') %>:</p>
<pre class="mb-0">config.good_job.enable_pauses = true</pre>
</div>
<% end %>
1 change: 1 addition & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ de:
index:
confirm_pause: Sind Sie sicher, dass Sie %{value} pausieren möchten?
confirm_unpause: Sind Sie sicher, dass Sie %{value} fortsetzen möchten?
disabled: Die experimentelle Pausenfunktion von GoodJob ist standardmäßig deaktiviert, da sie die Leistung beeinträchtigen kann. Konfigurieren Sie sie zur Aktivierung
job_class: Job-Klasse
label: Label
pause: Pausieren
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ en:
index:
confirm_pause: Are you sure you want to pause %{value}?
confirm_unpause: Are you sure you want to resume %{value}?
disabled: GoodJob's experimental Pauses feature is disabled by default because it may degrade performance. Configure to enable
job_class: Job Class
label: Label
pause: Pause
Expand Down
1 change: 1 addition & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ es:
index:
confirm_pause: "¿Está seguro de que desea pausar %{value}?"
confirm_unpause: "¿Está seguro de que desea reanudar %{value}?"
disabled: La función experimental de Pausas de GoodJob está desactivada por defecto porque puede degradar el rendimiento. Configure para habilitar
job_class: Clase de trabajo
label: Etiqueta
pause: Pausar
Expand Down
1 change: 1 addition & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ fr:
index:
confirm_pause: Êtes-vous sûr de vouloir mettre en pause %{value} ?
confirm_unpause: Êtes-vous sûr de vouloir reprendre %{value} ?
disabled: "La fonction expérimentale de Pauses de GoodJob est désactivée par défaut car elle peut dégrader les performances. Configurez pour activer"
job_class: Classe de tâche
label: Étiquette
pause: Pause
Expand Down
1 change: 1 addition & 0 deletions config/locales/it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ it:
index:
confirm_pause: Sei sicuro di voler mettere in pausa %{value}?
confirm_unpause: Sei sicuro di voler riprendere %{value}?
disabled: La funzione sperimentale Pause di GoodJob è disabilitata per impostazione predefinita perché potrebbe degradare le prestazioni. Configurare per abilitare
job_class: Classe del job
label: Etichetta
pause: Pausa
Expand Down
1 change: 1 addition & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ ja:
index:
confirm_pause: "%{value}を一時停止してもよろしいですか?"
confirm_unpause: "%{value}を再開してもよろしいですか?"
disabled: GoodJobの実験的な一時停止機能は、パフォーマンスが低下する可能性があるため、デフォルトでは無効になっています。有効にするには設定してください
job_class: ジョブクラス
label: ラベル
pause: 一時停止
Expand Down
1 change: 1 addition & 0 deletions config/locales/ko.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ ko:
index:
confirm_pause: "%{value}를 일시 중지하시겠습니까?"
confirm_unpause: "%{value}를 재개하시겠습니까?"
disabled: GoodJob의 실험적인 일시 중지 기능은 성능이 저하될 수 있으므로 기본적으로 비활성화되어 있습니다. 활성화하려면 구성하세요
job_class: 작업 클래스
label: 라벨
pause: 일시 중지
Expand Down
1 change: 1 addition & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ nl:
index:
confirm_pause: Weet u zeker dat u %{value} wilt pauzeren?
confirm_unpause: Weet u zeker dat u %{value} wilt hervatten?
disabled: De experimentele Pauzes-functie van GoodJob is standaard uitgeschakeld omdat het de prestaties kan verminderen. Configureer om in te schakelen
job_class: Taakklasse
label: Label
pause: Pauzeren
Expand Down
1 change: 1 addition & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ pt-BR:
index:
confirm_pause: Tem certeza que deseja pausar %{value}?
confirm_unpause: Tem certeza que deseja retomar %{value}?
disabled: O recurso experimental de Pausas do GoodJob está desativado por padrão porque pode degradar o desempenho. Configure para ativar
job_class: Classe do trabalho
label: Rótulo
pause: Pausar
Expand Down
1 change: 1 addition & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ ru:
index:
confirm_pause: Вы уверены, что хотите приостановить %{value}?
confirm_unpause: Вы уверены, что хотите возобновить %{value}?
disabled: Экспериментальная функция Паузы в GoodJob отключена по умолчанию, так как это может снизить производительность. Настройте для включения
job_class: Класс задачи
label: Метка
pause: Приостановить
Expand Down
1 change: 1 addition & 0 deletions config/locales/tr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ tr:
index:
confirm_pause: "%{value} duraklatmak istediğinizden emin misiniz?"
confirm_unpause: "%{value} devam ettirmek istediğinizden emin misiniz?"
disabled: GoodJob'ın deneysel Duraklatma özelliği, performansı düşürebileceği için varsayılan olarak devre dışıdır. Etkinleştirmek için yapılandırın
job_class: İş sınıfı
label: Etiket
pause: Duraklat
Expand Down
1 change: 1 addition & 0 deletions config/locales/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ uk:
index:
confirm_pause: Ви впевнені, що хочете призупинити %{value}?
confirm_unpause: Ви впевнені, що хочете відновити %{value}?
disabled: Експериментальна функція Паузи в GoodJob вимкнена за замовчуванням, оскільки це може знизити продуктивність. Налаштуйте для увімкнення
job_class: Клас завдання
label: Мітка
pause: Призупинити
Expand Down
21 changes: 20 additions & 1 deletion demo/app/jobs/example_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,26 @@ def perform(type = SUCCESS_TYPE)
elsif type == DEAD_TYPE
raise DeadError
elsif type == SLOW_TYPE
sleep 5
50.times do
break if blocking_reload?
sleep 0.1
end
end
end

private

def blocking_reload?
return false unless Rails.application.config.reloading_enabled?

ActiveSupport::Dependencies.interlock.raw_state do |threads|
# Find any thread attempting to unload (reload) code
return unless threads.any? { |_, info| info[:purpose] == :unload }

# Is the current thread blocking it? Likely yes.
info = threads[Thread.current]
info[:sharing] > 0 || # Thread holds a share lock
(info[:exclusive] && ![:load, :unload].include?(info[:purpose])) # Or holds incompatible exclusive lock
end
end
end
2 changes: 1 addition & 1 deletion demo/config/initializers/good_job.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Rails.application.configure do
config.good_job.enable_pause = true
config.good_job.enable_pauses = false
config.good_job.cron = {
example: {
cron: '*/5 * * * * *', # every 5 seconds
Expand Down
6 changes: 3 additions & 3 deletions lib/good_job/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -373,9 +373,9 @@ def enqueue_after_transaction_commit

# Whether the job processing can be paused.
# @return [Boolean]
def enable_pause
return options[:enable_pause] unless options[:enable_pause].nil?
return rails_config[:enable_pause] unless rails_config[:enable_pause].nil?
def enable_pauses
return options[:enable_pauses] unless options[:enable_pauses].nil?
return rails_config[:enable_pauses] unless rails_config[:enable_pauses].nil?
return ActiveModel::Type::Boolean.new.cast(env['GOOD_JOB_ENABLE_PAUSE']) unless env['GOOD_JOB_ENABLE_PAUSE'].nil?

DEFAULT_ENABLE_PAUSE
Expand Down
6 changes: 3 additions & 3 deletions spec/app/models/good_job/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1079,19 +1079,19 @@ def job_params
let!(:other_labeled_job) { described_class.create!(queue_name: "default", job_class: "DefaultJob", labels: ["low_priority"]) }

before do
allow(GoodJob.configuration).to receive(:enable_pause).and_return(enable_pause)
allow(GoodJob.configuration).to receive(:enable_pauses).and_return(enable_pauses)
end

context 'when enable_pause is false' do
let(:enable_pause) { false }
let(:enable_pauses) { false }

it 'returns all jobs' do
expect(described_class.exclude_paused).to contain_exactly(default_job, mailers_job, reports_job, labeled_job, other_labeled_job)
end
end

context 'when enable_pause is true' do
let(:enable_pause) { true }
let(:enable_pauses) { true }

it 'returns all jobs when nothing is paused' do
expect(described_class.exclude_paused.count).to eq 5
Expand Down

0 comments on commit ed7eadc

Please sign in to comment.