Dave Hoefler
2006-Jul-24  20:22 UTC
[Backgroundrb-devel] Mongrel + BackgrounDRb + File Column = Upload Progress Bar?
Hi there,
I''ve been digging around trying to find some information (mostly
examples)
on how one would handle a progress bar upload using BackgrounDRb and File
Column. I did read http://backgroundrb.rubyforge.org/ and Ezra''s blog
of
course, but I still have a few questions.
I made a simple upload form (as a test) that is submitted to the
"upload_song" action.
  def upload_song
    @song = Song.new(params[:song])
    @song.member_id = session[:member_id]
    @song.save
    redirect_to :action => ''edit_songs''
  end
The question is, do I create the session[:job_key] within this method like
so:  ??
  def upload_song
    # I know my :args is probably wrong. What is the correct argument for
args in this case?
    session[:job_key] = MiddleMan.new_worker(:class => :upload_worker, :args
=> "Uploading song...")
    @song = Song.new(params[:song])
    @song.member_id = session[:member_id]
    @song.save
    redirect_to :action => ''edit_songs''
  end
In my view, within the form parameters I have:
<%periodically_call_remote(:url => {:action =>
''get_progress''}, :frequency =>
1) %> which calls the below method:
def get_progress
    if request.xhr?
      progress_percent = MiddleMan.get_worker(session[:job_key]).progress
      render :update do |page|
        page.call(''progressPercent'',
''progressbar'', progress_percent)
        page.redirect_to ( :action => ''done'')   if
progress_percent >= 100
      end
    else
      redirect_to :action => ''index''
    end
  end
  def done
    MiddleMan.delete_worker(session[:job_key])
    redirect_to :action => ''edit_songs''
  end
Just for clarity sake, here''s my upload_worker class:
class UploadWorker < BackgrounDRb::Rails
  attr_reader :progress
  def do_work(args)
    @progress = 0
    start_working
  end
  def start_working
      Thread.new do
        while @progress < 100
          sleep rand / 2
          a = [1,3,5,7]
          @progress += a[rand(a.length-1)]
          if @progress > 100
            @progress = 100
          end
        end
      end
    end
  def progress
    puts "Rails is fetching progress: #{@progress}"
    Integer(@progress)
  end
end
Are there any good tutorials out there that walk through the implementation
of backgroundrb (specifically covering file uploads)? Am I close to getting
this thing to work?
Thank you,
Dave Hoefler
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/backgroundrb-devel/attachments/20060724/e8a870c4/attachment-0001.html
