brandonhilkert / sucker_punch

Sucker Punch is a Ruby asynchronous processing library using concurrent-ruby, heavily influenced by Sidekiq and girl_friday.
MIT License
2.64k stars 114 forks source link

passing thread-locales #96

Closed phoet closed 9 years ago

phoet commented 9 years ago

i'm playing around with ActiveJob and sucker_punch and i'm running into some issues with thread-locales that my app use for configuration.

there are two different aspects here:

both libraries have their state in a thread-locale that i would like to pass along with an active-record object to be worked on in a job. unfortunately i did not find a good point to hook into sucker-punch/active-job to do this. do you have some recommendations?

here is what happens:

before_enqueue #<GeoJob:0x007fb38bfe8328> label: hamburg
around_enqueue-1 #<GeoJob:0x007fb38bfe8328> label: hamburg
block.call -->
     ActiveJob::DeserializationError:
       Error while trying to deserialize arguments: set a label before calling 'label_id'
     # ./app/models/location.rb:18:in `block in <class:Location>'
     # (celluloid):0:in `remote procedure call'
     # (celluloid):0:in `remote procedure call'
     # ./app/jobs/geo_job.rb:9:in `call'
     # ./app/jobs/geo_job.rb:9:in `block in <class:GeoJob>'
     # ./app/models/location.rb:35:in `perform_geocoding'
     # ./spec/models/location_spec.rb:6:in `block (2 levels) in <top (required)>'

the error is thrown from a default_scope that has access to a thread-locale that would need to be properly initialized before getting called.

brandonhilkert commented 9 years ago

Can you not pass the values in to the job?

phoet commented 9 years ago

@brandonhilkert i can pass the values into the job, but i'd have to pass the entity per id + class instead of passing the serialized active-record model directly.

brandonhilkert commented 9 years ago

Passing through IDs is a safer approach https://github.com/mperham/sidekiq/wiki/Best-Practices

I don't know why thread locals wouldn't work, but it's also hard to say without seeing the application.

phoet commented 9 years ago

thread locales aren't transferred to a different thread, so one would need to pass the data and initialize it at some point before they can be accessed. I'll try to build a minimal example to make it clear. Am 29.01.2015 20:25 schrieb "Brandon Hilkert" notifications@github.com:

Passing through IDs is a safer approach https://github.com/mperham/sidekiq/wiki/Best-Practices

I don't know why thread locals wouldn't work, but it's also hard to say without seeing the application.

— Reply to this email directly or view it on GitHub https://github.com/brandonhilkert/sucker_punch/issues/96#issuecomment-72087350 .

phoet commented 9 years ago

it looks like this is properly addressed in rails-master with serialize and deserialize hooks for ActiveJob.