Browse Source

Feature: Mobile - Make Redis a dependency for the new tech stack, also on PostgreSQL.

Martin Gruner 2 years ago
parent
commit
038e7c13b2

+ 1 - 0
.gitlab/ci/browser/__templates__.yml

@@ -93,6 +93,7 @@
     - .variables_selenium_chrome
   services:
     - !reference [.services, postgresql]
+    - !reference [.services, redis]
     - !reference [.services, selenium-chrome]
   variables:
     RAILS_ENV: "production"

+ 1 - 0
.gitlab/ci/browser/capybara_integration.yml

@@ -13,6 +13,7 @@ capybara:integration:chrome:
   services:
     - !reference [.services, postgresql]
     - !reference [.services, selenium-chrome]
+    - !reference [.services, redis]
     - !reference [.services, auth]
 
 capybara:integration:firefox:

+ 1 - 0
.gitlab/ci/browser/integration_minitest_otrs.yml

@@ -8,6 +8,7 @@
     TZ: "Europe/Berlin"       # Required for the zammad-ci-otrsimport-app containers
   services:
     - !reference [.services, postgresql]
+    - !reference [.services, redis]
     - !reference [.services, selenium-chrome]
     - name: $CI_REGISTRY/docker/zammad-ci-otrsimport-db:otrs6
       alias: zammad-ci-otrsimport-db

+ 1 - 0
.gitlab/ci/lint.yml

@@ -9,6 +9,7 @@
     - .template_lint
   services:
     - !reference [.services, postgresql]
+    - !reference [.services, redis]
   before_script:
     - !reference [.scripts, source_rvm]
     - !reference [.scripts, bundle_install]

+ 1 - 0
.gitlab/ci/test/browser_build.yml

@@ -2,6 +2,7 @@ browser:build:
   stage: test
   services:
     - !reference [.services, postgresql]
+    - !reference [.services, redis]
   artifacts:
     expire_in: 1 week
     paths:

+ 3 - 3
.gitlab/configure_environment.rb

@@ -81,10 +81,10 @@ class ConfigureEnvironment
 
   def self.configure_redis
     has_redis = network_host_exists?('redis')
-    needs_redis = database_type == 'mysql' && ENV['ENABLE_EXPERIMENTAL_MOBILE_FRONTEND'] == 'true'
+    needs_redis = ENV['ENABLE_EXPERIMENTAL_MOBILE_FRONTEND'] == 'true'
 
     if needs_redis && !has_redis
-      raise 'Redis was not found, but is required for ActionCable on MySQL based systems.'
+      raise 'Redis was not found, but is required for ActionCable.'
     end
 
     if has_redis && [true, needs_redis].sample
@@ -93,7 +93,7 @@ class ConfigureEnvironment
       return
     end
 
-    puts 'Using File as web socket session store and the PostgreSQL adapter for ActionCable.'
+    puts 'Not using Redis.'
     @env_file_content += "unset REDIS_URL\n"
   end
 

+ 3 - 5
config/application.rb

@@ -82,11 +82,9 @@ module Zammad
     end
 
     # define websocket session store
-    config.websocket_session_store = if ENV['REDIS_URL'].present?
-                                       :redis
-                                     else
-                                       :file
-                                     end
+    # The web socket session store will fall back to localhost Redis usage if REDIS_URL is not set.
+    # In this case, or if forced via ZAMMAD_WEBSOCKET_SESSION_STORE_FORCE_FS_BACKEND, the FS back end will be used.
+    config.websocket_session_store = ENV['REDIS_URL'].present? && ENV['ZAMMAD_WEBSOCKET_SESSION_STORE_FORCE_FS_BACKEND'].blank? ? :redis : :file
 
     # default preferences by permission
     config.preferences_default_by_permission = {

+ 18 - 24
config/initializers/action_cable_preferences.rb

@@ -1,30 +1,24 @@
 # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
 
 if ENV['ENABLE_EXPERIMENTAL_MOBILE_FRONTEND'] == 'true'
-  if ENV['REDIS_URL'].present?
-    Rails.application.config.action_cable.cable = {
-      'adapter'        => 'redis',
-      'url'            => ENV['REDIS_URL'],
-      'channel_prefix' => "zammad_#{Rails.env}"
-    }
-    Rails.logger.info 'Using the "Redis" adapter for ActionCable.'
-  else
-    if ActiveRecord::Base.connection_db_config.configuration_hash[:adapter] == 'mysql'
-      raise 'Please provide a working redis instance via REDIS_URL - this is required on MySQL databases.'
-    end
+  require 'redis'
+  require 'hiredis'
 
-    # The 'postgresql' adapter does not work correctly in Capybara currently, so use
-    #   'test' instead.
-    if Rails.env.test?
-      Rails.application.config.action_cable.cable = {
-        'adapter' => 'test',
-      }
-      Rails.logger.info 'Using the "test" adapter for ActionCable.'
-    else
-      Rails.application.config.action_cable.cable = {
-        'adapter' => 'postgresql',
-      }
-      Rails.logger.info 'Using the "PostgreSQL" adapter for ActionCable.'
-    end
+  # If REDIS_URL is not set, fall back to default port / localhost, to ease configuration
+  #   for simple installations.
+  redis_url = ENV['REDIS_URL'].presence || 'redis://localhost:6379'
+  Rails.application.config.action_cable.cable = {
+    adapter:        :redis,
+    driver:         :hiredis,
+    url:            redis_url,
+    channel_prefix: "zammad_#{Rails.env}",
+  }
+  begin
+    Redis.new(driver: :hiredis, url: redis_url).ping
+    Rails.logger.info { "ActionCable is using the redis instance at #{redis_url}." }
+  rescue Redis::CannotConnectError => e
+    warn "There was an error trying to connect to Redis via #{redis_url}. Please make sure Redis is available."
+    warn e.inspect
+    exit! # rubocop:disable Rails/Exit
   end
 end

+ 1 - 5
config/initializers/log_websocket_session_store.rb

@@ -1,7 +1,3 @@
 # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
 
-if Rails.application.config.websocket_session_store.eql? :redis
-  Rails.logger.info 'Using Redis as web socket session store.'
-else
-  Rails.logger.info 'Using File as web socket session store.'
-end
+Rails.logger.info { "Using the #{Rails.application.config.websocket_session_store.capitalize} back end for Zammad's web socket session store." }