rubyiron.ioironworker

Inheritance within iron workers when using the iron_worker_ruby gem


I'm considering using IronWorker a project so that I can scale it easily (high traffic expected, with lots of background jobs).

In order to stay DRY, I'm trying to define workers using inheritance but I keep getting the following error:

/usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- base_worker.rb (LoadError)
    from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
    from /task/child_worker.rb:3:in `<top (required)>'
    from /task/runner.rb:344:in `require_relative'
    from /task/runner.rb:344:in `<main>'

Here is the base worker class:

#  app/workers/base_worker.rb
require 'net/http'
require 'uri'
require 'json'

class BaseWorker < IronWorker::Base
  attr_accessor :params

  # The run method is what IronWorker calls to run your worker
  def run
    data = custom_run(params)
    common_post_process(data)
  end

   def custom_run(params)
     #to be overwritten in the child class 
   end    

   def common_post_process(data)
      # some common post processing => DRY
      ....
   end       
end

And here is a child class :

# app/workers/child_worker.rb 
require 'net/http'
require 'uri'
require 'base_worker.rb'

class ChildWorker  <  BaseWorker
  merge "base_worker.rb"

  def custom_run(params)
     #custom work
  end

end

Any idea on how to fix this?


Solution

  • I'd recommend using our next generation gem, iron_worker_ng: https://github.com/iron-io/iron_worker_ruby_ng . The iron_worker gem is deprecated. And if you want to keep it similar style to what you have, your child_worker.rb might look like this:

    require 'net/http'
    require 'uri'
    require_relative 'base_worker.rb'
    
    class ChildWorker  <  BaseWorker
    
      def custom_run(params)
         #custom work
      end
    
    end
    
    # NG gem doesn't run anything in particular, so to run your method:
    cw = ChildWorker.new
    cw.custom_run(params)
    

    And in a child_worker.worker file:

    runtime 'ruby'
    
    file 'base_worker.rb'
    exec 'child_worker.rb'
    

    Then to upload it to IronWorker:

    iron_worker upload child_worker
    

    Then you can start queuing jobs for it:

    worker = IronWorkerNG::Client.new
    worker.tasks.create("child_worker", params)