I'm missing something, just not sure what. Sidekiq is up and running fine, I can see it in my terminal.
I have this worker, defined in app/workers/sqs_worker.rb
class SqsWorker
include Sidekiq::Worker
require 'aws-sdk'
def perform
#do something
end
end
And then in just a test file at app/test.rb I have the very simple code:
require 'sidekiq'
SqsWorker.new.perform_async
When I run the test.rb file I get this error: uninitialized constant SqsWorker (NameError)
Where did I go astray? I'm running Sidekiq (4.1.4)
I tried killing the running processes and restarting both Sidekiq and Redis to no luck.
uninitialized constant SqsWorker (NameError) indicates that your script in test.rb is not able to locate class SqsWorker
All you need to do is replace require 'sidekiq' with require_relative 'workers/sqs_worker' to make your script aware about location of SqsWorker class.
Probably, you ran the test.rb from outside of the scope of the application with something like that:
ruby app/test.rb
But for this purpose, You need to add to your test something like this:
require 'rubygems'
require 'bundler/setup'
require File.expand_path('../config/environment', __FILE__)
SqsWorker.new.perform_async
And run as this:
bundle exec ruby app/test.rb
Why do you need this? Because nowadays, the bundler manages your dependencies added to your app and therefore, you need to load the rails environment too and the last will load all the things under app/ basically.
Related
I have a repo for multiple deployment with using capistrano-multiconfig gem. The problem is that for some applications I want to require sikideq tasks. Something like this:
if fetch(:sidekiq_enabled?)
require 'capistrano/sidekiq'
require 'capistrano/sidekiq/monit'
end
I guess that Capfile is loaded before the capistrano configs. How to deal with this?
I was in the same situation, I wanted to require some gems per project and since my command line pattern is always the same to execute capistrano tasks, this is how I deal with it.
My pattern is like this:
$ bundle exec cap project:stage namespace:task
The solution was for me to get the first argument (here project) to know what kind of project it is and require the good recipe for the project.
So in Capfile, I write something like this
[...]
# Get project from the command line if any
current_project = ARGV[0].split(':')[0]
magento_projects = ["project_1"]
symfony_projects = ["project_2"]
# Include required tasks per project
if magento_projects.include?(current_project)
require 'capistrano/magento'
end
if symfony_projects.include?(current_project)
require 'capistrano/symfony'
end
[...]
Now when I type :
$ bundle exec cap project_1:staging deploy
Or :
$ bundle exec cap project_2:production deploy:check
It'll require different gems. Done :)
The question title pretty much sums it up, but here's a more chronological description:
I started a new rails 3.2.9 app, did not pass any special options (ie. did not skip test unit).
I added minitest-rails to the gemfile and ran bundle install.
I deleted the contents of the test folder, and ran rails g mini_test:install.
Now if I run rake test, nothing happens.
I can make my own rakefile and specify TestTask manually, but I don't get the options to do things like rake test:controllers that are supposed to come built-in unless I manually dupe all that.
Has anyone else run into this?
Make sure you add require 'test_helper' on top of your test file. e.g.
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
test "should pass" do
assert true
end
end
The auto generated test_helper file I have looks like that:
ENV["RAILS_ENV"] ||= "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
class ActiveSupport::TestCase
ActiveRecord::Migration.check_pending!
fixtures :all
end
Glad you are making the switch to MiniTest! I may be able to help you get on the right track.
Honestly, I would avoid rake entirely. Try running a test from the command line to make sure your testing suite is working.
ruby -Itest test/unit/something.rb
After you know your tests are passing then get guard-minitest and set it up to watch your files. When you save a change it will automatically run the test for you. The worst part of minitest and guard is the set up but once you get it going right you'll never want to go back.
https://github.com/guard/guard-minitest
Cheers
I guess you had not run/generate any controller or scaffold command so far.
Once you create a scaffold / controller / model and migrate the database your rake test will start working
Regarding rake test:controllers, when I tried to list out with rake -T it is not still listing
You may need to register minitest-rails as the default testing engine by adding the following to your config/application.rb file:
config.generators do |g|
g.test_framework :mini_test
end
After that, you can run controller tests with the following:
rake minitest:controllers
I'm having problems with the syntax for the Clockwork scheduler process. I'm actually having similar issues to what is discussed in this thread but never fully answered(How do I use Rails clockwork gem to run rake tasks?)
My 'scheduler.rake' is working correctly when I test it with a 'heroku rake send_notifications'. My clock.rb process is working too as it will trigger every 30 seconds. However, I'm having issues with the clock.rb's syntax to correctly run the 'send_notifications' task in my 'rake.scheduler'. Here's what it looks like:
# scheduler.rake
desc "This task is called by the Heroku scheduler add-on"
task :send_notifications => :environment do
puts "this test is working correctly!"
end
Here's what clock.rb looks like:
require File.expand_path('../../config/boot', __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'clockwork'
include Clockwork
every(30.seconds, 'Send notifications') {
# Heroku::API.new.post_ps('pocket-pal', 'rake scheduler:send_notifications')
rake scheduler:send_notifications
}
As you can see I've tried with a detached process using the Heroku API and invoking rake.
When I use the Heroku API, I get this error:
ERROR -- : uninitialized constant Heroku (NameError)
When I invoke rake, I get the following error:
ERROR -- : undefined local variable or method `send_notifications' for main:Object (NameError)
Does anyone know what the correct syntax is for either approach?
This answer works, however you need to follow its string syntax exactly. So in your case:
every(30.seconds, 'Send Notifications') {
`rake scheduler:send_notifications`
}
That means making sure to use ` ` for wrapping the rake task, not " " as that's tripped a few people up.
The docs from Hereko on implementing clockwork might help
Scheduled Jobs and Custom Clock Processes in Ruby with Clockwork
When I try to run my spec, I get an uninitialized constant error. My spec looks like this:
describe Facility do
it { should have_many(:units) }
it { should have_many(:facilities_users) }
it { should have_many(:administrators) }
it { should have_many(:facility_employees) }
end
The error is:
facility_spec.rb:1:in `<top (required)>': uninitialized constant Facility (NameError)
I certainly have a Facility model, so I'm not sure why this would happen.
You should try running rake spec instead of rspec spec.
But both may work.
If not working try Try bundle exec rspec spec or bundle exec rake spec.
Source: When trying to run rspec I get uninitialized constant.
Add the following at the top of your file:
require 'spec_helper'
If you are using the 'rspec-rails' gem, then run
rails g rspec:install
This will create the spec/spec_helper.rb file (you should edit it if you're not using ActiveRecord so it runs you spec setup correctly).
After that, ensure you are requiring the helper at the top of your spec files:
require 'spec_helper'
If this didn't work for you, there might be more issues like:
You're trying to test a file under the lib/ directory. In this case,
make sure this file is loaded with the environment
(config/application.rb -> autoload_paths) or require it explicitly.
The constant actually doesn't exist. It could be inside a namespace or just a typo.
In the spec file, require the file where Facility class is defined.
In schedule.rb file, the statement:
require "#{RAILS_ROOT}/config/environment.rb"
every "10 10 2 * * *" do
command "mysqldump -u #{#db_username} -p#{#db_password} --single-transaction #{#db_name} > #{#backup_Path}/#{#db_name}.sql 2> log/error_crontab.log"
end
When i try to execute the whenever cmd from terminal, getting the following error:
config/schedule.rb:48:in `initialize': uninitialized constant Whenever::JobList::RAILS_ROOT (NameError)
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `instance_eval'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/job_list.rb:19:in `initialize'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `new'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever.rb:16:in `cron'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:40:in `run'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/lib/whenever/command_line.rb:7:in `execute'
from /usr/local/lib/ruby/gems/1.9.1/gems/whenever-0.7.0/bin/whenever:38:in `<top (required)>'
from /usr/local/bin/whenever:19:in `load'
from /usr/local/bin/whenever:19:in `<main>'
i am using the require statement to get the dynamic values from the form to schedule the job. Please help to solve this issue?
Note: i have seen the following stackoverflow queries:
How to detect Rails environment inside whenever
Following this thread to get dynamic values, but facing problem with require statement.
Rails - Whenever gem - Dynamic values
Ruby/Rails - Whenever gem - Loop cron tasks
config file in schedule.rb with Rails Whenever gem?
Whenever doesn't require or depend on Rails at all, so when it runs, RAILS_ROOT is not defined, however because whenever's schedule.rb is generally kept in /config/schedule.rb, we can make an assumption that it is in a rails project, and set our own RAILS_ROOT like this:
# in schedule.rb
RAILS_ROOT = File.dirname(__FILE__) + '/..'
Edit: in the case that you actually need Rails loaded, do this:
# in schedule.rb
# this will require config/environment and load your entire rails environment
require File.expand_path(File.dirname(__FILE__) + "/environment")
The whenever developer already answered this question, check this out https://github.com/javan/whenever/issues/81
Javan Whenever no longer attempts to load your Rails environment. However, it does automatically set a path variable to the directory whenever was executed from. This should work just the same:
set :output, "#{path}/log/cron.log"
In Rails 4 try with:
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
in your schedule.rb file.
This way you also have access to all your active-record models and initializers.