Search code examples
ruby-on-railsrubygemsrake

Local rake tasks require development gems to be in production group


I have some rake tasks that I only perform locally. However since I refer to the some gem classes in rake tasks, this seems to require that the gem be installed and loaded on the production server. Among other things this increases deploy time and memory usage on the server.

This may or may not have to do with my setting:

config.autoload_paths += Dir["#{config.root}/lib/**/"]

I have a number of lib files and subdirectories.

I guess my options are to

  • move all my lib files somewhere else and add that path to autoload_paths
  • try to exclude the tasks dir from autoload_paths
  • do something fancy in the rake tasks themselves (if possible) to avoid the need to have the gems present.
  • configure rake tasks to live somewhere else (seems like a bad idea)

This seems like a pretty common issue and probably has a common way to solve it or avoid it. What am I missing?


Solution

  • Maybe not the most exciting answer but I just moved the require 'dev_gem' inside the rake task block for that task.

    namespace :elasticbeanstalk do
      desc 'Creates a new web & worker environment pair for testing'
      task :create do
        require 'aws-sdk-elasticbeanstalk'
    
         # Do stuff with beanstalk that we wouldn't from a production env
      end
    end
    

    This way the library only gets loaded when the rake task is invoked rather than when the rake task is defined.