From 5c3644c71e1f8862e3ff80788a75f60a85ecf3dc Mon Sep 17 00:00:00 2001 From: Guillermo Moreno Date: Mon, 22 May 2023 16:48:57 -0400 Subject: [PATCH 1/2] feat(redis): move redis to its own recipe and install redis-actionpack --- lib/potassium/assets/.circleci/config.yml.erb | 9 +--- lib/potassium/recipes/background_processor.rb | 30 ----------- lib/potassium/recipes/redis.rb | 51 +++++++++++++++++++ lib/potassium/templates/application.rb | 1 + spec/features/redis_spec.rb | 38 ++++++++++++++ 5 files changed, 92 insertions(+), 37 deletions(-) create mode 100644 lib/potassium/recipes/redis.rb create mode 100644 spec/features/redis_spec.rb diff --git a/lib/potassium/assets/.circleci/config.yml.erb b/lib/potassium/assets/.circleci/config.yml.erb index 0f1f85f1..3288b307 100644 --- a/lib/potassium/assets/.circleci/config.yml.erb +++ b/lib/potassium/assets/.circleci/config.yml.erb @@ -4,9 +4,7 @@ ruby-image: &ruby-image cimg/ruby:<%= ruby_version %> <%- if selected?(:database, :postgresql) -%> postgres-image: &postgres-image postgres:<%= Potassium::POSTGRES_VERSION %> <%- end -%> -<%- if selected?(:background_processor) -%> -redis-image: &redis-image redis -<%- end -%> +redis-image: &redis-image cimg/redis:6.2.12 env-vars: &env-vars BUNDLE_JOBS: 4 BUNDLE_PATH: vendor/bundle @@ -23,9 +21,7 @@ executors: <%- if selected?(:database, :postgresql) -%> - image: *postgres-image <%- end -%> - <%- if selected?(:background_processor) -%> - image: *redis-image - <%- end -%> lint-executor: docker: @@ -94,11 +90,10 @@ jobs: - browser-tools/install-chrome - browser-tools/install-chromedriver - <%- if selected?(:background_processor) -%> - run: name: Wait for redis service command: dockerize -wait tcp://localhost:6379 -timeout 1m - <%- end -%> + <%- if selected?(:database, :postgresql) -%> - run: name: Wait for postgres service diff --git a/lib/potassium/recipes/background_processor.rb b/lib/potassium/recipes/background_processor.rb index d46e0e3e..e9447ae9 100644 --- a/lib/potassium/recipes/background_processor.rb +++ b/lib/potassium/recipes/background_processor.rb @@ -14,8 +14,6 @@ def ask def create if get(:background_processor) add_sidekiq - add_docker_compose_redis_config - set_redis_dot_env end end @@ -40,7 +38,6 @@ def add_sidekiq append_to_file(".env.development", "DB_POOL=25\n") template("../assets/sidekiq.rb.erb", "config/initializers/sidekiq.rb", force: true) copy_file("../assets/sidekiq.yml", "config/sidekiq.yml", force: true) - copy_file("../assets/redis.yml", "config/redis.yml", force: true) recipe.mount_sidekiq_routes end end @@ -68,33 +65,6 @@ def mount_sidekiq_routes 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=COMMAND_EXPAND(make services-port SERVICE=redis PORT=6379) - REDIS_URL=redis://${REDIS_HOST}:${REDIS_PORT}/1 - TEXT - ) - end - def enabled_mailer? mailer_answer = get(:email_service) mailer_answer && ![:none, :None].include?(mailer_answer.to_sym) diff --git a/lib/potassium/recipes/redis.rb b/lib/potassium/recipes/redis.rb new file mode 100644 index 00000000..67289e14 --- /dev/null +++ b/lib/potassium/recipes/redis.rb @@ -0,0 +1,51 @@ +class Recipes::Redis < Rails::AppBuilder + def create + add_redis + add_docker_compose_redis_config + set_redis_dot_env + end + + def install + create + end + + def installed? + gem_exists?(/redis-actionpack/) + end + + def add_redis + run_action(:install_redis) do + gather_gem("redis-actionpack") + copy_file("../assets/redis.yml", "config/redis.yml", force: true) + end + end + + private + + def add_docker_compose_redis_config + compose = DockerHelpers.new('docker-compose.yml') + + service_definition = + <<~YAML + image: redis:6.2.12 + 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=COMMAND_EXPAND(make services-port SERVICE=redis PORT=6379) + REDIS_URL=redis://${REDIS_HOST}:${REDIS_PORT}/1 + TEXT + ) + end +end diff --git a/lib/potassium/templates/application.rb b/lib/potassium/templates/application.rb index 312f212d..4e36c850 100644 --- a/lib/potassium/templates/application.rb +++ b/lib/potassium/templates/application.rb @@ -45,6 +45,7 @@ create :yarn create :editorconfig create :mailer + create :redis create :background_processor create :schedule create :i18n diff --git a/spec/features/redis_spec.rb b/spec/features/redis_spec.rb new file mode 100644 index 00000000..b35e9a9d --- /dev/null +++ b/spec/features/redis_spec.rb @@ -0,0 +1,38 @@ +require "spec_helper" +require 'yaml' + +RSpec.describe "RedisProcessor" do + before :all do + drop_dummy_database + remove_project_directory + create_dummy_project("heroku" => true) + end + + context "when installed" do + it "adds redis-actionpack gem to Gemfile" do + gemfile_content = IO.read("#{project_path}/Gemfile") + expect(gemfile_content).to include("redis-actionpack") + end + + it "adds ENV vars" do + content = IO.read("#{project_path}/.env.development") + expect(content).to include('REDIS_HOST=127.0.0.1') + expect(content).to include( + 'REDIS_PORT=COMMAND_EXPAND(make services-port SERVICE=redis PORT=6379)' + ) + expect(content).to include('REDIS_URL=redis://${REDIS_HOST}:${REDIS_PORT}/1') + end + + it "adds redis.yml file" do + content = IO.read("#{project_path}/config/redis.yml") + expect(content).to include("REDIS_URL") + 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 From 210c7f9605dc90fb8d6b3fe8512083c60aba2e66 Mon Sep 17 00:00:00 2001 From: Guillermo Moreno Date: Mon, 22 May 2023 16:48:58 -0400 Subject: [PATCH 2/2] feat(redis): add session store config --- .../config/initializers/session_store.rb | 20 +++++++++++++++++++ lib/potassium/recipes/redis.rb | 6 ++++++ spec/features/redis_spec.rb | 5 +++++ 3 files changed, 31 insertions(+) create mode 100644 lib/potassium/assets/config/initializers/session_store.rb diff --git a/lib/potassium/assets/config/initializers/session_store.rb b/lib/potassium/assets/config/initializers/session_store.rb new file mode 100644 index 00000000..ae27d1f0 --- /dev/null +++ b/lib/potassium/assets/config/initializers/session_store.rb @@ -0,0 +1,20 @@ +# https://github.com/redis-store/redis-store/issues/358#issuecomment-1537920008 +class RedisClient::Config + alias original_initialize initialize + + def initialize(**kwargs) + # remove not supported kwargs + original_initialize( + **kwargs.except(:raw, :serializer, :marshalling, :namespace, :scheme) + ) + end +end + +Rails.application.config.session_store :redis_store, { + servers: [{ + url: ENV['REDIS_URL'] + }], + expire_after: 30.days, + key: '_app_session', + secure: Rails.env.production? +} diff --git a/lib/potassium/recipes/redis.rb b/lib/potassium/recipes/redis.rb index 67289e14..6d8919ca 100644 --- a/lib/potassium/recipes/redis.rb +++ b/lib/potassium/recipes/redis.rb @@ -3,6 +3,7 @@ def create add_redis add_docker_compose_redis_config set_redis_dot_env + add_session_store_config end def install @@ -48,4 +49,9 @@ def set_redis_dot_env TEXT ) end + + def add_session_store_config + copy_file("../assets/config/initializers/session_store.rb", + "config/initializers/session_store.rb", force: true) + end end diff --git a/spec/features/redis_spec.rb b/spec/features/redis_spec.rb index b35e9a9d..3ad4b96b 100644 --- a/spec/features/redis_spec.rb +++ b/spec/features/redis_spec.rb @@ -34,5 +34,10 @@ expect(compose_content[:services]).to include(:redis) end + + it 'copies session store config' do + content = IO.read("#{project_path}/config/initializers/session_store.rb") + expect(content).to include("RedisClient::Config") + end end end