A couple of problems that jump out regarding accessing/using session
data in that strategy you outlined:

 - the background threads/processes might end up using invalid session
data if the session data changes during the time from when you pass in
that session_id and when some background thread/process fetches that
session data from the db.

 - you're making multiple round-trips to the db by having each of the
background threads/processes fetch the session data from the db when
they could just access the data by having it passed in at the time
they are called/launched.

A safer/more-robust alternative would be to just pass in a copy of the
user's session data to each of the background workers to work with:

  ...
  ### in ./app/workers/foo_worker.rb
  class FooWorker < Workling::Base
    def bar(options)
      session_data = Session.find(options[:session_data])
      # do something with session_data hash ...
    end

    ...
  end

  ### in ./app/workers/biz_worker.rb
  class BizWorker < Workling::Base
    def baz(options)
      session_data = Session.find(options[:session_data])
      # do something with session_data hash ...
    end

    ...
  end

  ### in some_meth in ./app/controllers/some_controller.rb
  ...
  sd = session.data
  FooWorker.asynch_bar(:session_data =>sd.merge({}))
  BizWorker.asynch_baz(:session_data =>sd.merge({}))
  ...

Jeff

On May 25, 7:01 am, PierreW <wamre...@googlemail.com> wrote:
> Jeff, thanks a lot.
>
> Just one thing though: if I do this, does it mean I am taking the
> session data "out of" ActiveRecord? I was thinking that by only
> passing the session ID and by letting the worker retrieve the record
> in the DB, consistency would be guaranteed by ActiveRecord. But maybe
> I am getting this wrong?
>
> Indeed, I need to do quite a lot of things on this session data in the
> background: several workers are going to work on it at the same time,
> and one of them will have to create some threads… I am struggling to
> understand whether there will be concurrency risks - therefore I
> thought I would try and rely on ActiveRecord as much as possible.
>
> Here is in pseudo-code what I had in mind:
>
> ### in ./app/workers/foo_worker.rb
>   class FooWorker < Workling::Base
>     def bar(options)
>       session = Session.find(options[:session_id])
>       # do something with session.data hash ...
>     end
>   end
>
> ### in ./app/workers/foo_worker2.rb
>   class FooWorker2 < Workling::Base
>     def bar(options)
>       for i in 1..10
>         Thread.new(options[:session_id]) { |session_id| retrieve
> session data and do stuff }
>       end
>     end
>   end
>
> ### in some_meth in ./app/controllers/some_controller.rb
>   ...
>   FooWorker.asynch_bar(:session_id =>session.id)
>   FooWorker2.asynch_bar(:session_id =>session.id)
>   ...
>
> Am I getting it wrong?
>
> Thanks a lot!
> Pierre
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonrails-t...@googlegroups.com.
> To unsubscribe from this group, send email to 
> rubyonrails-talk+unsubscr...@googlegroups.com.
> For more options, visit this group 
> athttp://groups.google.com/group/rubyonrails-talk?hl=en.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-t...@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to