async def start_transaction(message, **kwargs): logger = log.ContextLogger() try: transaction_id = await operations.start_transaction( operations=[ protobuf.to_operation(operation) for operation in message.operations ], lifetime=datetime.timedelta(seconds=message.lifetime), autocommit=message.autocommit, restrictions=protobuf.to_restrictions(message.restrictions), logger=logger) except exceptions.NoOperationsInTransaction as e: raise tt_exceptions.ApiError( code='bank.start_transaction.no_operations_specified', message=str(e)) except exceptions.BalanceChangeExceededRestrictions as e: raise tt_exceptions.ApiError( code='bank.start_transaction.restrictions_exceeded', message=str(e)) return bank_pb2.StartTransactionResponse(transaction_id=transaction_id)
async def sync_users(bot_instance, config): logger = log.ContextLogger() logger.info('synchonisation: start') sync_event = event.get(conf.SYNC_EVENT_NAME) while True: sync_event.clear() data_synced = await _sync_data(bot_instance, config, logger=logger) orphans_removed = await _sync_orphan_discords(bot_instance, config, logger=logger) if data_synced or orphans_removed: logger.info('synchonisation: go to next iteration') sync_event.set() continue logger.info('synchonisation: wait for updates') await sync_event.wait()
async def rollback_transaction(message, **kwargs): logger = log.ContextLogger() try: await operations.rollback_transaction( transaction_id=message.transaction_id, logger=logger) except exceptions.NoTransactionToRollback as e: raise tt_exceptions.ApiError( code='bank.rollback_transaction.no_transacton_to_rollback', message=str(e)) return bank_pb2.RollbackTransactionResponse()
async def commit_transaction(message, **kwargs): logger = log.ContextLogger() try: await operations.commit_transaction( transaction_id=message.transaction_id, logger=logger) except exceptions.NoTransactionToCommit as e: raise tt_exceptions.ApiError( code='bank.commit_transaction.no_transacton_to_commit', message=str(e)) return bank_pb2.CommitTransactionResponse()
async def on_ready(self): logger = log.ContextLogger() logger.info('logged as "%s"', self.user) for guild in self.guilds: await self.check_permissions(guild) await self.reset_not_binded_user_properties(logger=logger) # not best solution, but it work and fast in implementation # TODO: rewrite to exclude records changing in database logger.info('request all users resync') await operations.force_all_game_data_update()
async def rollback_hanged_transactions(): results = await db.sql('SELECT id FROM transactions WHERE state=%(state)s AND created_at + lifetime < NOW()', {'state': relations.TRANSACTION_STATE.OPENED.value}) logging.info('found %s outdated transactions', len(results)) for row in results: try: logger = log.ContextLogger() await rollback_transaction(row['id'], logger=logger) except exceptions.NoTransactionToRollback: continue logging.info('all outdated transactions rolledback')
async def process_invoices(processor, config): logger = log.ContextLogger() logger.info('process payments: start') requere_processing_event = event.get(conf.PROCESS_INVOICE_EVENT_NAME) while True: requere_processing_event.clear() processor_called = await process_invoice(config, processor, logger) if processor_called: logger.info('process payments: go to next iteration') requere_processing_event.set() continue logger.info('process_invoice: wait for updates') await requere_processing_event.wait()
async def fill_subreport(self, subreport): logger = log.ContextLogger() logger.info('fill subreport for %s', subreport) answer = await self.request_report(str(subreport.data['id']), logger=logger) if answer is None: logger.error('no answer received, stop processing') return None if answer.result == data_protector_pb2.PluginReportResponse.ResultType.Value( 'FAILED'): logger.error('answer FAILED, stop processing') return None if answer.result != data_protector_pb2.PluginReportResponse.ResultType.Value( 'SUCCESS'): raise NotImplementedError('unknowm result type') if answer.data: report = s11n.from_json(answer.data) else: report = [] data = copy.copy(subreport.data) new_data = [(subreport.source, data_type, data_value) for data_type, data_value in report] data['report'].extend(new_data) logger.error('subreport updated, collected data: %s', len(new_data)) return subreport.replace(state=relations.SUBREPORT_STATE.READY, data=data)
async def process_deletion_request(self, request): logger = log.ContextLogger() logger.info('process deletion for %s', request) answer = await self.request_deletion(str(request.data['id']), logger) if answer is None: logger.error('no answer received, stop processing') return False, None if answer.result == data_protector_pb2.PluginReportResponse.ResultType.Value( 'FAILED'): logger.error('answer FAILED, stop processing') return False, None if answer.result != data_protector_pb2.PluginReportResponse.ResultType.Value( 'SUCCESS'): raise NotImplementedError('unknowm result type') logger.info('deletion processed successfully') return True, None
import os import getpass from tt_web import log from tt_web import utils from tt_web import postgresql as db from tt_web.tests import helpers as web_helpers from .. import service from .. import operations TEST_LOGGER = log.ContextLogger() class BaseTests(web_helpers.BaseTests): def create_application(self): return service.create_application(get_config(), loop=self.loop) async def clean_environment(self, app=None): await operations.clean_database() def get_config(): config_path = os.path.join(os.path.dirname(__file__), 'fixtures', 'config.json') config = utils.load_config(config_path) service_user = getpass.getuser()