I followed the instructions at
for creating a daemon. Simple enough, right? Wrong…
For some reason, I keep getting an error when running the daemon even
if I remove all my custom code from the generated code. Anybody know
what I’m doing wrong??
The error and the daemon code is at http://pastie.org/456601 as well
as below:
The error (from log/mailer.rb.log):
Logfile created on Thu Apr 23 19:06:31 -0700 2009 by /
*** below you find the most recent exception thrown, this will be
likely (but not certainly) the exception that made the application
exit abnormally ***
#<Errno::ENOENT: No such file or directory - ./lib/daemons/…/…/
vendor/rails/activesupport/lib/active_support/locale/en.yml>
*** 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>
#<Errno::ENOENT: No such file or directory - ./lib/daemons/…/…/
vendor/rails/activesupport/lib/active_support/locale/en.yml>
lib/daemons/mailer.rb
#!/usr/bin/env ruby
ENV[“RAILS_ENV”] ||= “production”
require File.dirname(FILE) + “/…/…/config/environment”
$running = true
Signal.trap(“TERM”) do
$running = false
end
while($running) do
I removed my custom code to test whether the error was a result of
what I
was trying to do, and the error still appears. The rest of the
code here
was created by the daemon_generator plugin.
sleep 10
end
lib/daemons/mailer_ctl
Again, no custom code here
#!/usr/bin/env ruby
require ‘rubygems’
require “daemons”
require ‘yaml’
require ‘erb’
file_name = File.dirname(FILE) + “/…/…/vendor/rails/
activesupport/lib/active_support.rb”
if(File.exists?(file_name))
require file_name
else
rails_version = File.new(File.dirname(FILE)+ “/…/…/config/
environment.rb”).read.scan(/^ RAILS_GEM_VERSION.=.*'|"['|"]/)
[0].to_s
gem ‘activesupport’, rails_version
require ‘active_support’
end
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) + ‘/mailer.rb’, options
lib/daemon.rb
Taken from http://snippets.dzone.com/posts/show/2265
require ‘fileutils’
module Daemon
WorkingDirectory = File.join(File.dirname(FILE), ‘…’)
class Base
def self.pid_fn
File.join(WorkingDirectory, “log”, “#{name}.pid”)
end
def self.daemonize
Controller.daemonize(self)
end
end
module PidFile
def self.store(daemon, pid)
File.open(daemon.pid_fn, ‘w’) {|f| f << pid}
end
def self.recall(daemon)
IO.read(daemon.pid_fn).to_i rescue nil
end
end
module Controller
def self.daemonize(daemon)
case !ARGV.empty? && ARGV[0]
when ‘start’
start(daemon)
when ‘stop’
stop(daemon)
when ‘restart’
stop(daemon)
start(daemon)
else
puts “Invalid command. Please specify start, stop or restart.”
exit
end
end
def self.start(daemon)
fork do
Process.setsid
exit if fork
PidFile.store(daemon, Process.pid)
Dir.chdir WorkingDirectory
File.umask 0000
STDIN.reopen "/dev/null"
#STDOUT.reopen "/dev/null", "a"
STDERR.reopen STDOUT
trap("TERM") {daemon.stop; exit}
daemon.start
end
end
def self.stop(daemon)
if !File.file?(daemon.pid_fn)
puts "Pid file not found. Is the daemon started?"
exit
end
pid = PidFile.recall(daemon)
FileUtils.rm(daemon.pid_fn)
pid && Process.kill("TERM", pid)
end
end
end