object.find fails in rakefile even though I'm including environment - ruby

I've got a rakefile with a cron job in it that accesses one of my models and then does a search on my database. I'm including my environment and see to be able to create my object, but when I try to run the "find" method, I get this error:
rake aborted!
undefined method `find' for #<Feeder:0x103611df0>
/Library/Ruby/Gems/1.8/gems/activemodel-3.0.4/lib/active_model/attribute_methods.rb:364:in `method_missing'
/Library/Ruby/Gems/1.8/gems/activerecord-3.0.4/lib/active_record/attribute_methods.rb:46:in `method_missing'
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:28
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:20:in `each'
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:20
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:18:in `each'
/Users/josh.kerr/Documents/Projects/joshkerr.jekyll/Rakefile:18
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/Library/Ruby/Gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/bin/rake:19:in `load'
/usr/bin/rake:19
Here is the code:
require 'rails/all'
require 'rake/clean'
require File.expand_path('../config/application', __FILE__)
require 'rubygems'
require 'sqlite3'
desc "This task is called by the Heroku cron add-on"
task :cron => :environment do
puts "Running cron at #{Time.now.strftime('%Y/%m/%d %H:%M:%S')}..."
require 'app/models/feeder'
num_feeds = 10
feedurls = ["http://twitter.com/statuses/user_timeline/2205491.rss","http://github.com/joshkerr.atom","http://gdata.youtube.com/feeds/base/users/joshkerr/uploads","http://api.flickr.com/services/feeds/photos_public.gne?id=95219360#N00&lang=en-us&format=rss_200","http://www.google.com/reader/public/atom/user%2F00755727578217480286%2Fstate%2Fcom.google%2Fbroadcast"]
feedurls.each do |feed|
rawfeed = Feedzirra::Feed.fetch_and_parse(feed)
for i in 0..num_feeds do
# Database call
fed = Feeder.new
fed.title = rawfeed.entries[i].title
fed.published = rawfeed.entries[i].published
fed.url = rawfeed.entries[i].url
fed.feed_id = rawfeed.entries[i].entry_id
fed.find(1) # <- FAILS HERE!!!
#if not fed.Find(1) then
if fed.url.include? "twitter.com" then
fed.feed_service="twitter"
elsif fed.url.include? "github.com" then
fed.feed_service="github"
elsif fed.url.include? "flickr.com" then
fed.feed_service="flickr"
elsif fed.url.include? "youtube.com" then
fed.feed_service="youtube"
elsif fed.feed_id.include? "google.com" then
fed.feed_service="reader"
else
fed.feed_service = "reader"
end
fed.save
#end
end
end
puts "Done creating feed"
puts "done."
end

find is a method on an ActiveRecord class, and is not defined on an instance of an ActiveRecord object. So, to find the Feeder with ID of 1, you'd call Feeder.find(1).
The other thing I noticed is this: the line task :cron => :environment means that the environment is already being loaded--this includes rails/all, rubygems (you're using Rails 3, which uses Bundler), sqlite3 (assuming it's in your Gemfile and you've run bundle install to generate your Gemfile.lock, and all your models. Try removing all the requires lines and see if your rake task still executes.

Related

How to check whether a function exist in ruby and also with parameter

My Ruby file
require 'puppet'
# ...
require 'googleauth'
module PuppetX
module Puppetlabs
module Google_cloud_dns
class Provider < PuppetX::Puppetlabs::Swagger::Provider
def client
self.class.client
end
My RSpec file
I am using this two alternatively, but none of them working:
require_relative'../../lib/puppet_x/puppetlabs/google_cloud_dns/provider'
File.expand_path("../../lib/puppet_x/puppetlabs/google_cloud_dns/provider.rb",File.dirname(__FILE__)))
require 'spec_helper'
require_relative '../../lib/puppet_x/puppetlabs/google_cloud_dns/provider'
File.expand_path("../../lib/puppet_x/puppetlabs/google_cloud_dns/provider.rb", File.dirname(__FILE__))
describe Provider do
it "checks function" do
provide = Provider.new
provide.should respond_to :client
end
end
The error which I am getting
rspec Mymodule/gc_dns/puppet-google_cloud_dns/spec/classes/provider_spec.rb --format documentation/home/vivekkumarmishra17/Mymodule/gc_dns/puppet-google_cloud_dns/spec/classes/provider_spec.rb:19:in `<top (required)>': uninitialized constant Provider (NameError)
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `load'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
from /var/lib/gems/1.9.1/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
from /usr/local/bin/rspec:23:in `load'
from /usr/local/bin/rspec:23:in `<main>'
Can anyone tell what I am doing wrong?

TypeError: no implicit conversion of nil into String. utils.rb:24:in `quote_ident'

I made rake task.
My rails app is running without problems.
Code
namespace :i18n_master do
task check: :environment do
read_model
models = ActiveRecord::Base.descendants.map do |model|
next if model.name.nil?
model2hash(model)
end
p models
end
def model2hash(model)
return Hash[model.name, model.column_names]
end
def read_model
Dir[Rails.root.to_s + '/app/models/**/*.rb'].each do |file|
begin
require file
rescue
end
end
end
end
Error
** Invoke i18n_master:check (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute i18n_master:check
rake aborted!
TypeError: no implicit conversion of nil into String
ror_app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/utils.rb:24:in `quote_ident'
ror_app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/utils.rb:24:in `quoted'
ror_app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/quoting.rb:31:in `quote_table_name'
ror_app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql_adapter.rb:742:in `column_definitions'
ror_app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/schema_statements.rb:186:in `columns'
ror_app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/connection_adapters/schema_cache.rb:43:in `columns'
ror_app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/attributes.rb:93:in `columns'
ror_app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.4/lib/active_record/model_schema.rb:260:in `column_names'
ror_app/lib/tasks/i18n_master.rake:13:in `model2hash'
ror_app/lib/tasks/i18n_master.rake:6:in `block (3 levels) in <top (required)>'
ror_app/lib/tasks/i18n_master.rake:4:in `map'
ror_app/lib/tasks/i18n_master.rake:4:in `block (2 levels) in <top (required)>'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/Users/shingonakanishi/.rvm/rubies/ruby-2.2.3/bin/rake:37:in `<main>'
Tasks: TOP => i18n_master:check
I read this
Rails 4.0.0 - Got "no implicit conversion of nil into String"
the above problems is gem for sqlite3.
so I think my problem is gem for postgresql.
But My rails app is running without problems.
And I use bundle.
% bundle pristine
Could not find command "pristine".
Why
Why TypeError: no implicit conversion of nil into String is occur in utils.rb:24:in quote_ident'
How to fix it?
I got it.
Globalize::ActiveRecord::Translation#column_names occur this error.
so, skip this.
namespace :i18n_master do
task check: :environment do
read_model
models = ActiveRecord::Base.descendants.map do |model|
next if model.name == 'Globalize::ActiveRecord::Translation'
model2hash(model)
end.compact
p models
end
def model2hash(model)
return Hash[model.name, model.column_names]
end
def read_model
Dir[Rails.root.to_s + '/app/models/**/*.rb'].each do |file|
begin
require file
rescue
end
end
end
end

Testing ruby (not rails) script with cucumber on Mac OS X

I am trying to get cucumber working on my Mac for testing a ruby script. I use cucumber with rails and have never had an issue setting it up there, but seem to be having quite an issue creating it just for a ruby script that I want to test.
Script background: I have created a script that serves mysql data via a TCPServer connection and I want to be able to test this functionality. So ideally I launch the script so that it accepts incoming connections and depending on what I am attempting to get to it presents an error or the data end point.
Here is what I have and the error is below the settings:
features/support/env.rb
require_relative File.expand_path('../../../lib/requirements',FILE)
require "capybara/cucumber"
require 'capybara/dsl'
require "rspec"
# Capybara.default_driver = :selenium
Capybara.app_host = 'http://localhost:7125'
World(Capybara.app = "Api")
World(Capybara::DSL)
World(RSpec::Matchers)
features/rmws.feature
Feature: Test conversion of mysql output to JSON
Scenario:
Given the index page can be reached
And There is MYSQL data
When converted to hash
And and /mysql location is hit
Then the data is show in JSON
features/step_definitions/rwms_step_definitions.rb
Given(/^the index page can be reached$/) do
visit 'http://localhost:7125'
expect(page).to have_content 'This is the index page'
end
Given(/^There is MYSQL data$/) do
pending # express the regexp above with the code you wish you had
end
When(/^converted to hash$/) do
pending # express the regexp above with the code you wish you had
end
When(/^and \/mysql location is hit$/) do
pending # express the regexp above with the code you wish you had
end
Then(/^the data is show in JSON$/) do
pending # express the regexp above with the code you wish you had
end
Output
$ cucumber
Feature: Test conversion of mysql output to JSON
Scenario: # features/rmws.feature:3
wrong argument type String (expected Module) (TypeError)
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/rb_support/rb_language.rb:150:in `extend'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/rb_support/rb_language.rb:150:in `block in extend_world'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/rb_support/rb_language.rb:149:in `each'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/rb_support/rb_language.rb:149:in `extend_world'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/rb_support/rb_language.rb:94:in `begin_rb_scenario'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/rb_support/rb_language.rb:128:in `begin_scenario'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/language_support/language_methods.rb:14:in `before'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime/support_code.rb:112:in `block in fire_hook'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime/support_code.rb:111:in `each'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime/support_code.rb:111:in `fire_hook'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime.rb:107:in `before'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime.rb:98:in `before_and_after'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime.rb:82:in `block in with_hooks'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime/support_code.rb:120:in `call'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime/support_code.rb:120:in `block (3 levels) in around'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/language_support/language_methods.rb:9:in `block in around'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/language_support/language_methods.rb:97:in `call'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/language_support/language_methods.rb:97:in `execute_around'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/language_support/language_methods.rb:8:in `around'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime/support_code.rb:119:in `block (2 levels) in around'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime/support_code.rb:123:in `call'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime/support_code.rb:123:in `around'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime.rb:94:in `around'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime.rb:81:in `with_hooks'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:13:in `execute'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/scenario.rb:32:in `block in accept'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/scenario.rb:79:in `with_visitor'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/scenario.rb:31:in `accept'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:58:in `block in visit_feature_element'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:170:in `broadcast'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:57:in `visit_feature_element'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/feature.rb:38:in `block in accept'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/feature.rb:37:in `each'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/feature.rb:37:in `accept'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:27:in `block in visit_feature'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:170:in `broadcast'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:26:in `visit_feature'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/features.rb:28:in `block in accept'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/features.rb:17:in `each'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/features.rb:17:in `each'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/features.rb:27:in `accept'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:21:in `block in visit_features'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:170:in `broadcast'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/ast/tree_walker.rb:20:in `visit_features'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/runtime.rb:49:in `run!'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/lib/cucumber/cli/main.rb:47:in `execute!'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/gems/cucumber-1.3.11/bin/cucumber:13:in `<top (required)>'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/bin/cucumber:23:in `load'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/bin/cucumber:23:in `<main>'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/bin/ruby_executable_hooks:15:in `eval'
/Users/lovell/.rvm/gems/ruby-2.0.0-p451/bin/ruby_executable_hooks:15:in `<main>'
$
You could use rake to run cucumber tests, I use it for a gem that I wrote. You can find additional doc here
require 'bundler'
require 'rake/clean'
require 'rake/testtask'
require 'cucumber'
require 'cucumber/rake/task'
require 'rspec/core/rake_task'
Cucumber::Rake::Task.new(:features) do |t|
t.cucumber_opts = "features --format html -o #{CUKE_RESULTS} --format pretty --no-source -x"
t.fork = false
end
Run cucumber --init to get the testing suite set up.

Thin fails spawning workers when running on EventMachine

I have a ruby web- / data-server application which is built on a Sinatra foundation. I'm adding EventMachine and em-websocket so that I can have async data transfer connections.
#! /usr/bin/env ruby
require 'yajl'
require 'json'
require 'em-websocket'
require 'sinatra/base'
require 'thin'
require './Actors/DataProvider.rb'
my_host = '10.2.56.87'
my_web_port = '4004'
my_web_sock = '8008'
EventMachine.epoll # choose kernel epoll over select for deterministic execution under load
EventMachine.run do
class SinatraApp < Sinatra::Base
configure do
set :bind, '10.2.56.87'
set :port, '4004'
end
encoder = Yajl::Encoder.new
communicator = DataProvider.new( encoder )
get '/' do
redirect '/index-work.html'
end
before do
content_type 'application/json'
end
post '/auth' do
end
post '/data/:id' do
puts 'got d i '
end
end
EventMachine::WebSocket.start( :host => my_host, :port => my_web_sock ) do | ws |
ws.onopen do
end
ws.onmessage do
end
ws.onclose do
end
end
begin
SinatraApp.run!
rescue => ex
puts "#{ ex.class}: #{ ex.message }"
end
end
The Thin server worked fine when just serving Sinatra, but now that I've added the EM foundation the following occurs:
/usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:192:in `block (2 levels) in start': undefined method `attach_server' for EventMachine:Module (NoMethodError)
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:191:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:191:in `block in start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/backends/prefork.rb:30:in `block (2 levels) in start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/backends/prefork.rb:23:in `block in start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker/worker.rb:52:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker/worker.rb:52:in `work'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:129:in `block (2 levels) in spawn_missing_workers'
from (eval):6:in `block in fork'
from (eval):6:in `fork'
from (eval):6:in `fork'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:126:in `block in spawn_missing_workers'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:124:in `times'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:124:in `spawn_missing_workers'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/preforker-0.1.1/lib/preforker.rb:44:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/backends/prefork.rb:37:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/thin-2.0.0.pre/lib/thin/server.rb:187:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/rack-1.4.1/lib/rack/handler/thin.rb:13:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/sinatra-1.3.3/lib/sinatra/base.rb:1350:in `run!'
from /home/devel/ISF_Server/server-work.rb:122:in `block in <top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `call'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run_machine'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/eventmachine-1.0.0/lib/eventmachine.rb:187:in `run'
from /home/devel/ISF_Server/server-work.rb:15:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/ruby-debug-ide-0.4.17.beta14/lib/ruby-debug-ide.rb:127:in `debug_load'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/ruby-debug-ide-0.4.17.beta14/lib/ruby-debug-ide.rb:127:in `debug_program'
from /usr/local/rvm/gems/ruby-1.9.3-p286/gems/ruby-debug-ide-0.4.17.beta14/bin/rdebug-ide:118:in `<top (required)>'
from -e:1:in `load'
from -e:1:in `<main>'
I do notice that the Thin gem was recently updated, but EM has not been. That's my next point of inquiry. Somebody else posted that Thin should not be run from within EM.run block, but the example I am building from had it this way.
Okay. I corresponded with Marc-Andre Cournoyer directly. He says, first, don't spawn This (through Sinatra) inside an EM.run block.
Secondly, the error is generated from Thin 2 (pre) which requires the EventMachine live Edition, or gem eventmachine-le.
Mo' bettah now. :D

NoMethodError: undefined method `belong_to' for #<RSpec::Core::ExampleGroup::Nested_4:0xa05d2a0>

So this is kind of baffling me as I can't quite figure out why it's happening. This only happens on my laptop (Ubuntu 11.04), and not elsewhere. I just seem to have something weird with the setup on this one computer.
I keep getting the following error when I run my specs:
be rake spec
Gives me:
NoMethodError: undefined method `belong_to' for #<RSpec::Core::ExampleGroup::Nested_4:0xb4eb2e4>
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-expectations-2.6.0/lib/rspec/matchers/method_missing.rb:9:in `method_missing'
/home/tom/work/ruby/litdistco-sales/spec/models/sales_item_spec.rb:5:in `block (2 levels) in <top (required)>'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `instance_eval'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:48:in `block in run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:107:in `with_around_hooks'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example.rb:45:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:294:in `block in run_examples'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `map'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:290:in `run_examples'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/example_group.rb:262:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block (2 levels) in run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `map'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:24:in `block in run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/reporter.rb:12:in `report'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:21:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:80:in `run_in_process'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:69:in `run'
/home/tom/.rvm/gems/ruby-1.9.2-p290#litdistco/gems/rspec-core-2.6.4/lib/rspec/core/runner.rb:11:in `block in autorun'
Here are the relevant lines from my spec file that generates the complaint:
describe SalesItem do
it { should belong_to(:publisher) }
it { should belong_to(:invoice) }
I'm running Rails 3.1.0. Here is ruby -v:
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]
Any tips /thoughts/ideas recommendations greatly appreciated.
Try adding this in your rails_helper.rb
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
RSpec core does not have such matcher. It looks like shoulda-matchers. Just make sure that it's installed and loaded in your spec_helper
I was having a hard time with this for awhile and then changed my spec from:
describe ModelName do
it { should belong_to(:model)}
end
to:
RSpec.describe ModelName, type: :model do
it { should belong_to(:model)}
end
and it suddenly worked
You can write specs like this
describe SalesItem do
describe "Associations" do
it "belongs_to publisher" do
assc = described_class.reflect_on_association(:publisher)
expect(assc.macro).to eq :belongs_to
end
end
end

Resources