Class: SolidQueueMonitor::BaseController

Inherits:
ApplicationController show all
Defined in:
app/controllers/solid_queue_monitor/base_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#set_flash_message

Instance Method Details

#apply_execution_sorting(relation, allowed_columns, default_column, default_direction = :desc) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 177

def apply_execution_sorting(relation, allowed_columns, default_column, default_direction = :desc)
  column = sort_params[:sort_by]
  direction = sort_params[:sort_direction]
  column = default_column unless allowed_columns.include?(column)
  direction = %w[asc desc].include?(direction) ? direction.to_sym : default_direction

  # Columns that exist on the jobs table, not on execution tables
  job_table_columns = %w[class_name queue_name]

  if job_table_columns.include?(column)
    relation.joins(:job).order("solid_queue_jobs.#{column}" => direction)
  else
    relation.order(column => direction)
  end
end

#apply_sorting(relation, allowed_columns, default_column, default_direction = :desc) ⇒ Object



169
170
171
172
173
174
175
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 169

def apply_sorting(relation, allowed_columns, default_column, default_direction = :desc)
  column = sort_params[:sort_by]
  direction = sort_params[:sort_direction]
  column = default_column unless allowed_columns.include?(column)
  direction = %w[asc desc].include?(direction) ? direction.to_sym : default_direction
  relation.order(column => direction)
end

#current_pageObject



9
10
11
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 9

def current_page
  (params[:page] || 1).to_i
end

#filter_by_arguments(relation) ⇒ Object



79
80
81
82
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 79

def filter_by_arguments(relation)
  # Use ILIKE for case-insensitive search in PostgreSQL
  relation.where('arguments::text ILIKE ?', "%#{params[:arguments]}%")
end

#filter_failed_jobs(relation) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 131

def filter_failed_jobs(relation)
  return relation unless params[:class_name].present? || params[:queue_name].present? || params[:arguments].present?

  if params[:class_name].present?
    relation = relation.where(job_id: SolidQueue::Job.where('class_name LIKE ?', "%#{params[:class_name]}%").select(:id))
  end

  if params[:queue_name].present?
    relation = if relation.column_names.include?('queue_name')
                 relation.where('queue_name LIKE ?', "%#{params[:queue_name]}%")
               else
                 relation.where(job_id: SolidQueue::Job.where('queue_name LIKE ?', "%#{params[:queue_name]}%").select(:id))
               end
  end

  if params[:arguments].present?
    relation = relation.where(job_id: SolidQueue::Job.where('arguments::text ILIKE ?', "%#{params[:arguments]}%").select(:id))
  end

  relation
end

#filter_jobs(relation) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 56

def filter_jobs(relation)
  relation = relation.where('class_name LIKE ?', "%#{params[:class_name]}%") if params[:class_name].present?
  relation = relation.where('queue_name LIKE ?', "%#{params[:queue_name]}%") if params[:queue_name].present?
  relation = filter_by_arguments(relation) if params[:arguments].present?

  if params[:status].present?
    case params[:status]
    when 'completed'
      relation = relation.where.not(finished_at: nil)
    when 'failed'
      relation = relation.where(id: SolidQueue::FailedExecution.select(:job_id))
    when 'scheduled'
      relation = relation.where(id: SolidQueue::ScheduledExecution.select(:job_id))
    when 'pending'
      relation = relation.where(finished_at: nil)
                         .where.not(id: SolidQueue::FailedExecution.select(:job_id))
                         .where.not(id: SolidQueue::ScheduledExecution.select(:job_id))
    end
  end

  relation
end

#filter_paramsObject



153
154
155
156
157
158
159
160
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 153

def filter_params
  {
    class_name: params[:class_name],
    queue_name: params[:queue_name],
    arguments: params[:arguments],
    status: params[:status]
  }
end

#filter_ready_jobs(relation) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 84

def filter_ready_jobs(relation)
  return relation unless params[:class_name].present? || params[:queue_name].present? || params[:arguments].present?

  if params[:class_name].present?
    relation = relation.where(job_id: SolidQueue::Job.where('class_name LIKE ?', "%#{params[:class_name]}%").select(:id))
  end

  relation = relation.where('queue_name LIKE ?', "%#{params[:queue_name]}%") if params[:queue_name].present?

  if params[:arguments].present?
    relation = relation.where(job_id: SolidQueue::Job.where('arguments::text ILIKE ?', "%#{params[:arguments]}%").select(:id))
  end

  relation
end

#filter_recurring_jobs(relation) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 116

def filter_recurring_jobs(relation)
  return relation unless params[:class_name].present? || params[:queue_name].present? || params[:arguments].present?

  relation = relation.where('class_name LIKE ?', "%#{params[:class_name]}%") if params[:class_name].present?
  relation = relation.where('queue_name LIKE ?', "%#{params[:queue_name]}%") if params[:queue_name].present?

  # Add arguments filtering if the model has arguments column
  if params[:arguments].present? && relation.column_names.include?('arguments')
    relation = relation.where('arguments::text ILIKE ?',
                              "%#{params[:arguments]}%")
  end

  relation
end

#filter_scheduled_jobs(relation) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 100

def filter_scheduled_jobs(relation)
  return relation unless params[:class_name].present? || params[:queue_name].present? || params[:arguments].present?

  if params[:class_name].present?
    relation = relation.where(job_id: SolidQueue::Job.where('class_name LIKE ?', "%#{params[:class_name]}%").select(:id))
  end

  relation = relation.where('queue_name LIKE ?', "%#{params[:queue_name]}%") if params[:queue_name].present?

  if params[:arguments].present?
    relation = relation.where(job_id: SolidQueue::Job.where('arguments::text ILIKE ?', "%#{params[:arguments]}%").select(:id))
  end

  relation
end

#paginate(relation) ⇒ Object



5
6
7
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 5

def paginate(relation)
  PaginationService.new(relation, current_page, per_page).paginate
end

#per_pageObject



13
14
15
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 13

def per_page
  SolidQueueMonitor.jobs_per_page
end

#preload_job_statuses(jobs) ⇒ Object

Preload job statuses to avoid N+1 queries



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 18

def preload_job_statuses(jobs)
  return if jobs.empty?

  # Get all job IDs
  job_ids = jobs.map(&:id)

  # Find all failed jobs in a single query
  failed_job_ids = SolidQueue::FailedExecution.where(job_id: job_ids).pluck(:job_id)

  # Find all scheduled jobs in a single query
  scheduled_job_ids = SolidQueue::ScheduledExecution.where(job_id: job_ids).pluck(:job_id)

  # Attach the status information to each job
  jobs.each do |job|
    job.instance_variable_set(:@failed, failed_job_ids.include?(job.id))
    job.instance_variable_set(:@scheduled, scheduled_job_ids.include?(job.id))
  end

  # Define the method to check if a job is failed
  SolidQueue::Job.class_eval do
    def failed?
      if instance_variable_defined?(:@failed)
        @failed
      else
        SolidQueue::FailedExecution.exists?(job_id: id)
      end
    end

    def scheduled?
      if instance_variable_defined?(:@scheduled)
        @scheduled
      else
        SolidQueue::ScheduledExecution.exists?(job_id: id)
      end
    end
  end
end

#sort_paramsObject



162
163
164
165
166
167
# File 'app/controllers/solid_queue_monitor/base_controller.rb', line 162

def sort_params
  {
    sort_by: params[:sort_by],
    sort_direction: params[:sort_direction]
  }
end