2018-10-10 22:43:50 +00:00
|
|
|
import logging
|
2018-12-09 23:15:10 +00:00
|
|
|
import logging.handlers
|
|
|
|
import os
|
2018-12-21 10:28:06 +00:00
|
|
|
import sys
|
2018-12-09 23:15:10 +00:00
|
|
|
|
|
|
|
from django.conf import settings as dj_settings
|
|
|
|
|
2019-12-18 22:27:06 +00:00
|
|
|
from YtManagerApp.services.scheduler.jobs.synchronize_job import SynchronizeJob
|
2019-12-16 20:19:50 +00:00
|
|
|
from YtManagerApp.services import Services
|
2018-12-10 21:23:00 +00:00
|
|
|
from django.db.utils import OperationalError
|
2018-12-09 23:15:10 +00:00
|
|
|
|
2018-12-21 10:28:06 +00:00
|
|
|
|
2018-12-09 23:15:10 +00:00
|
|
|
def __initialize_logger():
|
|
|
|
log_dir = os.path.join(dj_settings.DATA_DIR, 'logs')
|
|
|
|
os.makedirs(log_dir, exist_ok=True)
|
|
|
|
|
|
|
|
file_handler = logging.handlers.RotatingFileHandler(
|
|
|
|
os.path.join(log_dir, "log.log"),
|
|
|
|
maxBytes=1024 * 1024,
|
|
|
|
backupCount=5
|
|
|
|
)
|
2018-12-29 22:10:30 +00:00
|
|
|
file_handler.setLevel(dj_settings.LOG_LEVEL)
|
|
|
|
file_handler.setFormatter(logging.Formatter(dj_settings.LOG_FORMAT))
|
|
|
|
logging.root.addHandler(file_handler)
|
|
|
|
logging.root.setLevel(dj_settings.LOG_LEVEL)
|
2018-12-21 10:28:06 +00:00
|
|
|
|
|
|
|
if dj_settings.DEBUG:
|
|
|
|
console_handler = logging.StreamHandler(stream=sys.stdout)
|
|
|
|
console_handler.setLevel(logging.DEBUG)
|
2018-12-29 22:10:30 +00:00
|
|
|
console_handler.setFormatter(logging.Formatter(dj_settings.CONSOLE_LOG_FORMAT))
|
|
|
|
logging.root.addHandler(console_handler)
|
2018-12-09 23:15:10 +00:00
|
|
|
|
2018-10-10 22:43:50 +00:00
|
|
|
|
|
|
|
def main():
|
2018-12-09 23:15:10 +00:00
|
|
|
__initialize_logger()
|
|
|
|
|
2018-12-10 21:23:00 +00:00
|
|
|
try:
|
2019-12-18 22:27:06 +00:00
|
|
|
if Services.appConfig().initialized:
|
|
|
|
Services.scheduler().initialize()
|
2020-04-10 21:42:37 +00:00
|
|
|
SynchronizeJob.schedule_global_job()
|
2018-12-10 21:23:00 +00:00
|
|
|
except OperationalError:
|
2019-08-14 14:14:16 +00:00
|
|
|
# Settings table is not created when running migrate or makemigrations;
|
|
|
|
# Just don't do anything in this case.
|
2018-12-10 21:23:00 +00:00
|
|
|
pass
|
2018-12-09 23:15:10 +00:00
|
|
|
|
2018-10-10 22:43:50 +00:00
|
|
|
logging.info('Initialization complete.')
|