async def process_payments(): event.get(conf.PROCESS_INVOICE_EVENT_NAME).set() while True: await logic.process_invoices(logic.make_payment, config) await asyncio.sleep( config['custom']['sleep_if_no_payments_interval'])
async def register_invoice(invoice): stored_account = await operations.load_account_info(invoice.account_id) if stored_account is None: return Error('NoAccountForInvoice') # Do not check "removed by gdpr" state, since info about invoice (amount, game_id, etc) belongs to developers, not to users invoice_id = await operations.register_invoice(invoice) if invoice_id is not None: event.get(conf.PROCESS_INVOICE_EVENT_NAME).set() return Ok() stored_invoice = await operations.load_invoice(invoice.xsolla_id) if stored_invoice is None: return Error('UnknownError') if invoice == stored_invoice: event.get(conf.PROCESS_INVOICE_EVENT_NAME).set() return Ok() return Error('InvoiceDataDoesNotEqualToStored')
async def _update_game_data(account_id, type, data, force, logger, execute=db.sql): # TODO: rewrite not best solution of comparing json data logger.info('try to update account %s %s with data %s (force: %s)', account_id, type, data, force) result = await execute( '''INSERT INTO game_data (account, type, data, created_at, updated_at, synced_at) VALUES (%(account_id)s, %(type)s, %(data)s, NOW(), NOW(), NULL) ON CONFLICT (account, type) DO UPDATE SET data=EXCLUDED.data, updated_at=NOW() WHERE %(force)s OR game_data.data<>EXCLUDED.data RETURNING account''', { 'account_id': account_id, 'data': PGJson(data), 'type': type.value, 'force': force }) if result: event.get(conf.SYNC_EVENT_NAME).set()
async def force_game_data_update(account_id): result = await db.sql( 'UPDATE game_data SET synced_at=NULL WHERE account=%(account_id)s RETURNING id', {'account_id': account_id}) if result: event.get(conf.SYNC_EVENT_NAME).set()
async def update_users(): event.get(conf.SYNC_EVENT_NAME).set() while True: try: await logic.sync_users(DISCORD_BOT, config) except Exception: logging.exception( 'error in user synchonisation task, sleep and continue') await asyncio.sleep(config['sleep_on_sync_users_error'])
async def bind_discord_user(bind_code, discord_id): result = await db.transaction(_bind_discord_user, { 'bind_code': bind_code, 'discord_id': discord_id }) if result.is_success(): event.get(conf.SYNC_EVENT_NAME).set() return result
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 _unbind_discord_user(execute, arguments): discord_id = arguments['discord_id'] result = await execute( 'UPDATE accounts SET game_id=NULL, updated_at=NOW() WHERE discord_id=%(discord_id)s RETURNING id', {'discord_id': discord_id}) if not result: return await update_game_data(account_id=result[0]['id'], nickname=None, roles=[], force=True, execute=execute) event.get(conf.SYNC_EVENT_NAME).set()
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 force_all_game_data_update(): await db.sql('UPDATE game_data SET synced_at=NULL') event.get(conf.SYNC_EVENT_NAME).set()
def check_event_setupped(self, setupped): event.get(conf.SYNC_EVENT_NAME).clear() yield self.assertEqual(event.get(conf.SYNC_EVENT_NAME).is_set(), setupped)
def check_event_setupped(self, setupped): event.get(conf.PROCESS_INVOICE_EVENT_NAME).clear() yield self.assertEqual( event.get(conf.PROCESS_INVOICE_EVENT_NAME).is_set(), setupped)