Class: RigidWorkflow::RunsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/rigid_workflow/runs_controller.rb

Overview

Manages workflow run CRUD operations and bulk actions. Provides views for listing, filtering, and controlling workflow runs.

Constant Summary collapse

ALLOWED_BULK_ACTIONS =
%i[retry cancel].freeze

Instance Method Summary collapse

Methods inherited from ApplicationController

#default_url_options, stats_for_ever, unique_workflows_in_ever

Instance Method Details

#active_runsObject



17
18
19
20
# File 'app/controllers/rigid_workflow/runs_controller.rb', line 17

def active_runs
  @runs = filter_runs(%i[running]).page(page_params)
  render :index
end

#bulk_actionObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/rigid_workflow/runs_controller.rb', line 61

def bulk_action
  permitted = bulk_action_params
  return render_error("Invalid action") unless permitted[:action]

  runs = RigidWorkflow::Run.where(id: permitted[:ids])
  success_count = 0
  errors = []

  runs.each do |run|
    begin
      case permitted[:action]
      when :retry
        run.retry!
      when :cancel
        run.cancel!
      end
      success_count += 1
    rescue => error
      errors << "#{run.id}: #{error.message}"
    end
  end

  action_label =
    permitted[:action] == :retry ? "retry" : permitted[:action].to_s
  notice =
    "Applied #{action_label} to #{success_count} #{"run".pluralize(success_count)}."
  notice +=
    " #{errors.join(', ')} #{"run".pluralize(errors.size)} failed." if errors.any?

  # Redirect to show page if single run, otherwise to referrer or list
  if permitted[:ids].size == 1
    redirect_to runs_show_path(permitted[:ids].first), notice: notice
  else
    redirect_to request.referrer || admin_rigid_workflow_runs_path,
                notice: notice
  end
end

#completed_runsObject



22
23
24
25
# File 'app/controllers/rigid_workflow/runs_controller.rb', line 22

def completed_runs
  @runs = filter_runs(%i[completed]).page(page_params)
  render :index
end

#failed_runsObject



27
28
29
30
# File 'app/controllers/rigid_workflow/runs_controller.rb', line 27

def failed_runs
  @runs = filter_runs(%i[failed]).page(page_params)
  render :index
end

#indexObject



8
9
10
# File 'app/controllers/rigid_workflow/runs_controller.rb', line 8

def index
  @runs = filter_runs.page(page_params)
end

#pending_runsObject



12
13
14
15
# File 'app/controllers/rigid_workflow/runs_controller.rb', line 12

def pending_runs
  @runs = filter_runs(%i[pending]).page(page_params)
  render :index
end

#showObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/controllers/rigid_workflow/runs_controller.rb', line 32

def show
  @run =
    RigidWorkflow::Run.includes(steps: :attempts, signals: []).find(
      params[:id]
    )
  respond_to do |format|
    format.html
    format.json do
      render json: {
               workflow: @run,
               steps:
                 @run
                   .steps
                   .includes(:attempts)
                   .order(:id)
                   .map { |s|
                     s.attributes.merge(
                       attempts:
                         s.attempts.order(:attempt_number).map(&:attributes)
                     )
                   },
               signals: @run.signals.order(:id)
             }
    end
  end
end