I'm a bit of a backgroundrb noob, though i do use it for several different
things.  I usually follow the paradigm of keeping the controller code as
similar as possible, but instead of calling the original model method,
calling a new model method which calls a backgroundrb task, which in turn
calls the *original* model method.  eg instead of this

-controller calls 'my_obj.do_heavy_lifting'
-instance method do_heavy_lifting runs and returns
-controller continues

I do this

- controller calls 'my_obj.background_do_heavy_lifting'
- instance method background_do_heavy_lifting runs:it just calls a worker
method and returns
- controller continues
- worker method (in the background) calls 'my_obj.do_heavy_lifting'
-instance method do_heavy_lifting runs (in the background) and returns

Of course this assumes that do_heavy_lifting can be done asynchronously, ie
that the controller can carry on without waiting for it to finish.  But i
assume that's the case because otherwise why use backgroundrb?

BTW your code for adding a record to the database is kind of horrible and
very error prone - why can't you use AR::Base.create?


2009/10/14 Chris Sund <[email protected]>

> Hey Everyone,
>
> I'm trying to get backgroundrb running and I would consider myself an
> intermediate rails programmer. I have the following controller method that I
> would like to move to a worker method, but I'm not sure how to properly call
> it from my controller once I place it in the worker method. Any advice or
> help would be appreciated. Due to the fact I am still learning, code
> examples would be awesome, but not necessary. I can usually get things
> figured out given enough time.
>
> Thanks!
>
> Chris
>
>
> def download_reads
>
>     if fields_mapped?
>          mapped_fields.each do |row|
>
>            ActiveRecord::Base.connection.execute(
>                    "insert into readings(reading,
>                                             date,
>                                             year,
>                                accounting_period,
>                                       account_id,
>                                       water_usage,
>                                       created_at,
>                                       updated_at)
>                               values ('#{row[0]}',
>                                       '#{row[1]}',
>                                       '#{row[2]}',
>                                       '#{row[3]}',
>                                       '#{row[4]}',
>                                       '#{row[5]}',
>                                       '#{Time.current.to_s(:db)}',
>                                       '#{Time.current.to_s(:db)}');")
>
>          end
>          flash[:notice] = 'Water Readings Have Been Downloaded'
>         redirect_to :action => :download
>        else
>
>        end
>      rescue MapFields::InconsistentStateError
>        flash[:error] = 'Please try again'
>        redirect_to :action => :download
>      rescue MapFields::MissingFileContentsError
>        flash[:error] = 'Please upload a file'
>        redirect_to :action => :download
>
>    end
>
>
> _______________________________________________
> Backgroundrb-devel mailing list
> [email protected]
> http://rubyforge.org/mailman/listinfo/backgroundrb-devel
>
_______________________________________________
Backgroundrb-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/backgroundrb-devel

Reply via email to