Skip to content

Commit

Permalink
Merge pull request #335 from 0x0badc0de/block-scoped-subscribe-in-rak…
Browse files Browse the repository at this point in the history
…e-tasks

Use scoped subscription to ActiveSupport::Notifications
  • Loading branch information
pyromaniac committed Feb 20, 2016
2 parents 1116bad + faa8a87 commit 3ff53ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
7 changes: 5 additions & 2 deletions lib/chewy/rake_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ module Chewy
module RakeHelper
class << self

def subscribe_task_stats!
ActiveSupport::Notifications.subscribe('import_objects.chewy') do |name, start, finish, id, payload|
def subscribed_task_stats(&block)
callback = ->(name, start, finish, id, payload) do
duration = (finish - start).round(2)
puts " Imported #{payload[:type]} for #{duration}s, documents total: #{payload[:import].try(:[], :index).to_i}"
payload[:errors].each do |action, errors|
Expand All @@ -14,6 +14,9 @@ def subscribe_task_stats!
end
end if payload[:errors]
end
ActiveSupport::Notifications.subscribed(callback, 'import_objects.chewy') do
yield
end
end

def eager_load_chewy!
Expand Down
32 changes: 18 additions & 14 deletions lib/tasks/chewy.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,43 @@ require 'chewy/rake_helper'
namespace :chewy do
desc 'Destroy, recreate and import data to specified index'
task :reset, [:index] => :environment do |task, args|
Chewy::RakeHelper.subscribe_task_stats!
Chewy::RakeHelper.subscribed_task_stats do

if args[:index].present?
Chewy::RakeHelper.reset_index(args[:index])
else
Chewy::RakeHelper.reset_all
if args[:index].present?
Chewy::RakeHelper.reset_index(args[:index])
else
Chewy::RakeHelper.reset_all
end
end
end

namespace :reset do
desc 'Destroy, recreate and import data for all found indexes'
task all: :environment do
Chewy::RakeHelper.subscribe_task_stats!
Chewy::RakeHelper.reset_all
Chewy::RakeHelper.subscribed_task_stats do
Chewy::RakeHelper.reset_all
end
end
end

desc 'Updates data specified index'
task :update, [:index] => :environment do |task, args|
Chewy::RakeHelper.subscribe_task_stats!
Chewy::RakeHelper.subscribed_task_stats do

if args[:index].present?
Chewy::RakeHelper.update_index(args[:index])
else
Chewy::RakeHelper.update_all
if args[:index].present?
Chewy::RakeHelper.update_index(args[:index])
else
Chewy::RakeHelper.update_all
end
end
end

namespace :update do
desc 'Updates data for all found indexes'
task all: :environment do
Chewy::RakeHelper.subscribe_task_stats!
Chewy::RakeHelper.update_all
Chewy::RakeHelper.subscribed_task_stats do
Chewy::RakeHelper.update_all
end
end
end
end

0 comments on commit 3ff53ad

Please sign in to comment.