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

Deal correctly with errors when archiving logfiles #27

Merged
merged 1 commit into from
Sep 3, 2020
Merged
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
35 changes: 25 additions & 10 deletions src/ngamsServer/ngamsServer/janitor/rotated_logfiles_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
#
"""Deals with logging rotation files, archiving them, and removing old ones"""

import errno
import functools
import glob
import logging
import os
import socket

from ngamsLib.ngamsCore import rmFile, mvFile, loadPlugInEntryPoint
from ngamsLib import ngamsLib
Expand Down Expand Up @@ -52,6 +54,17 @@ def get_logfile_handler_plugins(cfg):

return _lh_plugins


def try_archiving(cfg, srv, fname):
if not cfg.getArchiveRotatedLogfiles():
return
file_uri = "file://" + fname
host, port = srv.get_self_endpoint()
proto = srv.get_server_access_proto()
ngamsPClient.ngamsPClient(host, port, proto=proto).archive(
file_uri, 'ngas/nglog')


def run(srvObj, stopEvt):

cfg = srvObj.getCfg()
Expand All @@ -61,19 +74,21 @@ def run(srvObj, stopEvt):
# ngamsServer.NgasRotatingFileHandler class, which is the file handler
# attached to the root logger in the server process
logger.debug("Checking if there are unsaved rotated logfiles")
for unsaved in glob.glob(os.path.join(logdir, 'LOG-ROTATE-*.nglog.unsaved')):
for unsaved in sorted(glob.glob(os.path.join(logdir, 'LOG-ROTATE-*.nglog.unsaved'))):

# Remove the .unsaved bit, leave the rest
fname = '.'.join(unsaved.split('.')[:-1])
mvFile(unsaved, fname)

# Connect to the server and send a pull ARCHIVE request
if cfg.getArchiveRotatedLogfiles():
file_uri = "file://" + fname
host, port = srvObj.get_self_endpoint()
proto = srvObj.get_server_access_proto()
ngamsPClient.ngamsPClient(host, port, proto=proto).archive(
file_uri, 'ngas/nglog')
try:
mvFile(unsaved, fname)
try_archiving(cfg, srvObj, fname)
except Exception as e:
mvFile(fname, unsaved)
Comment on lines -68 to +85
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on our conversation, is it necessary to first mvFile then on a possible exception restore? Is it more worthwhile to mvFile only after the successful archive?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I absolutely agree that the first move is not required in principle, and would make exception handling simpler. However, the ngamsPClient.archive method doesn't allow to pass down arbitrary filenames, and will always use the basename of the file on disk that is being archived.

As discussed, I'll just go ahead then with these changes, but will create an issue to remember implementing this improvement in the future.

if isinstance(e, socket.error) and e.errno == errno.ECONNREFUSED:
# this is expected when the server is just starting up,
# let's ignore for now
logger.warning("Server not up yet, postponing processing of %s", unsaved)
continue
raise

# Do additional things with our logfiles
for plugin in get_logfile_handler_plugins(cfg):
Expand Down