Switch work_request to return bool indicating success

pull/12/head
Guillermo Rodriguez 2023-07-22 19:37:26 -03:00
parent 929b05a37d
commit 534b39f268
No known key found for this signature in database
GPG Key ID: EC3AB66D5D83B392
4 changed files with 25 additions and 21 deletions

View File

@ -121,7 +121,7 @@ class SkynetDiscordFrontend:
ctx: discord.ext.commands.context.Context | discord.Message, ctx: discord.ext.commands.context.Context | discord.Message,
file_id: str | None = None, file_id: str | None = None,
binary_data: str = '' binary_data: str = ''
): ) -> bool:
send = ctx.channel.send send = ctx.channel.send
if params['seed'] == None: if params['seed'] == None:
@ -168,7 +168,7 @@ class SkynetDiscordFrontend:
await self.bot.channel.send( await self.bot.channel.send(
status_msg, status_msg,
'skynet has suffered an internal error trying to fill this request') 'skynet has suffered an internal error trying to fill this request')
return return False
enqueue_tx_id = res['transaction_id'] enqueue_tx_id = res['transaction_id']
enqueue_tx_link = f'[**Your request on Skynet Explorer**](https://explorer.{DEFAULT_DOMAIN}/v2/explore/transaction/{enqueue_tx_id})' enqueue_tx_link = f'[**Your request on Skynet Explorer**](https://explorer.{DEFAULT_DOMAIN}/v2/explore/transaction/{enqueue_tx_id})'
@ -222,7 +222,6 @@ class SkynetDiscordFrontend:
await asyncio.sleep(1) await asyncio.sleep(1)
if not ipfs_hash: if not ipfs_hash:
timeout_text = f'\n[{timestamp_pretty()}] **timeout processing request**' timeout_text = f'\n[{timestamp_pretty()}] **timeout processing request**'
embed = discord.Embed( embed = discord.Embed(
title='live updates', title='live updates',
@ -230,7 +229,7 @@ class SkynetDiscordFrontend:
color=discord.Color.blue()) color=discord.Color.blue())
await message.edit(embed=embed) await message.edit(embed=embed)
return return False
tx_link = f'[**Your result on Skynet Explorer**](https://explorer.{DEFAULT_DOMAIN}/v2/explore/transaction/{tx_hash})' tx_link = f'[**Your result on Skynet Explorer**](https://explorer.{DEFAULT_DOMAIN}/v2/explore/transaction/{tx_hash})'
@ -254,6 +253,8 @@ class SkynetDiscordFrontend:
logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link}!') logging.error(f'couldn\'t get ipfs hosted image at {ipfs_link}!')
embed.add_field(name='Error', value=f'couldn\'t get ipfs hosted image [**here**]({ipfs_link})!') embed.add_field(name='Error', value=f'couldn\'t get ipfs hosted image [**here**]({ipfs_link})!')
await message.edit(embed=embed, view=SkynetView(self)) await message.edit(embed=embed, view=SkynetView(self))
return False
else: else:
logging.info(f'success! sending generated image') logging.info(f'success! sending generated image')
await message.delete() await message.delete()
@ -265,3 +266,5 @@ class SkynetDiscordFrontend:
else: # txt2img else: # txt2img
embed.set_image(url=ipfs_link) embed.set_image(url=ipfs_link)
await send(embed=embed, view=SkynetView(self)) await send(embed=embed, view=SkynetView(self))
return True

View File

@ -115,9 +115,9 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
await db_call( await db_call(
'update_user_stats', user.id, 'txt2img', last_prompt=prompt) 'update_user_stats', user.id, 'txt2img', last_prompt=prompt)
ec = await work_request(user, status_msg, 'txt2img', params, ctx) success = await work_request(user, status_msg, 'txt2img', params, ctx)
if ec == None: if success:
await db_call('increment_generated', user.id) await db_call('increment_generated', user.id)
@bot.command(name='redo', help='Redo last request') @bot.command(name='redo', help='Redo last request')
@ -153,13 +153,13 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
**user_config **user_config
} }
ec = await work_request( success = await work_request(
user, status_msg, 'redo', params, ctx, user, status_msg, 'redo', params, ctx,
file_id=file_id, file_id=file_id,
binary_data=binary binary_data=binary
) )
if ec == None: if success:
await db_call('increment_generated', user.id) await db_call('increment_generated', user.id)
@bot.command(name='img2img', help='Responds with an image') @bot.command(name='img2img', help='Responds with an image')
@ -243,17 +243,16 @@ def create_handler_context(frontend: 'SkynetDiscordFrontend'):
last_binary=ipfs_hash last_binary=ipfs_hash
) )
ec = await work_request( sucess = await work_request(
user, status_msg, 'img2img', params, ctx, user, status_msg, 'img2img', params, ctx,
file_id=file_id, file_id=file_id,
binary_data=ipfs_hash binary_data=ipfs_hash
) )
if ec == None: if success:
await db_call('increment_generated', user.id) await db_call('increment_generated', user.id)
# TODO: DELETE BELOW # TODO: DELETE BELOW
# user = 'testworker3' # user = 'testworker3'
# status_msg = 'status' # status_msg = 'status'

View File

@ -118,7 +118,7 @@ class SkynetTelegramFrontend:
params: dict, params: dict,
file_id: str | None = None, file_id: str | None = None,
binary_data: str = '' binary_data: str = ''
): ) -> bool:
if params['seed'] == None: if params['seed'] == None:
params['seed'] = random.randint(0, 0xFFFFFFFF) params['seed'] = random.randint(0, 0xFFFFFFFF)
@ -161,7 +161,7 @@ class SkynetTelegramFrontend:
await self.update_status_message( await self.update_status_message(
status_msg, status_msg,
'skynet has suffered an internal error trying to fill this request') 'skynet has suffered an internal error trying to fill this request')
return return False
enqueue_tx_id = res['transaction_id'] enqueue_tx_id = res['transaction_id']
enqueue_tx_link = hlink( enqueue_tx_link = hlink(
@ -223,7 +223,7 @@ class SkynetTelegramFrontend:
f'\n[{timestamp_pretty()}] <b>timeout processing request</b>', f'\n[{timestamp_pretty()}] <b>timeout processing request</b>',
parse_mode='HTML' parse_mode='HTML'
) )
return return False
tx_link = hlink( tx_link = hlink(
'Your result on Skynet Explorer', 'Your result on Skynet Explorer',
@ -253,7 +253,7 @@ class SkynetTelegramFrontend:
reply_markup=build_redo_menu(), reply_markup=build_redo_menu(),
parse_mode='HTML' parse_mode='HTML'
) )
return return False
png_img = resp.raw png_img = resp.raw
with Image.open(io.BytesIO(resp.raw)) as image: with Image.open(io.BytesIO(resp.raw)) as image:
@ -290,3 +290,5 @@ class SkynetTelegramFrontend:
reply_markup=build_redo_menu(), reply_markup=build_redo_menu(),
parse_mode='HTML' parse_mode='HTML'
) )
return True

View File

@ -157,9 +157,9 @@ def create_handler_context(frontend: 'SkynetTelegramFrontend'):
await db_call( await db_call(
'update_user_stats', user.id, 'txt2img', last_prompt=prompt) 'update_user_stats', user.id, 'txt2img', last_prompt=prompt)
ec = await work_request(user, status_msg, 'txt2img', params) success = await work_request(user, status_msg, 'txt2img', params)
if ec == None: if success:
await db_call('increment_generated', user.id) await db_call('increment_generated', user.id)
@ -243,13 +243,13 @@ def create_handler_context(frontend: 'SkynetTelegramFrontend'):
last_binary=ipfs_hash last_binary=ipfs_hash
) )
ec = await work_request( success = await work_request(
user, status_msg, 'img2img', params, user, status_msg, 'img2img', params,
file_id=file_id, file_id=file_id,
binary_data=ipfs_hash binary_data=ipfs_hash
) )
if ec == None: if success:
await db_call('increment_generated', user.id) await db_call('increment_generated', user.id)
@ -307,13 +307,13 @@ def create_handler_context(frontend: 'SkynetTelegramFrontend'):
**user_config **user_config
} }
ec = await work_request( success = await work_request(
user, status_msg, 'redo', params, user, status_msg, 'redo', params,
file_id=file_id, file_id=file_id,
binary_data=binary binary_data=binary
) )
if ec == None: if success:
await db_call('increment_generated', user.id) await db_call('increment_generated', user.id)
# "proxy" handlers just request routers # "proxy" handlers just request routers