Asynchronous Task Orchestrator written in Rust to concurrently process HTTP request. This program uses a task pool (using tokio runtime) and asynchronously executes the blueprint task of fetch-sleep-print. serde and csv crates are used to read/write to CSV files.
-
Program starts in
async main(), it parses command line input to get the input CSV file path. -
It then reads the CSV and deserializes it using
serde, and csv row data into task_vector as struct (task_id, task_type). -
Task pool: For each task, a new asynchronous job is spawned using
tokio::spawn, andexecute_task()function is called concurrently. -
execute_task()follows the defined blueprint -- fetch_data from predefined
URLusingreqwest. - sleep (using tokio) for 5s.
- if task is successful, print message to stderr.
If fetch is unsuccessful, final status is
failed, iftask_type != process_data, final status isskipped. - fetch_data from predefined
-
Completed tasks are awaited and stored in a
result_vector, and again usingserdeandcsv, structs are serialized and written to the output CSV (stdout) specified in command line.
- Install Rust (stable):
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup default stable
- Clone the repository:
git clone <repo-url>
cd <repo-folder>
- Add dependencies:
cargo add tokio reqwest csv serde serde_derive
cargo build
cargo run -- <input_csv_path> > results.csv
Example:
cargo run -- ./src/io_files/input.csv > ./src/io_files/output.csv
- The assessment requirements can be done in both sequential manner, running tasks one by one, or running tasks concurrently using asynchronous behavior.
- Input file is assumed to have 2 columns only --
task_id(u64),task_type(string).task_idis unique, andtask_typeisprocess_data. If the later isn't true, the final_status for the respective task is markedFailed. - If the task_type is not
process_data,unsupported task_type:is logged inerror_infocolumn. - The URL to fetch request from is fixed (hardcoded in the program) in
URLvariable. - If the program is unable to fetch from the URL,
fetch_data failedis logged inerror_infocolumn. - The current collection method awaits the task handle in their order of creation (i.e. same as the input csv file), hence the output csv file tends to follow the same order. An alternative way to implement this would be using an unordered pool collector (e.g.,
FuturesUnordered) which would record task results in output csv in actual order of completion rather than order of awaiting their completion status. The actual order of task completion is completely asynchronous, and tasks can be completed in any order, as seen in the stderr.