views:

41

answers:

3

I have a datastore with a cache and a db, simple. The tricksy part is that I want a way to control if the the datastore hits the db in a real-time way. That is to say while the process is running I want to be able to toggle if it's connected to the db or not.

I looked into env variables, but it doesn't seem like those get updated as the process runs. Is there a simple way to get a bit from the command line into the running process, or do I just need to rely on ops being able to drop the db listeners in case of disaster?

Note that this is all being done in vanilla ruby - not ruby on rails.

Thanks! -Jess

A: 

A shared-memory strategy might be worth considering. Assuming you're running on a POSIX system, check out mmap for memory-mapped files, and SysVIPC for message queues, semaphores, and shared memory.

maerics
Yeah, I could do that - set up a command line script that'd pass the message over to the ruby process. It's a good point. I was hoping for a simpler solution, but everything, of course, is that underneath anyway. Kinda a pita to implement though.
Jess
A: 

I think you can use named pipes for simple communication:

#pipes.rb:
f = File.open 'mypipe', 'r+'
loop do
  begin
    s = f.read_nonblock 1
  rescue Exception
  end
  case s
  when '0'
    puts 'Turn off DB access!'
  when '1'
    puts 'Turn on DB access!'
  end
  sleep 1
end

And you can control your db access externally by writing to the named pipe:

jablan-mbp:dev $ echo 101 > mypipe 

Which results in:

jablan-mbp:dev $ ruby pipes.rb 
Turn on DB access!
Turn off DB access!
Turn on DB access!
Mladen Jablanović
Thank you. This is what we ended up doing. I just had some vain hope that there was something simpler, but really this isn't bad.
Jess
A: 

Assuming *NIX, have you considered signals? (kill -HUP pid) - http://ruby-doc.org/core/classes/Signal.html

Flavius Stef