jellyfin-kodi/service.py

97 lines
2.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#################################################################################################
import logging
import os
2019-01-09 21:46:22 +00:00
import threading
import sys
2015-03-13 21:24:59 +00:00
import xbmc
import xbmcvfs
import xbmcaddon
2015-03-13 21:24:59 +00:00
#################################################################################################
__addon__ = xbmcaddon.Addon(id='plugin.video.emby')
__base__ = xbmc.translatePath(os.path.join(__addon__.getAddonInfo('path'), 'resources', 'lib')).decode('utf-8')
__pcache__ = xbmc.translatePath(os.path.join(__addon__.getAddonInfo('profile'), 'emby')).decode('utf-8')
__cache__ = xbmc.translatePath('special://temp/emby').decode('utf-8')
if not xbmcvfs.exists(__pcache__ + '/'):
from resources.lib.helper.utils import copytree
2018-10-18 12:07:14 +00:00
copytree(os.path.join(__base__, 'objects'), os.path.join(__pcache__, 'objects'))
sys.path.insert(0, __cache__)
sys.path.insert(0, __pcache__)
2018-09-06 08:36:32 +00:00
sys.path.append(__base__)
2015-03-13 21:24:59 +00:00
#################################################################################################
2018-09-06 08:36:32 +00:00
from entrypoint import Service
from helper import settings
from emby import Emby
#################################################################################################
2018-09-06 08:36:32 +00:00
LOG = logging.getLogger("EMBY.service")
DELAY = int(settings('startupDelay') if settings('SyncInstallRunDone.bool') else 4 or 0)
2015-04-06 17:17:32 +00:00
#################################################################################################
2018-09-06 08:36:32 +00:00
2019-01-09 21:46:22 +00:00
class ServiceManager(threading.Thread):
''' Service thread.
To allow to restart and reload modules internally.
'''
exception = None
def __init__(self):
threading.Thread.__init__(self)
def run(self):
2019-01-13 10:21:35 +00:00
service = None
2019-01-09 21:46:22 +00:00
try:
2019-01-13 10:21:35 +00:00
service = Service()
if DELAY and xbmc.Monitor().waitForAbort(DELAY):
raise Exception("Aborted during startup delay")
2019-01-09 21:46:22 +00:00
service.service()
except Exception as error:
2019-01-13 10:21:35 +00:00
if not 'ExitService' in error and service is not None:
2019-01-09 21:46:22 +00:00
service.shutdown()
elif 'RestartService' in error:
service.reload_objects()
2019-01-09 21:46:22 +00:00
self.exception = error
2016-09-09 03:13:25 +00:00
if __name__ == "__main__":
2018-10-03 23:25:51 +00:00
LOG.warn("-->[ service ]")
2018-09-06 08:36:32 +00:00
LOG.warn("Delay startup by %s seconds.", DELAY)
while True:
2018-10-03 23:25:51 +00:00
try:
2019-01-09 21:46:22 +00:00
session = ServiceManager()
session.start()
session.join() # Block until the thread exits.
2019-01-09 21:46:22 +00:00
if 'RestartService' in session.exception:
continue
except Exception as error:
''' Issue initializing the service.
'''
2018-10-03 23:25:51 +00:00
LOG.exception(error)
2018-10-15 11:26:25 +00:00
break
2018-10-03 23:25:51 +00:00
LOG.warn("--<[ service ]")