Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delay restart (retry) #60

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/httpok.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ Command-Line Syntax
Disable "eager" monitoring: do not check the URL or emit mail if no
monitored process is in the RUNNING state.

.. cmdoption:: -w <seconds>, --delay=<seconds>

Number of seconds delay before really make a restart.

.. cmdoption:: <URL>

The URL to which to issue a GET request.
Expand Down
20 changes: 16 additions & 4 deletions superlance/httpok.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

doc = """\
httpok.py [-p processname] [-a] [-g] [-t timeout] [-c status_code] [-b inbody]
[-m mail_address] [-s sendmail] URL
[-m mail_address] [-s sendmail] [-w delay_seconds] URL

Options:

Expand Down Expand Up @@ -81,6 +81,8 @@

-E -- not "eager": do not check URL / emit mail if no process we are
monitoring is in the RUNNING state.

-w -- specify number of seconds delay before really make a restart.

URL -- The URL to which to issue a GET request.

Expand Down Expand Up @@ -114,7 +116,7 @@ def usage():
class HTTPOk:
connclass = None
def __init__(self, rpc, programs, any, url, timeout, status, inbody,
email, sendmail, coredir, gcore, eager, retry_time):
email, sendmail, coredir, gcore, eager, delay, retry_time):
self.rpc = rpc
self.programs = programs
self.any = any
Expand All @@ -128,6 +130,7 @@ def __init__(self, rpc, programs, any, url, timeout, status, inbody,
self.coredir = coredir
self.gcore = gcore
self.eager = eager
self.delay = delay
self.stdin = sys.stdin
self.stdout = sys.stdout
self.stderr = sys.stderr
Expand Down Expand Up @@ -272,6 +275,10 @@ def mail(self, email, subject, msg):
def restart(self, spec, write):
namespec = make_namespec(spec['group'], spec['name'])
if spec['state'] is ProcessStates.RUNNING:
if self.delay and (spec['now']-spec['start'])<self.delay:
write('%s is in RUNNING state %d seconds (<%d), delay restarting' % \
(namespec, spec['now']-spec['start'], self.delay))
return
if self.coredir and self.gcore:
corename = os.path.join(self.coredir, namespec)
cmd = self.gcore + ' "%s" %s' % (corename, spec['pid'])
Expand Down Expand Up @@ -299,7 +306,7 @@ def restart(self, spec, write):

def main(argv=sys.argv):
import getopt
short_args="hp:at:c:b:s:m:g:d:eE"
short_args="hp:at:c:b:s:m:g:d:eEw:"
long_args=[
"help",
"program=",
Expand All @@ -313,6 +320,7 @@ def main(argv=sys.argv):
"coredir=",
"eager",
"not-eager",
"delay=",
]
arguments = argv[1:]
try:
Expand All @@ -334,6 +342,7 @@ def main(argv=sys.argv):
email = None
timeout = 10
retry_time = 10
delay = None
status = '200'
inbody = None

Expand Down Expand Up @@ -375,6 +384,9 @@ def main(argv=sys.argv):
if option in ('-E', '--not-eager'):
eager = False

if option in ('-w', '--delay'):
delay = int(value)

url = arguments[-1]

try:
Expand All @@ -388,7 +400,7 @@ def main(argv=sys.argv):
return

prog = HTTPOk(rpc, programs, any, url, timeout, status, inbody, email,
sendmail, coredir, gcore, eager, retry_time)
sendmail, coredir, gcore, eager, delay, retry_time)
prog.runforever()

if __name__ == '__main__':
Expand Down
24 changes: 22 additions & 2 deletions superlance/tests/httpok_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _makeOne(self, *opts):
return self._getTargetClass()(*opts)

def _makeOnePopulated(self, programs, any, response=None, exc=None,
gcore=None, coredir=None, eager=True):
gcore=None, coredir=None, eager=True, delay=None):
if response is None:
response = DummyResponse()
rpc = DummyRPCServer()
Expand All @@ -76,9 +76,10 @@ def _makeOnePopulated(self, programs, any, response=None, exc=None,
inbody = None
gcore = gcore
coredir = coredir
delay = delay
prog = self._makeOne(rpc, programs, any, url, timeout, status,
inbody, email, sendmail, coredir, gcore, eager,
retry_time)
delay, retry_time)
prog.stdin = StringIO()
prog.stdout = StringIO()
prog.stderr = StringIO()
Expand Down Expand Up @@ -312,5 +313,24 @@ def test_runforever_connrefused_error(self):
self.assertEqual(mailed[1],
'Subject: httpok for http://foo/bar: bad status returned')

def test_runforever_not_eager_running_delay150(self):
programs = ['foo', 'bar']
any = None
prog = self._makeOnePopulated(programs, any, exc=True, eager=False, delay=150)
prog.stdin.write('eventname:TICK len:0\n')
prog.stdin.seek(0)
prog.runforever(test=True)
lines = [x for x in prog.stderr.getvalue().split('\n') if x]
self.assertEqual(lines[0],
("Restarting selected processes ['foo', 'bar']")
)
self.assertEqual(lines[1], 'foo is in RUNNING state 100 seconds (<150), delay restarting')
self.assertEqual(lines[2], 'bar not in RUNNING state, NOT restarting')
mailed = prog.mailed.split('\n')
self.assertEqual(len(mailed), 9)
self.assertEqual(mailed[0], 'To: [email protected]')
self.assertEqual(mailed[1],
'Subject: httpok for http://foo/bar: bad status returned')

if __name__ == '__main__':
unittest.main()