sunspot_offline 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e59f5f34c118a37d3f16bda51c7cc1cc855e517498e57f343223a242fefc2d56
4
+ data.tar.gz: bfe500c3473cf63d82ae692a2a33ac6cbcb66f3f24dc33ef49a0d5e99df28b92
5
+ SHA512:
6
+ metadata.gz: 5c94a3922fa1e5e143d1c93cd2df9f73d3918c43f3883caa9ae02d276a141408ed84758024c4313c2d36cf478ac77f5b7d0fe676a7d7858b9142088f03d439d1
7
+ data.tar.gz: 99a6f082841d494989de73c77ed47c21fe2bb1220d659eaff924e933698d3b73935535bbe6e7b481c7f162b6465e2854f2fd0c1b484d9591a7af3d0c2236581e
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Sergey Glukhov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # Sunspot Offline
2
+ Simple fail over method into your [sunspot_rails](https://github.com/sunspot/sunspot) + [sidekiq](https://github.com/mperham/sidekiq) setup
3
+
4
+ [![Build Status](https://travis-ci.org/anjlab/sunspot_offline.svg?branch=master)](https://travis-ci.org/anjlab/sunspot_offline)
5
+ [![Gem Version](https://badge.fury.io/rb/sunspot_offline.svg)](https://badge.fury.io/rb/sunspot_offline)
6
+
7
+ ## Why?
8
+
9
+ Because Solr sometimes fails, it happens. It might be a maintenance work you have to do or just Out-Of-Memory problems.
10
+ If you are running search-sensitive Rails app, you have to deal with it.
11
+ This gem was developed to postpone your index tasks automatically into a sidekiq queue if Solr engine becomes unavailable
12
+
13
+ ## Rails Support
14
+
15
+ Rails 5.0 and higher
16
+
17
+ ## Installation
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'sunspot_offline'
22
+ ```
23
+
24
+ And then execute:
25
+ ```bash
26
+ $ bundle
27
+ ```
28
+
29
+ Or install it yourself as:
30
+ ```bash
31
+ $ gem install sunspot_offline
32
+ ```
33
+
34
+ ## Configuration
35
+
36
+ By default gem is configured to use built-in workers which will auto-retry indexing in 1 hour is Solr goes offline.
37
+ This all is configurable, preferable via initializer:
38
+
39
+ ```ruby
40
+ # config/initializers/sunspot_offline.rb
41
+
42
+ SunspotOffline.configure do |config|
43
+ config.retry_delay = 1.hour
44
+ end
45
+ ```
46
+
47
+ ### Valid configuration options
48
+
49
+ |Option | Type| Default value| |
50
+ |---------------|-----|--------------|----------------------|
51
+ | index_job | Class| [bundled worker class](lib/sunspot_offline/sidekiq/index_worker.rb) | Sidekiq worker which will retry **saving new documents** to Solr. Accepts 2 arguments: ActiveRecord class and id (numbers, arrays, and hashes are all valid for this argument) |
52
+ | removal_job | Class| [bundled worker class](lib/sunspot_offline/sidekiq/removal_worker.rb) | Sidekiq worker which will retry **removing existing documents** from Solr. Accepts same set of arguments as `index_job`.
53
+ | retry_delay | Duration | 1 hour | Delay in which sidekiq will attempt to run `index_job` or `removal_job` |
54
+ | default_queue | String | 'default' | Sidekiq's named queue to use |
55
+ | solr_error_callback | Proc(exception) | `nil` | A proc which will be executed if Solr is detected to be offline |
56
+ | filter_sidekiq_job_callback | Proc(job_class) | `nil` | Since some Solr indexing might be happening inside yours sidekiq jobs they dont need to have a custom fail over, sidekiq is able to retry failures by itself. Using this proc you can configure which jobs could be filtered out:<br>`->(job_name) { job_name == 'MySolrIndexJob' }`. In this sample `MySolrIndexJob` wont be applied for a fail over |
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Run tests (`bundle exec appraisal install && bundle exec appraisal rake`)
64
+ 5. Push to the branch (`git push origin my-new-feature`)
65
+ 6. Create new Pull Request
66
+
67
+ ## License
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: %i[rubocop spec]
@@ -0,0 +1,22 @@
1
+ require 'sunspot'
2
+ require 'sunspot_offline/sunspot/abstract_search'
3
+ require 'sunspot_offline/rsolr/client'
4
+ require 'sunspot_offline/sidekiq/current_job_middleware'
5
+
6
+ module SunspotOffline
7
+ module Rails
8
+ class Railtie < ::Rails::Railtie
9
+ initializer 'sunspot_offline.init', before: 'sunspot_rails.init' do
10
+ ::Sunspot::Search::AbstractSearch.prepend SunspotOffline::Sunspot::AbstractSearch
11
+
12
+ ::RSolr::Client.prepend SunspotOffline::RSolr::Client
13
+
14
+ ::Sidekiq.configure_server do |config|
15
+ config.server_middleware do |chain|
16
+ chain.add SunspotOffline::Sidekiq::CurrentJobMiddleware
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,53 @@
1
+ module SunspotOffline
2
+ module RSolr
3
+ module Client
4
+ def add(documents, opts = {})
5
+ wrap_request(
6
+ deletion: false,
7
+ documents: -> { group_documents(documents.map { |doc| doc.field_by_name(:id).value }) }
8
+ ) { super }
9
+ end
10
+
11
+ def delete_by_id(id, opts = {})
12
+ wrap_request(deletion: true, documents: -> { group_documents(Array(id)) }) { super }
13
+ end
14
+
15
+ def delete_by_query(query, opts = {})
16
+ wrap_request(deletion: true, documents: -> { removal_documents(query) }) { super }
17
+ end
18
+
19
+ private
20
+
21
+ def wrap_request(deletion:, documents:)
22
+ yield
23
+ rescue StandardError => ex
24
+ raise ex if raise_exception?
25
+
26
+ job = deletion ? SunspotOffline.configuration.removal_job : SunspotOffline.configuration.index_job
27
+ perform_at = Time.zone.now + SunspotOffline.configuration.retry_delay
28
+ documents.call.each { |klass, list| job.perform_at(perform_at, klass, list) }
29
+ SunspotOffline.on_solr_error(ex)
30
+ end
31
+
32
+ def raise_exception?
33
+ sidekiq_job = SunspotOffline::Sidekiq::CurrentJobMiddleware.get
34
+ !sidekiq_job.nil? && (fail_over_job?(sidekiq_job) || SunspotOffline.filter_sidekiq_job?(sidekiq_job))
35
+ end
36
+
37
+ def fail_over_job?(job_name)
38
+ [SunspotOffline.configuration.index_job.name, SunspotOffline.configuration.removal_job.name].include?(job_name)
39
+ end
40
+
41
+ def group_documents(documents)
42
+ documents
43
+ .map { |id_text| id_text.split(' ') }
44
+ .group_by(&:first)
45
+ .map { |klass, list| [klass, list.map(&:last)] }
46
+ end
47
+
48
+ def removal_documents(query)
49
+ [query == '*:*' ? [nil, nil] : [query.split(':').last, nil]]
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,22 @@
1
+ require 'request_store'
2
+
3
+ module SunspotOffline
4
+ module Sidekiq
5
+ class CurrentJobMiddleware
6
+ STORE_KEY = 'Sidekiq.CurrentJob'
7
+
8
+ def initialize(*); end
9
+
10
+ def call(worker, _job, _queue)
11
+ RequestStore[STORE_KEY] = worker.class.name
12
+ yield
13
+ ensure
14
+ RequestStore[STORE_KEY] = nil
15
+ end
16
+
17
+ def self.get
18
+ RequestStore[STORE_KEY]
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'sidekiq'
2
+
3
+ module SunspotOffline
4
+ module Sidekiq
5
+ class IndexWorker
6
+ include ::Sidekiq::Worker
7
+
8
+ def perform(model_class, scope)
9
+ klass = model_class.classify.constantize
10
+ if klass.searchable?
11
+ if scope.is_a?(Array)
12
+ scope.each_slice(1000) { |slice| Sunspot.index(klass.where(id: slice)) }
13
+ elsif scope.is_a?(Hash)
14
+ klass.atomic_update(scope.with_indifferent_access)
15
+ else
16
+ klass.where(id: scope).take&.index
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ require 'sidekiq'
2
+
3
+ module SunspotOffline
4
+ module Sidekiq
5
+ class RemovalWorker
6
+ include ::Sidekiq::Worker
7
+
8
+ def perform(model_class, ids)
9
+ if ids.present?
10
+ Sunspot.remove_by_id(model_class, Array(ids))
11
+ else
12
+ Sunspot.remove_all(model_class)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,24 @@
1
+ module SunspotOffline
2
+ module Sunspot
3
+ module AbstractSearch
4
+ def execute
5
+ super
6
+ rescue RSolr::Error::ConnectionRefused, RSolr::Error::Http => ex
7
+ @solr_result = {
8
+ 'response' => {
9
+ 'numFound' => -1,
10
+ 'start' => 0,
11
+ 'docs' => []
12
+ },
13
+ 'facet_counts' => {
14
+ 'facet_queries' => {},
15
+ 'facet_fields' => {}
16
+ },
17
+ 'grouped' => {}
18
+ }
19
+ SunspotOffline.on_solr_error(ex)
20
+ self
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module SunspotOffline
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,37 @@
1
+ require 'ostruct'
2
+ require 'sunspot_offline/version'
3
+ require 'sunspot_offline/rails/railtie'
4
+ require 'sunspot_offline/sidekiq/index_worker'
5
+ require 'sunspot_offline/sidekiq/removal_worker'
6
+
7
+ module SunspotOffline
8
+ class << self
9
+ def configuration
10
+ @configuration ||= OpenStruct.new(
11
+ retry_delay: 1.hour,
12
+ solr_error_callback: ->(_exception) {},
13
+ filter_sidekiq_job_callback: ->(_job) { false }, # some Sidekiq jobs are allowed to fail and retry on their own
14
+ index_job: Sidekiq::IndexWorker, # Sidekiq job which will handle index retries
15
+ removal_job: Sidekiq::RemovalWorker, # Sidekiq job which will handle removal retries
16
+ default_queue: 'default'
17
+ )
18
+ end
19
+
20
+ def configure
21
+ if block_given?
22
+ yield(configuration)
23
+ [SunspotOffline::Sidekiq::IndexWorker, SunspotOffline::Sidekiq::RemovalWorker].each do |worker|
24
+ worker.sidekiq_options[:queue] = configuration.default_queue
25
+ end
26
+ end
27
+ end
28
+
29
+ def on_solr_error(exception)
30
+ configuration.solr_error_callback.call(exception) if configuration.solr_error_callback
31
+ end
32
+
33
+ def filter_sidekiq_job?(job_class_name)
34
+ configuration.filter_sidekiq_job_callback.call(job_class_name) if configuration.filter_sidekiq_job_callback
35
+ end
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,212 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sunspot_offline
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Glukhov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: appraisal
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.6'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.58.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.58.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: solr_wrapper
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '='
88
+ - !ruby/object:Gem::Version
89
+ version: '2.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '='
95
+ - !ruby/object:Gem::Version
96
+ version: '2.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.3'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.3'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '5'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '5'
125
+ - !ruby/object:Gem::Dependency
126
+ name: request_store
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.4'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.4'
139
+ - !ruby/object:Gem::Dependency
140
+ name: sidekiq
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '4'
146
+ type: :runtime
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '4'
153
+ - !ruby/object:Gem::Dependency
154
+ name: sunspot_rails
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '2.3'
160
+ type: :runtime
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '2.3'
167
+ description: Because Solr sometimes fails, it happens. It might be a maintenance work
168
+ you have to do or just Out-Of-Memory problems. If you are running search-sensitive
169
+ Rails app, you have to deal with it.This gem was developed to postpone your index
170
+ tasks automatically into a sidekiq queue if Solr engine becomes unavailable
171
+ email:
172
+ - sergey.glukhov@gmail.com
173
+ executables: []
174
+ extensions: []
175
+ extra_rdoc_files: []
176
+ files:
177
+ - MIT-LICENSE
178
+ - README.md
179
+ - Rakefile
180
+ - lib/sunspot_offline.rb
181
+ - lib/sunspot_offline/rails/railtie.rb
182
+ - lib/sunspot_offline/rsolr/client.rb
183
+ - lib/sunspot_offline/sidekiq/current_job_middleware.rb
184
+ - lib/sunspot_offline/sidekiq/index_worker.rb
185
+ - lib/sunspot_offline/sidekiq/removal_worker.rb
186
+ - lib/sunspot_offline/sunspot/abstract_search.rb
187
+ - lib/sunspot_offline/version.rb
188
+ homepage: https://github.com/anjlab/sunspot_offline
189
+ licenses:
190
+ - MIT
191
+ metadata: {}
192
+ post_install_message:
193
+ rdoc_options: []
194
+ require_paths:
195
+ - lib
196
+ required_ruby_version: !ruby/object:Gem::Requirement
197
+ requirements:
198
+ - - ">="
199
+ - !ruby/object:Gem::Version
200
+ version: '0'
201
+ required_rubygems_version: !ruby/object:Gem::Requirement
202
+ requirements:
203
+ - - ">="
204
+ - !ruby/object:Gem::Version
205
+ version: '0'
206
+ requirements: []
207
+ rubyforge_project:
208
+ rubygems_version: 2.7.6
209
+ signing_key:
210
+ specification_version: 4
211
+ summary: Simple failover method into your sunspot_rails + sidekiq setup
212
+ test_files: []