Skip to content

Commit

Permalink
Implement symlink and modify file storage path (#1347)
Browse files Browse the repository at this point in the history
* Add update_room_created_at

* Implement symlink and modify file storage path

* Fix rebase bug

* Add mkdir
  • Loading branch information
zzdhybthu authored May 14, 2024
1 parent f37e759 commit 72db2db
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/helpers/docker_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ const docker_cron = async () => {
HostConfig: {
Binds: [
`${sub_base_dir}/${queue_front.room_id}/output:/usr/local/output`,
`${sub_base_dir}/${queue_front.room_id}/source/${team_label_bind.team_id}:/usr/local/code`
`${sub_base_dir}/${queue_front.room_id}/source/${team_label_bind.team_id}:/usr/local/code`,
`${base_directory}/${contest_name}/code/${team_label_bind.team_id}/source:${base_directory}/${contest_name}/code/${team_label_bind.team_id}/source`
],
AutoRemove: false,
Memory: client_memory_limit * 1024 * 1024 * 1024,
Expand Down
36 changes: 20 additions & 16 deletions src/routes/arena.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ router.post("/create", authenticate(), async (req, res) => {
const base_directory = await utils.get_base_directory();

const mkdir_promises = team_ids.map(team_id => {
return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}`, { recursive: true })
return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}/source`, { recursive: true })
.then(() => {
return Promise.resolve(true);
})
Expand All @@ -157,7 +157,7 @@ router.post("/create", authenticate(), async (req, res) => {
const files_exist_promises = player_codes_flat.map((player_code, index) => {
const language = code_languages_flat[index];
const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`;
return fs.access(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`)
return fs.access(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`)
.then(() => {
return true;
})
Expand All @@ -183,8 +183,14 @@ router.post("/create", authenticate(), async (req, res) => {
const language = code_languages_flat[index_map[index]];
const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`;
console.debug("code_file_name: ", code_file_name);
return utils.downloadObject(`${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`,
`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`, cos, config)
return fs.mkdir(`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source`, { recursive: true })
.then(() => {
return utils.downloadObject(`${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`,
`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source/${code_file_name}`, cos, config)
})
.then(() => {
return fs.chmod(`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source/${code_file_name}`, 0o755);
})
.then(() => {
return Promise.resolve(true);
})
Expand All @@ -201,7 +207,7 @@ router.post("/create", authenticate(), async (req, res) => {
}
}

console.log("Files downloaded!")
console.log("Files downloaded!");

const map_files_count = await fs.readdir(`${base_directory}/${contest_name}/map/${map_id}`)
.then(files => {
Expand Down Expand Up @@ -233,7 +239,7 @@ router.post("/create", authenticate(), async (req, res) => {
console.log("Map downloaded!");

const files_count_promises = team_ids.map(team_id => {
return fs.readdir(`${base_directory}/${contest_name}/code/${team_id}`)
return fs.readdir(`${base_directory}/${contest_name}/code/${team_id}/source`)
.then(files => {
return files.length;
})
Expand All @@ -250,13 +256,13 @@ router.post("/create", authenticate(), async (req, res) => {
}

const files_clean_promises = team_ids.map((team_id, index) => {
if (files_count[index] < 18) {
if (files_count[index] < 10) {
return Promise.resolve(true);
}
return fs.readdir(`${base_directory}/${contest_name}/code/${team_id}`)
return fs.readdir(`${base_directory}/${contest_name}/code/${team_id}/source`)
.then(files => {
const files_stat_promises = files.map(file => {
return fs.stat(`${base_directory}/${contest_name}/code/${team_id}/${file}`)
return fs.stat(`${base_directory}/${contest_name}/code/${team_id}/source/${file}`)
.then(stat => {
return { file, stat };
});
Expand All @@ -269,9 +275,9 @@ router.post("/create", authenticate(), async (req, res) => {
return !players_codes[index].includes(file_stat.file.split(".")[0]);
});

const files_stat_to_delete = files_stat_filtered.slice(0, files_stat_filtered.length - 6);
const files_stat_to_delete = files_stat_filtered.slice(0, files_stat_filtered.length - 3);
const delete_promises = files_stat_to_delete.map(file_stat => {
return fs.unlink(`${base_directory}/${contest_name}/code/${team_id}/${file_stat.file}`);
return fs.unlink(`${base_directory}/${contest_name}/code/${team_id}/source/${file_stat.file}`);
});
return Promise.all(delete_promises);
}
Expand Down Expand Up @@ -308,19 +314,17 @@ router.post("/create", authenticate(), async (req, res) => {

console.log("Room created!")

await fs.mkdir(`${base_directory}/${contest_name}/arena/${room_id}/source`, { recursive: true });
const copy_promises = player_codes_flat.map((player_code, index) => {
const language = code_languages_flat[index];
const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`;
const arena_file_name = language === "cpp" ? `${player_labels_flat[index]}` : `${player_labels_flat[index]}.py`;
return fs.mkdir(`${base_directory}/${contest_name}/arena/${room_id}/source/${team_ids_flat[index]}`, { recursive: true })
.then(() => {
return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`,
// return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`,
// `${base_directory}/${contest_name}/arena/${room_id}/source/${team_ids_flat[index]}/${arena_file_name}`)
return fs.symlink(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`,
`${base_directory}/${contest_name}/arena/${room_id}/source/${team_ids_flat[index]}/${arena_file_name}`)
})
.then(() => {
return fs.chmod(`${base_directory}/${contest_name}/arena/${room_id}/source/${team_ids_flat[index]}/${arena_file_name}`, 0o755);
})
.then(() => {
return Promise.resolve(true);
})
Expand Down
3 changes: 2 additions & 1 deletion src/routes/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ router.post("/compile-finish", async (req, res) => {
localFilePath = `${base_directory}/${contest_name}/code/${team_id}/${code_id}/output/${code_id}`;
await utils.uploadObject(localFilePath, key, cos, config);
try {
await fs.copyFile(localFilePath, `${base_directory}/${contest_name}/code/${team_id}/${code_id}`);
await fs.copyFile(localFilePath, `${base_directory}/${contest_name}/code/${team_id}/source/${code_id}`);
await fs.chmod(`${base_directory}/${contest_name}/code/${team_id}/source/${code_id}`, 0o755);
} catch (err) {
console.log("copy file failed: ", err);
}
Expand Down
43 changes: 26 additions & 17 deletions src/routes/competition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ router.post("/start-all", authenticate(), async (req, res) => {
const base_directory = await utils.get_base_directory();

const mkdir_promises = team_list_available.map(team_id => {
return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}`, { recursive: true })
return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}/source`, { recursive: true })
.then(() => {
return Promise.resolve(true);
})
Expand All @@ -126,7 +126,7 @@ router.post("/start-all", authenticate(), async (req, res) => {
const files_exist_promises = details_list_available.map(detail => {
const language = detail.language;
const code_file_name = language === "cpp" ? `${detail.code_id}` : `${detail.code_id}.py`;
return fs.access(`${base_directory}/${contest_name}/code/${detail.team_id}/${code_file_name}`)
return fs.access(`${base_directory}/${contest_name}/code/${detail.team_id}/source/${code_file_name}`)
.then(() => {
return true;
})
Expand All @@ -151,8 +151,14 @@ router.post("/start-all", authenticate(), async (req, res) => {
const language = details_list_available[index_map[index]].language;
const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`;
console.debug("code_file_name: ", code_file_name);
return utils.downloadObject(`${contest_name}/code/${details_list_available[index_map[index]].team_id}/${code_file_name}`,
`${base_directory}/${contest_name}/code/${details_list_available[index_map[index]].team_id}/${code_file_name}`, cos, config)
return fs.mkdir(`${base_directory}/${contest_name}/code/${details_list_available[index_map[index]].team_id}/source`, { recursive: true })
.then(() => {
return utils.downloadObject(`${contest_name}/code/${details_list_available[index_map[index]].team_id}/${code_file_name}`,
`${base_directory}/${contest_name}/code/${details_list_available[index_map[index]].team_id}/source/${code_file_name}`, cos, config)
})
.then(() => {
return fs.chmod(`${base_directory}/${contest_name}/code/${details_list_available[index_map[index]].team_id}/source/${code_file_name}`, 0o755);
})
.then(() => {
return Promise.resolve(true);
})
Expand Down Expand Up @@ -264,12 +270,11 @@ router.post("/start-all", authenticate(), async (req, res) => {
const competition_file_name = language === "cpp" ? `${player_labels_flat[index]}` : `${player_labels_flat[index]}.py`;
return fs.mkdir(`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}`, { recursive: true })
.then(() => {
return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`,
// return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`,
// `${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`)
return fs.symlink(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`,
`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`)
})
.then(() => {
return fs.chmod(`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`, 0o755);
})
.then(() => {
return Promise.resolve(true);
})
Expand Down Expand Up @@ -440,7 +445,7 @@ router.post("/start-one", authenticate(), async (req, res) => {
const base_directory = await utils.get_base_directory();

const mkdir_promises = team_ids.map(team_id => {
return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}`, { recursive: true })
return fs.mkdir(`${base_directory}/${contest_name}/code/${team_id}/source`, { recursive: true })
.then(() => {
return Promise.resolve(true);
})
Expand All @@ -459,7 +464,7 @@ router.post("/start-one", authenticate(), async (req, res) => {
const files_exist_promises = player_codes_flat.map((player_code, index) => {
const language = code_languages_flat[index];
const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`;
return fs.access(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`)
return fs.access(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`)
.then(() => {
return true;
})
Expand All @@ -485,8 +490,14 @@ router.post("/start-one", authenticate(), async (req, res) => {
const language = code_languages_flat[index_map[index]];
const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`;
console.debug("code_file_name: ", code_file_name);
return utils.downloadObject(`${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`,
`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`, cos, config)
return fs.mkdir(`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source`, { recursive: true })
.then(() => {
return utils.downloadObject(`${contest_name}/code/${team_ids_flat[index_map[index]]}/${code_file_name}`,
`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source/${code_file_name}`, cos, config);
})
.then(() => {
return fs.chmod(`${base_directory}/${contest_name}/code/${team_ids_flat[index_map[index]]}/source/${code_file_name}`, 0o755);
})
.then(() => {
return Promise.resolve(true);
})
Expand Down Expand Up @@ -604,19 +615,17 @@ router.post("/start-one", authenticate(), async (req, res) => {

console.log("Room created!")

await fs.mkdir(`${base_directory}/${contest_name}/competition/${room_id}/source`, { recursive: true });
const copy_promises = player_codes_flat.map((player_code, index) => {
const language = code_languages_flat[index];
const code_file_name = language === "cpp" ? `${player_code}` : `${player_code}.py`;
const competition_file_name = language === "cpp" ? `${player_labels_flat[index]}` : `${player_labels_flat[index]}.py`;
return fs.mkdir(`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}`, { recursive: true })
.then(() => {
return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`,
// return fs.copyFile(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/${code_file_name}`,
// `${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`)
return fs.symlink(`${base_directory}/${contest_name}/code/${team_ids_flat[index]}/source/${code_file_name}`,
`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`)
})
.then(() => {
return fs.chmod(`${base_directory}/${contest_name}/competition/${room_id}/source/${team_ids_flat[index]}/${competition_file_name}`, 0o755);
})
.then(() => {
return Promise.resolve(true);
})
Expand Down

0 comments on commit 72db2db

Please sign in to comment.