Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set log_dir as relative path #36

Open
daveharris opened this issue Jul 23, 2015 · 10 comments
Open

How to set log_dir as relative path #36

daveharris opened this issue Jul 23, 2015 · 10 comments

Comments

@daveharris
Copy link

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

@onlinetocode
Copy link

Hi @daveharris,

I have the same problem here. Do you found a solution for this?

Thanks,
Maurice

@daveharris
Copy link
Author

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

@onlinetocode
Copy link

Thank you @daveharris, that works perfect!

@lingceng
Copy link

lingceng commented Jul 25, 2017

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

@daveharris
Copy link
Author

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?

@thuehlinger
Copy link
Owner

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?

@daveharris
Copy link
Author

@thuehlinger I believe so yes

@thuehlinger
Copy link
Owner

@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.

@thuehlinger
Copy link
Owner

@daveharris Did you have the chance to test this change?

@daveharris
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants