I'm using Sidekiq 3.1.2 without Rails like this:
$ sidekiq -vr sidekiq.rb
sidekiq.rb looks like this:
($LOAD_PATH << '.' << 'lib' << 'lib/workers').uniq!
require 'lookup_worker'
lib/workers/lookup_worker.rb looks like this:
require 'sidekiq'
class LookupWorker
include Sidekiq::Worker
def perform(*args)
puts "LookupWorker#perform fired with arguments #{args.map(&:inspect).join(', ')}"
end
end
But when I'm in irb and try
> LookupWorker.perform_async('asdf')
it gives me this:
WARN: {"retry"=>true, "queue"=>"default", "class"=>"LookupWorker", "args"=>["asdf"], "jid"=>"8c278868c5f05ec9beb1dbae", "enqueued_at"=>1402457226.9612548}
WARN: uninitialized constant LookupWorker
WARN: [backtrace, none of it from my code]
ERROR: Sidekiq::Processor crashed!
NameError: uninitialized constant LookupWorker
What am I missing?
So...
In LookupWorker, require was getting confused between sidekiq the gem and sidekiq the script on line 1.
My sidekiq.rb needed to be renamed as sidekiq_script.rb (or anything else really). Only gotcha is, I have to include the directory when running sidekiq:
$ sidekiq -r ./sidekiq_script.rb
not
$ sidekiq -r sidekiq_script.rb
Well I feel slightly stupid for that.
Related
I'm trying to do the first example of The rspec book
greeter_spec.rb
class RSpecGreeter
def greet
"Hello RSpec!"
end
end
describe "RSpec Greeter" do
it "should say 'Hello RSpec!' when it receives the greet() message" do
greeter = RSpecGreeter.new
greeting = greeter.greet
greeting.should == "Hello RSpec!"
end
end
When I run $ rspec greeter_spec.rb the output should be something like this:
.
Finished in 0.00075 seconds
1 example, 0 failures
but I got:
Could not find 'rspec/autorun'
This may happen if you're using rubygems as your package manager, but it is not
being required through some mechanism before executing the rspec command.
You may need to do one of the following in your shell:
# for bash/zsh
export RUBYOPT=rubygems
# for csh, etc.
set RUBYOPT=rubygems
For background, please see http://gist.github.com/54177.
I tried to include require 'rspec/autorun' at the top of the file but doesn't work, also I did what they suggest on the output but still not working.
I'm using ruby version 2.0.0p648, and rspec 2.0.0
This is a stripped down example of a real app I am building. When I execute my app, this is the result I get. You'll notice that it says that it is running before it is starting. You'll also notice it never says running after the start is issued.
bundle exec rackup
Using thin;
Sapp::App running.
Starting Sapp::App
== Sinatra (v1.4.7) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.7.0 codename Dunder Mifflin)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop
My config.ru is:
# http://www.rubydoc.info/gems/webmachine/Webmachine/Adapters/Rack
$started = false
require 'thin'
require 'sinatra'
set :server, (ENV['RACK_ENV'] == 'production' || ENV['RACK_ENV'] == 'staging' ? 'rack' : 'thin')
puts "Using #{settings.server};"
load 'webmachine/adapters/rack.rb'
load File.join(File.dirname(__FILE__), 'sapp.rb')
$started = true
puts 'Starting Sapp::App'
#Sapp::App.run!
Sinatra::Application.run!
I am setting $started just to try to fix this problem, but it doesn't help. My app is executed before it is set. I could control that but, and this is the rub, it does not execute after the run is issued.
sapp.rb is:
ENV['RACK_ENV'] ||= 'development'
Bundler.setup
$: << File.expand_path('../', __FILE__)
$: << File.expand_path('../lib', __FILE__)
require 'dotenv'
Dotenv.load(
File.expand_path("../.env.#{ENV['RACK_ENV']}", __FILE__),
File.expand_path("../.env", __FILE__))
module Sapp
class App < Sinatra::Application
puts 'Sapp::App has been started.' if $started
puts 'Sapp::App running.'
end
end
In the end, if nothing else, Once it says "Starting Sapp::App", it should also say "Sapp::App has been started." and "Sapp::App running."
For the record, both these options do the same thing:
Sapp::App.run!
Sinatra::Application.run!
Okay, I get it. I put the code in a class, but not it a method. Load or require both run open code like this. I need to wrap it in methods, and execute the methods, to do what I want to do.
Sinatra examples, which I followed, don't make this clear and simply avoid the topic. Many are so simple it doesn't make a difference, and some are just coded within the config.ru. I am coming from Rails and, while I knew this from Rails, it didn't make much of a difference since the vast majority of the code already exists in methods.
I've tried using Resque before and was met with unmitigated failure. I'm revisiting it again with the same results...
resque.rake:
require "resque/tasks"
task "resque:setup" => :environment
test.rb:
require 'resque'
class FileWorker
#queue = :save_to_file
def self.perform(str)
File.open('./' + Time.now.to_s + '.txt', 'w+') do |f|
f << "test 123"
end
end
end
Resque.enqueue(FileWorker, "12345567".split('').shuffle.join)
Gemfile:
gem 'resque'
gem 'rake'
It seems like running test.rb on its own successfully queues the job:
However, running rake resque:work QUEUE='*' in the same folder results in a warning,
WARNING: This way of doing signal handling is now deprecated. Please
see http://hone.heroku.com/resque/2012/08/21/resque-signals.html for
more info.
As well as the task being added to "failed" queue with the following reason: "exception":"NameError","error":"uninitialized constant FileWorker"
How do I get this to work? Seems like something quite obvious but there's tons of tutorials about Resque spanning many years - some painfully out of date and none explaining how to run workers so they don't fail.
Thanks in advance.
When you enqueue a task with Resque, what is stored on Redis is just the name of the job class (as a string) along with the arguments (again as strings) in a JSON object.
When a worker then tries to perform the task, it needs to be able to create an instance of the job class. It does this by using const_get and const_missing. This is where the error you are seeing occurs, since the worker does not have the definition of FileWorker available to it.
The error is the same as if you tried to get an unknown constant in irb:
> Object.const_missing "FileWorker"
NameError: uninitialized constant FileWorker
The solution is to make sure the definition of FileWorker is available to your workers. The simplest way to do this would be to just require test.rb from your Rakefile (or resque.rake). In your code this would involve adding another task to the queue, so you might want to move the FileWorker code to its own file where it can be required by both the rake file and the code enqueuing jobs.
test.rb:
require 'resque'
require './file_worker'
Resque.enqueue(FileWorker, "12345567".split('').shuffle.join)
Rakefile (note the :environment task only makes sense if you are using Rails and will give errors otherwise):
require "resque/tasks"
require "./file_worker"
file_worker.rb:
class FileWorker
#queue = :save_to_file
def self.perform(str)
File.open('./' + Time.now.to_s + '.txt', 'w+') do |f|
f << "test 123"
end
end
end
Now the workers will be able to create instances of FileWorker to complete the tasks.
The way to avoid the warning about signals is given in the page linked to in the message. Simply set the environment variable TERM_CHILD when calling rake:
$ rake resque:work QUEUE='*' TERM_CHILD=1
I have created a rake file in my Sinatra app to create indexes for a Mongodb collection and I am trying to pass the environment parameter in the rake task db:create_indexes.
Here is my db.rake file:
namespace :db do
task :create_indexes, :environment do |t, args|
puts "Environment : #{args}"
unless args[:environment]
puts "Must provide an environment"
exit
end
yaml = YAML.load_file("./config/mongoid.yml")
env_info = yaml[args[:environment]]
unless env_info
puts "Unknown environment"
exit
end
Mongoid.configure do |config|
config.from_hash(env_info)
end
Bin.mongoid:create_indexes
end
end
Also the Rakefile in the root of app contains:
require 'rake'
require 'rubygems'
require 'bundler/setup'
Dir.glob('lib/tasks/*.rake').each { |r| load r}
But whenver I try to run the rake task using the command rake db:create_indexes[development], I get the following error, no matches found: db:create_indexes[development]
Now I am clueless about how to solve this issue.
So the problem was not with the code but the shell I was using.
I use zsh shell in place of bash and it seems zsh require you to escape the brackets: rake my_task\['arg1'\].
Therefore the code works with rake db:create_indexes\['development'\].
I have a Ruby script that is trying to require the restclient module. When I reduce it down to just this one line, it still fails:
#!/usr/bin/env ruby
require 'restclient'
When I run it, I get the following error:
./test.rb:3:in `require': no such file to load -- restclient (LoadError)
from ./test2.rb:3
When I run irb, the module loads fine:
$ irb
>> require "restclient"
=> true
>>
As far as I can tell, it looks like both the script and irb have the same module paths:
$ ruby -e "puts $:"
/Library/Ruby/Site/1.8
/Library/Ruby/Site/1.8/powerpc-darwin10.0
/Library/Ruby/Site/1.8/universal-darwin10.0
/Library/Ruby/Site
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8/universal-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0
.
$ irb
>> puts $:
/Library/Ruby/Site/1.8
/Library/Ruby/Site/1.8/powerpc-darwin10.0
/Library/Ruby/Site/1.8/universal-darwin10.0
/Library/Ruby/Site
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby/1.8/universal-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/vendor_ruby
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/powerpc-darwin10.0
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/universal-darwin10.0
.
=> nil
>>
What would cause a module to load through irb, but not when run directly through Ruby?
One other confusing detail is that the restclient gem doesn't seem to be in my path to start with. How is irb finding it?
$ locate restclient | grep gems
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/bin/restclient
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/abstract_response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/exceptions.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/net_http_ext.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/payload.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/raw_response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/request.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/resource.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient/response.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/lib/restclient.rb
/Library/Ruby/Gems/1.8/gems/rest-client-1.6.1/spec/restclient_spec.rb
Thanks - Marc
Try
require "rubygems"
in the source code file, or starting the ruby program with ruby -rubygems filename.rb.