How to set log_dir as relative path
Hi,
I have a daemon which exists in a rails app so I'm wanting the configure daemons to put the pid and log files in the normal rails directories.
I have this setup:
options = {
app_name: 'test',
monitor: true,
dir: '../tmp/pids/',
log_output: true,
backtrace: true,
log_dir: '../log/',
output_logfilename: "test.log",
}
Daemons.run('test.rb', options)
This creates the pid files in RAILS_ROOT/tmp/pids but doesn't create a log file.
I tried removing the log_dir setting, which creates the file in RAILS_ROOT/tmp/pids.
I tried setting log_dir: '../../log/', thinking that log_dir could be relative to the dir setting. No log file is created. I assume that log_dir is relative to somewhere, but I don't know how.
I tried setting log_dir: '/tmp/', to an absolute path and that works fine.
I have a look at the code:
def logdir
logdir = options[:log_dir]
unless logdir
logdir = options[:dir_mode] == :system ? '/var/log' : pidfile_dir
end
logdir
end
def output_logfilename
filename = options[:output_logfilename]
unless filename
filename = @group.app_name + '.output'
end
filename
end
def output_logfile
(options[:log_output] && logdir) ? File.join(logdir, output_logfilename) : nil
end
def logfilename
filename = options[:logfilename]
unless filename
filename = @group.app_name + '.log'
end
filename
end
def logfile
logdir ? File.join(logdir, logfilename) : nil
end
I see that it's doing File.join(logdir, output_logfilename) but where is that relative to?
So my question is how can I have the pid files to tmp/pids and log file to log/? Am I missing something simple?
This gems is great, thanks for putting your time into it.
Thanks, Dave
Hi @daveharris,
I have the same problem here. Do you found a solution for this?
Thanks, Maurice
Hi @onlinetocode,
I eventually set the log_dir option to an absolute path, but used Ruby to build it for me:
log_dir: ::File.expand_path('../../log/', __FILE__),
Hope that helps. Dave
Thank you @daveharris, that works perfect!
It seems the output_logfile and logfile did not use File.expand_path, but the pid dir did. So it's more safe to always set the absolute path.
def output_logfile
if log_output_syslog?
'SYSLOG'
elsif log_output?
File.join logdir, output_logfilename
end
end
def logfile
if logdir
File.join logdir, logfilename
end
end
def self.dir(dir_mode, dir, script)
# nil script parameter is allowed as long as dir_mode is not :script
return nil if dir_mode == :script && script.nil?
case dir_mode
when :normal
return File.expand_path(dir)
when :script
return File.expand_path(File.join(File.dirname(script), dir))
when :system
return '/var/run'
else
fail Error.new("pid file mode '#{dir_mode}' not implemented")
end
end
Here's my runnable configuration:
#!/usr/bin/env ruby
require 'daemons'
base_dir = File.dirname(__FILE__)
file = File.expand_path('utils/process_accuse.rb', base_dir)
options = {
dir_mode: :normal,
dir: File.expand_path('../tmp/pids', base_dir),
monitor: true,
log_dir: File.expand_path('../log', base_dir),
log_output: true,
backtrace: true,
}
Daemons.run_proc(File.basename(file), options) do
exec "ruby #{file}"
end
Nice work @lingceng - that certainly explains why ::File.expand_path made it work.
Is there a fix in the works @thuehlinger? Is this project under active development?
The project is in what I would call "maintance mode". The discussed issue can certainly be fixed.
All we would need to do is to apply File.expand to dirand log_dir, right?
@thuehlinger I believe so yes
@daveharris I have pushed a fix. Can you test? If everything works as you were hoping for I would then release a new version of the gem.
@daveharris Did you have the chance to test this change?
Hey sorry @thuehlinger I haven't. I've moved onto a different project at work so I'm not using daemons now. I've been really busy on other stuff but will try and switch back to the other project and give it a test