Skip to content

Commit

Permalink
thoughtworks#33: Fixed cruise start and stop commands. Builders now r…
Browse files Browse the repository at this point in the history
…esponsible for creating and deleting their own pid files. Daemonization appears to be functioning in Ruby 1.9.2. Needs JRuby testing.
  • Loading branch information
bguthrie committed Jul 3, 2011
1 parent 80af107 commit 17ca0f8
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 18 deletions.
2 changes: 1 addition & 1 deletion app/models/project.rb
Expand Up @@ -372,7 +372,7 @@ def update_project_to_revision(build, revision)
end

def kill_build
Platform.kill_child_process(self.name)
Platform.kill_project_builder(self.name)
end

def build(revision = source_control.latest_revision, reasons = [])
Expand Down
21 changes: 18 additions & 3 deletions lib/cruise_control/init.rb
Expand Up @@ -51,9 +51,24 @@ def start
end

def stop
pid_file = File.join("tmp", "pids", "server.pid")
if File.exist?(pid_file)
exec "mongrel_rails stop -P #{pid_file}"
require ENV_PATH

stop_builders
stop_server
end

def stop_server
pid_file = Rails.root.join("tmp", "pids", "server.pid")

if pid_file.exist?
exec "kill -KILL #{pid_file.read.chomp}"
pid_file.delete
end
end

def stop_builders
Rails.root.join("tmp", "pids", "builders").children.each do |pid_file|
Platform.kill_child_process(pid_file.read.chomp)
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/file_lock.rb
Expand Up @@ -11,10 +11,10 @@ def initialize(lock_file_name, locked_object_description = lock_file_name)
def lock
if @@lock_files.include?(@lock_file_name)
raise AlreadyLockedError, "Already holding a lock on #@locked_object_description"

end

lock_file = File.open(@lock_file_name, 'w')

locked = lock_file.flock(File::LOCK_EX | File::LOCK_NB)
if locked
@@lock_files[@lock_file_name] = lock_file
Expand Down
13 changes: 7 additions & 6 deletions lib/platform.rb
Expand Up @@ -60,9 +60,6 @@ def create_child_process(project_name, command)

# safely exec
Process.detach(pid)
pid_file = project_pid_file(project_name)
FileUtils.mkdir_p(File.dirname(pid_file))
File.open(pid_file, "w") {|f| f.write pid }
rescue NotImplementedError # Kernel.fork exists but not implemented in Windows
Thread.new { system(command) }
end
Expand All @@ -78,9 +75,13 @@ def project_pid_file(project_name)
end
module_function :project_pid_file

def kill_child_process(project_name)
pid = File.read(project_pid_file(project_name)).chomp
kill_tree = File.join(RAILS_ROOT, 'script', 'killtree')
def kill_project_builder(project_name)
kill_child_process project_pid_file(project_name).read.chomp
end
module_function :kill_project_builder

def kill_child_process(pid)
kill_tree = Rails.root.join('script', 'killtree')
Kernel.system("#{kill_tree} #{pid}")
end
module_function :kill_child_process
Expand Down
18 changes: 13 additions & 5 deletions script/builder
Expand Up @@ -45,11 +45,22 @@ def write_to_log_and_console(message, severity = :info)
(puts message unless CRUISE_OPTIONS[:verbose]) rescue nil
end

def startup(project)
Platform.project_pid_file(project.name).tap do |f|
f.dirname.mkpath
f.open("w") {|f| f.write Process.pid }
end

ProjectBlocker.block(project)
end

def cleanup(project)
write_to_log_and_console "Builder for project '#{CRUISE_OPTIONS[:project_name]}' exited"
if project
ProjectBlocker.release(project) rescue nil
Platform.project_pid_file(project.name).delete
end

write_to_log_and_console "Builder for project '#{CRUISE_OPTIONS[:project_name]}' exited"
end

def load_project(path)
Expand All @@ -65,10 +76,7 @@ project = nil

begin
project = load_project(project_path)

# this will create builder.pid file in project's CC directory and grab an exclusive lock on it, or else
# blow up saying that something else is already locking it
ProjectBlocker.block(project)
startup(project)

write_to_log_and_console "Builder for project '#{project.name}' started"
puts "Logging to: #{File.expand_path(CRUISE_OPTIONS[:log_file_name])}"
Expand Down
3 changes: 2 additions & 1 deletion script/killtree
Expand Up @@ -8,7 +8,8 @@ def kill_tree(pid)
kill_tree(child)
end
end
system "kill -9 #{pid}"

system "kill -INT #{pid}"
end

kill_tree(ARGV[0])
2 changes: 1 addition & 1 deletion test/unit/project_test.rb
Expand Up @@ -527,7 +527,7 @@ class ProjectTest < ActiveSupport::TestCase
test "should be able to kill a build on demand" do
in_sandbox do |sandbox|
@project.path = sandbox.root
Platform.expects(:kill_child_process).with(@project.name)
Platform.expects(:kill_project_builder).with(@project.name)
@project.kill_build
end
end
Expand Down

0 comments on commit 17ca0f8

Please sign in to comment.