Skip to content

Commit

Permalink
Update thumbnail rake tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
eliotjordan committed Nov 29, 2017
1 parent a06c884 commit f5f1dcd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/services/thumbnail_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def placeholder_image

# Path to placeholder image based on the layer geometry.
def placeholder_image_path
geom_type = @document["layer_geom_type_s"].tr(" ", "-").downcase
geom_type = @document.fetch("layer_geom_type_s", "").tr(" ", "-").downcase
thumb_path = "#{placeholder_base_path}/thumbnail-#{geom_type}.png"
return "#{placeholder_base_path}/thumbnail-paper-map.png" unless File.exist?(thumb_path)
thumb_path
Expand Down
57 changes: 36 additions & 21 deletions lib/tasks/thumbnails.rake
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,50 @@ namespace :pulmap do
namespace :thumbnails do
desc 'Delete all cached thumbnails'
task delete: :environment do
file_base_path = Settings.THUMBNAIL.FILE_BASE_PATH || Rails.root.join("public")
thumb_path = Settings.THUMBNAIL.BASE_PATH || 'thumbnails'
FileUtils.rm_rf "#{file_base_path}/#{thumb_path}"
Rails.cache.clear
end
desc 'Pre-cache a thumbnail'
task :precache, %i[doc_id timeout] => [:environment] do |_t, args|
task :precache, %i[doc_id override_existing] => [:environment] do |_t, args|
begin
raise 'Please supply required arguments [document_id, timeout]' unless args[:doc_id] && args[:timeout]
raise 'Please supply required argument [document_id]' unless args[:doc_id]
document = Geoblacklight::SolrDocument.find(args[:doc_id])
raise Blacklight::Exceptions::RecordNotFound if document[:layer_slug_s] != args[:doc_id]
return unless document.available?
thumbnail = Thumbnail.new(document)
unless thumbnail.file_exists? || thumbnail.service_url.nil?
PersistThumbnail.new(url: thumbnail.service_url,
file_path: thumbnail.file_path_and_name,
content_type: 'image/png',
timeout: args[:timeout].to_i).create_file
if !Rails.cache.exist?("thumbnails/#{args[:doc_id]}") || args[:override_existing]
CacheThumbnailJob.perform_now(document.to_h)
end
end
end

desc 'Pre-cache all thumbnails'
task :precache_all, [:override_existing] => [:environment] do |_t, args|
begin
query = "layer_slug_s:*"
layers = 'layer_slug_s, layer_id_s, dc_rights_s, dct_provenance_s, layer_geom_type_s, dct_references_s'
index = Geoblacklight::SolrDocument.index
results = index.send_and_receive(index.blacklight_config.solr_path,
q: query,
fl: layers,
rows: 100_000_000)
num_found = results.response[:numFound]
doc_counter = 0
results.docs.each do |document|
doc_counter += 1
puts "#{document[:layer_slug_s]} (#{doc_counter}/#{num_found})"
begin
if !Rails.cache.exist?("thumbnails/#{document[:layer_slug_s]}") || args[:override_existing]
CacheThumbnailJob.perform_now(document.to_h)
end
rescue Blacklight::Exceptions::RecordNotFound
next
end
end
end
end

desc 'Pre-cache all thumbnails for an institution'
task :precache_all, %i[institution timeout] => [:environment] do |_t, args|
task :precache_institution, %i[institution override_existing] => [:environment] do |_t, args|
begin
raise 'Please supply required arguments[doc_id, institution, timeout]' unless args[:institution] && args[:timeout]
raise 'Please supply required arguments [institution]' unless args[:institution]
query = "dct_provenance_s:#{args[:institution]}"
layers = 'layer_slug_s, layer_id_s, dc_rights_s, dct_provenance_s dct_references_s'
index = Geoblacklight::SolrDocument.index
Expand All @@ -39,13 +59,8 @@ namespace :pulmap do
doc_counter += 1
puts "#{document[:layer_slug_s]} (#{doc_counter}/#{num_found})"
begin
next unless document.available?
thumbnail = Thumbnail.new(document)
unless thumbnail.file_exists? || thumbnail.service_url.nil?
PersistThumbnail.new(url: thumbnail.service_url,
file_path: thumbnail.file_path_and_name,
content_type: 'image/png',
timeout: args[:timeout].to_i).create_file
if !Rails.cache.exist?("thumbnails/#{document[:layer_slug_s]}") || args[:override_existing]
CacheThumbnailJob.perform_now(document.to_h)
end
rescue Blacklight::Exceptions::RecordNotFound
next
Expand Down

0 comments on commit f5f1dcd

Please sign in to comment.