Skip to content

Commit acf5766

Browse files
authored
Merge branch 'main' into remove-rails-dependency
2 parents b0abe10 + 4ae8f48 commit acf5766

File tree

10 files changed

+32
-12
lines changed

10 files changed

+32
-12
lines changed

Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
solid_queue (0.2.1)
4+
solid_queue (0.2.2)
55
activejob (>= 7.1)
66
activerecord (>= 7.1)
77
railties (>= 7.1)

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ $ bundle exec rake solid_queue:start
6666

6767
This will start processing jobs in all queues using the default configuration. See [below](#configuration) to learn more about configuring Solid Queue.
6868

69+
For small projects, you can run Solid Queue on the same machine as your webserver. When you're ready to scale, Solid Queue supports horizontal scaling out-of-the-box. You can run Solid Queue on a separate server from your webserver, or even run `bundle exec rake solid_queue:start` on multiple machines at the same time. If you'd like to designate some machines to be only dispatchers or only workers, use `bundle exec rake solid_queue:dispatch` or `bundle exec rake solid_queue:work`, respectively.
70+
6971
## Requirements
7072
Besides Rails 7.1, Solid Queue works best with MySQL 8+ or PostgreSQL 9.5+, as they support `FOR UPDATE SKIP LOCKED`. You can use it with older versions, but in that case, you might run into lock waits if you run multiple workers for the same queue.
7173

@@ -115,7 +117,7 @@ Everything is optional. If no configuration is provided, Solid Queue will run wi
115117
This will create a worker fetching jobs from all queues starting with `staging`. The wildcard `*` is only allowed on its own or at the end of a queue name; you can't specify queue names such as `*_some_queue`. These will be ignored.
116118

117119
Finally, you can combine prefixes with exact names, like `[ staging*, background ]`, and the behaviour with respect to order will be the same as with only exact names.
118-
- `threads`: this is the max size of the thread pool that each worker will have to run jobs. Each worker will fetch this number of jobs from their queue(s), at most and will post them to the thread pool to be run. By default, this is `5`. Only workers have this setting.
120+
- `threads`: this is the max size of the thread pool that each worker will have to run jobs. Each worker will fetch this number of jobs from their queue(s), at most and will post them to the thread pool to be run. By default, this is `3`. Only workers have this setting.
119121
- `processes`: this is the number of worker processes that will be forked by the supervisor with the settings given. By default, this is `1`, just a single process. This setting is useful if you want to dedicate more than one CPU core to a queue or queues with the same configuration. Only workers have this setting.
120122

121123

@@ -131,7 +133,7 @@ We recommend not mixing queue order with priorities but either choosing one or t
131133

132134
### Threads, processes and signals
133135

134-
Workers in Solid Queue use a thread pool to run work in multiple threads, configurable via the `threads` parameter above. Besides this, parallelism can be achieved via multiple processes, configurable via different workers or the `processes` parameter above.
136+
Workers in Solid Queue use a thread pool to run work in multiple threads, configurable via the `threads` parameter above. Besides this, parallelism can be achieved via multiple processes on one machine (configurable via different workers or the `processes` parameter above) or by horizontal scaling.
135137

136138
The supervisor is in charge of managing these processes, and it responds to the following signals:
137139
- `TERM`, `INT`: starts graceful termination. The supervisor will send a `TERM` signal to its supervised processes, and it'll wait up to `SolidQueue.shutdown_timeout` time until they're done. If any supervised processes are still around by then, it'll send a `QUIT` signal to them to indicate they must exit.

app/models/solid_queue/blocked_execution.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ def release_one(concurrency_key)
3030

3131
private
3232
def releasable(concurrency_keys)
33-
semaphores = Semaphore.where(key: concurrency_keys).select(:key, :value).index_by(&:key)
33+
semaphores = Semaphore.where(key: concurrency_keys).pluck(:key, :value).to_h
3434

3535
# Concurrency keys without semaphore + concurrency keys with open semaphore
36-
(concurrency_keys - semaphores.keys) | semaphores.select { |key, semaphore| semaphore.value > 0 }.map(&:first)
36+
(concurrency_keys - semaphores.keys) | semaphores.select { |_key, value| value > 0 }.keys
3737
end
3838
end
3939

app/models/solid_queue/execution/dispatching.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def dispatch_jobs(job_ids)
1010
jobs = Job.where(id: job_ids)
1111

1212
Job.dispatch_all(jobs).map(&:id).tap do |dispatched_job_ids|
13-
where(job_id: dispatched_job_ids).delete_all
13+
where(job_id: dispatched_job_ids).order(:job_id).delete_all
1414
SolidQueue.logger.info("[SolidQueue] Dispatched #{dispatched_job_ids.size} jobs")
1515
end
1616
end

app/models/solid_queue/job/clearable.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ module Clearable
66
extend ActiveSupport::Concern
77

88
included do
9-
scope :clearable, ->(finished_before: SolidQueue.clear_finished_jobs_after.ago) { where.not(finished_at: nil).where(finished_at: ...finished_before) }
9+
scope :clearable, ->(finished_before: SolidQueue.clear_finished_jobs_after.ago, class_name: nil) { where.not(finished_at: nil).where(finished_at: ...finished_before).where(class_name.present? ? { class_name: class_name } : {}) }
1010
end
1111

1212
class_methods do
13-
def clear_finished_in_batches(batch_size: 500, finished_before: SolidQueue.clear_finished_jobs_after.ago)
13+
def clear_finished_in_batches(batch_size: 500, finished_before: SolidQueue.clear_finished_jobs_after.ago, class_name: nil)
1414
loop do
15-
records_deleted = clearable(finished_before: finished_before).limit(batch_size).delete_all
15+
records_deleted = clearable(finished_before: finished_before, class_name: class_name).limit(batch_size).delete_all
1616
break if records_deleted == 0
1717
end
1818
end

app/models/solid_queue/scheduled_execution.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ScheduledExecution < Execution
55
include Dispatching
66

77
scope :due, -> { where(scheduled_at: ..Time.current) }
8-
scope :ordered, -> { order(scheduled_at: :asc, priority: :asc) }
8+
scope :ordered, -> { order(scheduled_at: :asc, priority: :asc, job_id: :asc) }
99
scope :next_batch, ->(batch_size) { due.ordered.limit(batch_size) }
1010

1111
assumes_attributes_from_job :scheduled_at

lib/solid_queue/configuration.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module SolidQueue
44
class Configuration
55
WORKER_DEFAULTS = {
66
queues: "*",
7-
threads: 5,
7+
threads: 3,
88
processes: 1,
99
polling_interval: 0.1
1010
}

lib/solid_queue/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module SolidQueue
2-
VERSION = "0.2.1"
2+
VERSION = "0.2.2"
33
end

test/integration/jobs_lifecycle_test.rb

+15
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,21 @@ class JobsLifecycleTest < ActiveSupport::TestCase
133133
SolidQueue::Job.clear_finished_in_batches
134134
end
135135
end
136+
137+
test "respect class name when clearing finished jobs" do
138+
10.times { AddToBufferJob.perform_later(2) }
139+
10.times { RaisingJob.perform_later(RuntimeError, "A") }
140+
jobs = SolidQueue::Job.all
141+
142+
jobs.each(&:finished!)
143+
144+
travel_to 3.days.from_now
145+
146+
SolidQueue::Job.clear_finished_in_batches(class_name: "AddToBufferJob")
147+
148+
assert_equal 0, SolidQueue::Job.where(class_name: "AddToBufferJob").count
149+
assert_equal 10, SolidQueue::Job.where(class_name: "RaisingJob").count
150+
end
136151

137152
private
138153
def deleting_finished_jobs

test/unit/dispatcher_test.rb

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
class DispatcherTest < ActiveSupport::TestCase
55
include ActiveSupport::Testing::MethodCallAssertions
66

7+
self.use_transactional_tests = false
8+
79
setup do
810
@dispatcher = SolidQueue::Dispatcher.new(polling_interval: 0.1, batch_size: 10)
911
end
@@ -56,6 +58,7 @@ class DispatcherTest < ActiveSupport::TestCase
5658
assert_equal 15, SolidQueue::ScheduledExecution.count
5759

5860
another_dispatcher = SolidQueue::Dispatcher.new(polling_interval: 0.1, batch_size: 10)
61+
5962
@dispatcher.start
6063
another_dispatcher.start
6164

0 commit comments

Comments
 (0)