[Backgroundrb-devel] How does ask_result() work?

Cédric akyrho at gmail.com
Fri Jul 25 03:52:21 EDT 2008


Thank you Hemant, this is working perfectly now !


On Thu, Jul 24, 2008 at 6:58 PM, hemant <gethemant at gmail.com> wrote:

> On Thu, Jul 24, 2008 at 8:16 PM, Cédric <akyrho at gmail.com> wrote:
> > As requested on IRC, here's my code.
> >
> > The worker :
> >
> > ---
> > require 'uri'
> > require 'net/http'
> > require 'RMagick'
> > require 'open-uri'
> > require 'rexml/document'
> >
> > class ThumbnailerWorker < BackgrounDRb::MetaWorker
> >   set_worker_name :thumbnailer_worker
> >   set_no_auto_load true
> >
> >   def create(args = nil)
> >     logger.info("[DEBUG] starting a thumbnailer worker (Time: " +
> > Time.now.to_s + ")")
> >     generate_thumbnails(args)
> >   end
> >
> >   def generate_thumbnails(args = nil) # args = @source.id
> >     logger.info("[DEBUG] generate_thumbnails(args = " + args.inspect +
> ")")
> >     @source_id = eval(args.to_s)
> >     cache['thumbnailing_for_source_'+ at source_id.to_s] = '0,0,0'
> >
> >     thumbnails = Thumbnail.find(:all, :conditions => ['medias.source_id=?
> > AND thumbnails.code=?', @source_id, PROCESSING_STATUS], :order =>
> > "medias.created_at ASC", :include => { :media => :source })
> >
> >     @failed = 0
> >     @success = 0
> >     @progress = 0
> >     @total = thumbnails.size unless thumbnails.nil?
> >
> >     if @total.nil? or @total == 0
> >       cache['thumbnailing_for_source_'+ at source_id.to_s] =
> > '-2,'+ at progress.to_s+','+ at total.to_s
> >     end
> >
> >     upload_directory = RAILS_ROOT + "/public/medias/"
> >     no_error_encountered = true
> >
> >     for thumbnail in thumbnails
> >       break if !no_error_encountered
> >
> >       if @progress == 0
> >         media_id = thumbnail.media_id
> >         begin
> >           Dir.mkdir(upload_directory + @source_id.to_s)
> >         end
> >       end
> >
> >       begin
> >         url = URI.split(thumbnail.original.sub(' ', '%20'))
> >
> >         resp = Net::HTTP.get_response(url[2], url[5])
> >         if resp.code.to_s == "200"
> >           http = Net::HTTP.start(url[2])
> >           resp = http.get(url[5])
> >           image_filename = upload_directory + thumbnail.id.to_s +
> '.tmp.jpg'
> >           open(image_filename, "w") { |file| file.write(resp.body) }
> >           pic = Magick::Image.read(image_filename).first
> >           GC.start
> >           maxwidth = 100
> >           maxheight = 100
> >           aspectratio = 1.0
> >           imgwidth = pic.columns
> >           imgheight = pic.rows
> >           imgratio = imgwidth.to_f / imgheight.to_f
> >           scaleratio = imgratio > aspectratio ? (maxwidth.to_f /
> imgwidth) :
> > (maxheight.to_f / imgheight)
> >           thumb = pic.thumbnail(scaleratio)
> >           thumb.write(thumbnail_location = upload_directory +
> > @source_id.to_s + '/' + thumbnail.id.to_s + '.jpg')
> >           File.delete(image_filename)
> >           no_error_encountered = File.exist?(thumbnail_location)
> >           @success += 1
> >         else
> >           logger.info("[WARN] thumbnail for " + thumbnail.original + "
> not
> > generated. response = " + resp.code.to_s)
> >           @failed += 1
> >         end
> >
> >         if no_error_encountered
> >           thumbnail.update_attribute("code", DEFAULT_STATUS)
> >         end
> >
> >         sleep 0.5
> >       rescue Exception => exc
> >         logger.info("[WARN] exception caught in thumbnailer_worker: " +
> > exc.inspect)
> >         logger.info("[WARN] backtrace: " + exc.backtrace.inspect)
> >         @failed += 1 if resp.code.to_s == "200"
> >       end
> >       @progress += 1
> >       if @progress == @total
> >         cache['thumbnailing_for_source_'+ at source_id.to_s] =
> > '9999,'+ at progress.to_s+','+ at total.to_s
> >       else
> >         cache['thumbnailing_for_source_'+ at source_id.to_s] =
> > @progress.to_s+','+ at progress.to_s+','+ at total.to_s
> >         logger.info("cache['#{'thumbnailing_for_source_'+ at source_id.to_s
> }']
> > = #{cache['thumbnailing_for_source_'+ at source_id.to_s].inspect}")
> >       end
> >     end
> >
> >     if no_error_encountered
> >       Media.update_all("code=#{DEFAULT_STATUS}", ["source_id=?",
> > @source_id])
> >       Source.update(@source_id, { "code" => DEFAULT_STATUS })
> >     else
> >       cache['thumbnailing_for_source_'+ at source_id.to_s] =
> > '-1,'+ at progress.to_s+','+ at total.to_s
> >     end
> >
> >     return
> >   end
> >
> >   def finish_work
> >     cache['thumbnailing_for_source_'+ at source_id.to_s] = '-9999,0,0'
> >     exit
> >   end
> > end
> > ---
> >
> > And the controller :
> >
> > ---
> > def submit
> > (some code)
> >                 MiddleMan.new_worker(
> >                   :worker => :thumbnailer_worker,
> >                   :worker_key => "thumbnailing_for_source_" +
> > @source.id.to_s,
> >                   :data => @source.id)
> > (some more code)
> > end
> >
> > def get_loaded_medias
> >         begin
> >           worker_status =
> >
> MiddleMan.worker(:thumbnailer_worker).ask_result("thumbnailing_for_source_#{params[:id]}").split(',')
>
>
> We have got a trouble in above code. Since you are creating a worker
> dynamically using new_worker() method and using a worker key, you must
> always, use that worker key to access that worker.
>
>
> MiddleMan.worker(:thumbnailer_worker,<whatever_worker_key_you_used>).ask_result("thumbnailing_for_source_#{params[:id]}").split(',')
>



-- 
Bousmanne Cédric

Jabber / XMPP : akyrho at gmail.com
Mail : akyrho at gmail.com
Blog : http://www.parenthese.be/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/backgroundrb-devel/attachments/20080725/0fe070c0/attachment.html>


More information about the Backgroundrb-devel mailing list