class CloudhdrJob < ActiveRecord::Base belongs_to :image, :polymorphic=>true scope :pending, :conditions => "cloudhdr_status != 'ready'" #scope :for_components, :conditions => "tracking_mode = 'component'" #scope :for_jobs, :conditions => "tracking_mode = 'job'" def initialize(params = {}, options={}) super #self.tracking_mode = "component" unless self.tracking_mode end def update_status job_data = Cloudhdr::Job.details(cloudhdr_job_id).body #puts job_data.to_json puts " CloudhdrJob #{id} = #{job_data["aasm_state"]}" puts "job_data = #{job_data}" puts "*" * 50 puts "cloudhdr_input_id = #{cloudhdr_input_id}" if cloudhdr_input_id job_data["inputs"].each do |input| #puts "input = #{input.to_json}" puts "input id = #{input["id"]} my cloudhdr_input_id = #{cloudhdr_input_id}" if input["id"] == cloudhdr_input_id input.symbolize_keys! params = { :class => image_type, :id => image_id, :job => { :id => job_data["id"] }, :input => input } CloudhdrJob.update_from_cloudhdr(params) end end elsif cloudhdr_output_id outputs = (job_data["thumbnails"] || []) + (job_data["hdrs"] || []) + (job_data["tone_mappings"] || []) + (job_data["composites"] || []) + (job_data["hdr_htmls"] || []) outputs.each do |output| if output["id"] == cloudhdr_output_id output.symbolize_keys! output[:url] = output[:base_url] + output[:filename] params = { :class => image_type, :id => image_id, :job => { :id => job_data["id"] }, :output => output } CloudhdrJob.update_from_cloudhdr(params) end end end puts "+++++++++++++++" #if job_data["aasm_state"] == "complete" # self.cloudhdr_status = "ready" # self.image.cloudhdr_status = "ready" # self.image.thumbnails.each do |t| # t.cloudhdr_status = "ready" # end # self.save # #self.image.save #end end def self.update_pending_jobs CloudhdrJob.pending.find_each do |e| e.update_status end end def self.update_from_cloudhdr(params) #if params.keys.include?("inputs") && params.keys.include?("outputs") update_from_cloudhdr_job_style(params) #else # update_from_cloudhdr_component_style(params) #end end def self.update_from_cloudhdr_job_style(params) if params[:job][:type] == 'ThumbnailJob' update_from_cloudhdr_thumbnail_job(params) elsif params[:job][:type] == 'CompositeJob' || params[:job][:type] == 'ToneMappingJob' update_from_cloudhdr_generic_job_with_thumbnails(params) else update_from_cloudhdr_generic_job(params) end end def self.update_from_cloudhdr_thumbnail_job(params) #debugger job = self.find_by_cloudhdr_job_id params[:job][:id] image = job.image img_params = params[:inputs].first update_image_and_job_from_img_params(image,job,img_params) params[:outputs].each do |thumb_params| thumbnail = image.thumbnail_for(thumb_params[:label]) update_image_and_job_from_img_params(thumbnail,job,thumb_params) thumbnail.cloudhdr_status = "ready" if thumbnail.respond_to?(:cloudhdr_status=) thumbnail.cloudhdr_status = "ready" if thumbnail.respond_to?(:cloudhdr_status=) thumbnail.save! end image.cloudhdr_status = "ready" if image.respond_to?(:cloudhdr_status=) image.cloudhdr_status = "ready" if image.respond_to?(:cloudhdr_status=) image.save! image.fire_ready_callback job.cloudhdr_status = "ready" job.save job end def self.update_from_cloudhdr_generic_job(params) #debugger job = self.find_by_cloudhdr_job_id params[:job][:id] image = job.image img_params = params[:outputs].first update_image_and_job_from_img_params(image,job,img_params) image.cloudhdr_status = "ready" if image.respond_to?(:cloudhdr_status=) image.cloudhdr_status = "ready" if image.respond_to?(:cloudhdr_status=) image.save! image.fire_ready_callback job.cloudhdr_status = "ready" job.save job end def self.update_from_cloudhdr_generic_job_with_thumbnails(params) #debugger job = self.for_jobs.find_by_cloudhdr_job_id params[:job][:id] image = job.image img_params = {} params[:outputs].each do |thumb_params| if thumb_params[:label].blank? img_params = thumb_params next end thumbnail = image.thumbnail_for(thumb_params[:label]) update_image_and_job_from_img_params(thumbnail,job,thumb_params) thumbnail.cloudhdr_status = "ready" if thumbnail.respond_to?(:cloudhdr_status=) thumbnail.cloudhdr_status = "ready" if thumbnail.respond_to?(:cloudhdr_status=) thumbnail.save! end update_image_and_job_from_img_params(image,job,img_params) image.cloudhdr_status = "ready" if image.respond_to?(:cloudhdr_status=) image.cloudhdr_status = "ready" if image.respond_to?(:cloudhdr_status=) image.save! image.fire_ready_callback job.cloudhdr_status = "ready" job.save job end def self.update_image_and_job_from_img_params(image,job,img_params) [ :file_size,:width,:height,:taken_at,:lat,:lng,:saturated_pixels,:gauss,:bits_per_pixel,:camera_make, :camera_model, :orientation, :exposure_time, :f_number, :iso_speed_rating, :exposure_bias_value, :focal_length, :focal_length_in_35mm_film, :subsec_time, :pixels, :processing_time].each do |att| setter = att.to_s + "=" if image.respond_to? setter and !img_params[att].blank? image.send setter, img_params[att] end if job.respond_to? setter and !img_params[att].blank? job.send setter, img_params[att] end end end end