ytsm/app/YtManagerApp/appmain.py

51 lines
1.3 KiB
Python
Raw Normal View History

2018-10-10 22:43:50 +00:00
import logging
import logging.handlers
import os
2018-12-21 10:28:06 +00:00
import sys
from django.conf import settings as dj_settings
from .management.appconfig import appconfig
from .management.jobs.synchronize import schedule_synchronize_global
from .scheduler import initialize_scheduler
from django.db.utils import OperationalError
2018-12-21 10:28:06 +00:00
def __initialize_logger():
log_dir = os.path.join(dj_settings.DATA_DIR, 'logs')
os.makedirs(log_dir, exist_ok=True)
2018-12-21 10:28:06 +00:00
handlers = []
file_handler = logging.handlers.RotatingFileHandler(
os.path.join(log_dir, "log.log"),
maxBytes=1024 * 1024,
backupCount=5
)
2018-12-21 10:28:06 +00:00
handlers.append(file_handler)
if dj_settings.DEBUG:
console_handler = logging.StreamHandler(stream=sys.stdout)
console_handler.setLevel(logging.DEBUG)
handlers.append(console_handler)
logging.basicConfig(
level=dj_settings.LOG_LEVEL,
format=dj_settings.LOG_FORMAT,
2018-12-21 10:28:06 +00:00
handlers=handlers
)
2018-10-10 22:43:50 +00:00
def main():
__initialize_logger()
try:
if appconfig.initialized:
initialize_scheduler()
schedule_synchronize_global()
except OperationalError:
# Settings table is not created when running migrate or makemigrations, so just don't do anything in this case.
pass
2018-10-10 22:43:50 +00:00
logging.info('Initialization complete.')