Skip to content

Instantly share code, notes, and snippets.

@sujaykumarh
Created August 11, 2024 06:09
Show Gist options
  • Save sujaykumarh/d174a0094e027993b338dac743094e22 to your computer and use it in GitHub Desktop.
Save sujaykumarh/d174a0094e027993b338dac743094e22 to your computer and use it in GitHub Desktop.
Run tasks in pralllel using Bun
/**
* Run multiple tasks prallel
*
* Usage => bun run tasks.ts
*/
/**
* ++++++ Utils ++++++
*/
export type Task = {
id: number;
name?: string;
delayMs?: number;
fn: Function;
};
const randomNumberBetween = (min: number, max: number): number => {
return Math.floor(Math.random() * (max - min + 1) + min);
}
const runTask = async (task: Task): Promise<any | void> => {
// console.log(`[runTask] running task ${task.id} ${task.name ?? "..."}`);
return new Promise((resolve, reject) => {
try {
resolve(task.fn());
} catch (err) {
reject(err);
}
})
}
const runTasks = async (batchName: string, tasks: Array<Task>): Promise<any> => {
try {
const results = await Promise.all(tasks.map(runTask));
console.log(`[${batchName}] results`, results);
if (results.length === tasks.length) console.log(`[${batchName}] βœ… All tasks run sucessfully! πŸŽ‰`);
} catch (e) {
console.log(`[${batchName}] πŸ›‘ a task failed`, e);
} finally {
console.log(`[${batchName}] all tasks have ran!`);
}
}
/**
* ++++++ Tasks ++++++
*/
var all_pass_tasks: Array<Task> = [];
var all_fail_tasks: Array<Task> = [];
var one_fail_tasks: Array<Task> = [];
var rand_fail_tasks: Array<Task> = [];
const TASKS_COUNT = 10;
const FAIL_TASK_ID = 2;
const TASK_SLEEP_MS = 500;
/**
* Generate all pass tasks
*/
for (let idx = 0; idx < TASKS_COUNT; idx++) {
all_pass_tasks.push({
id: idx,
name: "all_pass-" + idx,
delayMs: idx * 100,
fn: async () => {
await Bun.sleep(TASK_SLEEP_MS);
console.log("[all_pass] finished running task....", idx);
return idx;
}
})
}
/**
* Generate all fail tasks
*/
for (let idx = 0; idx < TASKS_COUNT; idx++) {
all_fail_tasks.push({
id: idx,
name: "all_fail-" + idx,
delayMs: idx * 100,
fn: async () => {
await Bun.sleep(TASK_SLEEP_MS);
throw "error at idx: " + idx;
console.log("[all_fail] finished running task....", idx);
}
})
}
/**
* Generate one fail tasks
*/
for (let idx = 0; idx < TASKS_COUNT; idx++) {
one_fail_tasks.push({
id: idx,
name: "one_fail-" + idx,
delayMs: idx * 100,
fn: async () => {
await Bun.sleep(TASK_SLEEP_MS);
if (idx === FAIL_TASK_ID) throw "error at idx: " + idx;
console.log("[one_fail] finished running task....", idx);
return idx;
}
})
}
/**
* Generate random fail tasks
*/
for (let idx = 0; idx < TASKS_COUNT; idx++) {
rand_fail_tasks.push({
id: idx,
name: "rand_fail-" + idx,
delayMs: idx * 100,
fn: async () => {
await Bun.sleep(TASK_SLEEP_MS);
if (idx === randomNumberBetween(0, TASKS_COUNT)) throw "error at idx: " + idx;
console.log("[rand_fail] finished running task....", idx);
return idx;
}
})
}
await runTasks("all_pass_tasks", all_pass_tasks);
await runTasks("all_fail_tasks", all_fail_tasks);
await runTasks("one_fail_tasks", one_fail_tasks);
await runTasks("rand_fail_tasks", rand_fail_tasks);
console.log("");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment