diff --git a/lib/tasks/client.rake b/lib/tasks/client.rake index dde6356d1..33fc86278 100644 --- a/lib/tasks/client.rake +++ b/lib/tasks/client.rake @@ -1,4 +1,70 @@ namespace :client do + desc "Create index for clients" + task :create_index => :environment do + Client.__elasticsearch__.create_index! + end + + desc "Delete index for clients" + task :delete_index => :environment do + Client.__elasticsearch__.delete_index! + end + + desc "Refresh index for clients" + task :refresh_index => :environment do + Client.__elasticsearch__.refresh_index! + end + + desc 'Index DOIs by client' + task :index_all_dois => :environment do + if ENV['CLIENT_ID'].nil? + puts "ENV['CLIENT_ID'] is required." + exit + end + + client = Client.where(deleted_at: nil).where(symbol: ENV['CLIENT_ID']).first + if client.nil? + puts "Client not found for client ID #{ENV['CLIENT_ID']}." + exit + end + + # index DOIs for client + puts "#{client.dois.length} DOIs will be indexed." + client.dois.find_each do |doi| + doi.__elasticsearch__.index_document + puts "DOI #{doi.doi} indexed." + end + end + + desc 'Import all clients' + task :import => :environment do + Client.import + end + + desc 'Import DOIs by client' + task :import_all_dois => :environment do + if ENV['CLIENT_ID'].nil? + puts "ENV['CLIENT_ID'] is required." + exit + end + + client = Client.where(deleted_at: nil).where(symbol: ENV['CLIENT_ID']).first + if client.nil? + puts "Client not found for client ID #{ENV['CLIENT_ID']}." + exit + end + + # import DOIs for client + puts "#{client.dois.length} DOIs will be imported." + client.dois.find_each do |doi| + begin + Doi.import_one(doi_id: doi.doi) + puts "DOI #{doi.doi} imported." + rescue TypeError, NoMethodError, RuntimeError, ActiveRecord::StatementInvalid, ActiveRecord::LockWaitTimeout, Elasticsearch::Transport::Transport::Errors::BadRequest => error + puts "[MySQL] Error importing metadata for " + doi.doi + ": " + error.message + end + end + end + desc 'Delete client transferred to other DOI registration agency' task :delete => :environment do if ENV['CLIENT_ID'].nil? @@ -107,50 +173,4 @@ namespace :client do puts "Client prefix for client #{target.symbol} and prefix #{prefix} created." end end - - desc 'Import DOIs by client' - task :import_all_dois => :environment do - if ENV['CLIENT_ID'].nil? - puts "ENV['CLIENT_ID'] is required." - exit - end - - client = Client.where(deleted_at: nil).where(symbol: ENV['CLIENT_ID']).first - if client.nil? - puts "Client not found for client ID #{ENV['CLIENT_ID']}." - exit - end - - # import DOIs for client - puts "#{client.dois.length} DOIs will be imported." - client.dois.find_each do |doi| - begin - Doi.import_one(doi_id: doi.doi) - puts "DOI #{doi.doi} imported." - rescue TypeError, NoMethodError, RuntimeError, ActiveRecord::StatementInvalid, ActiveRecord::LockWaitTimeout, Elasticsearch::Transport::Transport::Errors::BadRequest => error - puts "[MySQL] Error importing metadata for " + doi.doi + ": " + error.message - end - end - end - - desc 'Index DOIs by client' - task :index_all_dois => :environment do - if ENV['CLIENT_ID'].nil? - puts "ENV['CLIENT_ID'] is required." - exit - end - - client = Client.where(deleted_at: nil).where(symbol: ENV['CLIENT_ID']).first - if client.nil? - puts "Client not found for client ID #{ENV['CLIENT_ID']}." - exit - end - - # index DOIs for client - puts "#{client.dois.length} DOIs will be indexed." - client.dois.find_each do |doi| - doi.__elasticsearch__.index_document - puts "DOI #{doi.doi} indexed." - end - end end \ No newline at end of file diff --git a/lib/tasks/doi.rake b/lib/tasks/doi.rake index b9c9e6f9d..b7f4fca62 100644 --- a/lib/tasks/doi.rake +++ b/lib/tasks/doi.rake @@ -1,9 +1,40 @@ namespace :doi do - desc 'Store handle URL' - task :set_url => :environment do - from_date = ENV['FROM_DATE'] || (Time.zone.now - 1.day).strftime("%F") - response = Doi.set_url(from_date: from_date) - puts response + desc 'Index all DOIs' + task :index => :environment do + if ENV['YEAR'].present? + from_date = "#{ENV['YEAR']}-01-01" + until_date = "#{ENV['YEAR']}-12-31" + else + from_date = ENV['FROM_DATE'] || Date.current.strftime("%F") + until_date = ENV['UNTIL_DATE'] || Date.current.strftime("%F") + end + + index_time = ENV['INDEX_TIME'] || Time.zone.now.utc.iso8601 + client_id = ENV['CLIENT_ID'] + + Doi.index(from_date: from_date, until_date: until_date, index_time: index_time, client_id: client_id) + end + + desc 'Index DOIs per day' + task :index_by_day => :environment do + from_date = ENV['FROM_DATE'] || Date.current.strftime("%F") + + Doi.index_by_day(from_date: from_date) + puts "DOIs created on #{from_date} indexed." + end + + desc 'Index DOIs by ID' + task :index_by_ids => :environment do + from_id = (ENV['FROM_ID'] || 1).to_i + until_id = (ENV['UNTIL_ID'] || from_id + 499).to_i + + Doi.index_by_ids(from_id: from_id, until_id: until_id) + end + + desc 'Import all dois' + task :import => :environment do + Doi.__elasticsearch__.create_index! + Doi.import end desc 'Import all DOIs' @@ -50,36 +81,26 @@ namespace :doi do Doi.import_one(doi_id: ENV['DOI']) end - desc 'Index all DOIs' - task :index => :environment do - if ENV['YEAR'].present? - from_date = "#{ENV['YEAR']}-01-01" - until_date = "#{ENV['YEAR']}-12-31" - else - from_date = ENV['FROM_DATE'] || Date.current.strftime("%F") - until_date = ENV['UNTIL_DATE'] || Date.current.strftime("%F") - end - - index_time = ENV['INDEX_TIME'] || Time.zone.now.utc.iso8601 - client_id = ENV['CLIENT_ID'] - - Doi.index(from_date: from_date, until_date: until_date, index_time: index_time, client_id: client_id) + desc "Create index for dois" + task :create_index => :environment do + Doi.__elasticsearch__.create_index! end - desc 'Index DOIs per day' - task :index_by_day => :environment do - from_date = ENV['FROM_DATE'] || Date.current.strftime("%F") - - Doi.index_by_day(from_date: from_date) - puts "DOIs created on #{from_date} indexed." + desc "Delete index for dois" + task :delete_index => :environment do + Doi.__elasticsearch__.delete_index! end - desc 'Index DOIs by ID' - task :index_by_ids => :environment do - from_id = (ENV['FROM_ID'] || 1).to_i - until_id = (ENV['UNTIL_ID'] || from_id + 499).to_i + desc "Refresh index for dois" + task :refresh_index => :environment do + Doi.__elasticsearch__.refresh_index! + end - Doi.index_by_ids(from_id: from_id, until_id: until_id) + desc 'Store handle URL' + task :set_url => :environment do + from_date = ENV['FROM_DATE'] || (Time.zone.now - 1.day).strftime("%F") + response = Doi.set_url(from_date: from_date) + puts response end desc 'Set minted' diff --git a/lib/tasks/elasticsearch/client.rake b/lib/tasks/elasticsearch/client.rake deleted file mode 100644 index 341aa0f96..000000000 --- a/lib/tasks/elasticsearch/client.rake +++ /dev/null @@ -1,23 +0,0 @@ -namespace :elasticsearch do - namespace :client do - desc 'Import all clients' - task :import => :environment do - Client.import - end - - desc "Create index for clients" - task :create_index => :environment do - Client.__elasticsearch__.create_index! - end - - desc "Delete index for clients" - task :delete_index => :environment do - Client.__elasticsearch__.delete_index! - end - - desc "Refresh index for clients" - task :refresh_index => :environment do - Client.__elasticsearch__.refresh_index! - end - end -end \ No newline at end of file diff --git a/lib/tasks/elasticsearch/doi.rake b/lib/tasks/elasticsearch/doi.rake deleted file mode 100644 index 1d0e4713f..000000000 --- a/lib/tasks/elasticsearch/doi.rake +++ /dev/null @@ -1,24 +0,0 @@ -namespace :elasticsearch do - namespace :doi do - desc 'Import all dois' - task :import => :environment do - Doi.__elasticsearch__.create_index! - Doi.import - end - - desc "Create index for dois" - task :create_index => :environment do - Doi.__elasticsearch__.create_index! - end - - desc "Delete index for dois" - task :delete_index => :environment do - Doi.__elasticsearch__.delete_index! - end - - desc "Refresh index for dois" - task :refresh_index => :environment do - Doi.__elasticsearch__.refresh_index! - end - end -end \ No newline at end of file diff --git a/lib/tasks/elasticsearch/prefix.rake b/lib/tasks/elasticsearch/prefix.rake deleted file mode 100644 index d7f9fc55a..000000000 --- a/lib/tasks/elasticsearch/prefix.rake +++ /dev/null @@ -1,24 +0,0 @@ -namespace :elasticsearch do - namespace :prefix do - desc 'Import all prefixes' - task :import => :environment do - Prefix.__elasticsearch__.create_index! - Prefix.import - end - - desc "Create index for prefixes" - task :create_index => :environment do - Prefix.__elasticsearch__.create_index! - end - - desc "Delete index for prefixes" - task :delete_index => :environment do - Prefix.__elasticsearch__.delete_index! - end - - desc "Refresh index for prefixes" - task :refresh_index => :environment do - Prefix.__elasticsearch__.refresh_index! - end - end -end \ No newline at end of file diff --git a/lib/tasks/elasticsearch/provider.rake b/lib/tasks/elasticsearch/provider.rake deleted file mode 100644 index 89e660921..000000000 --- a/lib/tasks/elasticsearch/provider.rake +++ /dev/null @@ -1,24 +0,0 @@ -namespace :elasticsearch do - namespace :provider do - desc 'Import all providers' - task :import => :environment do - Provider.__elasticsearch__.create_index! - Provider.import - end - - desc "Create index for providers" - task :create_index => :environment do - Provider.__elasticsearch__.create_index! - end - - desc "Delete index for providers" - task :delete_index => :environment do - Provider.__elasticsearch__.delete_index! - end - - desc "Refresh index for providers" - task :refresh_index => :environment do - Provider.__elasticsearch__.refresh_index! - end - end -end \ No newline at end of file diff --git a/lib/tasks/provider.rake b/lib/tasks/provider.rake new file mode 100644 index 000000000..0d5649688 --- /dev/null +++ b/lib/tasks/provider.rake @@ -0,0 +1,22 @@ +namespace :provider do + desc 'Import all providers' + task :import => :environment do + Provider.__elasticsearch__.create_index! + Provider.import + end + + desc "Create index for providers" + task :create_index => :environment do + Provider.__elasticsearch__.create_index! + end + + desc "Delete index for providers" + task :delete_index => :environment do + Provider.__elasticsearch__.delete_index! + end + + desc "Refresh index for providers" + task :refresh_index => :environment do + Provider.__elasticsearch__.refresh_index! + end +end \ No newline at end of file