Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(background_processor): include redis docker configuration #329

Merged
merged 3 commits into from
Dec 9, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions lib/potassium/assets/redis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
development: &default
host: <%= ENV["BOXEN_REDIS_HOST"] || ENV["REDIS_HOST"] || "127.0.0.1" %>
port: <%= ENV["BOXEN_REDIS_PORT"] || ENV["REDIS_PORT"] || 6379 %>
url: <%= ENV.fetch("REDIS_URL") %>

test:
<<: *default
Expand Down
35 changes: 34 additions & 1 deletion lib/potassium/recipes/background_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def ask
end

def create
add_sidekiq if get(:background_processor)
if get(:background_processor)
add_sidekiq
add_docker_compose_redis_config
set_redis_dot_env
end
end

def install
Expand All @@ -26,6 +30,35 @@ def installed?
gem_exists?(/sidekiq/)
end

private

def add_docker_compose_redis_config
compose = DockerHelpers.new('docker-compose.yml')

service_definition =
<<~YAML
image: redis
ports:
- 6379
volumes:
- redis_data:/data
YAML

compose.add_service('redis', service_definition)
compose.add_volume('redis_data')
end

def set_redis_dot_env
append_to_file(
'.env.development',
<<~TEXT
REDIS_HOST=127.0.0.1
REDIS_PORT=$(make services-port SERVICE=redis PORT=6379)
REDIS_URL=redis://${REDIS_HOST}:${REDIS_PORT}/1
TEXT
)
end

def add_sidekiq
gather_gem("sidekiq")
add_adapters("sidekiq")
Expand Down
13 changes: 12 additions & 1 deletion spec/features/background_processor_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "spec_helper"
require 'yaml'

RSpec.describe "BackgroundProcessor" do
context "working with sidekiq" do
Expand Down Expand Up @@ -41,6 +42,9 @@
it "adds ENV vars" do
content = IO.read("#{project_path}/.env.development")
expect(content).to include("DB_POOL=25")
expect(content).to include('REDIS_HOST=127.0.0.1')
expect(content).to include('REDIS_PORT=$(make services-port SERVICE=redis PORT=6379)')
expect(content).to include('REDIS_URL=redis://${REDIS_HOST}:${REDIS_PORT}/1')
end

it "adds sidekiq.rb file" do
Expand All @@ -55,12 +59,19 @@

it "adds redis.yml file" do
content = IO.read("#{project_path}/config/redis.yml")
expect(content).to include("REDIS_HOST")
expect(content).to include("REDIS_URL")
end

it "mounts sidekiq app" do
content = IO.read("#{project_path}/config/routes.rb")
expect(content).to include("mount Sidekiq::Web => '/queue'")
end

it 'adds redis to docker-compose' do
compose_file = IO.read("#{project_path}/docker-compose.yml")
compose_content = YAML.safe_load(compose_file, symbolize_names: true)

expect(compose_content[:services]).to include(:redis)
end
end
end