@@ -0,0 +1,2023 @@
+#!/usr/bin/env python3%0A#%0A# Rotates and (optionally) compresses Supervisor logs using %60logrotate%60%0A#%0A# See:%0A#%0A# * https://www.rounds.com/blog/easy-logging-with-logrotate-and-supervisord/%0A# * https://gist.github.com/glarrain/6165987%0A#%0A%0Aimport os%0Aimport subprocess%0Aimport tempfile%0A%0Afrom mediawords.util.config import get_config%0Afrom mediawords.util.paths import mc_root_path%0Afrom mediawords.util.log import create_logger%0A%0A# Max. size of a single log file (in bytes)%0A__LOG_MAX_SIZE = 100 * 1024 * 1024%0A%0A# Number of old logs to keep%0A__OLD_LOG_COUNT = 7%0A%0Al = create_logger(__name__)%0A%0A%0A# noinspection SpellCheckingInspection%0Adef rotate_supervisor_logs():%0A    root_path = mc_root_path()%0A    l.debug('Media Cloud root path: %25s' %25 root_path)%0A%0A    config = get_config()%0A    child_log_dir = config%5B'supervisor'%5D%5B'childlogdir'%5D%0A    l.debug('Child log directory: %25s' %25 child_log_dir)%0A%0A    supervisor_logs_dir = os.path.join(root_path, child_log_dir)%0A    l.info('Supervisor logs path: %25s' %25 supervisor_logs_dir)%0A%0A    if not os.path.isdir(supervisor_logs_dir):%0A        raise Exception('Supervisor logs directory does not exist at path: %25s' %25 supervisor_logs_dir)%0A%0A    logrotate_config = '''%0A%25(supervisor_logs_dir)s/*.log %7B%0A    maxsize %25(log_max_size)d%0A    rotate %25(old_log_count)d%0A    copytruncate%0A    compress%0A    missingok%0A    notifempty%0A%7D%0A''' %25 %7B%0A        'supervisor_logs_dir': supervisor_logs_dir,%0A        'log_max_size': __LOG_MAX_SIZE,%0A        'old_log_count': __OLD_LOG_COUNT,%0A    %7D%0A%0A    logrotate_temp_fd, logrotate_temp_config_path = tempfile.mkstemp(suffix='.conf', prefix='logrotate')%0A    l.debug('Temporary logtorate config path: %25s' %25 logrotate_temp_config_path)%0A%0A    with os.fdopen(logrotate_temp_fd, 'w') as tmp:%0A        tmp.write(logrotate_config)%0A%0A    l.info('Running logrotate...')%0A    subprocess.check_call(%5B'logrotate', '--verbose', logrotate_temp_config_path%5D)%0A%0A    l.debug('Cleaning up temporary logrotate config...')%0A    os.unlink(logrotate_temp_config_path)%0A%0A%0Aif __name__ == '__main__':%0A    rotate_supervisor_logs()%0A

Add script to rotate Supervisor log files