Rails daemon do not start - ruby-on-rails

I have Rails 3.0.3 project and I trying to create daemon by this steps:
http://railscasts.com/episodes/129-custom-daemon
I've installed gem daemons
sudo gem install daemons
Then I've install daemon_generator
rails plugin install https://github.com/dougal/daemon_generator.git
Then created daemon
rails generate daemon game_processor
When I try to run daemon
./lib/daemons/game_processor_ctl start
I got error:
./lib/daemons/game_processor_ctl:2:in `require': no such file to load -- rubygems (LoadError)
from ./lib/daemons/game_processor_ctl:2
Code of daemon:
#!/usr/bin/env ruby
require 'rubygems'
require "daemons"
require 'yaml'
require 'erb'
gem 'activesupport', '>=3.0.0.beta4'
require 'active_support'
# For some reason, ActiveSupport 3.0.0 doesn't load.
# Load needed extension directly for now.
require "active_support/core_ext/object"
require "active_support/core_ext/hash"
options = YAML.load(
ERB.new(
IO.read(
File.dirname(FILE) + "/../../config/daemons.yml"
)).result).with_indifferent_access
options[:dir_mode] = options[:dir_mode].to_sym
Daemons.run File.dirname(FILE) + "/game_processor.rb", options
So, What's wrong? Why it dies, when trying to require rubygems?

Are you on Windows or *nix system - on Windows you should use ruby game_processor.rb start instead of _ctl.
Also as you are using it with Rails - then i think Rails server should be started in required mode as well to make Daemon run properly.

Related

Load gems inside scripts run from the shell outside our Rails application directory?

We have a mail gem installed in our vendor/cache directory inside a Rails application.
The script is called "test" and is not inside the Rails application directory.
#! /usr/local/rvm/wrappers/ruby-1.9.3-p194/ruby
require 'date'
require 'fileutils'
require 'openssl'
require 'yaml'
require 'mail'
require 'dalli'
I get the following error when I execute this script from outside the Rails application.
/usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- mail (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
I simply re-installed these gems (mail, dalli) in the standard Ruby path and it worked, but that's not ideal.
What do we need to so that these installed gems are found when we try to run this script outside of a Rails app? In other words, how do we specify the path to these gems?
Be sure that your gem are all declared in your Gemfile:
gem 'mail'
If you don't want them to be loaded by default, and load them only when needed, you can use the require statement that you are already using, and in your Gemfile add :require => false:
gem 'mail', :require => false
When you call your script from outside your Rails environment, and want to load the gems, prefix your script by bundle exec:
bundle exec my_script.rb
If you need to run your script from another location than your rails's app root, you must run:
BUNDLE_GEMFILE=/path/to/your/app/Gemfile bundle exec your_script
Keep in mind though that this may cause path issues if your script or your gems are looking for file in the path of your rails app

Ruby error: cannot load such file -- rest-client

I am using Ruby on Rails 4.
I am trying to
require 'rest-client'
in my controller so that I can parse the login information I am getting from a form and send it to an API.
I can verify that the gem is installed and is also in my Gemfile on the application root.
However, it is still throwing the "cannot load such file -- rest-client " when I try to require the file in my controller.
I have googled the error and most of the answers I saw were either the gem wasn't installed, wasn't in the Gemfile, or a combination of both those. Neither is the situation here.
Is my controller unable to access the rest-client gem for some reason? I have to use rest-client because it is required in the API.
This is the line I used to install the gem:
gem install rest-client
This is the homepage of the gem: https://github.com/archiloque/rest-client
Which just redirects you to https://github.com/rest-client/rest-client
I should also note that it works fine when I wasn't using the code in a Rails project but just running the commands in the Terminal.
Assuming you're using https://github.com/rest-client/rest-client (since you didn't specify), your require line should be
require 'rest-client'
according to the README. Also, make sure you restart your rails server after adding the gem to your Gemfile and running bundle.
Run the following command in your terminal:
gem install rest-client
and use require 'rest-client'. No need to change to rest_client.
in my case, none of the solutions in this thread worked
what did work, was to add the gem directly in the Gemfile:
gem 'rest-client'
after closing the rails server, exiting rails console and running bundle install,
I opened again the rails console and this time require 'rest-client' worked flawlessly
For me it was an issue with bundle (which I thought I had installed). Spoiler alert, I didn't, and this is how I fixed it. I'm on a Mac running OS X Yosemite and my terminal version is Darwin Kernel Version 14.3.0:
cd
gem install bundler
or
cd
sudo gem install bundler
If you get something along the lines of the following error:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
Finally, change your require line from:
require 'rest-client'
to
require 'rest_client'
Then run your code!
First ensure you have installed gem 'rest-client', ~> 1.8.0 on your gem file. Run bundle install and then require 'rest_client'. This worked for me.
Try require 'rest_client', instead of require 'rest-client'

Turn a ruby script into an always running job

I have created a program that I need to run constantly. It currently lives at scripts/mailman. I start it by doing this:
sudo bundle exec rails runner script/mailman &
It seams to stop after I logout of the server. Here is the contents of my mailman program:
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "mailman"
require "rb-inotify"
Mailman.config.logger = Logger.new("/var/log/mailman.log")
Mailman.config.maildir = '/var/mail'
require File.dirname(__FILE__) + "/../../config/application"
Rails.application.require_environment!
Mailman::Application.run do
default do
begin
Bin.receive_mail(message)
end
end
end
What is a good way to start this program automatically and keep it running always? I am running this on Ubuntu.
Use the 'daemons' gem as suggested here:
Make a Ruby program a daemon?
Seems also to be very popular on RubyToolbox
https://www.ruby-toolbox.com/categories/daemonizing
I've found that the daemons gem works well for this.
Assuming your posted code lives in script/mailman.rb, you can make a file script/mailman_ctl:
#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
Daemons.run('mailman.rb')
I typically give the options {:backtrace => true, :monitor => true} to the Daemons.run call, so that I have a better idea of what happened if the process ever dies.

Require ruby gems in rails controller

require 'rubygems'
require 'mechanize'
agent = Mechanize.new
page = agent.get("http://google.com/")
This simple script works ok.
But if I'am trying to add
require 'rubygems' and require 'mechanize' to the Rails controller, server gives:
LoadError in NewsController#find
no such file to load -- mechanize
I use RVM on Ubuntu 10.04 server machnine. Ruby version: 1.9.2, Rails version: 3.0.3.
Server: Passanger under Apache2.
P.S. If I run rails server and go to mysite.com:3000 all works without any error, so there is a problem with Passanger!
Please, help me!
You shouldn't require gems in your controller. Thats why Bundler was added to Rails 3.
Just add mechanize to your Gemfile like this
gem "mechanize"
and run
bundle install
on the command line.
Any gem mentioned here will be required on application startup.
The way you manage dependencies in Rails 3, is using the Gemfile and Bundler.
Edit your Gemfile and add
gem "mechanize"
Then run
$ bundle install
Restart the server. The library will automatically be loaded. No need to manually require RubyGems.

Custom Daemon with Rails 3

I'm trying to create a custom daemon that loads up the Rails environment.
My environment is as follows:
ruby-1.9.2-p180
rails 3.0.5
I did the following:
-Installed the daemons gem
-Installed daemon_generator plugin found here:
https://github.com/dougal/daemon_generator
-Generated a daemon: rails generate daemon listener
All this worked fine. When I run the daemon, it works.
However, as soon as I try to access an active record object like trying to retrieve a user, it blows up.
*** below you find the most recent exception thrown, this will be likely (but not certainly) the exception that made the application exit abnormally ***
#<NameError: method `recognize' not defined in Rack::Mount::RouteSet>
*** below you find all exception objects found in memory, some of them may have been thrown in your application, others may just be in memory because they are standard exceptions ***
#<NoMemoryError: failed to allocate memory>
#<SystemStackError: stack level too deep>
#<fatal: exception reentered>
#<NoMethodError: undefined method `eq' for nil:NilClass>
#<NameError: method `recognize' not defined in Rack::Mount::RouteSet>
Any thoughts on how to create a Daemon that loads up Rails 3.0.5?
I prefer to roll my own rails daemon controllers. Here is a simple example that works for most cases:
script/daemon
#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
ENV["APP_ROOT"] ||= File.expand_path("#{File.dirname(__FILE__)}/..")
ENV["RAILS_ENV_PATH"] ||= "#{ENV["APP_ROOT"]}/config/environment.rb"
script = "#{ENV["APP_ROOT"]}/daemons/#{ARGV[1]}"
Daemons.run(script, dir_mode: :normal, dir: "#{ENV["APP_ROOT"]}/tmp/pids")
daemons/your_daemon_script.rb
require ENV["RAILS_ENV_PATH"]
loop {
... your code ...
}
You can control your deamons by using the following commands:
script/daemon run your_daemon_script.rb
script/daemon start your_daemon_script.rb
script/daemon stop your_daemon_script.rb
This enables me to easily add new daemons and I can easily load rails in each script if necessary.
I had a lot of problems trying get daemon_generator working. I got my daemon working by skipping the daemon_generator all together and just using the daemons gem (v1.1.3).
in urserver_control.rb (in root ruby app directory):
#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
require 'TweetMsg'
Daemons.run('urserver.rb')
in urserver.rb:
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), 'config', 'environmen
t'))
require "rubygems"
--- insert your code here ---
You can test by running your server directly ruby urserver.rb or ruby urserver_controller run
And then once that is working starting and stopping the controllerruby urserver_control.rb {start | stop | run }
Looking at the code in https://github.com/dougal/daemon_generator/blob/master/lib/generators/daemon/templates/script.rb it appears they load things in the wrong order...
Looking at my delayed_job daemon script and config.ru they load config/environment.rb (which in turn loads application.rb and initializes the app)
So a possible fix would be to edit the generated script and make it only require 'config/environment.rb'
I tried this:
#!/usr/bin/env ruby
# You might want to change this
ENV["RAILS_ENV"] ||= "development"
require File.dirname(__FILE__) + "/../config/environment"
$running = true
Signal.trap("TERM") do
$running = false
end
while($running) do
# Replace this with your code
Rails.logger.auto_flushing = true
o = Order.last
Rails.logger.info "The latest order is #{o.id}"
sleep 10
end
and it yielded no errors... (Tried both Rails 3.0.3 and 3.0.5)
I had problems running daemon as is on my staging server (Rails 3.0.7, ruby 1.8.7, passenger 3.0.0). Neither
require File.dirname(FILE) + "/../../config/application"
Rails.application.require_environment!
nor
require File.dirname(FILE) + "/../config/environment"
Worked.
I fixed it by re-installing the standard config.ru in rails root (which I had de-installed to integrate w passenger... not sure now how I will get daemons & passenger to work together now ...)

Resources