Skip to content

Commit

Permalink
async indexing / deletion done
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Siri committed Oct 12, 2015
1 parent 547223f commit 389d934
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 2 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc
gem 'elasticsearch-rails'
gem 'elasticsearch-model'
gem 'bootstrap-sass'
gem 'sidekiq'

gem 'sequel'

Expand Down
58 changes: 58 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,47 @@ GEM
builder (3.2.2)
byebug (4.0.5)
columnize (= 0.9.0)
celluloid (0.17.1.2)
bundler
celluloid-essentials
celluloid-extras
celluloid-fsm
celluloid-pool
celluloid-supervision
dotenv
nenv
rspec-logsplit (>= 0.1.2)
timers (>= 4.1.1)
celluloid-essentials (0.20.2.1)
bundler
dotenv
nenv
rspec-logsplit (>= 0.1.2)
timers (>= 4.1.1)
celluloid-extras (0.20.1)
bundler
dotenv
nenv
rspec-logsplit (>= 0.1.2)
timers (>= 4.1.1)
celluloid-fsm (0.20.1)
bundler
dotenv
nenv
rspec-logsplit (>= 0.1.2)
timers (>= 4.1.1)
celluloid-pool (0.20.1)
bundler
dotenv
nenv
rspec-logsplit (>= 0.1.2)
timers (>= 4.1.1)
celluloid-supervision (0.20.1.1)
bundler
dotenv
nenv
rspec-logsplit (>= 0.1.2)
timers (>= 4.1.1)
coderay (1.1.0)
coffee-rails (4.1.0)
coffee-script (>= 2.2.0)
Expand All @@ -58,7 +99,9 @@ GEM
execjs
coffee-script-source (1.9.1.1)
columnize (0.9.0)
connection_pool (2.2.0)
debug_inspector (0.0.2)
dotenv (2.0.2)
elasticsearch (1.0.8)
elasticsearch-api (= 1.0.7)
elasticsearch-transport (= 1.0.7)
Expand All @@ -79,6 +122,7 @@ GEM
globalid (0.3.6)
activesupport (>= 4.1.0)
hashie (3.4.1)
hitimes (1.2.3)
i18n (0.7.0)
jbuilder (2.2.13)
activesupport (>= 3.0.0, < 5)
Expand All @@ -103,6 +147,7 @@ GEM
ruby-progressbar
multi_json (1.11.0)
multipart-post (2.0.0)
nenv (0.2.0)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
pg (0.18.1)
Expand Down Expand Up @@ -141,6 +186,10 @@ GEM
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
rdoc (4.2.0)
redis (3.2.1)
redis-namespace (1.5.2)
redis (~> 3.0, >= 3.0.4)
rspec-logsplit (0.1.3)
ruby-progressbar (1.7.5)
sass (3.4.13)
sass-rails (5.0.3)
Expand All @@ -153,6 +202,12 @@ GEM
json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0)
sequel (4.21.0)
sidekiq (3.5.0)
celluloid (~> 0.17.0)
connection_pool (~> 2.2, >= 2.2.0)
json (~> 1.0)
redis (~> 3.2, >= 3.2.1)
redis-namespace (~> 1.5, >= 1.5.2)
slop (3.6.0)
spring (1.3.6)
sprockets (3.3.3)
Expand All @@ -164,6 +219,8 @@ GEM
thor (0.19.1)
thread_safe (0.3.5)
tilt (1.4.1)
timers (4.1.1)
hitimes
turbolinks (2.5.3)
coffee-rails
tzinfo (1.2.2)
Expand Down Expand Up @@ -196,6 +253,7 @@ DEPENDENCIES
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
sequel
sidekiq
spring
turbolinks
uglifier (>= 1.3.0)
Expand Down
20 changes: 20 additions & 0 deletions app/jobs/indexer_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class IndexerJob < ActiveJob::Base
queue_as :elasticsearch

def perform(operation, record_id)
logger.debug [operation, "Movie: #{record_id}"]
self.send(operation, record_id)
end

private

def index(record_id)
record = Movie.find(record_id)
record.__elasticsearch__.index_document
end

def delete(record_id)
record = Movie.find(record_id)
record.__elasticsearch__.delete_document
end
end
16 changes: 14 additions & 2 deletions app/models/movie.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
class Movie < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
# we don't need the line below because we're
# implementing our own
# include Elasticsearch::Model::Callbacks

after_commit :index_document, on: [:create, :update]
after_commit :delete_document, on: :destroy

def index_document
IndexerJob.perform_later('index', self.id)
end

def delete_document
IndexerJob.perform_later('delete', self.id)
end

validates :name, uniqueness: true

Expand Down Expand Up @@ -46,7 +59,6 @@ def multi_match_query(query)
end
end


class RelationError < StandardError
def initialize(msg = "That Relationship Type doesn't exist")
super(msg)
Expand Down
1 change: 1 addition & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Application < Rails::Application
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.active_job.queue_adapter = :sidekiq

# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
Expand Down
5 changes: 5 additions & 0 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Sidekiq.configure_server do |config|
config.server_middleware do |chain|
chain.remove Sidekiq::Middleware::Server::RetryJobs
end
end
7 changes: 7 additions & 0 deletions test/jobs/indexer_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class IndexerJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 389d934

Please sign in to comment.