Index: trunk/pybal/pybal/util.py |
— | — | @@ -9,8 +9,10 @@ |
10 | 10 | |
11 | 11 | class LogFile(object): |
12 | 12 | def __init__(self, filename): |
| 13 | + self.filename = filename |
| 14 | + self.lineEnded = True |
| 15 | + |
13 | 16 | self.file = file(filename, 'a') |
14 | | - self.lineEnded = True |
15 | 17 | |
16 | 18 | def write(self, s): |
17 | 19 | """ |
— | — | @@ -26,3 +28,12 @@ |
27 | 29 | self.lineEnded = line.endswith('\n') |
28 | 30 | |
29 | 31 | self.file.flush() |
| 32 | + |
| 33 | + def reopen(self): |
| 34 | + """ |
| 35 | + Close the logfile and reopen it. Useful for log rotation. |
| 36 | + """ |
| 37 | + |
| 38 | + self.file.close() |
| 39 | + self.file = file(self.filename, 'a') |
| 40 | + self.lineEnded = True |
Index: trunk/pybal/pybal/pybal.py |
— | — | @@ -366,7 +366,7 @@ |
367 | 367 | |
368 | 368 | # When the first child terminates, all processes in the second child |
369 | 369 | # are sent a SIGHUP, so it's ignored. |
370 | | - signal.signal( signal.SIGHUP, signal.SIG_IGN ) |
| 370 | + #signal.signal( signal.SIGHUP, signal.SIG_IGN ) |
371 | 371 | |
372 | 372 | try: |
373 | 373 | # Fork a second child to prevent zombies. Since the first child is |
— | — | @@ -397,11 +397,11 @@ |
398 | 398 | except ( AttributeError, ValueError ): |
399 | 399 | maxfd = 256 # default maximum |
400 | 400 | |
401 | | - for fd in range( 0, maxfd ): |
402 | | - try: |
403 | | - os.close( fd ) |
404 | | - except OSError: # ERROR (ignore) |
405 | | - pass |
| 401 | + #for fd in range( 0, maxfd ): |
| 402 | + # try: |
| 403 | + # os.close( fd ) |
| 404 | + # except OSError: # ERROR (ignore) |
| 405 | + # pass |
406 | 406 | |
407 | 407 | # Redirect the standard file descriptors to /dev/null. |
408 | 408 | os.open( "/dev/null", os.O_RDONLY ) # standard input (0) |
— | — | @@ -441,13 +441,22 @@ |
442 | 442 | |
443 | 443 | if signum == signal.SIGTERM: |
444 | 444 | terminate() |
| 445 | + elif signum == signal.SIGHUP: |
| 446 | + # Cycle logfiles |
| 447 | + from util import LogFile |
| 448 | + if isinstance(sys.stdout, LogFile): |
| 449 | + print "Cycling log file..." |
| 450 | + sys.stdout.reopen() |
445 | 451 | |
446 | 452 | def installSignalHandlers(): |
447 | 453 | """ |
448 | 454 | Installs Unix signal handlers, e.g. to run terminate() on TERM |
449 | 455 | """ |
450 | 456 | |
451 | | - signal.signal(signal.SIGTERM, sighandler) |
| 457 | + signals = [signal.SIGTERM, signal.SIGHUP] |
| 458 | + |
| 459 | + for sig in signals: |
| 460 | + signal.signal(sig, sighandler) |
452 | 461 | |
453 | 462 | def main(): |
454 | 463 | from twisted.internet import reactor |
— | — | @@ -476,12 +485,12 @@ |
477 | 486 | from util import LogFile |
478 | 487 | try: |
479 | 488 | logfile = '/var/log/pybal.log' |
480 | | - sys.stdout = LogFile(logfile) |
| 489 | + sys.stdout = sys.stderr = LogFile(logfile) |
481 | 490 | except: |
482 | 491 | print "Unable to open logfile %s, using stdout" % logfile |
483 | 492 | |
484 | 493 | # Install signal handlers |
485 | | - installSignalHandlers |
| 494 | + installSignalHandlers() |
486 | 495 | |
487 | 496 | for section in config.sections(): |
488 | 497 | cfgtuple = ( |