uninitialized constant sidekiq worker - ruby-on-rails

New rails project.
Rails 5.0.2
Sidekiq 4.2.10
I ran rails g sidekiq:worker deposit_collector and then filled out the code I needed inside the perform method.
To test I logged into the rails console and typed: DepositCollector.perform_async and I get the error:
NameError: uninitialized constant DepositCollectorWorker
The worker is where it should be in the app/workers/ folder. I've used sidekiq on several projects before and have never run into this.

By default, Rails will include all subdirectories of the app folder in the autoload paths list. You can review the list of autoload paths in the console with:
puts ActiveSupport::Dependencies.autoload_paths
But Rails only looks for these paths at boot time. So when you add a new folder, like app/workers, it is not enough to restart the Rails console. You need to both exit the Rails console and stop the Spring Application Preloader with:
spring stop
Then start up the Rails console again and the files in the app/workers folder will load properly.

I didn't have spring installed so I took the lazy approach and turned on eager loading in my config/environments/development.rb file:
config.eager_load = true
and then turned it back to false after restarting my server.

Related

Rails 3 application errors in production mode

I have application on Rails 3. Everything works just fine, but as we know best performance should be on Production mode.
When I set environment as Production in Justhost control panel and in environment.rb file
with
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Darbs::Application.initialize!
ENV['RAILS_ENV'] ||= 'production'
And touch tmp/restart.txt
It gives me error:
Ruby (Rack) application could not be started
Error message:
Admin is not a class
And when I check current environment with Rail.env.production? it returns false.
I tried several restarts and so on without success.
in production log file I get Connecting to database specified by database.yml
It means some problem with connecting with specified database?
Any suggestions ?
Thanks.

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 - Undefined Method in Staging, but fine in local development

I'm working on a project and I added a migration to add a project_page_description field to a Company Model. When I've ran the migrations,
#company = Company.first
#company.project_page_description
works locally, but any time I attempt to access project_page_description after deploying, I get an undefined method error. Why does it work locally but not remotely?
I've deployed all code so the codebases are identical, I've ran all migrations, I'm using Bundler and Capistrano, and I'm deploying to a CentOS server.
Also, besides stop programming, what can I do to stop this from happening again?
Looks like the migrations have not been run. Maybe you ran them using the development environment? Try running them like so: RAILS_ENV=staging bundle exec rake db:migrate.
Check your bundler groups, application config, and environment initializer files, maybe you have something defined as being development-only and not staging? Also try opening a console up on staging and trying to manually load and invoke the module that's not found. Remember that require returns true if the module was not already loaded.

Loading rails env in production

I have deployed a new Rails site to a Linux VM using Capistrano. I am using nginx as the front end and running my Rails app using unicorn.
If I try to run rake routes on the server, I get an error telling me that Rails is not installed, even though Rails is installed. The problem seems to be that the gem search directory is different for the app and the logged in user.
How do I load the Rails environment that my app is seeing as the logged in user?
Just use:
RAILS_ENV=production bundle exec rake routes
The RAILS_ENV part sets your environment variable so your app is loaded in full production mode, including database settings and so on.
The bundle exec part is necessary so that any commands that come after that are executed within the environment of the gems installed in your Gemfile.

uninitialized constant during rake assets:precompile for lib class

Rails 3.2.2
When running rake assets:precompile I get the following error:
uninitialized constant Redirect
Redirect is a custom middleware class that redirects naked domain requests from mydomain.com to www.mydomain.com.
I load the middleware in production.rb using:
config.middleware.use Redirect
The redirect.rb is located at lib/middleware/redirect.rb. I load the path in application.rb using:
config.autoload_paths += %W(#{config.root}/lib/middleware)
It works fine when you run the application, and other rake tasks run fine. But running rake assets:precompile appears to not load the lib properly. I first noticed the issue running on Heroku, but I've been able to reproduce locally no problem.
Any ideas? Thanks!
You probably have config.assets.initialize_on_precompile = false set somewhere.
I experienced this error after setting that configuration for something related to Heroku. To fix, i just require "#{Rails.root}/lib/my_middleware.rb" right above the line where I configure the app to use the middleware.
I was getting the same error for loading a class from /lib and assigning it to a ::GLOBAL variable.
This was because I had forgotten to place it inside an after_initialize block, which is how I had done it in development.
config.after_initialize do
::GLOBAL = MyLib::MyClass.new
end
Hope this helps somebody!

Resources