Passing a Block to a delayed_job
block, delayed-job, ruby, ruby-on-rails
Solution
I found a method of doing this. It is kind of hacky but works well. I found this article that talks about creating a SerializableProc class. If I pass this to the function then everything works great.
Problem
I have a function that is marked to be handled asynchronously by delayed_job: ``` class CapJobs def execute(params, id) begin unless Rails.env == "test" Capistrano::CLI.parse(params).execute! end rescue site = Site.find(id) site.records.create!(:date => DateTime.now, :action => "Task Failure: #{params[0]}", :type => :failure) site.save ensure yield id end end handle_asynchronously :execute end ``` When I run this function I pass in a block: ``` capjobs = CapJobs.new capjobs.execute(parameters, @site.id) do |id| asite = Site.find(id) asite.records.create!(:date => DateTime.now, :action => "Created", :type => :init) asite.status = "On Demo" asite.dev = true asite.save end ``` This works fine when run without delayed_job but when run with it I get the following error ``` 2012-08-13T09:24:36-0300: [Worker(delayed_job host:eagle pid:12089)] SitesHelper::CapJobs#execute_without_delay failed with LocalJumpError: no block given (yield) - 0 failed attempts 2012-08-13T09:24:36-0300: [Worker(delayed_job host:eagle pid:12089)] PERMANENTLY removing SitesHelper::CapJobs#execute_without_delay because of 1 consecutive failures. 2012-08-13T09:24:36-0300: [Worker(delayed_job host:eagle pid:12089)] 1 jobs processed at 0.0572 j/s, 1 failed ... ``` It seems not to pick up the block that was passed in. Is this not the correct way of doing this or should I find a different method?