Ruby on Rails resque task gets error - ruby-on-rails

I'm trying to use resque in my ruby on rails application.
I created resque.rake file in lib/tasks folder
require 'resque/tasks'
task 'resque:setup' => :environment
I've started redis server by following line
redis-server /usr/local/etc/redis.conf
I have this RakeFile in my application:
require_relative 'config/application'
Rails.application.load_tasks
But when I run the following command to start rake
rake resque:work QUEUE='*'
I'm getting this error:
LoadError: cannot load such file -- resque/tasks
I can't see What I'm missing,
Any suggestions ?
Thanks.
Note: I'm using rails 5.0.1

In rails you can add this in your config/application.rb
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
Don't worry this is really easy to miss in the Rails doc's. It's just a small mention on this page
Go to this page and search for "config.autoload_paths" if you'd like to read more about it.
http://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoload-paths
Also depending on how you build the app (with or without documentation) you may see comments about this in the application.rb
Rails::Initializer.run do |config|
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{RAILS_ROOT}/extras )
config.load_paths << "#{RAILS_ROOT}/app/models/some_model_group"
config.load_paths << "#{RAILS_ROOT}/lib"
end

Related

Loading initializers during assets precompile

I am trying to precompile the assets while deploying to production environment. I am also trying to automate it for first time installation on a server using capistrano. It looks like none of the initializer's are getting loaded during the assets precompile process.
I am facing these two problems
For the first time installation I am generating a initializer file(initializers/freshinstall.rb) on the fly with the below content
config.assets.initialize_on_precompile = false
so that precompile doesn't check the database which doesn't exist yet
I also have some vendor files, and their locations are set in the asset pipeline, and are placed in separate initializer file initializers/vendor.rb
MyApp::Application.config.assets.paths << "#Rails.root}/vendor/assets/images/xxxx" << "# {Rails.root}/vendor/assets/images/xxxx/helpers"
MyApp::Application.config.assets.paths << "#{Rails.root}/vendor/assets/stylesheets/xxxx"
MyApp::Application.config.assets.paths << "#{Rails.root}/vendor/assets/stylesheets/yyyy" << "#{Rails.root}/vendor/assets/images/yyyy"
When capistrano runs the assets precompile task, its not able to find the vendor paths or stopping it from looking into the database. This brings me to a conclusion that the initializer's are not getting loaded. After going through some stackoverflow questions. I even added a railtie to config/application.rb
module AssetsInitializers
class Railtie < Rails::Railtie
initializer "assets_initializers.initialize_rails",
:group => :assets do |app|
require "#{Rails.root}/config/initializers/freshinstall.rb"
require "#{Rails.root}/config/initializers/vendor.rb"
end
end
end
But I still don't see any initializers getting loaded. Can I get some info on the internals of the boot process of an rail application, and also why the initializers are not getting loaded during the assets precompile process.
Some documentations would be really helpful to understand this. The Rails documentation is very minimal with respect to railtie and the initializer method.
http://guides.rubyonrails.org/configuring.html
Thank you in advance
Finally was able to figure out how to do it. This works for my single server capistrano deployment. Below is the capistrano task for my new_deploy on to a fresh server.
set :fresh_install, false
task :new_deploy do
set :fresh_install,true
deploy.setup
#The assets:precompile process is part of the deploy.update. Before the precompile process, we will create database.
deploy.update
deploy.migrate
run_seed
#load unicorn server
end
Dont do any configuration setting mentioned below
config.assets.initialize_on_precompile = false # no need of this in application.rb
Instead just before assets:precompile create your database
before "deploy:assets:precompile" , "yourapp:create_database"
See that you check if the fresh install flag is set, then only create the database.This method will also be called during regular deploy when ever you are updating the server with latest revision. During that scenario fresh_install flag will be false.
desc 'Create a new database'
task :create_database, :roles => :app do
if fresh_install
run "cd #{release_path}; bundle exec rake db:create RAILS_ENV=#{rails_env}"
end
end

Heroku not running migrations due to ruby module

I am unable to run migrations in Heroku, which I believe is due to a module I created in my lib directory. After executing the command heroku run rake db:migrate I receive the below error:
uninitialized constant ApplicationController::PgTools
/app/app/controllers/application_controller.rb:4:in <class:ApplicationController>
Line 4 of the Application controller is include PgTools, which is there to gain access to methods within the PgTools module I created.
Despite the heroku migration failing, I am able to run rake db:migrate in my local dev environments without fail (please note that both environments utilize postgres databases).
I also have the following two lines in my application.rb file
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
I resolved the error by renaming Pg_Tools.rb to pgtools.rb and modifying all include PgTools statements to include Pgtools
Links that I used in the troubleshooting process are shown below
Rails 3 library not loading until require
http://www.williambharding.com/blog/technology/rails-3-autoload-modules-and-classes-in-production/

Rails 2.2 - rake task loading gems after lib and so losing patches

In my lib folder i have various files which patch various core classes and gems in rails. When i run rails (unicorn, mongrel, the console), the gems get loaded first and then the patches in lib, so that the lib patches override the gems.
When i run a rake task, however, the lib files are loaded before the gems, meaning that the original gem methods override the patches and the patches are effectively ignored.
Does anyone know how i can fix this? My Rakefile just has this:
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
The rake task i'm running is set up like this:
desc "blah"
task :convert_worlds, [:new_instrument] => :environment do |t, args|
...
end
Grateful for any advice - Max

rake db:create issue

I have just started working on one project, and I have configured my system according to the requirement of the project i.e.
rails v => 2.3.11
ruby v => 1.8.7
When i run command "rake db:create" it comes up with error as
rake aborted!
no such file to load -- rake/rdoctask
/home/jeet/Desktop/Projects/myapp/Rakefile:8
here i am adding the content of Rakefile
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require(File.join(File.dirname(__FILE__), 'config', 'boot'))
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'tasks/rails'
I have checked the answer of this, but its not working for me.
Please suggest me a better solution
I have seen the same problem.
Possible solution: by adding gem 'rdoc' to my Gemfile and then run bundle install.

Installing gems tries to load the gems in my custom rake tasks before installing it

I'm developing an application using rails 2.3.5, gitorious and deploy with vlad, the OS is Linux Mint 9 Isadora.
vlad:setup and vlad:update are ok.
But when I vlad:migrate and have the same error than if I ssh on the server and try a rake gems:install
rake aborted!
no such file to load -- ya2yaml
/var/www/path/to/releases/20100622030150/Rakefile:10
(See full trace by running task with --trace)
My config/environment.rb is good:
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION
# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
Rails::Initializer.run do |config|
config.gem "haml"
I have a custom task in lib/tasks/db_fixtures.rake that requires ya2yaml:
namespace :export do
desc 'Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override.'
require 'rubygems'
require 'ya2yaml'
task :fixtures => [:environment] do
When I rename this file it is not loaded by rake and I don't have the error anymore when I rake gems:install
So my guess is that it looks like rake gems:install tries to load the libs in my custom tasks before installing the gems for some reason and throw me an error.
What would be a solution?
Thanks,
this might work? delay the requires on your rake task by moving them into the task itself
task :fixtures => [:environment] do
require 'rubygems'
require 'ya2yaml'
# ...
by default running rake tasks 'loads' all of the rake files

Resources