2

I am trying to run multiple instances of the same code/script using the Daemons gem. I've been playing around with it in an IRB session and can't seem to get the functionality I'm looking for. I want to run the same script multiple times keeping track of the PID so that I can manually start and stop processes. More specifically, I have a rails model whose instance will control a single process and so I will need to start and stop it using something like

mydaemon = MyDaemon.create
mydaemon.start # starts the process
mydaemon.stop  # stops the process

However, in order to achieve something like this I wanted to store the PIDs for the process in active record. When I run the following code in IRB:

require 'rubygems'        # if you use RubyGems
require 'daemons'

task1 = Daemons.call(:multiple => true) do
  loop {
    File.open("/tmp/daemon_test.log", "w") {|f| f.write(Time.now.to_s + "\n")}
    sleep 5
  }
end

the task automatically starts (without ever calling start on task1), and instead of returning the pid, it returns the Daemons::Application object associated with task1. I only have access to the pid when I start a second instance of task1 i.e.:

task1.start
 => 574 # PID of process but now I have 2 proc's running, one of which I don't know the PID of

Am I using Daemons wrong? or is there no way to get the PID of the very first process that starts automatically when calling Daemons.call?

Thanks, Tomek

Tomek
  • 4,689
  • 15
  • 44
  • 52
  • I wouldn't expect daemonizing code to play nicely inside `irb`. I'd suggest testing via scripts instead. – sarnold Jun 21 '12 at 22:57
  • @sarnold looks like running the script produces the same results https://gist.github.com/2969149 – Tomek Jun 21 '12 at 23:06

1 Answers1

0

The docs say that an Daemons::Application object has a public attribute pid which returns a Daemons::Pid object, so try using that task1.pid.pid.

dbenhur
  • 20,008
  • 4
  • 48
  • 45
  • running task1.pid.pid returns nil as can be seen in the comment I made above https://gist.github.com/2969149. – Tomek Jun 22 '12 at 15:01