Skip to content

Commit

Permalink
Add retry functionality for batches (#1456)
Browse files Browse the repository at this point in the history
Closes #1450
  • Loading branch information
bensheldon authored Aug 2, 2024
1 parent aebdb97 commit b0c9707
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 15 deletions.
6 changes: 6 additions & 0 deletions app/controllers/good_job/batches_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,11 @@ def index
def show
@batch = GoodJob::BatchRecord.find(params[:id])
end

def retry
@batch = GoodJob::Batch.find(params[:id])
@batch.retry
redirect_back(fallback_location: batches_path, notice: t(".notice"))
end
end
end
25 changes: 21 additions & 4 deletions app/models/good_job/batch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,12 @@ def enqueue(active_jobs = [], **properties, &block)
if record.new_record?
record.save!
else
record.with_advisory_lock(function: "pg_advisory_lock") do
record.enqueued_at_will_change!
record.finished_at_will_change!
record.update!(enqueued_at: nil, finished_at: nil)
record.transaction do
record.with_advisory_lock(function: "pg_advisory_xact_lock") do
record.enqueued_at_will_change!
record.finished_at_will_change!
record.update!(enqueued_at: nil, finished_at: nil)
end
end
end

Expand Down Expand Up @@ -135,6 +137,21 @@ def add(active_jobs = nil, &block)
buffer.active_jobs
end

def retry
Rails.application.executor.wrap do
buffer = GoodJob::Adapter::InlineBuffer.capture do
record.transaction do
record.with_advisory_lock(function: "pg_advisory_xact_lock") do
record.update!(discarded_at: nil, finished_at: nil)
record.jobs.discarded.each(&:retry_job)
record._continue_discard_or_finish(lock: false)
end
end
end
buffer.call
end
end

def active_jobs
record.jobs.map(&:active_job)
end
Expand Down
6 changes: 6 additions & 0 deletions app/views/good_job/batches/_table.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<%= batch.jobs.size %>
</div>
<div class="col text-end">
<% if batch.discarded? %>
<%= link_to retry_batch_path(batch), method: :put, class: "btn btn-sm btn-outline-primary", title: t("good_job.batches.actions.retry"), data: { confirm: t("good_job.batches.actions.confirm_retry") } do %>
<%= render_icon "arrow_clockwise" %>
<%= t "good_job.batches.actions.retry" %>
<% end %>
<% end %>
<%= tag.button type: "button", class: "btn btn-sm text-muted", role: "button",
title: t("good_job.actions.inspect"),
data: { bs_toggle: "collapse", bs_target: "##{dom_id(batch, 'properties')}" },
Expand Down
8 changes: 8 additions & 0 deletions app/views/good_job/batches/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
<h2 class="h5 mt-2"><%= @batch.description %></h2>
</nav>
</div>
<div class="col text-end">
<% if @batch.discarded? %>
<%= button_to retry_batch_path(@batch), method: :put, class: "btn btn-sm btn-outline-primary", form_class: "d-inline-block", aria: { label: t("good_job.batches.actions.retry") }, title: t("good_job.batches.actions.retry"), data: { confirm: t("good_job.batches.actions.confirm_retry") } do %>
<%= render_icon "arrow_clockwise" %>
<%= t "good_job.actions.retry" %>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ de:
reschedule: Umplanen
retry: Wiederholen
batches:
actions:
confirm_retry: Bist du sicher, dass du diesen Batch wiederholen willst?
retry: Batch wiederholen
index:
older_batches: Ältere Batches
title: Batches
Expand All @@ -24,6 +27,8 @@ de:
retry: Job wiederholen
title: Aktionen
no_jobs_found: Keine Jobs gefunden.
retry:
notice: Batch wurde wiederholt
show:
attributes: Attribute
batched_jobs: Batch-Jobs
Expand Down
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ en:
reschedule: Reschedule
retry: Retry
batches:
actions:
confirm_retry: Are you sure you want to retry this batch?
retry: Retry
index:
older_batches: Older batches
title: Batches
Expand All @@ -24,6 +27,8 @@ en:
retry: Retry Job
title: Actions
no_jobs_found: No jobs found.
retry:
notice: Batch has been retried
show:
attributes: Attributes
batched_jobs: Batched Jobs
Expand Down
5 changes: 5 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ es:
reschedule: Reprogramar
retry: Reintentar
batches:
actions:
confirm_retry: "¿Estás seguro que querés reintentar este lote?"
retry: Reintentar
index:
older_batches: Lotes anteriores
title: Lotes
Expand All @@ -24,6 +27,8 @@ es:
retry: Reintentar tarea
title: Acciones
no_jobs_found: No hay tareas.
retry:
notice: El lote ha sido reintentado
show:
attributes: Atributos
batched_jobs: Tareas en lote
Expand Down
5 changes: 5 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ fr:
reschedule: Reprogrammer
retry: Recommencez
batches:
actions:
confirm_retry: Voulez-vous vraiment réessayer ce lot ?
retry: Réessayer
index:
older_batches: Lots plus anciens
title: Lots
Expand All @@ -24,6 +27,8 @@ fr:
retry: Réessayer le job
title: Actions
no_jobs_found: Aucun job trouvé.
retry:
notice: Le lot a été réessayé
show:
attributes: Attributs
batched_jobs: Jobs groupés
Expand Down
5 changes: 5 additions & 0 deletions config/locales/it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ it:
reschedule: Riprogramma
retry: Riprova
batches:
actions:
confirm_retry: Sei sicuro di voler riprovare questo batch?
retry: Riprova
index:
older_batches: Batch più vecchi
title: Batch
Expand All @@ -24,6 +27,8 @@ it:
retry: Riprova job
title: Azioni
no_jobs_found: Nessun job trovato.
retry:
notice: Il batch è stato riprovato
show:
attributes: Attributi
batched_jobs: Job raggruppati
Expand Down
15 changes: 10 additions & 5 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ ja:
reschedule: 再スケジュールする
retry: 再試行する
batches:
actions:
confirm_retry: このバッチを再試行してもよろしいですか?
retry: バッチを再試行する
index:
older_batches: より古いバッチ
title: バッチ
Expand All @@ -24,6 +27,8 @@ ja:
retry: ジョブを再試行する
title: アクション
no_jobs_found: ジョブが見つかりませんでした。
retry:
notice: バッチが再試行されました
show:
attributes: 属性
batched_jobs: バッチ処理されたジョブ
Expand Down Expand Up @@ -122,9 +127,9 @@ ja:
discard:
notice: ジョブが破棄されました
executions:
application_trace: Application Trace
full_trace: Full Trace
in_queue: 待機中
application_trace: アプリケーショントレース
full_trace: フルトレース
in_queue: キュー内
runtime: 実行時間
title: 実行
force_discard:
Expand Down Expand Up @@ -198,8 +203,8 @@ ja:
index:
average_duration: 平均所要時間
chart_title: ジョブの総実行時間(秒
executions: 処刑
job_class: 職種
executions: 実行
job_class: ジョブクラス
maximum_duration: 最大持続時間
minimum_duration: 最小期間
title: パフォーマンス
Expand Down
13 changes: 9 additions & 4 deletions config/locales/ko.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ ko:
reschedule: 재예약하기
retry: 다시 시도하기
batches:
actions:
confirm_retry: 이 배치를 다시 시도하시겠습니까?
retry: 배치 다시 시도
index:
older_batches: 더 오래된 배치
title: 배치
Expand All @@ -24,6 +27,8 @@ ko:
retry: 작업 다시 시도
title: 작업 액션
no_jobs_found: 작업이 없습니다.
retry:
notice: 배치가 다시 시도되었습니다.
show:
attributes: 속성
batched_jobs: 배치된 작업
Expand Down Expand Up @@ -122,8 +127,8 @@ ko:
discard:
notice: 작업이 폐기되었습니다.
executions:
application_trace: Application Trace
full_trace: Full Trace
application_trace: 애플리케이션 추적
full_trace: 전체 추적
in_queue: 대기 중
runtime: 실행 시간
title: 실행
Expand Down Expand Up @@ -198,8 +203,8 @@ ko:
index:
average_duration: 평균 지속 시간
chart_title: 총 작업 실행 시간(초)
executions: 처형
job_class: 직업군
executions: 실행
job_class: 작업 클래스
maximum_duration: 최대 기간
minimum_duration: 최소 기간
title: 성능
Expand Down
5 changes: 5 additions & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ nl:
reschedule: Opnieuw plannen
retry: Opnieuw proberen
batches:
actions:
confirm_retry: Weet je zeker dat je deze batch opnieuw wilt proberen?
retry: Batch opnieuw proberen
index:
older_batches: Oudere partijen
title: Partijen
Expand All @@ -24,6 +27,8 @@ nl:
retry: Taak opnieuw
title: Acties
no_jobs_found: Geen vacatures gevonden.
retry:
notice: Batch is opnieuw geprobeerd
show:
attributes: attributen
batched_jobs: Gegroepeerde banen
Expand Down
5 changes: 5 additions & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ pt-BR:
reschedule: Reagendar
retry: Tentar novamente
batches:
actions:
confirm_retry: Tem certeza de que deseja tentar novamente este lote?
retry: Tentar novamente
index:
older_batches: Lotes antigos
title: Lotes
Expand All @@ -24,6 +27,8 @@ pt-BR:
retry: Tentar Tarefa Novamente
title: Ações
no_jobs_found: Nenhuma tarefa encontrada.
retry:
notice: O lote foi tentado novamente
show:
attributes: Atributos
batched_jobs: Tarefas em Lote
Expand Down
5 changes: 5 additions & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ ru:
reschedule: Перенести
retry: Повторить попытку
batches:
actions:
confirm_retry: Вы уверены, что хотите повторить эту группу заданий?
retry: Повторить группу заданий
index:
older_batches: Старые группы заданий
title: Группы заданий
Expand All @@ -24,6 +27,8 @@ ru:
retry: Повторить задание
title: Действия
no_jobs_found: Заданий не найдено
retry:
notice: Группа заданий была повторена
show:
attributes: Атрибуты
batched_jobs: Группы заданий
Expand Down
5 changes: 5 additions & 0 deletions config/locales/tr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ tr:
reschedule: Yeniden planla
retry: Tekrar dene
batches:
actions:
confirm_retry: Bu grubu yeniden denemek istediğinizden emin misiniz?
retry: Grubu yeniden dene
index:
older_batches: Daha eski toplu işlemler
title: Toplu İşlemler
Expand All @@ -24,6 +27,8 @@ tr:
retry: İşi Tekrar Dene
title: İşlemler
no_jobs_found: İş bulunamadı.
retry:
notice: Grup yeniden denendi
show:
attributes: Özellikler
batched_jobs: Toplu İşlenmiş İşler
Expand Down
7 changes: 6 additions & 1 deletion config/locales/uk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ uk:
reschedule: Перепланувати
retry: Повторити
batches:
actions:
confirm_retry: Ви впевнені, що хочете повторити цю групу завдань?
retry: Повторити групу завдань
index:
older_batches: Старі пакети
title: Пакети
Expand All @@ -24,6 +27,8 @@ uk:
retry: Повторити задачу
title: Дії
no_jobs_found: Задачі не знайдені.
retry:
notice: Групу завдань було повторено
show:
attributes: Атрибути
batched_jobs: Задачі в пакеті
Expand Down Expand Up @@ -256,7 +261,7 @@ uk:
queue_name: Назва черги
search: Пошук
navbar:
batches: Batches
batches: Пакети
cron_schedules: Cron
jobs: Задачі
live_poll: Живе Опитування
Expand Down
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
get 'jobs/metrics/primary_nav', to: 'metrics#primary_nav', as: :metrics_primary_nav
get 'jobs/metrics/job_status', to: 'metrics#job_status', as: :metrics_job_status

resources :batches, only: %i[index show]
resources :batches, only: %i[index show] do
member do
put :retry
end
end

resources :cron_entries, only: %i[index show], param: :cron_key do
member do
Expand Down
1 change: 1 addition & 0 deletions sorbet/rbi/todo.rbi
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module ::CustomError; end
module ::DONE; end
module ::DiscardableError; end
module ::DiscardedJob; end
module ::DiscardOnceJob; end
module ::ERROR_TRIGGERED; end
module ::ErrorJob; end
module ::ExpectedError; end
Expand Down
Loading

0 comments on commit b0c9707

Please sign in to comment.