Skip to content

Commit

Permalink
factor stop out of runner.py
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Apr 9, 2012
1 parent b760f0f commit a986fe8
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 51 deletions.
56 changes: 5 additions & 51 deletions master/buildbot/scripts/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,47 +159,6 @@ class StopOptions(BasedirMixin, base.SubcommandOptions):
def getSynopsis(self):
return "Usage: buildbot stop [<basedir>]"

def stop(config, signame="TERM", wait=False):
import signal
basedir = config['basedir']
quiet = config['quiet']

if not base.isBuildmasterDir(config['basedir']):
print "not a buildmaster directory"
sys.exit(1)

os.chdir(basedir)
try:
with open("twistd.pid", "rt") as f:
pid = int(f.read().strip())
except:
raise BuildbotNotRunningError
signum = getattr(signal, "SIG"+signame)
timer = 0
try:
os.kill(pid, signum)
except OSError, e:
if e.errno != 3:
raise

if not wait:
if not quiet:
print "sent SIG%s to process" % signame
return
time.sleep(0.1)
while timer < 10:
# poll once per second until twistd.pid goes away, up to 10 seconds
try:
os.kill(pid, 0)
except OSError:
if not quiet:
print "buildbot process %d is dead" % pid
return
timer += 1
time.sleep(1)
if not quiet:
print "never saw process go away"

class RestartOptions(BasedirMixin, base.SubcommandOptions):
optFlags = [
['quiet', 'q', "Don't display startup log messages"],
Expand All @@ -215,14 +174,14 @@ def restart(config):
print "not a buildmaster directory"
sys.exit(1)

from buildbot.scripts.startup import start
from buildbot.scripts import stop, startup
try:
stop(config, wait=True)
stop.stop(config, wait=True)
except BuildbotNotRunningError:
pass
if not quiet:
print "now restarting buildbot process.."
start(config)
startup.start(config)

class StartOptions(BasedirMixin, base.SubcommandOptions):
optFlags = [
Expand Down Expand Up @@ -914,13 +873,8 @@ def run():

start(so)
elif command == "stop":
try:
stop(so, wait=True)
except BuildbotNotRunningError:
if not so['quiet']:
print "buildmaster not running"
sys.exit(0)

from buildbot.scripts import stop
sys.exit(stop.stop(so, wait=True))
elif command == "restart":
restart(so)
elif command == "reconfig" or command == "sighup":
Expand Down
71 changes: 71 additions & 0 deletions master/buildbot/scripts/stop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

import time
import os
import errno
import signal
from buildbot.scripts import base

def stop(config, signame="TERM", wait=False):
basedir = config['basedir']
quiet = config['quiet']

if not base.isBuildmasterDir(config['basedir']):
print "not a buildmaster directory"
return 1

pidfile = os.path.join(basedir, 'twistd.pid')
try:
with open(pidfile, "rt") as f:
pid = int(f.read().strip())
except:
if not config['quiet']:
print "buildmaster not running"
return 0

signum = getattr(signal, "SIG"+signame)
try:
os.kill(pid, signum)
except OSError, e:
if e.errno != errno.ESRCH:
raise
else:
if not config['quiet']:
print "buildmaster not running"
try:
os.unlink(pidfile)
except:
pass
return 0

if not wait:
if not quiet:
print "sent SIG%s to process" % signame
return 0

time.sleep(0.1)
for _ in range(10):
# poll once per second until twistd.pid goes away, up to 10 seconds
try:
os.kill(pid, 0)
except OSError:
if not quiet:
print "buildbot process %d is dead" % pid
return 0
time.sleep(1)
if not quiet:
print "never saw process go away"
return 1
119 changes: 119 additions & 0 deletions master/buildbot/test/unit/test_scripts_stop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members

from __future__ import with_statement

import os
import time
import signal
from twisted.trial import unittest
from buildbot.scripts import stop
from buildbot.test.util import dirs, misc, compat

def mkconfig(**kwargs):
config = dict(quiet=False, basedir=os.path.abspath('basedir'))
config.update(kwargs)
return config

class TestStop(misc.StdoutAssertionsMixin, dirs.DirsMixin, unittest.TestCase):

def setUp(self):
self.setUpDirs('basedir')
self.setUpStdoutAssertions()

def tearDown(self):
self.tearDownDirs()

# tests

def do_test_stop(self, config, kill_sequence, is_running=True, **kwargs):
with open(os.path.join('basedir', 'buildbot.tac'), 'wt') as f:
f.write("Application('buildmaster')")
if is_running:
with open("basedir/twistd.pid", 'wt') as f:
f.write('1234')
def sleep(t):
what, exp_t = kill_sequence.pop(0)
self.assertEqual((what, exp_t), ('sleep', t))
self.patch(time, 'sleep', sleep)
def kill(pid, signal):
exp_sig, result = kill_sequence.pop(0)
self.assertEqual((pid,signal), (1234,exp_sig))
if isinstance(result, Exception):
raise result
else:
return result
self.patch(os, 'kill', kill)
rv = stop.stop(config, **kwargs)
self.assertEqual(kill_sequence, [])
return rv

@compat.skipUnlessPlatformIs('posix')
def test_stop_not_running(self):
rv = self.do_test_stop(mkconfig(), [], is_running=False)
self.assertStdout('not running')
self.assertEqual(rv, 0)

@compat.skipUnlessPlatformIs('posix')
def test_stop_dead_but_pidfile_remains(self):
rv = self.do_test_stop(mkconfig(),
[ (signal.SIGTERM, OSError(3, 'No such process')) ])
self.assertEqual(rv, 0)
self.assertFalse(os.path.exists(os.path.join('basedir', 'twistd.pid')))
self.assertStdout('not running')

@compat.skipUnlessPlatformIs('posix')
def test_stop_dead_but_pidfile_remains_quiet(self):
rv = self.do_test_stop(mkconfig(quiet=True),
[ (signal.SIGTERM, OSError(3, 'No such process')) ],)
self.assertEqual(rv, 0)
self.assertFalse(os.path.exists(os.path.join('basedir', 'twistd.pid')))
self.assertWasQuiet()

@compat.skipUnlessPlatformIs('posix')
def test_stop_dead_but_pidfile_remains_wait(self):
rv = self.do_test_stop(mkconfig(),
[ (signal.SIGTERM, OSError(3, 'No such process')) ],
wait=True)
self.assertEqual(rv, 0)
self.assertFalse(os.path.exists(os.path.join('basedir', 'twistd.pid')))

@compat.skipUnlessPlatformIs('posix')
def test_stop_slow_death_wait(self):
rv = self.do_test_stop(mkconfig(), [
(signal.SIGTERM, None),
('sleep', 0.1),
(0, None), # polling..
('sleep', 1),
(0, None),
('sleep', 1),
(0, None),
('sleep', 1),
(0, OSError(3, 'No such process')),
],
wait=True)
self.assertStdout('is dead')
self.assertEqual(rv, 0)

@compat.skipUnlessPlatformIs('posix')
def test_stop_slow_death_wait_timeout(self):
rv = self.do_test_stop(mkconfig(), [
(signal.SIGTERM, None),
('sleep', 0.1), ] +
[ (0, None),
('sleep', 1), ] * 10,
wait=True)
self.assertStdout('never saw process')
self.assertEqual(rv, 1)

0 comments on commit a986fe8

Please sign in to comment.