Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add pidfile support

  • Loading branch information...
commit b260ca2ebcf76e6b3616c3a96e273733590d89df 1 parent efd62ad
@joejulian authored
Showing with 40 additions and 1 deletion.
  1. +34 −0 beaver/pidfile.py
  2. +6 −1 bin/beaver
View
34 beaver/pidfile.py
@@ -0,0 +1,34 @@
+import fcntl
+import os
+
+class PidFile(object):
+ """Context manager that locks a pid file. Implemented as class
+ not generator because daemon.py is calling .__exit__() with no parameters
+ instead of the None, None, None specified by PEP-343."""
+ # pylint: disable=R0903
+
+ def __init__(self, path):
+ self.path = path
+ self.pidfile = None
+
+ def __enter__(self):
+ self.pidfile = open(self.path, "a+")
+ try:
+ fcntl.flock(self.pidfile.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
+ except IOError:
+ raise SystemExit("Already running according to " + self.path)
+ self.pidfile.seek(0)
+ self.pidfile.truncate()
+ self.pidfile.write(str(os.getpid()))
+ self.pidfile.flush()
+ self.pidfile.seek(0)
+ return self.pidfile
+
+ def __exit__(self, exc_type=None, exc_value=None, exc_tb=None):
+ try:
+ self.pidfile.close()
+ except IOError as err:
+ # ok if file was just closed elsewhere
+ if err.errno != 9:
+ raise
+ os.remove(self.path)
View
7 bin/beaver
@@ -5,12 +5,14 @@ import argparse
import logging
import sys
import time
+import lockfile
import beaver.file_config
import beaver.beaver_config
import beaver.transport
import beaver.utils
import beaver.worker
+import beaver.pidfile
from beaver import __version__ as full_version
@@ -53,6 +55,7 @@ parser = argparse.ArgumentParser(description='Beaver logfile shipper',
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-c', '--configfile', help='ini config file path', dest='config', default='/dev/null')
parser.add_argument('-D', '--daemon', help='daemonize in the background', dest='daemon', default=False, action='store_true')
+parser.add_argument('-P', '--pid', help='path to pid file', default=None, dest='pidfile')
parser.add_argument('-d', '--debug', help='enable debug mode', dest='debug', default=False, action='store_true')
parser.add_argument('-f', '--files', help='space-separated filelist to watch, can include globs (*.log). Overrides --path argument', dest='files', default=None, nargs='+')
parser.add_argument('--format', help='format to use when sending to transport', default=None, dest='format', choices=['json', 'msgpack', 'string'])
@@ -119,7 +122,9 @@ def main(args):
if __name__ == "__main__":
if args.daemon:
- with daemon.DaemonContext():
+ with daemon.DaemonContext(
+ pidfile=beaver.pidfile.PidFile(args.pidfile),
+ ):
main(args = args)
else:
main(args = args)
Please sign in to comment.
Something went wrong with that request. Please try again.