Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,64 @@ jobs:

- name: Run RSpec
run: bundle exec rspec

# The default suite runs on in-memory SQLite; these re-run it on the other
# engines Solid Queue is hosted on, so adapter-specific SQL (filters, chart
# buckets) is proven where it actually executes.
test-mysql:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
MYSQL_DATABASE: solid_queue_monitor_test
# A non-UTC server is what exposes time-zone bugs in the SQL.
TZ: America/Sao_Paulo
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping -h 127.0.0.1"
--health-interval=5s --health-timeout=5s --health-retries=20
env:
BUNDLE_WITH: mysql
DATABASE_URL: mysql2://root@127.0.0.1:3306/solid_queue_monitor_test
steps:
- uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3"
bundler-cache: true

- name: Run RSpec
run: bundle exec rspec

test-postgresql:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: solid_queue_monitor_test
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready -U postgres"
--health-interval=5s --health-timeout=5s --health-retries=20
env:
BUNDLE_WITH: postgresql
DATABASE_URL: postgresql://postgres:postgres@127.0.0.1:5432/solid_queue_monitor_test
steps:
- uses: actions/checkout@v4

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: "3.3"
bundler-cache: true

- name: Run RSpec
run: bundle exec rspec
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

## [Unreleased]


### Changed

- The Overview's "Active Jobs" card no longer counts failed jobs. It was the sum of ready, scheduled, in-progress and failed jobs, and since failed jobs pile up until someone retries or discards them while the others drain within seconds, it usually showed the same number as the Failed card. It now counts the jobs waiting to run or running: ready, scheduled, in-progress and blocked. Failed jobs keep their own card. Expect the number to drop after upgrading.
- The test suite can run on MySQL or PostgreSQL by setting `DATABASE_URL` (SQLite in memory is still the default), and CI now runs it on MySQL 8 (with a non-UTC server) and PostgreSQL 16 as well. The adapter gems are in the optional `mysql` and `postgresql` Bundler groups.

### Added

Expand All @@ -15,8 +15,10 @@

- The `redirect_to` parameter on Retry/Discard (failed jobs), Execute (scheduled jobs) and Pause/Resume (queues) was passed straight to `redirect_to`, so a crafted link or form could send the user to another site after the action. Hosts on Rails' default open-redirect protection (`load_defaults 7.0` or later) got a 500 error instead, after the action had already run. Only paths within the dashboard are followed now, checked with Rails' `url_from`; anything else (other hosts, protocol-relative or backslash URLs, other schemes, paths outside the dashboard, or paths Rails can't parse) falls back to the page's usual redirect.
- Retrying a failed job could leave it with no execution at all, so it never ran again and was shown as pending forever. The monitor moved the job back to the ready queue and deleted its failed execution itself, inside an `ActiveRecord::Base` transaction. That is the app's primary database: when Solid Queue has its own database (`config.solid_queue.connects_to`, the Rails 8 default), the two steps committed separately. A worker could pick the job up in between and fail it again, Solid Queue recorded that failure on the same failed execution, and the monitor then deleted it. Retry, per row and bulk, now calls Solid Queue's own `FailedExecution#retry`, which does both in one transaction on Solid Queue's database. Retried jobs now also get their execution counters reset (so `retry_on ... attempts:` starts over) and respect `limits_concurrency`, as a retry from Solid Queue itself does. Retrying a failed job that someone else retried or discarded after it was loaded no longer raises an error, and a bulk retry now counts a job whose retry raises (e.g. a lock timeout) as failed and carries on with the rest instead of stopping halfway with an error page. The service's success message now reads "Job queued for retry", since a job with a concurrency limit can go to blocked executions instead of the ready queue.


- The arguments filter returned a 500 on MySQL and SQLite on every page that offers it (Failed, Ready, Scheduled, In Progress and Recurring Jobs, and the queue details page). It was built as `arguments::text ILIKE ?`, which only PostgreSQL accepts. It now goes through Arel's `matches`, which renders `ILIKE` on PostgreSQL and `LIKE` on the other adapters. It ignores case on PostgreSQL and SQLite, and on MySQL it follows the column's collation (case-insensitive under the default `utf8mb4_0900_ai_ci`, case-sensitive under `utf8mb4_bin`). `%` and `_` in the search term now match literally instead of acting as wildcards.
- The global search was case-sensitive on PostgreSQL, where plain `LIKE` respects case, so `usermailer` did not find `UserMailerJob`. It now uses the same Arel `matches` as the filters. Its `%` escaping also did nothing on SQLite, which has no default escape character. `%` and `_` now match literally on every adapter.
- On MySQL, the overview chart put jobs in the wrong time bucket whenever the database server's time zone was not UTC, and jobs near the edge of the window disappeared from it. `UNIX_TIMESTAMP` reads a `DATETIME` in the session time zone, but Rails stores UTC. The bucket index now uses `TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00', col)`, which does not depend on the session time zone.
- With `ActiveRecord.default_timezone = :local`, the overview chart put jobs in the wrong time bucket on every adapter, and on short ranges dropped recent jobs entirely. Rails then stores local wall time, but the bucket math read it as UTC. The window start is now shifted by the same local offset.

## [2.4.0] - 2026-11-17

Expand Down
10 changes: 10 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ group :development, :test do
gem 'rubocop-rspec'
gem 'sqlite3'
end

# Database adapters for running the suite beyond SQLite (see spec_helper.rb).
# Optional so a plain `bundle install` needs no MySQL or PostgreSQL client libs.
group :mysql, optional: true do
gem 'mysql2'
end

group :postgresql, optional: true do
gem 'pg'
end
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ GEM
mini_portile2 (2.8.9)
minitest (6.0.1)
prism (~> 1.5)
mysql2 (0.5.7)
bigdecimal
net-imap (0.6.2)
date
net-protocol
Expand All @@ -150,6 +152,8 @@ GEM
parser (3.3.7.1)
ast (~> 2.4.1)
racc
pg (1.7.0)
pg (1.7.0-arm64-darwin)
pp (0.6.3)
prettyprint
prettyprint (0.2.0)
Expand Down Expand Up @@ -294,6 +298,8 @@ PLATFORMS

DEPENDENCIES
factory_bot_rails
mysql2
pg
rails-controller-testing
rspec-rails
rubocop
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ Please make sure to update tests as appropriate and follow the existing code sty

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.

The suite runs on in-memory SQLite by default. Because some queries differ between adapters, it can also run on MySQL or PostgreSQL, the way CI does. Install the matching optional Bundler group, create an empty database, and point `DATABASE_URL` at it:

```bash
bundle config set --local with mysql # or: postgresql
bundle install
DATABASE_URL=mysql2://root@127.0.0.1/solid_queue_monitor_test bundle exec rspec
DATABASE_URL=postgresql://postgres@127.0.0.1/solid_queue_monitor_test bundle exec rspec
```

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
Expand Down
29 changes: 14 additions & 15 deletions app/controllers/solid_queue_monitor/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,16 @@ def filter_jobs(relation)
end

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

def arguments_match(relation = SolidQueue::Job)
pattern = "%#{ActiveRecord::Base.sanitize_sql_like(params[:arguments].to_s)}%"
relation.arel_table[:arguments].matches(Arel::Nodes.build_quoted(pattern), '\\')
end

def jobs_matching_arguments
SolidQueue::Job.where(arguments_match).select(:id)
end

def filter_ready_jobs(relation)
Expand All @@ -90,9 +98,7 @@ def filter_ready_jobs(relation)

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 = relation.where(job_id: jobs_matching_arguments) if params[:arguments].present?

relation
end
Expand All @@ -106,9 +112,7 @@ def filter_scheduled_jobs(relation)

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 = relation.where(job_id: jobs_matching_arguments) if params[:arguments].present?

relation
end
Expand All @@ -120,10 +124,7 @@ def filter_recurring_jobs(relation)
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 = filter_by_arguments(relation) if params[:arguments].present? && relation.column_names.include?('arguments')

relation
end
Expand All @@ -143,9 +144,7 @@ def filter_failed_jobs(relation)
end
end

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

relation
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def filter_in_progress_jobs(relation)
relation = relation.where(job_id: SolidQueue::Job.where('class_name LIKE ?', "%#{params[:class_name]}%").select(:id))
end

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

relation
end
Expand Down
25 changes: 22 additions & 3 deletions app/services/solid_queue_monitor/chart_data_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def build_buckets(start_time, bucket_seconds)
# The bucket index is computed as: (epoch(column) - epoch(start_time)) / interval
# This works identically on PostgreSQL and SQLite.
def bucket_counts(model, column, start_time, end_time, interval, exclude_nil: false)
start_epoch = start_time.to_i
start_epoch = stored_epoch(start_time)
expr = bucket_index_expr(model, column, start_epoch, interval)

scope = model.where(column => start_time..end_time)
Expand All @@ -74,14 +74,33 @@ def bucket_counts(model, column, start_time, end_time, interval, exclude_nil: fa
# rubocop:enable Style/HashTransformKeys
end

# The epoch of `time` in the frame the database computes for a stored
# timestamp. Every bucket expression below reads the zone-less stored
# value as if it were UTC. That is true when Rails stores UTC (the
# default). With `ActiveRecord.default_timezone = :local`, Rails stores the
# process's local wall time instead, so the start of the window is shifted
# by the same local offset to keep both sides in one frame.
def stored_epoch(time)
return time.to_i if ActiveRecord.default_timezone == :utc

epoch = time.to_i
epoch + Time.at(epoch).getlocal.utc_offset
end

def fill_buckets(buckets, index_counts)
buckets.map { |b| index_counts.fetch(b[:index], 0) }
end

# Cross-DB bucket index expression.
# PostgreSQL: CAST((EXTRACT(EPOCH FROM col) - start) / interval AS INTEGER)
# SQLite: CAST((CAST(strftime('%s', col) AS INTEGER) - start) / interval AS INTEGER)
# MySQL: CAST((UNIX_TIMESTAMP(col) - start) / interval AS SIGNED)
# MySQL: FLOOR((TIMESTAMPDIFF(SECOND, '1970-01-01', col) - start) / interval)
# (not UNIX_TIMESTAMP: it reads a DATETIME in the session time
# zone, which need not match the zone Rails stored it in — a
# mismatch shifts every bucket and drops rows out of the window)
#
# All three read the stored value as UTC wall time; `stored_epoch` puts
# `start` in the same frame.
#
# The adapter is detected from the model's own connection, not
# ActiveRecord::Base's. Solid Queue is commonly hosted on a dedicated
Expand All @@ -91,7 +110,7 @@ def bucket_index_expr(model, column, start_epoch, interval_seconds)
if adapter?(model, 'sqlite')
"CAST((CAST(strftime('%s', #{column}) AS INTEGER) - #{start_epoch}) / #{interval_seconds} AS INTEGER)"
elsif adapter?(model, 'mysql') || adapter?(model, 'trilogy')
"FLOOR((UNIX_TIMESTAMP(#{column}) - #{start_epoch}) / #{interval_seconds})"
"FLOOR((TIMESTAMPDIFF(SECOND, '1970-01-01 00:00:00', #{column}) - #{start_epoch}) / #{interval_seconds})"
else
"FLOOR((EXTRACT(EPOCH FROM #{column}) - #{start_epoch}) / #{interval_seconds})::integer"
end
Expand Down
Loading
Loading