Skip to content

Commit

Permalink
feature: remove dry-* trash
Browse files Browse the repository at this point in the history
  • Loading branch information
isqad committed Apr 9, 2019
1 parent 2981055 commit 89bec6b
Show file tree
Hide file tree
Showing 12 changed files with 75 additions and 74 deletions.
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
source 'https://gems.railsc.ru'
source 'https://rubygems.org'

gem "riddle", github: "pat/riddle", branch: "develop", require: false
gem "riddle", git: "https://github.com/pat/riddle", branch: "develop", require: false
gem 'pg', '< 1'

# Specify your gem's dependencies in ts_customizer.gemspec
gemspec
gem 'dry-auto_inject', '< 0.6.0'
5 changes: 0 additions & 5 deletions lib/sphinx/integration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
require 'active_support/all'
require "dry/container"
require "dry/auto_inject"
require 'redis'
require 'redis-mutex'
require 'resque-integration'
Expand All @@ -26,9 +24,6 @@ module Integration
autoload :ServerStatus, 'sphinx/integration/server_status'
autoload :TransmitterJob, 'sphinx/integration/transmitter_job'
autoload :ReplayerJob, 'sphinx/integration/replayer_job'

Container = ::Dry::Container.new
AutoInject = ::Dry::AutoInject(Container)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/sphinx/integration/extensions/thinking_sphinx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def log(message, severity = ::Logger::INFO)
alias_method :info, :log

def logger
@logger ||= ::Sphinx::Integration::Container["logger.sphinx_log"]
@logger ||= ::Sphinx::Integration.fetch(:di)[:loggers][:sphinx_file].call
end
end
end
15 changes: 9 additions & 6 deletions lib/sphinx/integration/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ module HelperAdapters
end

class Helper
include ::Sphinx::Integration::AutoInject.hash["logger.notificator", logger: "logger.stdout"]

%i(running? stop start suspend resume restart clean copy_config reload).each do |method_name|
define_method(method_name) do
begin
Expand All @@ -23,8 +21,6 @@ class Helper
end

def initialize(options = {})
super

::ThinkingSphinx.context.define_indexes

@options = options
Expand Down Expand Up @@ -57,7 +53,7 @@ def configure
def index
log "Index sphinx"

::Sphinx::Integration::Mysql::Replayer.new(logger: logger).reset
::Sphinx::Integration::Mysql::Replayer.new.reset

@indexes.each do |index|
index.indexing do
Expand All @@ -72,7 +68,6 @@ def index
end
rescue StandardError => error
log_error(error)
raise
end

alias_method :reindex, :index
Expand Down Expand Up @@ -110,5 +105,13 @@ def log_error(exception)
logger.debug(exception.backtrace.join("\n")) if exception.backtrace
notificator.call(exception.message)
end

def logger
@logger ||= ::Sphinx::Integration.fetch(:di)[:loggers][:stdout].call
end

def notificator
@notificator ||= ::Sphinx::Integration.fetch(:di)[:error_notificator]
end
end
end
8 changes: 4 additions & 4 deletions lib/sphinx/integration/helper_adapters/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ module HelperAdapters
class Base
CORE_POSTFIX = 'core'.freeze

include ::Sphinx::Integration::AutoInject.hash[logger: "logger.stdout"]

def initialize(options = {})
super

@options = options
end

private

def logger
@logger ||= ::Sphinx::Integration.fetch(:di)[:loggers][:stdout].call
end

def config
ThinkingSphinx::Configuration.instance
end
Expand Down
6 changes: 4 additions & 2 deletions lib/sphinx/integration/helper_adapters/remote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module Sphinx
module Integration
module HelperAdapters
class SshProxy
include ::Sphinx::Integration::AutoInject.hash[logger: "logger.stdout"]

DEFAULT_SSH_OPTIONS = {
user: "sphinx",
port: 22,
Expand Down Expand Up @@ -70,6 +68,10 @@ def execute(*args)
end
end

def logger
@logger ||= ::Sphinx::Integration.fetch(:di)[:loggers][:stdout].call
end

class Remote < Base
def initialize(*)
super
Expand Down
6 changes: 4 additions & 2 deletions lib/sphinx/integration/mysql/replayer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ module Sphinx
module Integration
module Mysql
class Replayer
include ::Sphinx::Integration::AutoInject.hash[logger: "logger.stdout"]

TIMEOUT = 60.seconds

delegate :mysql_client, :update_log, :soft_delete_log, to: :sphinx_config
Expand Down Expand Up @@ -92,6 +90,10 @@ def soft_delete(ids_by_indexes)
def sphinx_config
@sphinx_config ||= ThinkingSphinx::Configuration.instance.tap { |config| config.mysql_read_timeout = TIMEOUT }
end

def logger
@logger ||= ::Sphinx::Integration.fetch(:di)[:loggers][:stdout].call
end
end
end
end
Expand Down
60 changes: 31 additions & 29 deletions lib/sphinx/integration/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,37 @@ class Railtie < Rails::Railtie
end

initializer "sphinx-integration.common", before: :load_config_initializers do |app|
::Sphinx::Integration::Container.namespace("logger") do
register "stdout", -> do
logger = ::Logger.new(STDOUT)
logger.formatter = ::Logger::Formatter.new
logger.level = ::Logger.const_get(::ThinkingSphinx::Configuration.instance.log_level.upcase)
logger
end

register "sphinx_log", -> do
logger = ::Logger.new(::Rails.root.join("log", "sphinx.log"))
logger.formatter = ::Logger::Formatter.new
logger.level = ::Logger.const_get(::ThinkingSphinx::Configuration.instance.log_level.upcase)
logger
end

register "index_log", -> do
logger = ::Logger.new(::Rails.root.join("log", "index.log"))
logger.formatter = ::Logger::Formatter.new
logger.level = ::Logger::INFO
logger
end

register "notificator", ->(message) do
client = ::Twinkle::Client
client.create_message("sadness", "#{message} on #{`hostname`}", hashtags: ["#sphinx"]) if client.config.token
end
end

app.config.sphinx_integration = {rebuild: {pass_sphinx_stop: false}}
app.config.sphinx_integration = {
rebuild: {pass_sphinx_stop: false},
# Custom DI container
di: {
loggers: {
stdout: -> {
logger = ::Logger.new(STDOUT)
logger.formatter = ::Logger::Formatter.new
logger.level = ::Logger.const_get(::ThinkingSphinx::Configuration.instance.log_level.upcase)
logger
},
sphinx_file: -> {
logger = ::Logger.new(::Rails.root.join("log", "sphinx.log"))
logger.formatter = ::Logger::Formatter.new
logger.level = ::Logger.const_get(::ThinkingSphinx::Configuration.instance.log_level.upcase)
logger
},
indexer_file: -> {
logger = ::Logger.new(::Rails.root.join("log", "index.log"))
logger.formatter = ::Logger::Formatter.new
logger.level = ::Logger::INFO
logger
}
},
error_notificator: ->(message) {
client = ::Twinkle::Client
client.create_message("sadness", "#{message} on #{`hostname`}", hashtags: ["#sphinx"]) if client.config.token
client
}
}
}
end

initializer 'sphinx_integration.rspec' do
Expand Down
2 changes: 1 addition & 1 deletion lib/sphinx/integration/replayer_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ReplayerJob

def self.execute(index_name)
replayer = ::Sphinx::Integration::Mysql::Replayer.new(
logger: ::Sphinx::Integration::Container['logger.index_log']
logger: ::Sphinx::Integration.fetch(:di)[:loggers][:indexer_file].call
)

replayer.replay
Expand Down
26 changes: 13 additions & 13 deletions lib/sphinx/integration/tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,65 +7,65 @@ namespace :sphinx do
task :start, [:host] => :environment do |_, args|
Sphinx::Integration::Helper.new(
host: args[:host],
logger: Sphinx::Integration::Container["logger.index_log"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][:indexer_file].call
).start
end

desc 'Stop Sphinx'
task :stop, [:host] => :environment do |_, args|
Sphinx::Integration::Helper.new(
host: args[:host],
logger: Sphinx::Integration::Container["logger.index_log"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][:indexer_file].call
).stop
end

desc 'Suspend Sphinx'
task :suspend, [:host] => :environment do |_, args|
Sphinx::Integration::Helper.new(
host: args[:host],
logger: Sphinx::Integration::Container["logger.index_log"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][:indexer_file].call
).suspend
end

desc 'Resume Sphinx'
task :resume, [:host] => :environment do |_, args|
Sphinx::Integration::Helper.new(
host: args[:host],
logger: Sphinx::Integration::Container["logger.index_log"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][:indexer_file].call
).resume
end

desc 'Restart Sphinx'
task :restart, [:host] => :environment do |_, args|
Sphinx::Integration::Helper.new(
host: args[:host],
logger: Sphinx::Integration::Container["logger.index_log"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][:indexer_file].call
).restart
end

desc "Index Sphinx. Task args: host (default: ''), rotate (default: true), log_device (default: index_log), *indexes (optional)"
desc "Index Sphinx. Task args: host (default: ''), rotate (default: true), log_device (default: indexer_file), *indexes (optional)"
task :index, %i(host rotate log_device) => :environment do |_, args|
Rails.application.eager_load!

rotate = %w(true yes y 1).include?(args[:rotate].presence || 'true')
log_device = args[:log_device].presence || 'index_log'
log_device = args[:log_device].presence.to_sym || :indexer_file

Sphinx::Integration::Helper.new(
host: args[:host],
rotate: rotate,
indexes: args.extras,
logger: ::Sphinx::Integration::Container["logger.#{log_device}"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][log_device].call
).index
end

desc 'Rebuild Sphinx'
task :rebuild, %i[log_device] => ['sphinx:set_indexing_mode', :environment] do |_, args|
Rails.application.eager_load!

log_device = args[:log_device].presence || 'index_log'
log_device = args[:log_device].presence.to_sym || :indexer_file

Sphinx::Integration::Helper.new(
logger: Sphinx::Integration::Container["logger.#{log_device}"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][log_device].call
).rebuild
end

Expand All @@ -87,23 +87,23 @@ namespace :sphinx do
task :copy_conf, [:host] => :environment do |_, args|
Sphinx::Integration::Helper.new(
host: args[:host],
logger: ::Sphinx::Integration::Container["logger.index_log"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][:indexer_file].call
).copy_config
end

desc 'Clean Sphinx files (indexes and binlogs)'
task :clean, [:host] => :environment do |_, args|
Sphinx::Integration::Helper.new(
host: args[:host],
logger: ::Sphinx::Integration::Container["logger.index_log"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][:indexer_file].call
).clean
end

desc 'Reload config or rotate indexes'
task :reload, [:host] => :environment do |_, args|
Sphinx::Integration::Helper.new(
host: args[:host],
logger: ::Sphinx::Integration::Container["logger.index_log"]
logger: ::Sphinx::Integration.fetch(:di)[:loggers][:indexer_file].call
).reload
end
end
14 changes: 7 additions & 7 deletions spec/sphinx/integration/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@

context "when raised exception" do
it "logs a error" do
logger = spy(:logger)
notificator = spy(:notificator)
helper = described_class.new(default_options.merge(logger: logger, notificator: notificator))
helper = described_class.new(default_options)

expect(adapter).to receive(:index).and_raise(StandardError.new("error message"))
expect { helper.index }.to raise_error(StandardError)
expect(logger).to have_received(:error).with("error message")
expect(notificator).to have_received(:call).with("error message")
allow(adapter).to receive(:index).and_raise(StandardError.new("error message"))

expect_any_instance_of(::Logger).to receive(:error).with("error message")
expect(Sphinx::Integration[:di][:error_notificator]).to receive(:call).with("error message")

helper.index
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions sphinx-integration.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'thinking-sphinx', '= 2.0.14'
gem.add_runtime_dependency 'net-ssh'
gem.add_runtime_dependency 'request_store', '>= 1.2.1'
gem.add_runtime_dependency 'dry-container', '>= 0.3.4'
gem.add_runtime_dependency 'dry-auto_inject', '>= 0.3.0'
gem.add_runtime_dependency 'twinkle-client', '>= 0.2.0'
gem.add_runtime_dependency 'resque-integration', '>= 3.5.0'

Expand Down

0 comments on commit 89bec6b

Please sign in to comment.