Ejemplo n.º 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)
Ejemplo n.º 2
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)