Skip to content

Commit

Permalink
Make res.send ahead (#1343)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzdhybthu authored May 9, 2024
1 parent d9826e0 commit 41a3941
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 25 deletions.
32 changes: 22 additions & 10 deletions src/routes/arena.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ router.post("/create", authenticate(), async (req, res) => {

console.log("Dependencies checked!")

res.status(200).send("200 OK: Dependencies checked!");

const base_directory = await utils.get_base_directory();

const mkdir_promises = team_ids.map(team_id => {
Expand All @@ -149,7 +151,8 @@ router.post("/create", authenticate(), async (req, res) => {
const mkdir_result = await Promise.all(mkdir_promises);
console.debug("mkdir_result: ", mkdir_result);
if (mkdir_result.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Code directory creation failed");
// return res.status(500).send("500 Internal Server Error: Code directory creation failed");
return;
}

const files_exist_promises = player_codes_flat.map((player_code, index) => {
Expand Down Expand Up @@ -194,7 +197,8 @@ router.post("/create", authenticate(), async (req, res) => {
const download_results_flat = await Promise.all(download_promises);
console.debug("download_results: ", download_results_flat);
if (download_results_flat.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Code download failed");
// return res.status(500).send("500 Internal Server Error: Code download failed");
return;
}
}

Expand Down Expand Up @@ -222,7 +226,8 @@ router.post("/create", authenticate(), async (req, res) => {
`${base_directory}/${contest_name}/map/${map_id}/${map_id}.txt`, cos, config)
.catch((err) => {
console.log(`Download ${map_id}.txt failed: ${err}`)
return res.status(500).send("500 Internal Server Error: Map download failed");
// return res.status(500).send("500 Internal Server Error: Map download failed");
return;
});
}

Expand All @@ -241,7 +246,8 @@ router.post("/create", authenticate(), async (req, res) => {
const files_count = await Promise.all(files_count_promises);
console.debug("files_count: ", files_count);
if (files_count.some(count => count === -1)) {
return res.status(500).send("500 Internal Server Error: Code files count failed");
// return res.status(500).send("500 Internal Server Error: Code files count failed");
return;
}

const files_clean_promises = team_ids.map((team_id, index) => {
Expand Down Expand Up @@ -282,20 +288,23 @@ router.post("/create", authenticate(), async (req, res) => {
const files_clean_result = await Promise.all(files_clean_promises);
console.debug("files_clean: ", files_clean_result);
if (files_clean_result.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Code files clean failed");
// return res.status(500).send("500 Internal Server Error: Code files clean failed");
return;
}

console.log("Files cleaned!")

const room_id = await hasura.insert_room(contest_id, "Waiting", map_id);
console.debug("room_id: ", room_id);
if (!room_id) {
return res.status(500).send("500 Internal Server Error: Room not created");
// return res.status(500).send("500 Internal Server Error: Room not created");
return;
}

const insert_room_teams_affected_rows = await hasura.insert_room_teams(room_id, team_ids, team_labels, players_roles, players_codes);
if (insert_room_teams_affected_rows !== team_ids.length) {
return res.status(500).send("500 Internal Server Error: Room teams not created");
// return res.status(500).send("500 Internal Server Error: Room teams not created");
return;
}

console.log("Room created!")
Expand Down Expand Up @@ -324,7 +333,8 @@ router.post("/create", authenticate(), async (req, res) => {
const copy_result = await Promise.all(copy_promises);
console.debug("copy_result: ", copy_result);
if (copy_result.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Code copy failed");
// return res.status(500).send("500 Internal Server Error: Code copy failed");
return;
}

console.log("Files copied!")
Expand All @@ -340,11 +350,13 @@ router.post("/create", authenticate(), async (req, res) => {
});

console.log("Docker pushed!")
return res.status(200).send("200 OK: Arena created!");
// return res.status(200).send("200 OK: Arena created!");
return;

} catch (e) {
console.error(e);
return res.status(500).send("500 Internal Server Error: Unknown error" + e);
// return res.status(500).send("500 Internal Server Error: Unknown error" + e);
return;
}
});

Expand Down
49 changes: 34 additions & 15 deletions src/routes/competition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ router.post("/start-all", authenticate(), async (req, res) => {

console.log("Dependencies checked!");

res.status(200).send("200 OK: Dependencies checked!");

const base_directory = await utils.get_base_directory();

const mkdir_promises = team_list_available.map(team_id => {
Expand All @@ -115,7 +117,8 @@ router.post("/start-all", authenticate(), async (req, res) => {
const mkdir_result = await Promise.all(mkdir_promises);
console.debug("mkdir_result: ", mkdir_result);
if (mkdir_result.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Code directory creation failed");
// return res.status(500).send("500 Internal Server Error: Code directory creation failed");
return;
}

console.log("Directories created!");
Expand Down Expand Up @@ -161,7 +164,8 @@ router.post("/start-all", authenticate(), async (req, res) => {
const download_results = await Promise.all(download_promises);
console.debug("download_results: ", download_results);
if (download_results.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Code download failed");
// return res.status(500).send("500 Internal Server Error: Code download failed");
return;
}

console.log("Code downloaded!");
Expand All @@ -186,7 +190,8 @@ router.post("/start-all", authenticate(), async (req, res) => {
`${base_directory}/${contest_name}/map/${map_id}/${map_id}.txt`, cos, config)
.catch((err) => {
console.log(`Download ${map_id}.txt failed: ${err}`)
return res.status(500).send("500 Internal Server Error: Map download failed");
// return res.status(500).send("500 Internal Server Error: Map download failed");
return;
});
}

Expand Down Expand Up @@ -305,16 +310,19 @@ router.post("/start-all", authenticate(), async (req, res) => {
const start_results = await Promise.all(start_competition_promises);
console.debug("start_results: ", start_results);
if (start_results.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Competition start failed");
// return res.status(500).send("500 Internal Server Error: Competition start failed");
return;
}

console.log("Competitions started!");

return res.status(200).send("200 OK: Competition Created!");
// return res.status(200).send("200 OK: Competition Created!");
return;

} catch (err) {
console.error(err);
return res.status(500).send("500 Internal Server Error: " + err);
// return res.status(500).send("500 Internal Server Error: " + err);
return;
}

});
Expand Down Expand Up @@ -432,6 +440,8 @@ router.post("/start-one", authenticate(), async (req, res) => {

console.log("Dependencies checked!")

res.status(200).send("200 OK: Dependencies checked!");

const base_directory = await utils.get_base_directory();

const mkdir_promises = team_ids.map(team_id => {
Expand All @@ -447,7 +457,8 @@ router.post("/start-one", authenticate(), async (req, res) => {
const mkdir_result = await Promise.all(mkdir_promises);
console.debug("mkdir_result: ", mkdir_result);
if (mkdir_result.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Code directory creation failed");
// return res.status(500).send("500 Internal Server Error: Code directory creation failed");
return;
}

const files_exist_promises = player_codes_flat.map((player_code, index) => {
Expand Down Expand Up @@ -492,7 +503,8 @@ router.post("/start-one", authenticate(), async (req, res) => {
const download_results_flat = await Promise.all(download_promises);
console.debug("download_results: ", download_results_flat);
if (download_results_flat.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Code download failed");
// return res.status(500).send("500 Internal Server Error: Code download failed");
return;
}
}

Expand Down Expand Up @@ -520,7 +532,8 @@ router.post("/start-one", authenticate(), async (req, res) => {
`${base_directory}/${contest_name}/map/${map_id}/${map_id}.txt`, cos, config)
.catch((err) => {
console.log(`Download ${map_id}.txt failed: ${err}`)
return res.status(500).send("500 Internal Server Error: Map download failed");
// return res.status(500).send("500 Internal Server Error: Map download failed");
return;
});
}

Expand Down Expand Up @@ -577,18 +590,21 @@ router.post("/start-one", authenticate(), async (req, res) => {
const delete_results = await Promise.all(delete_room_promises);
console.debug("delete_results: ", delete_results);
if (delete_results.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Room delete failed");
// return res.status(500).send("500 Internal Server Error: Room delete failed");
return;
}

const room_id = await hasura.insert_room_competition(contest_id, "Waiting", map_id, round_id);
console.debug("room_id: ", room_id);
if (!room_id) {
return res.status(500).send("500 Internal Server Error: Room not created");
// return res.status(500).send("500 Internal Server Error: Room not created");
return;
}

const insert_room_teams_affected_rows = await hasura.insert_room_teams(room_id, team_ids, team_labels, players_roles, players_codes);
if (insert_room_teams_affected_rows !== team_ids.length) {
return res.status(500).send("500 Internal Server Error: Room teams not created");
// return res.status(500).send("500 Internal Server Error: Room teams not created");
return;
}

console.log("Room created!")
Expand Down Expand Up @@ -617,7 +633,8 @@ router.post("/start-one", authenticate(), async (req, res) => {
const copy_result = await Promise.all(copy_promises);
console.debug("copy_result: ", copy_result);
if (copy_result.some(result => !result)) {
return res.status(500).send("500 Internal Server Error: Code copy failed");
// return res.status(500).send("500 Internal Server Error: Code copy failed");
return;
}

console.log("Files copied!")
Expand All @@ -634,11 +651,13 @@ router.post("/start-one", authenticate(), async (req, res) => {
});

console.log("Docker pushed!")
return res.status(200).send("200 OK: Competition created!");
// return res.status(200).send("200 OK: Competition created!");
return;

} catch (e) {
console.error(e);
return res.status(500).send("500 Internal Server Error: Unknown error" + e);
// return res.status(500).send("500 Internal Server Error: Unknown error" + e);
return;
}
});

Expand Down

0 comments on commit 41a3941

Please sign in to comment.