def cleanup_old_sessions():
"""
Cleans up old session directories inside the `session_dir`. Any directories
found that are older than the `auth_timeout` (global gateone setting) will
be removed. The modification time is what will be checked.
"""
logging.debug("cleanup_old_sessions()")
disabled = timedelta(0) # If the user sets auth_timeout to "0"
session_dir = define_options()['session_dir']
#settings = get_settings(options.settings_dir)
settings = get_settings(define_options()['settings_dir'])
expiration_str = settings['*']['gateone'].get('auth_timeout', "14d")
expiration = convert_to_timedelta(expiration_str)
if expiration != disabled:
#for session in os.listdir(options.session_dir):
for session in os.listdir(session_dir):
# If it's in the SESSIONS dict it's still valid for sure
if session not in SESSIONS:
if len(session) != 45:
# Sessions are always 45 characters long. This check allows
# us to skip the 'broadcast' file which also lives in the
# session_dir. Why not just check for 'broacast'? Just in
# case we put something else there in the future.
continue
#session_path = os.path.join(options.session_dir, session)
session_path = os.path.join(session_dir, session)
mtime = time.localtime(os.stat(session_path).st_mtime)
# Convert to a datetime object for easier comparison
mtime = datetime.fromtimestamp(time.mktime(mtime))
if datetime.now() - mtime > expiration:
import shutil
from applications.utils import kill_session_processes
# The log is older than expiration, remove it and kill any
# processes that may be remaining.
kill_session_processes(session)
logger.info(_(
"Removing old session files due to age (>%s old): %s" %
(expiration_str, session_path)))
shutil.rmtree(session_path, ignore_errors=True)
评论列表
文章目录