Skip to content
treeder edited this page Mar 23, 2012 · 1 revision

Merging Models, Classes and Code

If you need to use other Ruby files in your worker, you'll want to bring them into your worker using the merge command. This is useful for models, ruby code files, .erb templates, and other files your worker will need.

class MyWorker < IronWorker::Base

  merge "../models/user.rb"
  merge "../models/account.rb"
  merge "../../lib/user_setup.rb"
  merge "../templates/user_mailer/welcome_message.html.erb"
  merge "../templates/user_mailer/welcome_message.text.erb"    
  ...

The models, files or folders will appear at the top level of user_dir .

Notes:

  • The merge command not only brings a .rb file into the bundle to upload to IronWorker, it will also require it before loading the worker.
  • You want to use the merge commands here and not load commands for files, folders, and workers. Nested folder structure are placed at the same top leve within IronWorker. (./site_stats/client.rb => ./client.rb within IronWorker)
  • Note that you can use the is_local? and is_remote? commands to determine if your worker is running locally or remotely in IronWorker.
  • Templates are common to merge but image files and other media files should probably be stored in a datastore like AWS S3 and then retrieved by your worker. (There's an example of this in the IronWorker examples on Github.)

Merge Order and Class Dependencies

It's important to merge files in the correct order of merging in order to take into account dependencies and class hierarchy. (For gem dependencies, see the merge_gem page.) For example, if you have a module that several classes, one or more of which inherit from one of the classes.

module ImageProcessors
  class ImageProcessor
  class ColorCorrectionProcessor < ImageProcessor 

Then you will want to merge the files in the order of their hierarchy. Otherwise, you'll receive "uninitialized constant" and other errors when you upload or run the worker.

# It's important to merge in the order of dependency.
merge 'image_processor.rb'
merge 'color_correction_processor.rb'

File Access Within IronWorker

The files will be in the user_dir directory that is a part of your process sandbox within IronWorker. For the merge actions above, the user_dir folder would contain the following:

> user_dir (on IronWorker)
user_dir/
  ...
  account.rb
  ...
  user.rb
  welcome_message.html.erb 
  welcome_message.text.erb

Note that you can create directories and perform file operations as you would in your app environment.

Dir.pwd

FileUtils.mkpath "templates/user_mailing"
FileUtils.mkdir_p "templates/user_mailing"

FileUtils.mv "welcome_message.html.erb", new_dir_full

...

Tip: You can list the files using the following code within a worker.

user_files = %x[ls #{user_dir.inspect}]
log "#{user_files}"

Unmerging Code

And just as you can merge, you can also unmerge. Unmerge is generally useful in Rails since it merges some things automatically, but you may not want them:

class MyWorker < IronWorker::Base

  unmerge "../models/user"

Merging Other Files

You can also merge non-Ruby files with the same merge command.

merge 'config.yml'

Then you could load this config in your worker by using:

@config = YAML.load_file('config.yml')

merge - will add file to bundle and require it before loading worker merge_folder will just add whole folder to bundle and will not require anything

Merging Folders

To merge folders, you can use the merge_folder command to bring in sets of .rb files that might be used by your workers. For example, to merge your models outside of Rails, you can do the following:

merge_folder '../models/'

This will pick up all the .rb files in the top level of that folder. (The merge command will not only add an .rb file to the bundle, it will also require it before loading the worker. The merge_folder command will just add a folder of .rb files to the bundle but will not performa a require them.) The files will appear at the top level of the user_dir directory (in other words, the directory structure is not preserved).

For nested folders, merge them in individually using merge_folder. For non .rb files, use the merge command with the file extension.

# Merge sub_folders individually.
merge_folder '../models/models_sub_dir/

# And non .rb files as well.
merge '../templates/user_mailer/welcome_message.html.erb' 
merge '../templates/user_mailer/welcome_message.text.erb'

Troubleshooting

  • You can use the Code tab to take a look at what gets included in your worker. By downloading the files that have been uploaded and inspecting them you can determine what might be missing. (You can also look in user_dir on IronWorker but that entails running the worker and logging the contents.)
  • If you change a model or other merged file, then you may need to modify your worker to make sure the package is reuploaded. (Workers and code libraries are cached within IronWorker. They are automatically re-uploaded when the worker changes but changes in merged files may not automatically trigger the upload.)
  • If you're using ActiveRecord then you'll want to merge the gem in prior to merging in your models.
# Merge the activerecord gem in prior to the models
merge_gem 'activerecord', :require => 'active_record'
merge_folder "../models/"

More Information on Merging Gems, Files, Folders, Mailers, and Workers

If you have any questions on how merging works, you can inspect the IronWorker gem directly.

You can also connect with us at Iron.io Support.