Skip to content

Commit 01ac962

Browse files
committed
Fix 50+ task creation with Discord bot enabled
Since we do not support batch importing tasks, the frontend spams the backend with a lot of task creations. Due to throttling from Discord, it fails to create the extra Discord category for 50+ challenges. This will break the channel creation. So the fix for this without supporting batch import, is to import with batches from 10 where each first task is done in serial and the other 9 in parallel.
1 parent f692065 commit 01ac962

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

api/src/discord/agile/hooks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ const discordMutationHook = (_build: Build) => (fieldContext: Context<any>) => {
198198
//add challenges to the ctf channel discord
199199
switch (fieldContext.scope.fieldName) {
200200
case "createTask":
201-
handleCreateTask(
201+
await handleCreateTask(
202202
guild,
203203
args.input.ctfId,
204204
args.input.title,

front/src/components/Dialogs/TaskImportDialog.vue

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,30 @@ export default defineComponent({
281281
}));
282282
},
283283
async createTasks(tasks: ParsedTask[]) {
284-
for (const task of tasks) {
285-
const r = await this.createTask(this.ctf.id, task);
286-
const newTask = r?.data?.createTask?.task;
287-
if (newTask) {
288-
await this.addTagsForTask(task.tags, makeId(newTask.id));
284+
const batchSize = 10;
285+
for (let i = 0; i < tasks.length; i += batchSize) {
286+
const batch = tasks.slice(i, i + batchSize);
287+
// Process the first task in the batch serially
288+
// to make sure the Discord bot will create the categories correctly.
289+
if (batch.length > 0) {
290+
const firstTask = batch[0];
291+
const r = await this.createTask(this.ctf.id, firstTask);
292+
const newTask = r?.data?.createTask?.task;
293+
if (newTask) {
294+
await this.addTagsForTask(firstTask.tags, makeId(newTask.id));
295+
}
296+
}
297+
// Process the rest of the batch in parallel
298+
if (batch.length > 1) {
299+
await Promise.all(
300+
batch.slice(1).map(async (task) => {
301+
const r = await this.createTask(this.ctf.id, task);
302+
const newTask = r?.data?.createTask?.task;
303+
if (newTask) {
304+
return this.addTagsForTask(task.tags, makeId(newTask.id));
305+
}
306+
}),
307+
);
289308
}
290309
}
291310
},

0 commit comments

Comments
 (0)