Skip to content

Commit

Permalink
Merge pull request #123 from fly-apps/solid_queue_plugin
Browse files Browse the repository at this point in the history
Solid queue plugin
  • Loading branch information
rubys authored Nov 2, 2024
2 parents b8a37cb + fcfd5c9 commit b6dfb64
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 57 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Layout/EndOfLine:

Layout/EmptyLineAfterMagicComment:
Enabled: true
Exclude:
- '**/test/**/*'

Layout/EmptyLinesAroundAccessModifier:
Enabled: true
Expand Down Expand Up @@ -158,6 +160,7 @@ Style/FrozenStringLiteralComment:
- 'activestorage/db/update_migrate/**/*.rb'
- 'actionmailbox/db/migrate/**/*.rb'
- 'actiontext/db/migrate/**/*.rb'
- 'test/**/*.rb'

Style/MapToHash:
Enabled: true
Expand Down
18 changes: 8 additions & 10 deletions lib/generators/dockerfile_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ def generate_app
force: File.exist?("fly.toml")
end

if using_solidq? && deploy_database == "sqlite3" && File.exist?("config/puma.rb")
append_to_file "config/puma.rb", "\n# Run the Solid Queue's supervisor\nplugin :solid_queue\n"
end

if using_litefs?
template "litefs.yml.erb", "config/litefs.yml"

Expand Down Expand Up @@ -1212,29 +1216,23 @@ def dbprep_command

def procfile
if using_passenger?
base = {
{
nginx: "nginx"
}
elsif options.nginx?
base = {
{
nginx: '/usr/sbin/nginx -g "daemon off;"',
rails: "./bin/rails server -p 3001"
}
elsif using_thruster?
base = {
{
rails: "bundle exec thrust ./bin/rails server"
}
else
base = {
{
rails: "./bin/rails server"
}
end

if using_solidq? && (deploy_database == "sqlite3")
base.merge(solidq: "bundle exec rake solid_queue:start")
else
base
end
end

def using_thruster?
Expand Down
10 changes: 10 additions & 0 deletions test/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ def check_database_config
assert_equal expected, results
end

def check_puma
results = IO.read("config/puma.rb")

IO.write("#{@results}/puma.rb", results) if @capture

expected = IO.read("#{@results}/puma.rb")

assert_equal expected, results
end

def teardown
return if ENV["TEST_KEEP"]
Dir.chdir ".."
Expand Down
24 changes: 7 additions & 17 deletions test/results/solid_queue_sqlite3/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,27 @@ FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
apt-get install --no-install-recommends -y curl libsqlite3-0 ruby-foreman && \
apt-get install --no-install-recommends -y curl libsqlite3-0 && \
rm -rf /var/lib/apt/lists /var/cache/apt/archives

RUN gem install foreman

# Copy built artifacts: gems, application
COPY --from=build "${BUNDLE_PATH}" "${BUNDLE_PATH}"
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
ARG UID=xxx \
GID=1000
RUN groupadd -f -g $GID rails && \
useradd -u $UID -g $GID rails --create-home --shell /bin/bash && \
RUN groupadd --system --gid 1000 rails && \
useradd rails --uid 1000 --gid 1000 --create-home --shell /bin/bash && \
mkdir /data && \
chown -R rails:rails db log storage tmp /data
USER rails:rails
chown -R 1000:1000 db log storage tmp /data
USER 1000:1000

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3"

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Build a Procfile for production use
COPY <<-"EOF" /rails/Procfile.prod
rails: ./bin/rails server
solidq: bundle exec rake solid_queue:start
EOF

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
EXPOSE 80
VOLUME /data
CMD ["foreman", "start", "--procfile=Procfile.prod"]
CMD ["bundle", "exec", "thrust", "./bin/rails", "server"]
22 changes: 0 additions & 22 deletions test/results/solid_queue_sqlite3/docker-compose.yml

This file was deleted.

4 changes: 0 additions & 4 deletions test/results/solid_queue_sqlite3/fly.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@
source="data"
destination="/data"

[[statics]]
guest_path = "/rails/public"
url_prefix = "/"

44 changes: 44 additions & 0 deletions test/results/solid_queue_sqlite3/puma.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This configuration file will be evaluated by Puma. The top-level methods that
# are invoked here are part of Puma's configuration DSL. For more information
# about methods provided by the DSL, see https://puma.io/puma/Puma/DSL.html.
#
# Puma starts a configurable number of processes (workers) and each process
# serves each request in a thread from an internal thread pool.
#
# You can control the number of workers using ENV["WEB_CONCURRENCY"]. You
# should only set this value when you want to run 2 or more workers. The
# default is already 1.
#
# The ideal number of threads per worker depends both on how much time the
# application spends waiting for IO operations and on how much you wish to
# prioritize throughput over latency.
#
# As a rule of thumb, increasing the number of threads will increase how much
# traffic a given process can handle (throughput), but due to CRuby's
# Global VM Lock (GVL) it has diminishing returns and will degrade the
# response time (latency) of the application.
#
# The default is set to 3 threads as it's deemed a decent compromise between
# throughput and latency for the average Rails application.
#
# Any libraries that use a connection pool or another resource pool should
# be configured to provide at least as many connections as the number of
# threads. This includes Active Record's `pool` parameter in `database.yml`.
threads_count = ENV.fetch("RAILS_MAX_THREADS", 3)
threads threads_count, threads_count

# Specifies the `port` that Puma will listen on to receive requests; default is 3000.
port ENV.fetch("PORT", 3000)

# Allow puma to be restarted by `bin/rails restart` command.
plugin :tmp_restart

# Run the Solid Queue supervisor inside of Puma for single-server deployments
plugin :solid_queue if ENV["SOLID_QUEUE_IN_PUMA"]

# Specify the PID file. Defaults to tmp/pids/server.pid in development.
# In other environments, only set the PID file if requested.
pidfile ENV["PIDFILE"] if ENV["PIDFILE"]

# Run the Solid Queue's supervisor
plugin :solid_queue
6 changes: 2 additions & 4 deletions test/test_solidq_sqlite3.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@
require_relative "base"

class TestSolidQueueSqlite3 < TestBase
@rails_options = "--database=sqlite3"
@generate_options = "--compose"
@rails_options = "--database=sqlite3 --main"

def app_setup
system "bundle add solid_queue"
FileUtils.touch "fly.toml"
IO.write "app/jobs/DummyJob.rb", "class DummyJob < ApplicationJob; end"
end

def test_solidq
check_dockerfile
check_toml
check_compose
check_puma
end
end

0 comments on commit b6dfb64

Please sign in to comment.