app/models/effective/delayed_job.rb in effective_assets-0.1 vs app/models/effective/delayed_job.rb in effective_assets-1.0.0

- old
+ new

@@ -2,98 +2,53 @@ # Call with DelayedJob.new.process_asset_images(...) # Run jobs locally with "rake jobs:work" module Effective class DelayedJob - def process_asset(asset) - DelayedJob.configure_carrierwave + def process_asset(obj) + if obj.kind_of?(Effective::Asset) + asset = obj + else + asset = Effective::Asset.where(:id => (obj.to_i rescue 0)).first + end - if asset and !asset.processed? and asset.upload_file.present? - begin - puts "Processing an asset ID ##{asset.id}..." + if asset.present? && !asset.processed? && asset.upload_file.present? && asset.upload_file != 'placeholder' + puts "Processing asset ##{asset.id} from #{asset.upload_file}." - if asset.upload_file.include?("#{Asset.s3_base_path}") - if asset.image? - puts "Asset is an image in our S3 assets directory. Downloading and processing..." + if asset.upload_file.include?(Effective::Asset.string_base_path) + puts "String-based Asset processing and uploading..." - # Carrierwave must download the file, process it, then re-upload it to S3 - asset.remote_data_url = asset.upload_file - asset.processed = true - asset.save! - else - puts "Asset is a non-image in our S3 uploads directory. Copying to final location..." + asset.data.cache_stored_file! + asset.data.retrieve_from_cache!(asset.data.cache_name) + asset.data.recreate_versions! + elsif asset.upload_file.include?(Effective::Asset.s3_base_path) + puts "S3 Uploaded Asset downloading and processing..." + # Carrierwave must download the file, process it, then upload the generated versions to S3 + # We only want to process if it's an image, so we don't download zips or videos + asset.remote_data_url = asset.url if asset.image? + else + puts "Non S3 Asset downloading and processing..." + puts "Downloading #{asset.url}" - # We have uploaded a video, or something non-image to our S3 bucket. - # We do not currently process anything. + # Carrierwave must download the file, process it, then upload it and generated verions to S3 + # We only want to process if it's an image, so we don't download zips or videos + asset.remote_data_url = asset.upload_file + end - puts "Marking local asset as processed..." - asset.update_column(:data, asset.file_name) - asset.processed = true - asset.save! - end - elsif asset.upload_file.include?(Asset.string_base_path) - puts "Asset is a string-based asset. Processing..." + asset.processed = true + asset.save! - asset.data.cache_stored_file! - asset.data.retrieve_from_cache!(asset.data.cache_name) - asset.data.recreate_versions! - asset.processed = true - asset.save! - else - puts "Asset is not an s3 uploaded asset. Downloading and processing..." + (GC.start rescue nil) - # Carrierwave must download the file, process it, then re-upload it to S3 - asset.remote_data_url = asset.upload_file - asset.processed = true - asset.save! - end - - puts "Successfully processed the asset." - rescue => e - puts "An error occurred while processing an asset:" - puts e.message - puts e.backtrace.inspect - end + puts "Successfully processed the asset." end end handle_asynchronously :process_asset - def reprocess_all_assets - DelayedJob.configure_carrierwave - - Asset.all.each do |asset| - begin - puts "Processing Asset ID=#{asset.id}..." - asset.data.cache_stored_file! - asset.data.retrieve_from_cache!(asset.data.cache_name) - asset.data.recreate_versions! - asset.save! - puts "Successfully processed #{asset.inspect}" - rescue => e - puts "ERROR: #{asset.id} -> #{e.to_s}" - end - end + def reprocess_asset(id) + Effective::Asset.find(id).reprocess! + (GC.start rescue nil) end - handle_asynchronously :reprocess_all_assets - - def self.configure_carrierwave - return if defined? @@configured_carrierwave - - CarrierWave.configure do |config| - config.fog_credentials = { - :provider => 'AWS', - :aws_access_key_id => EffectiveAssets.aws_access_key_id, - :aws_secret_access_key => EffectiveAssets.aws_secret_access_key - } - config.fog_directory = EffectiveAssets.aws_bucket - config.fog_public = EffectiveAssets.aws_acl.to_s.include?('public') - config.fog_attributes = {'Cache-Control'=>'max-age=315576000'} - config.cache_dir = "#{Rails.root}/tmp/uploads" # For heroku - end - - Rails.logger.info "CONFIGURED CARRIERWAVE" - - @@configured_carrierwave = true - end + handle_asynchronously :reprocess_asset end end