1
0
mirror of https://github.com/spl0k/supysonic.git synced 2024-11-10 04:02:17 +00:00
supysonic/bin/supysonic-watcher
2019-03-31 18:06:45 +02:00

61 lines
1.7 KiB
Python
Executable File

#!/usr/bin/env python
# coding: utf-8
# This file is part of Supysonic.
# Supysonic is a Python implementation of the Subsonic server API.
#
# Copyright (C) 2014-2019 Alban 'spl0k' Féron
#
# Distributed under terms of the GNU AGPLv3 license.
import logging
from logging.handlers import TimedRotatingFileHandler
from signal import signal, SIGTERM, SIGINT
from time import sleep
from supysonic.config import IniConfig
from supysonic.db import init_database, release_database
from supysonic.watcher import SupysonicWatcher
logger = logging.getLogger('supysonic')
watcher = None
def setup_logging(config):
if config['log_file']:
if config['log_file'] == '/dev/null':
log_handler = logging.NullHandler()
else:
log_handler = TimedRotatingFileHandler(config['log_file'], when = 'midnight')
log_handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(message)s"))
else:
log_handler = logging.StreamHandler()
log_handler.setFormatter(logging.Formatter("[%(levelname)s] %(message)s"))
logger.addHandler(log_handler)
if 'log_level' in config:
level = getattr(logging, config['log_level'].upper(), logging.NOTSET)
logger.setLevel(level)
def __terminate(signum, frame):
logger.debug("Got signal %i. Stopping...", signum)
watcher.stop()
release_database()
if __name__ == "__main__":
config = IniConfig.from_common_locations()
setup_logging(config.DAEMON)
signal(SIGTERM, __terminate)
signal(SIGINT, __terminate)
init_database(config.BASE['database_uri'])
watcher = SupysonicWatcher(config)
watcher.start()
while watcher.running:
sleep(2)
release_database()