Пример #1
0
    def refresh_session(cls, force_refresh=False):
        with cls._session_lock.writer_lock:
            do_start_timer = False

            if force_refresh or cls._do_refresh_session():
                do_start_timer = True

                cls._clear_nimble_session_id_map()

                session = cls._refresh_session()

                if session:
                    cls._session = session

                    with SmoothStreamsDatabase.get_write_lock(
                    ), SmoothStreamsDatabase.get_access_lock().shared_lock:
                        db_session = SmoothStreamsDatabase.create_session()

                        try:
                            db_session.merge(
                                SmoothStreamsSetting(
                                    'session',
                                    jsonpickle.encode(cls._session)))
                            db_session.commit()
                        except Exception:
                            (type_, value_, traceback_) = sys.exc_info()
                            logger.error('\n'.join(
                                traceback.format_exception(
                                    type_, value_, traceback_)))

                            db_session.rollback()
                        finally:
                            db_session.close()

                if cls._refresh_session_timer:
                    cls._refresh_session_timer.cancel()
            elif not cls._refresh_session_timer:
                do_start_timer = True

            if do_start_timer:
                interval = (cls._get_session_parameter('expires_on') -
                            datetime.now(pytz.utc)).total_seconds() - 1800
                cls._refresh_session_timer = Timer(interval,
                                                   cls._timed_refresh_session)
                cls._refresh_session_timer.daemon = True
                cls._refresh_session_timer.start()

                logger.debug(
                    'Started SmoothStreams session refresh timer\n'
                    'Interval => %s seconds',
                    interval,
                )
Пример #2
0
    def _initialize(cls, **kwargs):
        do_refresh_session = False

        if 'do_refresh_session' in kwargs:
            do_refresh_session = kwargs['do_refresh_session']
        else:
            with SmoothStreamsDatabase.get_access_lock().shared_lock:
                db_session = SmoothStreamsDatabase.create_session()

                try:
                    setting_row = SmoothStreamsDatabaseAccess.query_setting(
                        db_session, 'session')

                    if setting_row is not None:
                        cls._session = jsonpickle.decode(setting_row.value)

                        current_date_time_in_utc = datetime.now(pytz.utc)

                        if current_date_time_in_utc < cls._session[
                                'expires_on']:
                            logger.debug(
                                'Loaded SmoothStreams session\n'
                                'Authorization token => %s\n'
                                'Expires on          => %s',
                                cls._session['authorization_token'],
                                cls._session['expires_on'].astimezone(
                                    tzlocal.get_localzone()).strftime(
                                        '%Y-%m-%d %H:%M:%S%z'),
                            )
                        else:
                            do_refresh_session = True
                    else:
                        do_refresh_session = True
                finally:
                    db_session.close()

        if do_refresh_session:
            cls.refresh_session(force_refresh=True)