From 697661cbf2483a99c9d3e7e507586f2bb1bb8a56 Mon Sep 17 00:00:00 2001 From: Martin Gruner Date: Mon, 5 Jul 2021 13:38:40 +0000 Subject: [PATCH] Maintenance: Only load gems when they are needed to save memory otherwise. --- .gitlab/ci/integration/slack.yml | 2 -- Gemfile | 21 ++++++++++--------- Gemfile.lock | 18 +++++++++------- .../integration/exchange_controller.rb | 1 + app/models/channel/driver/sms/twilio.rb | 1 + app/models/transaction/slack.rb | 1 + config/application.rb | 11 +++++----- lib/enrichment/clearbit/user.rb | 1 + lib/excel_sheet.rb | 3 +++ lib/sequencer/unit/exchange/connection.rb | 1 + .../zendesk/mixin/incremental_export.rb | 1 + .../import/zendesk/objects_total_count.rb | 1 + lib/sequencer/unit/zendesk/client.rb | 1 + spec/controllers/integration/exchange_spec.rb | 1 + spec/lib/import/exchange/folder_spec.rb | 1 + .../zendesk/sub_sequence/base_examples.rb | 2 ++ spec/requests/integration/exchange_spec.rb | 1 + test/integration/slack_test.rb | 2 +- 18 files changed, 45 insertions(+), 25 deletions(-) diff --git a/.gitlab/ci/integration/slack.yml b/.gitlab/ci/integration/slack.yml index 893012bd7..e0b9fcc64 100644 --- a/.gitlab/ci/integration/slack.yml +++ b/.gitlab/ci/integration/slack.yml @@ -10,6 +10,4 @@ slack: - when: on_success script: - bundle exec rake zammad:db:unseeded - - echo "gem 'slack-ruby-client'" >> Gemfile.local - - bundle install -j $(nproc) - bundle exec rails test test/integration/slack_test.rb diff --git a/Gemfile b/Gemfile index 2e207bbb3..1afdb5319 100644 --- a/Gemfile +++ b/Gemfile @@ -45,7 +45,7 @@ gem 'pundit' gem 'rszr', '0.5.2' # performance - Memcached -gem 'dalli' +gem 'dalli', require: false # asset handling - coffee-script gem 'coffee-rails' @@ -95,7 +95,6 @@ gem 'omniauth-weibo-oauth2' gem 'gmail_xoauth' gem 'koala' gem 'telegramAPI' -# Updating twitter to 'master' seems to break things, so freeze the revision for now. gem 'twitter' # channels - email additions @@ -112,7 +111,7 @@ gem 'biz' gem 'diffy' # feature - excel output -gem 'writeexcel' +gem 'writeexcel', require: false # feature - device logging gem 'browser' @@ -125,21 +124,20 @@ gem 'icalendar-recurrence' gem 'telephone_number' # feature - SMS -gem 'twilio-ruby' +gem 'twilio-ruby', require: false # feature - ordering gem 'acts_as_list' # integrations -gem 'clearbit' +gem 'clearbit', require: false gem 'net-ldap' -gem 'slack-notifier' -gem 'zendesk_api' +gem 'slack-notifier', require: false +gem 'zendesk_api', require: false # integrations - exchange -gem 'autodiscover', git: 'https://github.com/zammad-deps/autodiscover' -gem 'rubyntlm', git: 'https://github.com/wimm/rubyntlm' -gem 'viewpoint' +gem 'autodiscover', git: 'https://github.com/zammad-deps/autodiscover', require: false +gem 'viewpoint', require: false # integrations - S/MIME gem 'openssl' @@ -218,6 +216,9 @@ group :development, :test do # refresh ENVs in CI environment gem 'dotenv', require: false + + # Slack helper for testing + gem 'slack-ruby-client', require: false end # Want to extend Zammad with additional gems? diff --git a/Gemfile.lock b/Gemfile.lock index 675b4080e..95b3c372a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,9 +1,3 @@ -GIT - remote: https://github.com/wimm/rubyntlm - revision: 53969639b87b9e5d5fef560f19cf0d977259591c - specs: - rubyntlm (0.1.2) - GIT remote: https://github.com/zammad-deps/autodiscover revision: ee9b53dfa797ce6d4f970b82beea7fbdd2df56bb @@ -228,6 +222,8 @@ GEM faraday (>= 0.8) faraday-net_http (1.0.1) faraday-net_http_persistent (1.1.0) + faraday_middleware (1.0.0) + faraday (~> 1.0) ffi (1.15.3) ffi-compiler (1.0.1) ffi (>= 1.0.0) @@ -243,6 +239,7 @@ GEM octokit (~> 4.6) rainbow (>= 2.2.1) rake (>= 10.0) + gli (2.20.1) globalid (0.4.2) activesupport (>= 4.2.0) gmail_xoauth (0.4.2) @@ -525,6 +522,7 @@ GEM nokogiri (>= 1.10.5) rexml ruby2_keywords (0.0.4) + rubyntlm (0.6.3) rubyzip (2.3.0) sassc (2.4.0) ffi (~> 1.9) @@ -553,6 +551,12 @@ GEM simplecov (>= 0.4.1) simplecov_json_formatter (0.1.3) slack-notifier (2.4.0) + slack-ruby-client (0.17.0) + faraday (>= 1.0) + faraday_middleware + gli + hashie + websocket-driver slop (3.6.0) spring (2.1.1) spring-commands-rspec (1.0.4) @@ -721,13 +725,13 @@ DEPENDENCIES rubocop-performance rubocop-rails rubocop-rspec - rubyntlm! sassc-rails selenium-webdriver shoulda-matchers simplecov simplecov-rcov slack-notifier + slack-ruby-client spring spring-commands-rspec spring-commands-testunit diff --git a/app/controllers/integration/exchange_controller.rb b/app/controllers/integration/exchange_controller.rb index 069d78a47..b4def1687 100644 --- a/app/controllers/integration/exchange_controller.rb +++ b/app/controllers/integration/exchange_controller.rb @@ -7,6 +7,7 @@ class Integration::ExchangeController < ApplicationController def autodiscover answer_with do + require 'autodiscover' # Only load this gem when it is really used. client = Autodiscover::Client.new( email: params[:user], password: params[:password], diff --git a/app/models/channel/driver/sms/twilio.rb b/app/models/channel/driver/sms/twilio.rb index 722e7c4aa..da7c36e2d 100644 --- a/app/models/channel/driver/sms/twilio.rb +++ b/app/models/channel/driver/sms/twilio.rb @@ -142,6 +142,7 @@ class Channel::Driver::Sms::Twilio private def api(options) + require 'twilio-ruby' # Only load this gem when it is really used. @api ||= ::Twilio::REST::Client.new options[:account_id], options[:token] end end diff --git a/app/models/transaction/slack.rb b/app/models/transaction/slack.rb index 161c6b672..1d22a95b3 100644 --- a/app/models/transaction/slack.rb +++ b/app/models/transaction/slack.rb @@ -168,6 +168,7 @@ class Transaction::Slack Rails.logger.debug { "sent webhook (#{@item[:type]}/#{ticket.id}/#{local_config['webhook']})" } + require 'slack-notifier' # Only load this gem when it is really used. notifier = Slack::Notifier.new( local_config['webhook'], channel: local_config['channel'], diff --git a/config/application.rb b/config/application.rb index 1cda3131d..b0f182327 100644 --- a/config/application.rb +++ b/config/application.rb @@ -47,11 +47,12 @@ module Zammad config.api_path = '/api/v1' # define cache store - config.cache_store = if ENV['MEMCACHE_SERVERS'].present? - [:mem_cache_store, ENV['MEMCACHE_SERVERS'], { expires_in: 7.days }] - else - [:zammad_file_store, Rails.root.join('tmp', "cache_file_store_#{Rails.env}"), { expires_in: 7.days }] - end + if ENV['MEMCACHE_SERVERS'].present? + require 'dalli' # Only load this gem when it is really used. + config.cache_store = [:mem_cache_store, ENV['MEMCACHE_SERVERS'], { expires_in: 7.days }] + else + config.cache_store = [:zammad_file_store, Rails.root.join('tmp', "cache_file_store_#{Rails.env}"), { expires_in: 7.days }] + end # define websocket session store config.websocket_session_store = if ENV['REDIS_URL'].present? diff --git a/lib/enrichment/clearbit/user.rb b/lib/enrichment/clearbit/user.rb index f13e0ff00..4ac5aaedd 100644 --- a/lib/enrichment/clearbit/user.rb +++ b/lib/enrichment/clearbit/user.rb @@ -148,6 +148,7 @@ module Enrichment } begin + require 'clearbit' # Only load this gem when it is really used. ::Clearbit.key = @config['api_key'] result = ::Clearbit::Enrichment.find(email: @local_user.email, stream: true) record[:response] = { code: 200, content: result.to_s } diff --git a/lib/excel_sheet.rb b/lib/excel_sheet.rb index d609c4b8d..951445e26 100644 --- a/lib/excel_sheet.rb +++ b/lib/excel_sheet.rb @@ -3,6 +3,9 @@ class ExcelSheet def initialize(title:, header:, records:, locale:, timezone: nil) + + require 'writeexcel' # Only load this gem when it is really used. + @title = title @header = header @records = records diff --git a/lib/sequencer/unit/exchange/connection.rb b/lib/sequencer/unit/exchange/connection.rb index a89c2c0ff..d2d6c4bb1 100644 --- a/lib/sequencer/unit/exchange/connection.rb +++ b/lib/sequencer/unit/exchange/connection.rb @@ -11,6 +11,7 @@ class Sequencer private def ews_connection + require 'viewpoint' # Only load this gem when it is really used. Viewpoint::EWSClient.new( config[:endpoint], config[:user], diff --git a/lib/sequencer/unit/import/zendesk/mixin/incremental_export.rb b/lib/sequencer/unit/import/zendesk/mixin/incremental_export.rb index a8563bfb3..3103da8fb 100644 --- a/lib/sequencer/unit/import/zendesk/mixin/incremental_export.rb +++ b/lib/sequencer/unit/import/zendesk/mixin/incremental_export.rb @@ -15,6 +15,7 @@ class Sequencer end def resource_collection + require 'zendesk_api' # Only load this gem when it is really used. @resource_collection ||= "::ZendeskAPI::#{resource_klass}".constantize.incremental_export(client, 1) end diff --git a/lib/sequencer/unit/import/zendesk/objects_total_count.rb b/lib/sequencer/unit/import/zendesk/objects_total_count.rb index 3451e8af6..708fb5fc2 100644 --- a/lib/sequencer/unit/import/zendesk/objects_total_count.rb +++ b/lib/sequencer/unit/import/zendesk/objects_total_count.rb @@ -27,6 +27,7 @@ class Sequencer # that it returns max. 1000. That's why we need to update the total # number while importing in the resource loop def request(object) + require 'zendesk_api' # Only load this gem when it is really used. resource_class = "::ZendeskAPI::#{object.to_s.singularize}".safe_constantize if resource_class.respond_to?(:incremental_export) # read as: ::ZendeskAPI::Ticket.incremental_export(client, 1) diff --git a/lib/sequencer/unit/zendesk/client.rb b/lib/sequencer/unit/zendesk/client.rb index bd3df924e..b5b60104e 100644 --- a/lib/sequencer/unit/zendesk/client.rb +++ b/lib/sequencer/unit/zendesk/client.rb @@ -10,6 +10,7 @@ class Sequencer private def client + require 'zendesk_api' # Only load this gem when it is really used. ZendeskAPI::Client.new do |config| config.url = Setting.get('import_zendesk_endpoint') diff --git a/spec/controllers/integration/exchange_spec.rb b/spec/controllers/integration/exchange_spec.rb index edbcb41a5..bae16af7a 100644 --- a/spec/controllers/integration/exchange_spec.rb +++ b/spec/controllers/integration/exchange_spec.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ require 'rails_helper' +require 'autodiscover' # Only load this gem when it is really used. RSpec.describe 'Exchange integration endpoint', type: :request do before { authenticated_as(admin_with_admin_user_permissions) } diff --git a/spec/lib/import/exchange/folder_spec.rb b/spec/lib/import/exchange/folder_spec.rb index b1ea5fcb5..baef7b411 100644 --- a/spec/lib/import/exchange/folder_spec.rb +++ b/spec/lib/import/exchange/folder_spec.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ require 'rails_helper' +require 'viewpoint' # Only load this gem when it is really used. RSpec.describe Import::Exchange::Folder do # see https://github.com/zammad/zammad/issues/2152 diff --git a/spec/lib/sequencer/unit/import/zendesk/sub_sequence/base_examples.rb b/spec/lib/sequencer/unit/import/zendesk/sub_sequence/base_examples.rb index 900f9cad1..1e6a2548e 100644 --- a/spec/lib/sequencer/unit/import/zendesk/sub_sequence/base_examples.rb +++ b/spec/lib/sequencer/unit/import/zendesk/sub_sequence/base_examples.rb @@ -1,5 +1,7 @@ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ +require 'zendesk_api' # Only load this gem when it is really used. + RSpec.shared_examples 'Sequencer::Unit::Import::Zendesk::SubSequence::Base' do describe 'error handling' do before do diff --git a/spec/requests/integration/exchange_spec.rb b/spec/requests/integration/exchange_spec.rb index edbcb41a5..bae16af7a 100644 --- a/spec/requests/integration/exchange_spec.rb +++ b/spec/requests/integration/exchange_spec.rb @@ -1,6 +1,7 @@ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ require 'rails_helper' +require 'autodiscover' # Only load this gem when it is really used. RSpec.describe 'Exchange integration endpoint', type: :request do before { authenticated_as(admin_with_admin_user_permissions) } diff --git a/test/integration/slack_test.rb b/test/integration/slack_test.rb index d260c9fa4..2028d5ba9 100644 --- a/test/integration/slack_test.rb +++ b/test/integration/slack_test.rb @@ -1,7 +1,7 @@ # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/ require 'integration_test_helper' -require 'slack' +require 'slack-ruby-client' # Only load this gem when it is really used. class SlackTest < ActiveSupport::TestCase