job_harbor 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +16 -1
  4. data/app/assets/stylesheets/job_harbor/application.css +1 -1
  5. data/app/assets/stylesheets/job_harbor/application.tailwind.css +0 -31
  6. data/app/components/job_harbor/badge_component.rb +12 -28
  7. data/app/components/job_harbor/info_line_component.rb +20 -0
  8. data/app/components/job_harbor/job_filters_component.rb +7 -2
  9. data/app/components/job_harbor/job_row_component.rb +12 -11
  10. data/app/components/job_harbor/job_search_form_component.html.erb +4 -0
  11. data/app/components/job_harbor/job_search_form_component.rb +9 -0
  12. data/app/components/job_harbor/jobs_table_component.html.erb +26 -0
  13. data/app/components/job_harbor/jobs_table_component.rb +10 -0
  14. data/app/components/job_harbor/nav_link_component.rb +1 -2
  15. data/app/components/job_harbor/pagination_component.rb +10 -10
  16. data/app/components/job_harbor/queue_card_component.rb +4 -6
  17. data/app/components/job_harbor/queue_pause_form_component.html.erb +5 -0
  18. data/app/components/job_harbor/queue_pause_form_component.rb +6 -0
  19. data/app/components/job_harbor/stat_card_component.rb +10 -13
  20. data/app/components/job_harbor/status_tabs_component.rb +43 -0
  21. data/app/components/job_harbor/worker_card_component.rb +3 -13
  22. data/app/controllers/job_harbor/jobs_controller.rb +45 -55
  23. data/app/controllers/job_harbor/queues/pauses_controller.rb +32 -0
  24. data/app/controllers/job_harbor/queues_controller.rb +1 -6
  25. data/app/controllers/job_harbor/workers_controller.rb +1 -0
  26. data/app/models/job_harbor/chart_data.rb +10 -4
  27. data/app/models/job_harbor/dashboard_stats.rb +5 -31
  28. data/app/models/job_harbor/job_presenter.rb +5 -67
  29. data/app/models/job_harbor/job_query.rb +105 -0
  30. data/app/models/job_harbor/job_status.rb +90 -0
  31. data/app/models/job_harbor/queue_stats.rb +9 -38
  32. data/app/views/job_harbor/dashboard/index.html.erb +14 -39
  33. data/app/views/job_harbor/jobs/index.html.erb +5 -56
  34. data/app/views/job_harbor/jobs/search.html.erb +4 -27
  35. data/app/views/job_harbor/jobs/show.html.erb +21 -55
  36. data/app/views/job_harbor/queues/index.html.erb +2 -0
  37. data/app/views/job_harbor/queues/show.html.erb +5 -54
  38. data/app/views/job_harbor/recurring_tasks/show.html.erb +9 -31
  39. data/app/views/job_harbor/workers/index.html.erb +6 -4
  40. data/app/views/layouts/job_harbor/application.html.erb +27 -25
  41. data/config/routes.rb +9 -7
  42. data/lib/job_harbor/configuration.rb +6 -2
  43. data/lib/job_harbor/engine.rb +9 -1
  44. data/lib/job_harbor/time_formats.rb +12 -0
  45. data/lib/job_harbor/version.rb +1 -1
  46. data/lib/job_harbor.rb +5 -0
  47. metadata +15 -3
  48. data/app/helpers/job_harbor/application_helper.rb +0 -4
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JobHarbor
4
+ class StatusTabsComponent < ApplicationComponent
5
+ # status is the key of the open tab, or nil for "All"; counts is keyed "all" and by status key.
6
+ def initialize(status:, counts:)
7
+ @status = status
8
+ @counts = counts
9
+ end
10
+
11
+ def call
12
+ content_tag(:div, class: "flex gap-1 mb-4 flex-wrap") do
13
+ safe_join([ all_tab, *JobStatus.all.map { |job_status| status_tab(job_status) } ])
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def all_tab
20
+ link_to jobs_path, class: tab_classes(selected: !@status) do
21
+ safe_join([ "All ", count_tag("all", "ml-1 opacity-75") ])
22
+ end
23
+ end
24
+
25
+ def status_tab(job_status)
26
+ link_to public_send(job_status.path_helper), class: tab_classes(selected: @status == job_status.key) do
27
+ safe_join([
28
+ content_tag(:span, "", class: "circle #{job_status.circle_class}"),
29
+ " #{job_status.label} ",
30
+ count_tag(job_status.key, "opacity-75")
31
+ ])
32
+ end
33
+ end
34
+
35
+ def count_tag(key, css_class)
36
+ content_tag(:span, @counts[key], class: css_class)
37
+ end
38
+
39
+ def tab_classes(selected:)
40
+ "badge #{selected ? 'badge-primary' : 'badge-secondary'}"
41
+ end
42
+ end
43
+ end
@@ -50,19 +50,9 @@ module JobHarbor
50
50
  def details
51
51
  content_tag(:div, class: "space-y-1") do
52
52
  safe_join([
53
- info_line("Hostname", @worker.hostname),
54
- info_line("PID", @worker.pid),
55
- info_line("Last Heartbeat", heartbeat_time)
56
- ])
57
- end
58
- end
59
-
60
- def info_line(label, value)
61
- content_tag(:div, class: "info-line") do
62
- safe_join([
63
- content_tag(:span, label, class: "info-line-label"),
64
- content_tag(:span, "", class: "info-line-separator"),
65
- content_tag(:span, value, class: "info-line-value")
53
+ render(InfoLineComponent.new(label: "Hostname", value: @worker.hostname)),
54
+ render(InfoLineComponent.new(label: "PID", value: @worker.pid)),
55
+ render(InfoLineComponent.new(label: "Last Heartbeat", value: heartbeat_time))
66
56
  ])
67
57
  end
68
58
  end
@@ -13,16 +13,10 @@ module JobHarbor
13
13
  @class_name = filter_param(:class_name)
14
14
  @queue_name = filter_param(:queue_name)
15
15
 
16
- @pagy, @jobs = JobPresenter.all_with_status(
17
- @status,
18
- page: params[:page],
19
- per_page: @per_page,
20
- class_name: @class_name,
21
- queue_name: @queue_name
22
- )
23
- @counts = job_counts
24
- @bulk_count = bulk_scope(@status)&.count
25
- @filter_data = filter_data
16
+ @page_info, @jobs = job_query.page(params[:page], per_page: @per_page)
17
+ @counts = JobQuery.counts
18
+ @bulk_count = job_query.bulk_jobs&.count
19
+ @filter_data = JobQuery.filter_options
26
20
  set_page_title @status ? "#{@status.titleize} Jobs" : "All Jobs"
27
21
  end
28
22
 
@@ -31,10 +25,10 @@ module JobHarbor
31
25
  end
32
26
 
33
27
  def search
34
- query = params[:q].to_s.strip
35
- if query.present?
28
+ text = params[:q].to_s.strip
29
+ if text.present?
36
30
  @per_page = per_page_param
37
- @pagy, @jobs = JobPresenter.search(query, page: params[:page], per_page: @per_page)
31
+ @page_info, @jobs = JobQuery.search(text).page(params[:page], per_page: @per_page)
38
32
  set_page_title "Search Results"
39
33
  else
40
34
  redirect_to jobs_path
@@ -44,7 +38,7 @@ module JobHarbor
44
38
  def retry
45
39
  if @job.can_retry?
46
40
  perform_retry(@job)
47
- redirect_to job_path(@job), notice: "Job has been queued for retry."
41
+ redirect_to return_path || job_path(@job), allow_other_host: false, notice: "Job has been queued for retry."
48
42
  else
49
43
  redirect_to job_path(@job), alert: "This job cannot be retried."
50
44
  end
@@ -52,7 +46,7 @@ module JobHarbor
52
46
 
53
47
  def discard
54
48
  if @job.can_discard? && @job.discard
55
- redirect_to jobs_path(status: params[:return_status]), notice: "Job has been discarded."
49
+ redirect_to return_path || jobs_path(status: params[:return_status]), allow_other_host: false, notice: "Job has been discarded."
56
50
  else
57
51
  redirect_to job_path(@job), alert: "This job cannot be discarded."
58
52
  end
@@ -61,12 +55,12 @@ module JobHarbor
61
55
  end
62
56
 
63
57
  def retry_all
64
- count = retry_all_jobs(params[:status])
58
+ count = retry_all_jobs
65
59
  redirect_to bulk_redirect_path, notice: "#{helpers.pluralize(count, "job")} queued for retry."
66
60
  end
67
61
 
68
62
  def discard_all
69
- count = discard_all_jobs(params[:status])
63
+ count = discard_all_jobs
70
64
  redirect_to bulk_redirect_path, notice: "#{helpers.pluralize(count, "job")} discarded."
71
65
  end
72
66
 
@@ -78,29 +72,35 @@ module JobHarbor
78
72
  redirect_to jobs_path, alert: "Job not found."
79
73
  end
80
74
 
81
- def per_page_param
82
- per_page = params[:per_page].to_i
83
- valid_sizes = JobHarbor::PerPageSelectorComponent::PAGE_SIZES
84
- valid_sizes.include?(per_page) ? per_page : sq_config.jobs_per_page
75
+ # Only a path inside this engine's mount, so a crafted param can't send the operator elsewhere.
76
+ def return_path
77
+ path = url_from(params[:return_to]) if params[:return_to].is_a?(String)
78
+ return unless path && within_mount?(path)
79
+
80
+ queue_page_gone?(path) ? queues_path : path
81
+ end
82
+
83
+ def within_mount?(path)
84
+ mount = request.script_name
85
+ return false unless path == mount || path.start_with?("#{mount}/", "#{mount}?")
86
+
87
+ raw = path.split("?", 2).first
88
+ # %2e%2e and %2f are resolved by the browser, so judge the decoded path too.
89
+ [ raw, URI.decode_www_form_component(raw) ].none? { |candidate| candidate.match?(%r{//|\\|\s}) || candidate.split("/").include?("..") }
90
+ rescue ArgumentError
91
+ false
85
92
  end
86
93
 
87
- def filter_data
88
- {
89
- class_names: SolidQueue::Job.distinct.pluck(:class_name).sort,
90
- queue_names: SolidQueue::Job.distinct.pluck(:queue_name).sort
91
- }
94
+ # A queue with no jobs left stops existing, so its page would answer "Queue not found" and drop the notice.
95
+ def queue_page_gone?(path)
96
+ name = path.split("?", 2).first[%r{\A#{Regexp.escape(request.script_name)}/queues/([^/]+)\z}, 1]
97
+ name.present? && QueueStats.find(Rack::Utils.unescape_path(name)).nil?
92
98
  end
93
99
 
94
- def job_counts
95
- {
96
- all: SolidQueue::Job.count,
97
- pending: SolidQueue::ReadyExecution.count,
98
- scheduled: SolidQueue::ScheduledExecution.count,
99
- in_progress: SolidQueue::ClaimedExecution.count,
100
- failed: SolidQueue::FailedExecution.count,
101
- blocked: SolidQueue::BlockedExecution.count,
102
- finished: SolidQueue::Job.where.not(finished_at: nil).count
103
- }
100
+ def per_page_param
101
+ per_page = params[:per_page].to_i
102
+ valid_sizes = JobHarbor::PerPageSelectorComponent::PAGE_SIZES
103
+ valid_sizes.include?(per_page) ? per_page : sq_config.jobs_per_page
104
104
  end
105
105
 
106
106
  def perform_retry(job)
@@ -110,19 +110,8 @@ module JobHarbor
110
110
  failed_execution.retry
111
111
  end
112
112
 
113
- # Nil for tabs without bulk actions.
114
- def bulk_scope(status)
115
- executions = case status
116
- when "failed" then SolidQueue::FailedExecution
117
- when "blocked" then SolidQueue::BlockedExecution
118
- end
119
- return unless executions
120
-
121
- JobPresenter.apply_filters(
122
- SolidQueue::Job.where(id: executions.select(:job_id)),
123
- class_name: filter_param(:class_name),
124
- queue_name: filter_param(:queue_name)
125
- )
113
+ def job_query
114
+ @job_query ||= JobQuery.new(status: params[:status], class_name: filter_param(:class_name), queue_name: filter_param(:queue_name))
126
115
  end
127
116
 
128
117
  def reject_malformed_filters
@@ -137,17 +126,18 @@ module JobHarbor
137
126
  jobs_path(status: params[:status], class_name: filter_param(:class_name).presence, queue_name: filter_param(:queue_name).presence)
138
127
  end
139
128
 
140
- def retry_all_jobs(status)
141
- return 0 unless status == "failed"
129
+ def retry_all_jobs
130
+ job_status = JobStatus.find(params[:status])
131
+ return 0 unless job_status&.bulk_retry?
142
132
 
143
- bulk_scope(status).in_batches(of: BULK_BATCH_SIZE).sum { |jobs| SolidQueue::FailedExecution.retry_all(jobs) }
133
+ job_query.bulk_jobs.in_batches(of: BULK_BATCH_SIZE).sum { |jobs| job_status.execution_class.retry_all(jobs) }
144
134
  end
145
135
 
146
- def discard_all_jobs(status)
147
- scope = bulk_scope(status)
136
+ def discard_all_jobs
137
+ scope = job_query.bulk_jobs
148
138
  return 0 unless scope
149
139
 
150
- executions = status == "failed" ? SolidQueue::FailedExecution : SolidQueue::BlockedExecution
140
+ executions = JobStatus.find(params[:status]).execution_class
151
141
  scope.in_batches(of: BULK_BATCH_SIZE).sum do |jobs|
152
142
  ids = jobs.ids
153
143
  executions.discard_all_from_jobs(jobs)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JobHarbor
4
+ module Queues
5
+ class PausesController < ApplicationController
6
+ def create
7
+ name = params[:name].try(:gsub, /\A[[:space:]]+|[[:space:]]+\z/, "").presence
8
+
9
+ if name.nil?
10
+ redirect_to queues_path, alert: "Enter a queue name."
11
+ elsif name.length > QueueStats::MAX_NAME_LENGTH
12
+ redirect_to queues_path, alert: "Queue name must be #{QueueStats::MAX_NAME_LENGTH} characters or fewer."
13
+ elsif name.match?(/[[:cntrl:]]/)
14
+ redirect_to queues_path, alert: "Queue name can't contain control characters."
15
+ else
16
+ pause(QueueStats.new(name))
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def pause(queue)
23
+ if queue.paused?
24
+ redirect_to queues_path, notice: "Queue '#{queue.name}' is already paused."
25
+ else
26
+ queue.pause
27
+ redirect_to queues_path, notice: "Queue '#{queue.name}' has been paused."
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -10,12 +10,7 @@ module JobHarbor
10
10
  end
11
11
 
12
12
  def show
13
- @pagy, @jobs = JobPresenter.all_with_status(
14
- nil,
15
- page: params[:page],
16
- per_page: sq_config.jobs_per_page,
17
- queue_name: @queue.name
18
- )
13
+ @page_info, @jobs = JobQuery.new(queue_name: @queue.name).page(params[:page], per_page: sq_config.jobs_per_page)
19
14
  set_page_title "Queue: #{@queue.name}"
20
15
  end
21
16
 
@@ -5,6 +5,7 @@ module JobHarbor
5
5
  def index
6
6
  @workers = SolidQueue::Process.order(last_heartbeat_at: :desc)
7
7
  @active_count = Processes.alive.count
8
+ @live_workers_count = Processes.live_workers.count
8
9
  @stale_count = @workers.count - @active_count
9
10
  set_page_title "Workers"
10
11
  end
@@ -23,7 +23,7 @@ module JobHarbor
23
23
 
24
24
  def series
25
25
  @series ||= {
26
- labels: bucket_starts.map { |time| format_label(time) },
26
+ labels: labels,
27
27
  completed: counts_per_bucket(SolidQueue::Job, :finished_at),
28
28
  failed: counts_per_bucket(SolidQueue::FailedExecution, :created_at),
29
29
  enqueued: counts_per_bucket(SolidQueue::Job, :created_at)
@@ -40,13 +40,19 @@ module JobHarbor
40
40
  Array.new(bucket_count) { |index| @cutoff + index * @interval }
41
41
  end
42
42
 
43
- def format_label(time)
44
- @duration <= 24.hours ? time.strftime("%H:%M") : time.strftime("%b %d")
43
+ # Clock labels repeat or skip an hour across a daylight-saving change, so a series that spans one names the zone.
44
+ def labels
45
+ starts = bucket_starts
46
+ return starts.map { |time| time.strftime("%b %d") } if @duration > 24.hours
47
+
48
+ pattern = starts.map(&:utc_offset).uniq.one? ? "%H:%M" : "%H:%M %Z"
49
+ starts.map { |time| time.strftime(pattern) }
45
50
  end
46
51
 
47
52
  # One row of SUM(CASE ...) per series: ANSI SQL, so it needs no per-database date truncation.
48
53
  def counts_per_bucket(model, time_field)
49
- column = "#{model.quoted_table_name}.#{model.connection.quote_column_name(time_field)}"
54
+ quoted_field = model.with_connection { |connection| connection.quote_column_name(time_field) }
55
+ column = "#{model.quoted_table_name}.#{quoted_field}"
50
56
  sums = bucket_starts.each_with_index.map { |start, index| bucket_sum(model, column, start, last: index == bucket_count - 1) }
51
57
 
52
58
  model.where(time_field => @cutoff..).pick(*sums).map(&:to_i)
@@ -2,9 +2,7 @@
2
2
 
3
3
  module JobHarbor
4
4
  class DashboardStats
5
- attr_reader :pending_count, :scheduled_count, :in_progress_count,
6
- :failed_count, :blocked_count, :finished_count,
7
- :workers_count, :queues_count, :throughput_per_hour,
5
+ attr_reader :status_counts, :finished_last_24h, :workers_count, :queues_count, :throughput_per_hour,
8
6
  :recent_failures
9
7
 
10
8
  def initialize
@@ -14,39 +12,15 @@ module JobHarbor
14
12
  private
15
13
 
16
14
  def calculate_stats
17
- @pending_count = ready_executions_count
18
- @scheduled_count = scheduled_executions_count
19
- @in_progress_count = claimed_executions_count
20
- @failed_count = failed_executions_count
21
- @blocked_count = blocked_executions_count
22
- @finished_count = finished_jobs_count
15
+ @status_counts = JobStatus.all.reject(&:finished?).to_h { |status| [ status.key, status.count ] }
16
+ @finished_last_24h = finished_last_24h_count
23
17
  @workers_count = active_workers_count
24
18
  @queues_count = queues_count_calc
25
19
  @throughput_per_hour = calculate_throughput
26
20
  @recent_failures = fetch_recent_failures
27
21
  end
28
22
 
29
- def ready_executions_count
30
- SolidQueue::ReadyExecution.count
31
- end
32
-
33
- def scheduled_executions_count
34
- SolidQueue::ScheduledExecution.count
35
- end
36
-
37
- def claimed_executions_count
38
- SolidQueue::ClaimedExecution.count
39
- end
40
-
41
- def failed_executions_count
42
- SolidQueue::FailedExecution.count
43
- end
44
-
45
- def blocked_executions_count
46
- SolidQueue::BlockedExecution.count
47
- end
48
-
49
- def finished_jobs_count
23
+ def finished_last_24h_count
50
24
  SolidQueue::Job.where("finished_at > ?", 24.hours.ago).count
51
25
  end
52
26
 
@@ -55,7 +29,7 @@ module JobHarbor
55
29
  end
56
30
 
57
31
  def queues_count_calc
58
- SolidQueue::Job.distinct.count(:queue_name)
32
+ QueueStats.names.size
59
33
  end
60
34
 
61
35
  def calculate_throughput
@@ -4,18 +4,6 @@ module JobHarbor
4
4
  class JobPresenter
5
5
  include ActionView::Helpers::DateHelper
6
6
 
7
- # In precedence order: the first execution a job holds decides its status.
8
- EXECUTION_STATUSES = {
9
- failed_execution: "failed",
10
- claimed_execution: "in_progress",
11
- blocked_execution: "blocked",
12
- scheduled_execution: "scheduled",
13
- ready_execution: "pending"
14
- }.freeze
15
-
16
- LIKE_ESCAPE = "!"
17
- UUID_FORMAT = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
18
-
19
7
  delegate :id, :created_at, :queue_name, :priority,
20
8
  :active_job_id, :scheduled_at, :finished_at, :discard,
21
9
  to: :@job
@@ -98,11 +86,11 @@ module JobHarbor
98
86
  end
99
87
 
100
88
  def can_retry?
101
- status == "failed"
89
+ JobStatus.find(status)&.retryable || false
102
90
  end
103
91
 
104
92
  def can_discard?
105
- %w[pending scheduled failed blocked].include?(status)
93
+ JobStatus.find(status)&.discardable || false
106
94
  end
107
95
 
108
96
  def to_param
@@ -134,8 +122,8 @@ module JobHarbor
134
122
  end
135
123
 
136
124
  def determine_status
137
- EXECUTION_STATUSES.each do |association, status|
138
- return status if execution_for(association)
125
+ JobStatus.by_precedence.each do |status|
126
+ return status.key if execution_for(status.execution_association)
139
127
  end
140
128
 
141
129
  @job.finished_at.present? ? "finished" : "unknown"
@@ -147,57 +135,7 @@ module JobHarbor
147
135
 
148
136
  class << self
149
137
  def find(id)
150
- new(SolidQueue::Job.preload(*EXECUTION_STATUSES.keys).find(id))
151
- end
152
-
153
- def all_with_status(status = nil, page: 1, per_page: 25, class_name: nil, queue_name: nil)
154
- jobs = scope_for(status)
155
- jobs = apply_filters(jobs, class_name: class_name, queue_name: queue_name)
156
- paginate(jobs, page: page, per_page: per_page)
157
- end
158
-
159
- def apply_filters(scope, class_name: nil, queue_name: nil)
160
- scope = scope.where(class_name: class_name) if class_name.present?
161
- scope = scope.where(queue_name: queue_name) if queue_name.present?
162
- scope
163
- end
164
-
165
- def search(query, page: 1, per_page: 25)
166
- paginate(search_scope(query), page: page, per_page: per_page)
167
- end
168
-
169
- private
170
-
171
- def scope_for(status)
172
- if status.to_s == "finished"
173
- SolidQueue::Job.where.not(finished_at: nil)
174
- elsif (association = EXECUTION_STATUSES.key(status.to_s))
175
- SolidQueue::Job.joins(association)
176
- else
177
- SolidQueue::Job.all
178
- end
179
- end
180
-
181
- def search_scope(query)
182
- jobs = SolidQueue::Job
183
- pattern = "%#{jobs.sanitize_sql_like(query, LIKE_ESCAPE)}%"
184
- scope = jobs.where("LOWER(class_name) LIKE LOWER(:pattern) ESCAPE :escape", pattern: pattern, escape: LIKE_ESCAPE)
185
- # Matching argument text would let a search reveal values the page masks.
186
- unless JobHarbor.configuration.filtering_parameters?
187
- scope = scope.or(jobs.where("LOWER(arguments) LIKE LOWER(:pattern) ESCAPE :escape", pattern: pattern, escape: LIKE_ESCAPE))
188
- end
189
- scope = scope.or(jobs.where(id: query)) if query.match?(/\A\d+\z/)
190
- scope = scope.or(jobs.where(active_job_id: query)) if query.match?(UUID_FORMAT)
191
- scope
192
- end
193
-
194
- def paginate(scope, page:, per_page:)
195
- page = [ page.to_i, 1 ].max
196
- offset = (page - 1) * per_page
197
- total = scope.count
198
-
199
- jobs = scope.preload(*EXECUTION_STATUSES.keys).order(id: :desc).limit(per_page).offset(offset).map { |job| new(job) }
200
- [ PageInfo.new(page: page, per_page: per_page, count: total), jobs ]
138
+ new(SolidQueue::Job.preload(*JobStatus.execution_associations).find(id))
201
139
  end
202
140
  end
203
141
  end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JobHarbor
4
+ class JobQuery
5
+ LIKE_ESCAPE = "!"
6
+ UUID_FORMAT = /\A\h{8}-\h{4}-\h{4}-\h{4}-\h{12}\z/
7
+
8
+ class << self
9
+ def search(text)
10
+ new(search: text)
11
+ end
12
+
13
+ def filter_options
14
+ {
15
+ class_names: SolidQueue::Job.distinct.pluck(:class_name).sort,
16
+ queue_names: SolidQueue::Job.distinct.pluck(:queue_name).sort
17
+ }
18
+ end
19
+
20
+ def counts
21
+ { "all" => SolidQueue::Job.count }.merge(JobStatus.all.to_h { |job_status| [ job_status.key, job_status.count ] })
22
+ end
23
+ end
24
+
25
+ def initialize(status: nil, class_name: nil, queue_name: nil, search: nil)
26
+ @status = status
27
+ @class_name = class_name
28
+ @queue_name = queue_name
29
+ @search = search
30
+ end
31
+
32
+ def jobs
33
+ filtered(status_scope)
34
+ end
35
+
36
+ # Nil for tabs without bulk actions.
37
+ def bulk_jobs
38
+ job_status = JobStatus.find(@status)
39
+ return unless job_status&.bulk_actions
40
+
41
+ filtered(SolidQueue::Job.where(id: job_status.execution_class.select(:job_id)))
42
+ end
43
+
44
+ def page(number, per_page:)
45
+ scope = jobs
46
+ total = scope.count
47
+ pages = (total.to_f / per_page).ceil
48
+ current = [ [ number.to_i, 1 ].max, [ pages, 1 ].max ].min
49
+ offset = (current - 1) * per_page
50
+
51
+ presenters = scope.preload(*JobStatus.execution_associations).order(id: :desc).limit(per_page).offset(offset).map { |job| JobPresenter.new(job) }
52
+ [ PageInfo.new(page: current, per_page: per_page, count: total), presenters ]
53
+ end
54
+
55
+ private
56
+
57
+ def status_scope
58
+ JobStatus.find(@status)&.jobs || SolidQueue::Job.all
59
+ end
60
+
61
+ def filtered(scope)
62
+ scope = scope.where(class_name: @class_name) if @class_name.present?
63
+ scope = scope.where(queue_name: @queue_name) if @queue_name.present?
64
+ scope = scope.merge(matching_search) if @search.present?
65
+ scope
66
+ end
67
+
68
+ # The arguments column holds the whole Active Job envelope; search only its arguments.
69
+ def matching_search
70
+ jobs = SolidQueue::Job
71
+ return jobs.where(id: @search) if @search.match?(/\A\d+\z/)
72
+
73
+ pattern = "%#{jobs.sanitize_sql_like(@search, LIKE_ESCAPE)}%"
74
+ scope = jobs.where("LOWER(class_name) LIKE LOWER(:pattern) ESCAPE :escape", pattern: pattern, escape: LIKE_ESCAPE)
75
+ # Matching argument text would let a search reveal values the page masks.
76
+ unless JobHarbor.configuration.filtering_parameters?
77
+ if (arguments_sql = job_arguments_sql)
78
+ scope = scope.or(jobs.where("LOWER(#{arguments_sql}) LIKE LOWER(:pattern) ESCAPE :escape", pattern: pattern, escape: LIKE_ESCAPE))
79
+ end
80
+ end
81
+ scope = scope.or(jobs.where(active_job_id: @search)) if @search.match?(UUID_FORMAT)
82
+ scope
83
+ end
84
+
85
+ # NULL for rows that aren't a JSON object, so a bad row can't break search.
86
+ def job_arguments_sql
87
+ column = "#{SolidQueue::Job.quoted_table_name}.arguments"
88
+ adapter, version = SolidQueue::Job.with_connection { |connection| [ connection.adapter_name, connection.database_version ] }
89
+
90
+ case adapter
91
+ when /postg/i
92
+ if version >= 160_000
93
+ "CASE WHEN pg_input_is_valid(#{column}, 'jsonb') THEN (#{column}::jsonb -> 'arguments')::text END"
94
+ else
95
+ # Best effort below PostgreSQL 16, which has no pg_input_is_valid.
96
+ "CASE WHEN #{column} ~ '^\\s*\\{.*\\}\\s*$' THEN (#{column}::jsonb -> 'arguments')::text END"
97
+ end
98
+ when /mysql|trilogy/i
99
+ "CASE WHEN JSON_VALID(#{column}) THEN CAST(JSON_EXTRACT(#{column}, '$.arguments') AS CHAR) END"
100
+ when /sqlite/i
101
+ "CASE WHEN json_valid(#{column}) THEN json_extract(#{column}, '$.arguments') END"
102
+ end
103
+ end
104
+ end
105
+ end