Skip to content

Instantly share code, notes, and snippets.

@agibralter
Last active December 11, 2015 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agibralter/4548607 to your computer and use it in GitHub Desktop.
Save agibralter/4548607 to your computer and use it in GitHub Desktop.
Trying to get `ThinkingSphinx::Test.start_with_autostop` and Redis to stop when guard exits.
# -*- encoding : utf-8 -*-
module LocalRedis
module ClassMethods
def run_in_foreground!
unless pid = fork
Dir.chdir(Rails.root)
exec(cmd)
end
trap_sigs(pid)
Process.wait(pid)
end
def run!
unless pid = fork
Dir.chdir(Rails.root)
exec(cmd)
end
trap_sigs(pid)
Kernel.at_exit do
status = $!.status if $!
kill('TERM', pid)
exit status if status
end
end
private
def trap_sigs(pid)
%w(TERM INT QUIT).each do |sig|
Signal.trap(sig) { kill(sig, pid) }
end
end
def cmd
"redis-server #{Rails.root.join('config', "redis-#{Rails.env}.conf")}"
end
def kill(signal, pid)
File.delete(Rails.root.join('tmp', 'redis-test-dump.rdb')) rescue nil
Process.kill(signal, pid)
end
end
extend ClassMethods
end
Spork.prefork do
ENV['RAILS_ENV'] = 'test'
# ...
# Set up Redis.
require 'local_redis'
LocalRedis.run!
# Set up ThinkingSphinx.
require 'thinking_sphinx/test'
ThinkingSphinx::Test.init
ThinkingSphinx::Test.start_with_autostop
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment