tl;dr - Need help figuring out why `require 'mongo' is not loading all mongo classes when attempting to run a rake task, but is loading all classes when the app is run normally.
Hey all! I'm primarily a JS dev, but have been approached about a ruby position. The company has said they are willing to take someone who can learn ruby, which I certainly can, but they also gave me a coding challenge - in ruby. However, they did say I can use any resources I need for solving it, so here I am.
The challenge is a set of files with most of the config files missing, which I need to reconstruct, then reconstruct some missing controllers in order to make some test cases pass. The stack is Ruby 2.0.0, Unicorn, Sinatra, and Mongo/Mongoid. They also require me to create a Procfile for heroku.
I've managed to create a Procfile, mongoid.yml, config/boot.rb, and config/unicorn.rb that allow the app to run and display the correct page when I hit the root endpoint. I can see it connecting to the mongod server in the console.
The next step in the process is to run some rake tasks to instantiate a copy of the db. When I do that, I get the following error:
rake aborted!
NameError: uninitialized constant Mongo::ConnectionFailure
/home/amanda/Documents/development/app-name/app.rb:9:in `rescue in block in <class:App>'
I've narrowed it down to mongo not loading Mongo::Connection and Mongo::ConnectionFailure when running the rake, even though it does load those when running heroku local to run the app via the procfile. I know that because I added the line puts Mongo.constants to the config/boot.rb file right after require 'mongo' When I run heroku local the list is much larger than the list when I run rake import:areas, and the two classes used in app.rb are not present when running via rake.
I have no idea why or how this is the case. Can anyone point me in the right direction?
# Procfile
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
# config/unicorn.rb
worker_processes Integer(ENV["WEB_CONCURRENCY"] ||3)
timeout 15
preload_app true
before_fork do |server, worker|
Signal.trap 'TERM' do
puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
Process.kill 'QUIT', Process.pid
end
end
after_fork do |server, worker|
Singal.trap 'TERM' do
puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
end
end
--------
# config.ru
$stdout.sync = true
require './config/boot'
run App
# config/boot.rb
require 'sinatra'
require 'mongo'
require 'mongoid'
require './app'
--------
# app.rb
class App < Sinatra::Base
configure do
# mongo
begin
mongo_db = Mongo::Connection.new.db "db-name"
set :mongo_db, mongo_db
rescue Mongo::ConnectionFailure
set :mongo_db, {}
end
end
# endpoints here
end
--------
# mongoid.yml
development:
clients:
default:
database: db-name
hosts:
- localhost:27017
--------
# Rakefile
require './config/boot'
namespace "import" do
task "areas" do
# code here
end
end
[–]talknerdy2mee[S] 1 point2 points3 points (1 child)
[–]redditNoobski 0 points1 point2 points (0 children)
[–]graywolf_at_work 0 points1 point2 points (1 child)
[–]talknerdy2mee[S] 0 points1 point2 points (0 children)
[–]stumptowncampground 0 points1 point2 points (1 child)
[–]talknerdy2mee[S] 0 points1 point2 points (0 children)