Skip to content

Commit

Permalink
tweak background jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Oct 24, 2018
1 parent 9a78da0 commit c2f1d06
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
21 changes: 10 additions & 11 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,20 +235,19 @@ def self.index(options={})
# get every day between from_date and until_date
(from_date..until_date).each do |d|
DoiIndexByDayJob.perform_later(from_date: d.strftime("%F"))
end

"Queued indexing for DOIs created from #{from_date.strftime("%F")} until #{until_date.strftime("%F")}."
puts "Queued indexing for DOIs created on #{d.strftime("%F")}."
end
end

def self.index_by_day(options={})
from_date = options[:from_date].present? ? Date.parse(options[:from_date]) : Date.current
until_date = from_date + 1.day
return nil unless options[:from_date].present?

errors = 0
count = 0

logger = Logger.new(STDOUT)

Doi.where("created >= ?", from_date.strftime("%F") + " 00:00:00").where("created < ?", until_date.strftime("%F") + " 00:00:00").where("updated > indexed").find_in_batches(batch_size: 500) do |dois|
Doi.where(created: [options[:from_date] + " 00:00:00", options[:from_date] + " 23:59:59"]).not_indexed.find_in_batches(batch_size: 500) do |dois|
response = Doi.__elasticsearch__.client.bulk \
index: Doi.index_name,
type: Doi.document_type,
Expand All @@ -260,22 +259,22 @@ def self.index_by_day(options={})
end

if errors > 1
logger.info "[Elasticsearch] #{errors} errors indexing #{count} DOIs created on #{from_date.strftime("%F")}."
logger.info "[Elasticsearch] #{errors} errors indexing #{count} DOIs created on #{options[:from_date]}."
elsif count > 1
logger.info "[Elasticsearch] Indexed #{count} DOIs created on #{from_date.strftime("%F")}."
logger.info "[Elasticsearch] Indexed #{count} DOIs created on #{options[:from_date]}."
end
rescue Elasticsearch::Transport::Transport::Errors::RequestEntityTooLarge, Faraday::ConnectionFailed => error
logger.info "[Elasticsearch] Error #{error.message} indexing DOIs created on #{from_date.strftime("%F")}."
logger.info "[Elasticsearch] Error #{error.message} indexing DOIs created on #{options[:from_date]}."

count = 0

Doi.where("created >= ?", from_date.strftime("%F") + " 00:00:00").where("created < ?", until_date.strftime("%F") + " 00:00:00").where("updated > indexed").find_each do |doi|
Doi.where(created: [options[:from_date] + " 00:00:00", options[:from_date] + " 23:59:59"]).not_indexed.find_each do |doi|
IndexJob.perform_later(doi)
doi.update_column(:indexed, Time.zone.now)
count += 1
end

logger.info "[Elasticsearch] Indexed #{count} DOIs created on #{from_date.strftime("%F")}."
logger.info "[Elasticsearch] Indexed #{count} DOIs created on #{options[:from_date]}."
end

def uid
Expand Down
7 changes: 3 additions & 4 deletions lib/tasks/doi.rake
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ namespace :doi do
from_date = "#{ENV['YEAR']}-01-01"
until_date = "#{ENV['YEAR']}-12-31"
else
from_date = ENV['FROM_DATE'] || Date.current.beginning_of_month.strftime("%F")
until_date = ENV['UNTIL_DATE'] || Date.current.end_of_month.strftime("%F")
from_date = ENV['FROM_DATE'] || Date.current.strftime("%F")
until_date = ENV['UNTIL_DATE'] || Date.current.strftime("%F")
end

response = Doi.index(from_date: from_date, until_date: until_date)
puts response
Doi.index(from_date: from_date, until_date: until_date)
end

desc 'Index DOIs per day'
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/tasks/doi_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
ENV['UNTIL_DATE'] = "2018-08-05"

let!(:doi) { create_list(:doi, 10) }
let(:output) { "Queued indexing for DOIs created from 2018-01-04 until 2018-08-05.\n" }
let(:output) { "Queued indexing for DOIs created on 2018-01-04.\n" }

it "prerequisites should include environment" do
expect(subject.prerequisites).to include("environment")
end

it "should run the rake task" do
expect(capture_stdout { subject.invoke }).to eq(output)
expect(capture_stdout { subject.invoke }).to start_with(output)
end

it "should enqueue an DoiIndexByDayJob" do
Expand Down

0 comments on commit c2f1d06

Please sign in to comment.