示例#1
0
def run_main():
    logging.basicConfig(
        filename='DualisWatcher.log', level=logging.DEBUG,
        format='%(asctime)s  %(levelname)s  {%(module)s}  %(message)s', datefmt='%Y-%m-%d %H:%M:%S'
    )

    logging.info('--- main started ---------------------')
    if IS_DEBUG:
        logging.info('Debug-Mode detected. Errors will not be logged but instead re-risen.')
        debug_logger = logging.getLogger()
        debug_logger.setLevel(logging.ERROR)
        debug_logger.addHandler(ReRaiseOnError())

    try:
        logging.debug('Loading config...')
        config = ConfigHelper()
        config.load()
    except BaseException as e:
        logging.error('Error while trying to load the Configuration! Exiting...', extra={'exception':e})
        sys.exit(-1)

    mail_service = MailService(config)

    try:
        dualis = DualisService(config)

        logging.debug('Checking for changes for the configured Dualis-Account....')
        results = dualis.fetch_and_check_state()
        changes = results[0]
        course_names = results[1]

        if changes.diff_count > 0:
            logging.info('%s changes found for the configured Dualis-Account.'%(changes.diff_count))
            token = dualis.get_token()
            mail_service.notify_about_changes_in_results(changes, course_names, token)
            dualis.save_state()
        else:
            logging.info('No changes found for the configured Dualis-Account.')

        schedule = ScheduleService(config)
        if schedule.is_activated:
            logging.debug('Checking for changes for the configured Schedule...')
            changes = schedule.fetch_and_check_state()
            if len(changes) > 0:
                logging.info('%s changes found for the configured Schedule.'%(len(changes)))
                mail_service.notify_about_changes_in_schedule(changes, schedule.uid)
                schedule.save_state()
            else:
                logging.info('No changes found for the configured Schedule.')

        logging.debug('All done. Exiting...')
    except BaseException as e:
        error_formatted = traceback.format_exc()
        logging.error(error_formatted, extra={'exception':e})
        mail_service.notify_about_error(str(e))
        logging.debug('Exception-Handling completed. Exiting...', extra={'exception' : e})
        sys.exit(-1)
示例#2
0
def run_main(storage_path,
             skip_cert_verify=False,
             without_downloading_files=False):
    logging.basicConfig(
        filename=os.path.join(storage_path, 'MoodleDownloader.log'),
        level=logging.DEBUG,
        format='%(asctime)s  %(levelname)s  {%(module)s}  %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S')

    logging.info('--- main started ---------------------')
    Log.info('Moodle Downloader starting...')
    if IS_DEBUG:
        logging.info(
            'Debug-Mode detected. Errors will not be logged but instead' +
            ' re-risen.')
        debug_logger = logging.getLogger()
        debug_logger.setLevel(logging.ERROR)
        debug_logger.addHandler(ReRaiseOnError())

    try:
        logging.debug('Loading config...')
        Log.debug('Loading config...')
        config = ConfigHelper(storage_path)
        config.load()
    except BaseException as e:
        logging.error('Error while trying to load the Configuration! ' +
                      'Exiting...',
                      extra={'exception': e})
        Log.error('Error while trying to load the Configuration!')
        sys.exit(-1)

    r_client = False
    try:
        sentry_dsn = config.get_property('sentry_dsn')
        if sentry_dsn:
            sentry_sdk.init(sentry_dsn)
    except BaseException:
        pass

    mail_service = MailService(config)
    console_service = ConsoleService(config)

    try:
        moodle = MoodleService(config, storage_path, skip_cert_verify)

        logging.debug(
            'Checking for changes for the configured Moodle-Account....')
        Log.debug('Checking for changes for the configured Moodle-Account...')
        changed_courses = moodle.fetch_state()

        diff_count = 0

        logging.debug('Start downloading changed files...')
        Log.debug('Start downloading changed files...')

        if (without_downloading_files):
            downloader = FakeDownloadService(changed_courses, moodle,
                                             storage_path)
        else:
            downloader = DownloadService(changed_courses, moodle, storage_path)
        downloader.run()

        changed_courses_to_notify = moodle.recorder.changes_to_notify()

        for course in changed_courses:
            diff_count += len(course.files)

        if diff_count > 0:
            logging.info(
                '%s changes found for the configured Moodle-Account.' %
                (diff_count))

            Log.success('%s changes found for the configured Moodle-Account.' %
                        (diff_count))

            console_service.notify_about_changes_in_moodle(changed_courses)
        else:
            logging.info('No changes found for the configured Moodle-Account.')
            Log.warning('No changes found for the configured Moodle-Account.')

        if (len(changed_courses_to_notify) > 0):
            mail_service.notify_about_changes_in_moodle(
                changed_courses_to_notify)
            moodle.recorder.notified(changed_courses_to_notify)

        logging.debug('All done. Exiting...')
        Log.success('All done. Exiting..')
    except BaseException as e:
        error_formatted = traceback.format_exc()
        logging.error(error_formatted, extra={'exception': e})

        if r_client:
            sentry_sdk.capture_exception(e)

        mail_service.notify_about_error(str(e))

        logging.debug('Exception-Handling completed. Exiting...',
                      extra={'exception': e})
        Log.critical('Exception:\n%s' % (error_formatted))
        Log.error('The following error occurred during execution: %s' %
                  (str(e)))

        sys.exit(-1)
示例#3
0
def run_main():
    logging.basicConfig(
        filename='DualisWatcher.log',
        level=logging.DEBUG,
        format='%(asctime)s  %(levelname)s  {%(module)s}  %(message)s',
        datefmt='%Y-%m-%d %H:%M:%S')

    logging.info('--- main started ---------------------')
    if IS_DEBUG:
        logging.info(
            'Debug-Mode detected. Errors will not be logged but instead re-risen.'
        )
        debug_logger = logging.getLogger()
        debug_logger.setLevel(logging.ERROR)
        debug_logger.addHandler(ReRaiseOnError())

    try:
        logging.debug('Loading config...')
        config = ConfigHelper()
        config.load()
    except BaseException as e:
        logging.error(
            'Error while trying to load the Configuration! Exiting...',
            extra={'exception': e})
        sys.exit(-1)

    r_client = None
    try:
        sentry_dsn = config.get_property('sentry_dsn')
        if sentry_dsn:
            r_client = RavenClient(sentry_dsn,
                                   auto_log_stacks=True,
                                   release=fetch_git_sha(
                                       os.path.dirname(__file__)))
    except BaseException:
        pass

    mail_service = MailService(config)

    try:
        dualis = DualisService(config)

        logging.debug(
            'Checking for changes for the configured Dualis-Account....')
        results = dualis.fetch_and_check_state()
        changes = results[0]
        course_names = results[1]

        if changes.diff_count > 0:
            logging.info(
                '%s changes found for the configured Dualis-Account.' %
                (changes.diff_count))
            mail_service.notify_about_changes_in_results(changes, course_names)
            dualis.save_state()
        else:
            logging.info('No changes found for the configured Dualis-Account.')

        schedule = ScheduleService(config)
        if schedule.is_activated:
            logging.debug(
                'Checking for changes for the configured Schedule...')
            changes = schedule.fetch_and_check_state()
            if len(changes) > 0:
                logging.info('%s changes found for the configured Schedule.' %
                             (len(changes) - 1))
                mail_service.notify_about_changes_in_schedule(
                    changes, schedule.uid)
                schedule.save_state()
            else:
                logging.info('No changes found for the configured Schedule.')

        logging.debug('All done. Exiting...')
    except DualisSleepingError:
        logging.info('Dualis is sleeping, exiting and soon trying again.')
        sys.exit(-1)
    except BaseException as e:
        error_formatted = traceback.format_exc()
        logging.error(error_formatted, extra={'exception': e})

        if r_client:
            r_client.captureException(exec_info=True)

        mail_service.notify_about_error(str(e))

        logging.debug('Exception-Handling completed. Exiting...',
                      extra={'exception': e})
        sys.exit(-1)
示例#4
0
def run_main(storage_path,
             skip_cert_verify=False,
             without_downloading_files=False):

    log_formatter = logging.Formatter(
        '%(asctime)s  %(levelname)s  {%(module)s}  %(message)s',
        '%Y-%m-%d %H:%M:%S')
    log_file = os.path.join(storage_path, 'MoodleDownloader.log')
    log_handler = RotatingFileHandler(log_file,
                                      mode='a',
                                      maxBytes=1 * 1024 * 1024,
                                      backupCount=2,
                                      encoding=None,
                                      delay=0)

    log_handler.setFormatter(log_formatter)
    log_handler.setLevel(logging.DEBUG)

    app_log = logging.getLogger()
    app_log.setLevel(logging.DEBUG)
    app_log.addHandler(log_handler)

    logging.info('--- main started ---------------------')
    Log.info('Moodle Downloader starting...')
    if IS_DEBUG:
        logging.info(
            'Debug-Mode detected. Errors will not be logged but instead re-risen.'
        )
        debug_logger = logging.getLogger()
        debug_logger.setLevel(logging.ERROR)
        debug_logger.addHandler(ReRaiseOnError())

    try:
        logging.debug('Loading config...')
        Log.debug('Loading config...')
        config = ConfigHelper(storage_path)
        config.load()
    except BaseException as e:
        logging.error(
            'Error while trying to load the Configuration! Exiting...',
            extra={'exception': e})
        Log.error('Error while trying to load the Configuration!')
        sys.exit(-1)

    r_client = False
    try:
        sentry_dsn = config.get_property('sentry_dsn')
        if sentry_dsn:
            sentry_sdk.init(sentry_dsn)
    except BaseException:
        pass

    mail_service = MailService(config)
    tg_service = TelegramService(config)
    console_service = ConsoleService(config)

    PathTools.filename_character_map = config.get_filename_character_map()

    try:
        if not IS_DEBUG:
            process_lock.lock(storage_path)

        moodle = MoodleService(config, storage_path, skip_cert_verify)

        logging.debug(
            'Checking for changes for the configured Moodle-Account....')
        Log.debug('Checking for changes for the configured Moodle-Account...')
        changed_courses = moodle.fetch_state()

        logging.debug('Start downloading changed files...')
        Log.debug('Start downloading changed files...')

        if without_downloading_files:
            downloader = FakeDownloadService(changed_courses, moodle,
                                             storage_path)
        else:
            downloader = DownloadService(changed_courses, moodle, storage_path,
                                         skip_cert_verify)
        downloader.run()

        changed_courses_to_notify = moodle.recorder.changes_to_notify()

        if len(changed_courses_to_notify) > 0:
            console_service.notify_about_changes_in_moodle(
                changed_courses_to_notify)
            mail_service.notify_about_changes_in_moodle(
                changed_courses_to_notify)
            tg_service.notify_about_changes_in_moodle(
                changed_courses_to_notify)

            moodle.recorder.notified(changed_courses_to_notify)

        else:
            logging.info('No changes found for the configured Moodle-Account.')
            Log.warning('No changes found for the configured Moodle-Account.')

        process_lock.unlock(storage_path)

        logging.debug('All done. Exiting...')
        Log.success('All done. Exiting..')
    except BaseException as e:
        if not isinstance(e, process_lock.LockError):
            process_lock.unlock(storage_path)

        error_formatted = traceback.format_exc()
        logging.error(error_formatted, extra={'exception': e})

        if r_client:
            sentry_sdk.capture_exception(e)

        short_error = '%s\r\n%s' % (str(e), traceback.format_exc(limit=1))
        mail_service.notify_about_error(short_error)
        tg_service.notify_about_error(short_error)

        logging.debug('Exception-Handling completed. Exiting...',
                      extra={'exception': e})
        Log.critical('Exception:\n%s' % (error_formatted))
        Log.error('The following error occurred during execution: %s' %
                  (str(e)))

        sys.exit(-1)