Class: RoundhouseUi::JobsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/roundhouse_ui/jobs_controller.rb

Overview

Enqueue new jobs and edit/re-enqueue existing ones. Opt-in via RoundhouseUi.allow_job_editing (off by default — a sharp tool).

Sidekiq has no in-place edit: a job in a set is keyed by its payload, so an "edit" is delete-the-old + push-the-modified.

Constant Summary collapse

SET_BUILDERS =
{
  "dead"      => -> { Sidekiq::DeadSet.new },
  "retry"     => -> { Sidekiq::RetrySet.new },
  "scheduled" => -> { Sidekiq::ScheduledSet.new }
}.freeze
REDIRECTS =
{ "dead" => :dead_set_path, "retry" => :retries_path, "scheduled" => :scheduled_path }.freeze

Constants inherited from ApplicationController

ApplicationController::AUDIT_VERBS

Instance Method Summary collapse

Methods inherited from ApplicationController

#redirect_to

Instance Method Details

#createObject



29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/roundhouse_ui/jobs_controller.rb', line 29

def create
  args  = parse_args!(params[:args])
  klass = params[:job_class].to_s.strip
  raise ArgumentError, "Job class is required" if klass.empty?

  queue = params[:queue].presence || "default"
  Sidekiq::Client.push("class" => klass, "queue" => queue, "args" => args)
  redirect_to queues_path, notice: "Enqueued #{klass}#{queue}."
rescue ArgumentError => e
  render_form_error(:new, jobs_path, e)
end

#editObject



41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/roundhouse_ui/jobs_controller.rb', line 41

def edit
  @entry = find_entry or return redirect_to(root_path, alert: "Job not found.")
  @mode = :edit
  @job = {
    "class" => @entry.item["class"],
    "queue" => @entry.queue,
    "args"  => JSON.pretty_generate(@entry.item["args"] || [])
  }
  @action_path = job_path(set: params[:set], jid: params[:jid])
end

#newObject



23
24
25
26
27
# File 'app/controllers/roundhouse_ui/jobs_controller.rb', line 23

def new
  @mode = :new
  @job  = { "class" => "", "queue" => "default", "args" => "[]" }
  @action_path = jobs_path
end

#showObject

Read-only inspection — available without allow_job_editing.



18
19
20
21
# File 'app/controllers/roundhouse_ui/jobs_controller.rb', line 18

def show
  @set = params[:set]
  @entry = find_entry or return redirect_to(root_path, alert: "Job not found.")
end

#updateObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/roundhouse_ui/jobs_controller.rb', line 52

def update
  entry = find_entry or return redirect_to(root_path, alert: "Job not found.")
  args  = parse_args!(params[:args])
  klass = params[:job_class].presence || entry.item["class"]
  queue = params[:queue].presence || entry.queue

  entry.delete
  Sidekiq::Client.push("class" => klass, "queue" => queue, "args" => args)
  redirect_to send(REDIRECTS[params[:set]]), notice: "Edited & re-enqueued #{klass}#{queue}."
rescue ArgumentError => e
  @action_path = job_path(set: params[:set], jid: params[:jid])
  render_form_error(:edit, @action_path, e)
end