def set_timeout(self, timeout): ''' Sets global timeout of request processor. @param timeout: timeout ''' session = get_current_session() context = session.get_context() context['timeout'] = timeout session.update()
def begin(self, **kargs): ''' Begins a transaction and return it. @param auto_commit: auto commit flag @param pool_name: connection pool name @param is_root: root transaction flag @return: Transaction ''' auto_commit = kargs.get('auto_commit', False) pool_name = kargs.get('pool_name', TransactionManager.DEFAULT_TRANSACTION) if not pool_name: pool_name = TransactionManager.DEFAULT_TRANSACTION is_root = kargs.get('is_root', False) parent_transaction = None connection = None timeout = None if not is_root: parent_transaction = self.__get_current_root_transaction__( pool_name) if not issubclass(type(parent_transaction), TwoPhaseCommitTransaction): if parent_transaction is not None: connection = parent_transaction.get_connection() else: connection = self._database_manager.open(pool_name) trx = Transaction(self, connection, auto_commit, parent_transaction) trx._pool_name = pool_name current_session = get_current_session() if current_session is not None: client_request = current_session.get_client_request() if client_request.timeout is not None and client_request.timeout > 0: timeout = client_request.timeout self.set_timeout(trx, timeout) if not parent_transaction: connection.transaction = trx self.__add_root_transaction__(pool_name, trx) return trx
def get_current_channel_id(self): ''' Returns current channel that user logged in with. @rtype: str @return: channel ID ''' session = get_current_session() if session is not None: context = session.get_context() return context.get('channel') return None
def _before_execute_command_(self, command, *args, **kargs): ''' Runs before execution method of all hooks. @param command: command ''' # Setting start processing time in command manager current_session = get_current_session() internal_context = current_session.get_internal_context() internal_context['start_process_time'] = time.time() if len(self._hooks): for hook in self._hooks: if hook.is_enable(): hook.before_execute(self, command, *args, **kargs)
def before_execute(self, commander, command, *args, **kargs): ''' This method will be called before command execution. @param commander: commander @param command: command ''' if not self._is_required_to_record_request(command): return request = session_services.get_current_session().get_client_request() coordinator_services.record_request( command.get_option('recorder_type'), request) logging_services.debug( 'Request [{0}] for service [{1}] recorded.'.format( request.id, command.get_key()))
def exception(self, commander, command, error, *args, **kargs): ''' This method will be called whenever an exception occurred during executing a command. @param commander: commander @param command: command @param error: exception instance ''' if not self._is_required_to_record_request(command): return request = session_services.get_current_session().get_client_request() coordinator_services.set_failed(command.get_option('recorder_type'), request, error) logging_services.debug( 'Request [{0}] for service [{1}] failed.'.format( request.id, command.get_key()))
def get_timeout(self): ''' Returns get global timeout value. @rtype: int @return: timeout value ''' session = get_current_session() timeout = session.get_context().get('timeout') if timeout is None: config_store = \ config_services.get_app_config_store('request_processor') timeout = config_store.get('global', 'timeout') if timeout is not None: timeout = int(timeout) return timeout
def commit(self): # Checking request timeout if self._timeout is not None and self._timeout > 0: # Getting client request client_request = get_current_session().get_client_request() if (datetime.datetime.now() - client_request.recieve_date).seconds > self._timeout: self.rollback() message = _( 'Transaction timeout [{0} second(s)] reached for request [{1}].' ) raise TimeoutException( message.format(self._timeout, client_request)) if self.is_commitable(): self._set_status_(Transaction.StatusEnum.COMMITTING) if self.get_manager().commit(self): self._set_status_(Transaction.StatusEnum.COMMITTED) else: self._set_status_(Transaction.StatusEnum.COMMIT_FAILED) self.get_manager().rollback(self)
def _write_user_log(self, user, status, **options): """ Writes down user activity log. @param dict user: user info @param int status: user status """ if user is None: return with transaction_services.begin_root(): store = transaction_services.get_current_transaction_store() history = UserHistoryEntity() history.id = unicode(unique_id_services.get_id("uuid")) history.user_id = user.id current_session = session_services.get_current_session() if current_session is not None: history.user_history_client_ip = current_session.get_client_request().ip history.user_history_date = datetime.now() history.user_history_status = status history.user_history_message = unicode(options.get("message")) store.add(history)
def _create_request(self, *args, **kwargs): ''' Creates request object. ''' current_session = get_current_session() current_client_request = current_session.get_client_request() current_user_id = current_session.get_user_id() request = \ ClientRequest.from_dict({'request_date': datetime.datetime.now(), 'command_key': self.get_name(), 'command_args': args[1:], 'command_kwargs': kwargs, 'id': current_client_request.id, 'transaction_id': current_client_request.get('transaction_id'), 'trace_id': current_client_request.get('trace_id'), 'ip': current_session.get_client_ip(), 'ticket': '', 'user_name': '{0}-{1}'.format(current_user_id.branch_code, current_user_id.code), 'context': current_session.get_call_context()}) return request
def handler(signum, frame): # Getting client request client_request = get_current_session().get_client_request() message = _('Request timeout [{0} second(s)] reached for request [{1}].') raise TimeoutException(message.format(timeout, client_request))