# Path: myDevices/utils/logger.py
# def exception(message):
#     if checkFlood(message):
#         return
#     LOGGER.exception(message)
# 
# def info(message):
#     # if checkFlood(message):
#     #     return
#     LOGGER.info(message)
# 
# def warn(message):
#     if checkFlood(message):
#         return
#     LOGGER.warn(message)
# 
# def error(message, *args, **kwargs):
#     if checkFlood(message):
#         return
#     LOGGER.error(message, *args, **kwargs)
# 
# def debug(message):
#     outerFrame = getouterframes(currentframe())[1][0]
#     (args, _, _, values) = getargvalues(outerFrame)
#     argsValue = ''
#     
#     for i in args:
#         if i is 'self':
#             continue
#         argsValue += "(%s=%s)" % (i, str(values[i]))
#     
#     stack = extract_stack()
#     (filename, line, procname, text) = stack[-2]
#     LOGGER.debug(str(filename) + ' ' + str(procname) + str(argsValue) + ':' + str(line) + '> '  + str(message))
# 
# Path: myDevices/utils/subprocess.py
# def executeCommand(command, increaseMemoryLimit=False, disablePipe=False):
#     """Execute a specified command, increasing the processes memory limits if specified"""
#     debug('executeCommand: ' +  command)
#     output = ''
#     returncode = 1
#     try:
#         preexec = None
#         pipe = PIPE
#         if increaseMemoryLimit:
#             preexec = setMemoryLimits
#         if disablePipe:
#             debug('Disable pipe to prevent child exiting when parent exits')
#             pipe = DEVNULL
#         process = Popen(command, stdout=pipe, stderr=pipe, shell=True, preexec_fn=preexec)
#         (stdout_data, stderr_data) = process.communicate()
#         returncode = process.wait()
#         returncode = process.returncode
#         # debug('executeCommand: stdout_data {}, stderr_data {}'.format(stdout_data, stderr_data))
#         if stdout_data:
#             output = stdout_data.decode('utf-8')
#             stdout_data = None
#     except:
#         exception('executeCommand failed: ' + command)
#     return (output, returncode)


from sys import exit
from datetime import datetime
from myDevices.utils.logger import exception, info, warn, error, debug
from myDevices.utils.subprocess import executeCommand
        #-3=Temporary failure in name resolution
        info('Daemon failure handling ' + str(error))
        if error in errorList:
            Daemon.Restart()
        if component not in failureCount:
            Daemon.Reset(component)
        failureCount[component] += 1
        now = datetime.now()
        if startFailure[component] == 0:
            startFailure[component] = now
        elapsedTime = now - startFailure[component]
        if (elapsedTime.total_seconds() >= RESET_TIMEOUT) or (failureCount[component] > FAILURE_COUNT):
            warn('Daemon::OnFailure myDevices is going to restart after ' + str(component) + ' failed: ' + str(elapsedTime.total_seconds()) + ' seconds and ' + str(failureCount) + ' times')
            Daemon.Restart()

    @staticmethod
    def Reset(component):
        """Reset failure count for component"""
        startFailure[component] = 0
        failureCount[component] = 0

    @staticmethod
    def Restart():
        """Restart the agent daemon"""
        try:
            info('Daemon restarting myDevices' )
            (output, returncode) = executeCommand('sudo service myDevices restart')
            debug(str(output) + ' ' + str(returncode))
            del output
        except:

# Path: myDevices/utils/logger.py
# def exception(message):
#     if checkFlood(message):
#         return
#     LOGGER.exception(message)
# 
# def info(message):
#     # if checkFlood(message):
#     #     return
#     LOGGER.info(message)
# 
# def warn(message):
#     if checkFlood(message):
#         return
#     LOGGER.warn(message)
# 
# def error(message, *args, **kwargs):
#     if checkFlood(message):
#         return
#     LOGGER.error(message, *args, **kwargs)
# 
# def debug(message):
#     outerFrame = getouterframes(currentframe())[1][0]
#     (args, _, _, values) = getargvalues(outerFrame)
#     argsValue = ''
#     
#     for i in args:
#         if i is 'self':
#             continue
#         argsValue += "(%s=%s)" % (i, str(values[i]))
#     
#     stack = extract_stack()
#     (filename, line, procname, text) = stack[-2]
#     LOGGER.debug(str(filename) + ' ' + str(procname) + str(argsValue) + ':' + str(line) + '> '  + str(message))
# 
# Path: myDevices/utils/subprocess.py
# def executeCommand(command, increaseMemoryLimit=False, disablePipe=False):
#     """Execute a specified command, increasing the processes memory limits if specified"""
#     debug('executeCommand: ' +  command)
#     output = ''
#     returncode = 1
#     try:
#         preexec = None
#         pipe = PIPE
#         if increaseMemoryLimit:
#             preexec = setMemoryLimits
#         if disablePipe:
#             debug('Disable pipe to prevent child exiting when parent exits')
#             pipe = DEVNULL
#         process = Popen(command, stdout=pipe, stderr=pipe, shell=True, preexec_fn=preexec)
#         (stdout_data, stderr_data) = process.communicate()
#         returncode = process.wait()
#         returncode = process.returncode
#         # debug('executeCommand: stdout_data {}, stderr_data {}'.format(stdout_data, stderr_data))
#         if stdout_data:
#             output = stdout_data.decode('utf-8')
#             stdout_data = None
#     except:
#         exception('executeCommand failed: ' + command)
#     return (output, returncode)


# Path: myDevices/utils/daemon.py
from sys import exit
from datetime import datetime
from myDevices.utils.logger import exception, info, warn, error, debug
from myDevices.utils.subprocess import executeCommand

        #-3=Temporary failure in name resolution
        info('Daemon failure handling ' + str(error))
        if error in errorList:
            Daemon.Restart()
        if component not in failureCount:
            Daemon.Reset(component)
        failureCount[component] += 1
        now = datetime.now()
        if startFailure[component] == 0:
            startFailure[component] = now
        elapsedTime = now - startFailure[component]
        if (elapsedTime.total_seconds() >= RESET_TIMEOUT) or (failureCount[component] > FAILURE_COUNT):
            warn('Daemon::OnFailure myDevices is going to restart after ' + str(component) + ' failed: ' + str(elapsedTime.total_seconds()) + ' seconds and ' + str(failureCount) + ' times')
            Daemon.Restart()

    @staticmethod
    def Reset(component):
        """Reset failure count for component"""
        startFailure[component] = 0
        failureCount[component] = 0

    @staticmethod
    def Restart():
        """Restart the agent daemon"""
        try:
            info('Daemon restarting myDevices' )
            (output, returncode) = executeCommand('sudo service myDevices restart')
            debug(str(output) + ' ' + str(returncode))
            del output
        except:

            exception("Daemon::Restart unexpected error")