def fetch_design_thumb(site,locale=I18n.locale,fetch_size=nil,force_update = false) thread = Multithread.where(:key=>"fetch_design_thumbs").first if (thread && thread.status["status"] == "Finished" rescue false) thread.destroy thread = nil end if thread.nil? thread = Multithread.create(:key=>"fetch_design_thumbs") thread.status = {} thread.status["status"] = "fetching" thread.status["finish_list"] = [] thread.save fetch_size = "1366*768px" if fetch_size.blank? Dir.glob("#{Rails.root}/app/templates/*").each do |template| template_json = "#{template}/template.json" thumb_path = "#{template}/assets/images/design_thumb.png" next unless File.exist?(template_json) next if (File.exist?(thumb_path) && !force_update) data = JSON.parse(File.read(template_json)) rescue {} design = { "key"=>data["key"], "title"=>data["title"], "author"=>data["author"], "thumbnail"=>data["thumbnail"] } Phantomjs.path puts "Fetching thumbnail for #{design["key"]}" cmd = "#{Phantomjs.path} --ignore-ssl-errors=true --ssl-protocol=any rasterize.js #{site.root_url}/#{locale}/xhr/preview_pages?template_key=#{design["key"]} #{thumb_path} #{fetch_size}" puts ['cmd',cmd] error = `#{cmd}` puts "Finish fetching #{design["key"]}!" if File.exist?(thumb_path) design["thumbnail"] = "assets/images/design_thumb.png" File.open(template_json,"w+") do |f| f.write(design.to_json) end thread.status["finish_list"] << design["key"] thread.save end if error.present? puts error thread.status["error"] = [] if thread.status["error"].nil? thread.status["error"] << error end end thread.status["status"] = "Finished" thread.save end end