Skip to content

Commit d5bad5a

Browse files
committed
Add bin/jobs binstub that gets copied over on installation
And a nicer Cli class to use over in the binstub with better args instead of environment variables.
1 parent ac71ae3 commit d5bad5a

File tree

6 files changed

+60
-6
lines changed

6 files changed

+60
-6
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Solid Queue is a DB-based queuing backend for [Active Job](https://edgeguides.rubyonrails.org/active_job_basics.html), designed with simplicity and performance in mind.
44

5-
Besides regular job enqueuing and processing, Solid Queue supports delayed jobs, concurrency controls, pausing queues, numeric priorities per job, priorities by queue order, and bulk enqueuing (`enqueue_all` for Active Job's `perform_all_later`). _Improvements to logging and instrumentation, a better CLI tool, a way to run within an existing process in "async" mode, and some way of specifying unique jobs are coming very soon._
5+
Besides regular job enqueuing and processing, Solid Queue supports delayed jobs, concurrency controls, pausing queues, numeric priorities per job, priorities by queue order, and bulk enqueuing (`enqueue_all` for Active Job's `perform_all_later`).
66

77
Solid Queue can be used with SQL databases such as MySQL, PostgreSQL or SQLite, and it leverages the `FOR UPDATE SKIP LOCKED` clause, if available, to avoid blocking and waiting on locks when polling jobs. It relies on Active Job for retries, discarding, error handling, serialization, or delays, and it's compatible with Ruby on Rails multi-threading.
88

@@ -59,14 +59,14 @@ Finally, you need to run the migrations:
5959
$ bin/rails db:migrate
6060
```
6161

62-
After this, you'll be ready to enqueue jobs using Solid Queue, but you need to start Solid Queue's supervisor to run them.
62+
After this, you'll be ready to enqueue jobs using Solid Queue, but you need to start Solid Queue's supervisor to run them. You can use the provided binstub:`
6363
```bash
64-
$ bundle exec rake solid_queue:start
64+
$ bin/jobs
6565
```
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. Depending on the configuration, you can designate some machines to run only dispatchers or only workers. See the [configuration](#configuration) section for more details on this.
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 `bin/jobs` on multiple machines at the same time. Depending on the configuration, you can designate some machines to run only dispatchers or only workers. See the [configuration](#configuration) section for more details on this.
7070

7171
## Requirements
7272
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.
@@ -159,6 +159,16 @@ When receiving a `QUIT` signal, if workers still have jobs in-flight, these will
159159
If processes have no chance of cleaning up before exiting (e.g. if someone pulls a cable somewhere), in-flight jobs might remain claimed by the processes executing them. Processes send heartbeats, and the supervisor checks and prunes processes with expired heartbeats, which will release any claimed jobs back to their queues. You can configure both the frequency of heartbeats and the threshold to consider a process dead. See the section below for this.
160160

161161

162+
### Fork vs. async mode
163+
164+
By default, the supervisor will fork additional processes for each worker and dispatcher so that they run in different processes. This provides the best isolation and performance, but can have additional memory usage. Alternatively, you can run all workers and dispatchers in the same process as the supervisor by passing `--mode async` when starting it:
165+
```
166+
bin/jobs --mode async
167+
```
168+
169+
If you use the `async` mode, the `processes` option in the configuration will be ignored.
170+
171+
162172
### Dedicated database configuration
163173

164174
Solid Queue can be configured to run on a different database than the main application.

lib/generators/solid_queue/install/USAGE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ Example:
77
This will perform the following:
88
Installs solid_queue migrations
99
Replaces Active Job's adapter in environment configuration
10+
Installs bin/jobs binstub to start the supervisor

lib/generators/solid_queue/install/install_generator.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@ def add_solid_queue
1414
end
1515
end
1616

17-
say "Copying sample configuration"
18-
copy_file "config.yml", "config/solid_queue.yml"
17+
if File.exist?("config/solid_queue.yml")
18+
say "Skipping sample configuration as config/solid_queue.yml exists"
19+
else
20+
say "Copying sample configuration"
21+
copy_file "config.yml", "config/solid_queue.yml"
22+
end
23+
24+
say "Copying binstub"
25+
copy_file "jobs", "bin/jobs"
26+
chmod "bin/jobs", 0755 & ~File.umask, verbose: false
1927
end
2028

2129
def create_migrations
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env ruby
2+
3+
require_relative "../config/environment"
4+
require "solid_queue/cli"
5+
6+
SolidQueue::Cli.start(ARGV)

lib/solid_queue/cli.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
require "thor"
4+
5+
module SolidQueue
6+
class Cli < Thor
7+
class_option :config_file, type: :string, aliases: "-c", default: Configuration::DEFAULT_CONFIG_FILE_PATH, desc: "Path to config file"
8+
class_option :mode, type: :string, default: "fork", enum: %w[ fork async ], desc: "Whether to fork processes for workers and dispatchers (fork) or to run these in the same process as the supervisor (async)"
9+
10+
def self.exit_on_failure?
11+
true
12+
end
13+
14+
desc :start, "Starts Solid Queue supervisor to dispatch and perform enqueued jobs. Default command."
15+
default_command :start
16+
17+
def start
18+
SolidQueue::Supervisor.start(mode: options["mode"], load_configuration_from: options["config_file"])
19+
end
20+
end
21+
end

test/unit/configuration_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class ConfigurationTest < ActiveSupport::TestCase
1919
assert_processes configuration, :dispatcher, 1, batch_size: SolidQueue::Configuration::DISPATCHER_DEFAULTS[:batch_size]
2020
end
2121

22+
test "default configuration when config given is empty" do
23+
configuration = SolidQueue::Configuration.new(load_from: {})
24+
25+
assert_equal 2, configuration.configured_processes.count
26+
assert_processes configuration, :worker, 1, queues: [ "*" ]
27+
assert_processes configuration, :dispatcher, 1, batch_size: SolidQueue::Configuration::DISPATCHER_DEFAULTS[:batch_size]
28+
end
29+
2230
test "read configuration from default file" do
2331
configuration = SolidQueue::Configuration.new
2432
assert 3, configuration.configured_processes.count

0 commit comments

Comments
 (0)