Commit 9cd93e5398dffea302acfb0e9f74668054bbc1bf

Authored by Andrew Kane
2 parents 0fd225f8 b56c3f97

Merge branch 'activejob'

1 ## 0.8.3 [unreleased] 1 ## 0.8.3 [unreleased]
2 2
  3 +- Added support for ActiveJob
3 - Added `timeout` setting 4 - Added `timeout` setting
4 - Fixed import with no records 5 - Fixed import with no records
5 6
@@ -5,3 +5,4 @@ gemspec @@ -5,3 +5,4 @@ gemspec
5 5
6 gem "sqlite3" 6 gem "sqlite3"
7 gem "activerecord", "~> 4.1.0" 7 gem "activerecord", "~> 4.1.0"
  8 +gem "activejob_backport"
@@ -343,7 +343,7 @@ There are three strategies for keeping the index synced with your database. @@ -343,7 +343,7 @@ There are three strategies for keeping the index synced with your database.
343 end 343 end
344 ``` 344 ```
345 345
346 - Supports [Delayed Job](https://github.com/collectiveidea/delayed_job) only at the moment 346 + And [install Active Job](https://github.com/ankane/activejob_backport) for Rails 4.1 and below
347 347
348 3. Manual 348 3. Manual
349 349
gemfiles/mongoid4.gemfile
@@ -4,3 +4,4 @@ source 'https://rubygems.org' @@ -4,3 +4,4 @@ source 'https://rubygems.org'
4 gemspec path: "../" 4 gemspec path: "../"
5 5
6 gem "mongoid", "~> 4.0.0" 6 gem "mongoid", "~> 4.0.0"
  7 +gem "activejob_backport"
lib/searchkick.rb
@@ -12,6 +12,14 @@ require "searchkick/model" @@ -12,6 +12,14 @@ require "searchkick/model"
12 require "searchkick/tasks" 12 require "searchkick/tasks"
13 require "searchkick/logging" if defined?(Rails) 13 require "searchkick/logging" if defined?(Rails)
14 14
  15 +# background jobs
  16 +begin
  17 + require "active_job"
  18 +rescue LoadError
  19 + # do nothing
  20 +end
  21 +require "searchkick/reindex_v2_job" if defined?(ActiveJob)
  22 +
15 module Searchkick 23 module Searchkick
16 class MissingIndexError < StandardError; end 24 class MissingIndexError < StandardError; end
17 class UnsupportedVersionError < StandardError; end 25 class UnsupportedVersionError < StandardError; end
lib/searchkick/model.rb
@@ -33,7 +33,11 @@ module Searchkick @@ -33,7 +33,11 @@ module Searchkick
33 include Searchkick::Similar 33 include Searchkick::Similar
34 34
35 def reindex_async 35 def reindex_async
36 - Delayed::Job.enqueue Searchkick::ReindexJob.new(self.class.name, id) 36 + if defined?(Searchkick::ReindexV2Job)
  37 + Searchkick::ReindexV2Job.perform_later(self.class.name, id.to_s)
  38 + else
  39 + Delayed::Job.enqueue Searchkick::ReindexJob.new(self.class.name, id.to_s)
  40 + end
37 end 41 end
38 42
39 if callbacks 43 if callbacks
lib/searchkick/reindex_v2_job.rb 0 → 100644
@@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
  1 +module Searchkick
  2 + class ReindexV2Job < ActiveJob::Base
  3 + queue_as :searchkick
  4 +
  5 + def perform(klass, id)
  6 + model = klass.constantize
  7 + record = model.find(id) rescue nil # TODO fix lazy coding
  8 + index = model.searchkick_index
  9 + if !record or !record.should_index?
  10 + # hacky
  11 + record ||= model.new
  12 + record.id = id
  13 + begin
  14 + index.remove record
  15 + rescue Elasticsearch::Transport::Transport::Errors::NotFound
  16 + # do nothing
  17 + end
  18 + else
  19 + index.store record
  20 + end
  21 + end
  22 +
  23 + end
  24 +end
test/reindex_job_test.rb
@@ -15,7 +15,7 @@ class TestReindexJob &lt; Minitest::Test @@ -15,7 +15,7 @@ class TestReindexJob &lt; Minitest::Test
15 product = Product.create!(name: "Boom") 15 product = Product.create!(name: "Boom")
16 Product.searchkick_index.refresh 16 Product.searchkick_index.refresh
17 assert_search "*", [] 17 assert_search "*", []
18 - Searchkick::ReindexJob.new("Product", product.id).perform 18 + Searchkick::ReindexJob.new("Product", product.id.to_s).perform
19 Product.searchkick_index.refresh 19 Product.searchkick_index.refresh
20 assert_search "*", ["Boom"] 20 assert_search "*", ["Boom"]
21 end 21 end
@@ -25,7 +25,7 @@ class TestReindexJob &lt; Minitest::Test @@ -25,7 +25,7 @@ class TestReindexJob &lt; Minitest::Test
25 Product.reindex 25 Product.reindex
26 assert_search "*", ["Boom"] 26 assert_search "*", ["Boom"]
27 product.destroy 27 product.destroy
28 - Searchkick::ReindexJob.new("Product", product.id).perform 28 + Searchkick::ReindexJob.new("Product", product.id.to_s).perform
29 Product.searchkick_index.refresh 29 Product.searchkick_index.refresh
30 assert_search "*", [] 30 assert_search "*", []
31 end 31 end
test/reindex_v2_job_test.rb 0 → 100644
@@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
  1 +require_relative "test_helper"
  2 +
  3 +class TestReindexV2Job < Minitest::Test
  4 +
  5 + def setup
  6 + skip if !defined?(ActiveJob)
  7 + super
  8 + Searchkick.disable_callbacks
  9 + end
  10 +
  11 + def teardown
  12 + Searchkick.enable_callbacks
  13 + end
  14 +
  15 + def test_create
  16 + product = Product.create!(name: "Boom")
  17 + Product.searchkick_index.refresh
  18 + assert_search "*", []
  19 + Searchkick::ReindexV2Job.perform_later("Product", product.id.to_s)
  20 + Product.searchkick_index.refresh
  21 + assert_search "*", ["Boom"]
  22 + end
  23 +
  24 + def test_destroy
  25 + product = Product.create!(name: "Boom")
  26 + Product.reindex
  27 + assert_search "*", ["Boom"]
  28 + product.destroy
  29 + Searchkick::ReindexV2Job.perform_later("Product", product.id.to_s)
  30 + Product.searchkick_index.refresh
  31 + assert_search "*", []
  32 + end
  33 +
  34 +end
test/test_helper.rb
@@ -13,6 +13,8 @@ Searchkick.client.transport.logger = Logger.new(&quot;elasticsearch.log&quot;) @@ -13,6 +13,8 @@ Searchkick.client.transport.logger = Logger.new(&quot;elasticsearch.log&quot;)
13 13
14 I18n.config.enforce_available_locales = true 14 I18n.config.enforce_available_locales = true
15 15
  16 +ActiveJob::Base.logger = nil if defined?(ActiveJob)
  17 +
16 if defined?(Mongoid) 18 if defined?(Mongoid)
17 19
18 def mongoid2? 20 def mongoid2?