Skip to content

Commit fef80aa

Browse files
committed
simplified the submission flow for discord
1 parent 0ab770e commit fef80aa

2 files changed

Lines changed: 52 additions & 67 deletions

File tree

src/discord-cluster-manager/cogs/leaderboard_cog.py

Lines changed: 32 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -61,52 +61,6 @@ async def select_gpu_view(
6161
await view.wait()
6262
return view
6363

64-
async def on_submit_hook( # noqa: C901
65-
self,
66-
interaction: discord.Interaction,
67-
leaderboard_name: Optional[str],
68-
script: discord.Attachment,
69-
mode: SubmissionMode,
70-
cmd_gpus: Optional[List[str]],
71-
) -> int:
72-
"""
73-
Called as the main body of a submission to route to the correct runner.
74-
"""
75-
# Read the template file
76-
submission_content = await script.read()
77-
78-
try:
79-
submission_content = submission_content.decode()
80-
except UnicodeError:
81-
await send_discord_message(
82-
interaction, "Could not decode your file. Is it UTF-8?", ephemeral=True
83-
)
84-
return -1
85-
86-
if not interaction.response.is_done():
87-
await interaction.response.defer(ephemeral=True)
88-
89-
req = SubmissionRequest(
90-
code=submission_content,
91-
file_name=script.filename,
92-
user_id=interaction.user.id,
93-
user_name=interaction.user.global_name or interaction.user.name,
94-
gpus=cmd_gpus,
95-
leaderboard=leaderboard_name,
96-
)
97-
req = prepare_submission(req, self.bot.leaderboard_db)
98-
99-
if req.gpus is None:
100-
view = await self.select_gpu_view(interaction, leaderboard_name, req.task_gpus)
101-
req.gpus = view.selected_gpus
102-
103-
reporter = MultiProgressReporterDiscord(interaction)
104-
sub_id, results = await self.bot.backend.submit_full(req, mode, reporter)
105-
106-
if mode == SubmissionMode.LEADERBOARD:
107-
await self.post_submit_hook(interaction, sub_id)
108-
return sub_id
109-
11064
def generate_run_verdict(self, run: RunItem, sub_data: SubmissionItem):
11165
medals = {1: "🥇 First", 2: "🥈 Second", 3: "🥉 Third"}
11266

@@ -176,18 +130,42 @@ async def submit(
176130
mode: SubmissionMode,
177131
gpu: Optional[str],
178132
):
179-
if not self.bot.backend.accepts_jobs:
133+
if gpu is not None:
134+
gpu = [gpu.strip() for gpu in gpu.split(",")]
135+
136+
submission_content = await script.read()
137+
138+
try:
139+
submission_content = submission_content.decode()
140+
except UnicodeError:
180141
await send_discord_message(
181-
interaction,
182-
"The bot is currently not accepting any new submissions, please try again later.",
183-
ephemeral=True,
142+
interaction, "Could not decode your file. Is it UTF-8?", ephemeral=True
184143
)
185-
return
144+
return -1
186145

187-
if gpu is not None:
188-
gpu = [gpu.strip() for gpu in gpu.split(",")]
146+
if not interaction.response.is_done():
147+
await interaction.response.defer(ephemeral=True)
189148

190-
return await self.on_submit_hook(interaction, leaderboard_name, script, mode, gpu)
149+
req = SubmissionRequest(
150+
code=submission_content,
151+
file_name=script.filename,
152+
user_id=interaction.user.id,
153+
user_name=interaction.user.global_name or interaction.user.name,
154+
gpus=gpu,
155+
leaderboard=leaderboard_name,
156+
)
157+
req = prepare_submission(req, self.bot.backend)
158+
159+
if req.gpus is None:
160+
view = await self.select_gpu_view(interaction, leaderboard_name, req.task_gpus)
161+
req.gpus = view.selected_gpus
162+
163+
reporter = MultiProgressReporterDiscord(interaction)
164+
sub_id, results = await self.bot.backend.submit_full(req, mode, reporter)
165+
166+
if mode == SubmissionMode.LEADERBOARD:
167+
await self.post_submit_hook(interaction, sub_id)
168+
return sub_id
191169

192170
@app_commands.command(name="test", description="Start a testing/debugging run")
193171
@app_commands.describe(

src/discord-cluster-manager/submission.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import dataclasses
3+
import typing
34
from datetime import datetime
45
from typing import Optional, Union
56

@@ -8,6 +9,9 @@
89
from task import LeaderboardTask
910
from utils import KernelBotError
1011

12+
if typing.TYPE_CHECKING:
13+
from backend import KernelBackend
14+
1115

1216
@dataclasses.dataclass
1317
class SubmissionRequest:
@@ -27,7 +31,14 @@ class ProcessedSubmissionRequest(SubmissionRequest):
2731
task_gpus: list
2832

2933

30-
def prepare_submission(req: SubmissionRequest, lb_db: LeaderboardDB) -> ProcessedSubmissionRequest:
34+
def prepare_submission(
35+
req: SubmissionRequest, backend: "KernelBackend"
36+
) -> ProcessedSubmissionRequest:
37+
if not backend.accepts_jobs:
38+
raise KernelBotError(
39+
"The bot is currently not accepting any new submissions, please try again later."
40+
)
41+
3142
if profanity.contains_profanity(req.file_name):
3243
raise KernelBotError("Please provide a non rude filename")
3344

@@ -41,10 +52,10 @@ def prepare_submission(req: SubmissionRequest, lb_db: LeaderboardDB) -> Processe
4152
req = handle_popcorn_directives(req)
4253
assert req.leaderboard is not None
4354

44-
leaderboard = lookup_leaderboard(req.leaderboard, lb_db)
55+
leaderboard = lookup_leaderboard(req.leaderboard, backend.db)
4556
check_deadline(leaderboard)
4657

47-
task_gpus = get_avail_gpus(req.leaderboard, lb_db)
58+
task_gpus = get_avail_gpus(req.leaderboard, backend.db)
4859

4960
if req.gpus is not None:
5061
for g in req.gpus:
@@ -70,7 +81,7 @@ def lookup_leaderboard(leaderboard: str, lb_db: LeaderboardDB) -> LeaderboardIte
7081
with lb_db as db:
7182
leaderboard_item = db.get_leaderboard(leaderboard)
7283
if not leaderboard_item:
73-
raise KernelBotError(f"Leaderboard {leaderboard} not found.")
84+
raise KernelBotError(f"Leaderboard `{leaderboard}` not found.")
7485
return leaderboard_item
7586

7687

@@ -106,15 +117,11 @@ def handle_popcorn_directives(req: SubmissionRequest) -> SubmissionRequest:
106117
req.gpus = info["gpus"]
107118

108119
if info["leaderboard"] is not None:
109-
if req.leaderboard is not None:
110-
pass
111-
# TODO how do we handle this?
112-
# await send_discord_message(
113-
# interaction,
114-
# "Leaderboard name specified in the command doesn't match the one "
115-
# f"in the submission script header. Submitting to `{leaderboard_name}`",
116-
# ephemeral=True,
117-
# )
120+
if req.leaderboard is not None and req.leaderboard != info["leaderboard"]:
121+
raise KernelBotError(
122+
f"Leaderboard name `{req.leaderboard}` specified in the command doesn't match the one "
123+
f"in the submission script header `{info['leaderboard']}`."
124+
)
118125
else:
119126
req.leaderboard = info["leaderboard"]
120127

0 commit comments

Comments
 (0)