Sidekiq with docker-compose

Set up sidekiq with local support for docker-compose
Icons/chart bar
Used 49 times
Created by
T TobiasBales

Usage

Run this command in your Rails app directory in the terminal:

rails app:template LOCATION="https://railsbytes.com/script/VwysYA"
Template Source

Review the code before running this template on your machine.

def file_contains?(filename, string)
  File.foreach(filename).detect { |line| line.include?(string) }
end

gem 'sidekiq'
gem 'sidekiq-statistic'
gem 'redis' unless file_contains? 'Gemfile', "Gem 'redis'"
gem 'hiredis' unless file_contains? 'Gemfile', "Gem 'hiredis'"

Bundler.with_unbundled_env { run 'bundle install' }

unless File.exist? 'docker-compose.yml'
  create_file 'docker-compose.yml' do <<~YAML
    version: "3.1"

    services:
  YAML
  end
end

inject_into_file 'docker-compose.yml' do <<-YAML
  redis-sidekiq:
    image: redis
    restart: always
    volumes:
      - ./tmp/redis-sidekiq:/data
    ports:
      - 6380:6379
  YAML
end

application do <<~EOF
  config.active_job.queue_adapter = :sidekiq
  EOF
end


inject_into_file 'config/routes.rb' do <<~EOF
  require "sidekiq/web"
  if Rails.env.production?
    Sidekiq::Web.use Rack::Auth::Basic do |username, password|
      ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(username), ::Digest::SHA256.hexdigest(ENV["SIDEKIQ_USERNAME"])) &
        ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(password), ::Digest::SHA256.hexdigest(ENV["SIDEKIQ_PASSWORD"]))
    end
  end
  EOF
end

route 'mount Sidekiq::Web => "/sidekiq"'

if File.exist? 'Procfile'
  inject_into_file 'Procfile' do <<~EOF
    worker: bundle exec sidekiq -t 25
  EOF
  end
end

if File.exist? 'Procfile.dev'
  inject_into_file 'Procfile.dev' do <<~EOF
    worker: bundle exec sidekiq -t 25
  EOF
  end
end
Comments

Sign up or Login to leave a comment.