Last active
December 11, 2015 04:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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