Skip to content

Commit

Permalink
Merge pull request #657 from golemfactory/feature/JST-526
Browse files Browse the repository at this point in the history
feat: task executor emits lifecycle events through an EventEmitter
  • Loading branch information
pgrzy-golem authored Nov 20, 2023
2 parents 4727159 + e9e6f33 commit 1c334c6
Show file tree
Hide file tree
Showing 78 changed files with 180 additions and 95 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
(async function main() {
const executor = await TaskExecutor.create("9a3b5d67b0b27746283cb5f287c13eab1beaa12d92a9f536b747c7ae");
await executor.run(async (ctx) => console.log(await ctx.run("echo 'Hello World'")).stdout);
await executor.end();
await executor.shutdown();
})();
```

Expand Down
2 changes: 1 addition & 1 deletion examples/blender/blender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async function main(subnetTag: string, driver?: string, network?: string, debug?
const results = await Promise.all(futureResults);
results.forEach((result) => console.log(result));

await executor.end();
await executor.shutdown();
}

program
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
}
});

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

console.log(result);
await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

res.on("data", (data) => (data.index == 2 ? console.log(data.stdout) : ""));
res.on("error", (error) => console.error(error));
res.on("close", () => executor.end());
res.on("close", () => executor.shutdown());
});
})();
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
}
});

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

console.log(result);

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { TaskExecutor } = require("@golem-sdk/golem-js");
});

const result = await executor.run(async (ctx) => (await ctx.run("node -v")).stdout);
await executor.end();
await executor.shutdown();

console.log("Task result:", result);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

const result = await executor.run(async (ctx) => (await ctx.run("node -v")).stdout);
await executor.end();
await executor.shutdown();

console.log("Task result:", result);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

const result = await executor.run<string | unknown>(async (ctx) => (await ctx.run("node -v")).stdout);
await executor.end();
await executor.shutdown();

console.log("Task result:", result);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});
await Promise.all(futureResults);

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});
}

await executor.end();
await executor.shutdown();
})();
2 changes: 1 addition & 1 deletion examples/docs-examples/examples/executing-tasks/map.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
const results = await Promise.all(futureResults);
results.forEach((result) => console.log(result.stdout));

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
const results = await Promise.all(futureResults);
results.forEach((result) => console.log(result.stdout));

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

const result = await executor.run(async (ctx) => (await ctx.run("node -v")).stdout);

await executor.end();
await executor.shutdown();

console.log("Task result:", result);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ const myFilter = async (proposal) => {
await executor.run(async (ctx) =>
console.log((await ctx.run(`echo "This task is run on ${ctx.provider.id}"`)).stdout, ctx.provider.id),
);
await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

await executor.run(async (ctx) => console.log((await ctx.run("echo 'Hello World'")).stdout));
await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ for (let i = 0; i < whiteListNames.length; i++) {
await executor.run(async (ctx) =>
console.log((await ctx.run(`echo "This task is run on ${ctx.provider.name}"`)).stdout, ctx.provider.name),
);
await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

console.log(result);
await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ import * as fs from "fs";
console.log("md5 of the file sent to provider: ", result);
console.log("Locally computed md5: ", hash);

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

console.log(output.stdout);

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

const result = await executor.run(async (ctx) => (await ctx.run("node -v")).stdout);
await executor.end();
await executor.shutdown();

console.log("Task result:", result);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

console.log(result);
await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ <h3>Logs</h3>
})
.catch((e) => logger.error(e));

await executor.end();
await executor.shutdown();
if (result?.data) setResponse(result.data);
}
window.run = run;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ import * as fs from "fs";
console.log("md5 of the file sent to provider: ", result);
console.log("Locally computed md5: ", hash);

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
appendResults(JSON.stringify(output.data.output, null, "\t"));
})
.catch((e) => logger.error(e));
await executor.end();
await executor.shutdown();
}
document.getElementById("echo").onclick = run;
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

console.log(output.stdout);

await executor.end();
await executor.shutdown();
})();
2 changes: 1 addition & 1 deletion examples/docs-examples/examples/using-app-keys/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

const result = await executor.run(async (ctx) => (await ctx.run("node -v")).stdout);
await executor.end();
await executor.shutdown();

console.log("Task result:", result);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

const result = await executor.run(async (ctx) => (await ctx.run("node -v")).stdout);

await executor.end();
await executor.shutdown();

console.log("Task result:", result);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

const result = await executor.run(async (ctx) => (await ctx.run("node -v")).stdout);

await executor.end();
await executor.shutdown();

console.log("Task result:", result);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

console.log(result);
await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

res.on("data", (result) => console.log(result));
res.on("error", (error) => console.error(error));
res.on("close", () => executor.end());
res.on("close", () => executor.shutdown());
});
})();
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
});

console.log(result);
await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
const result = await executor.run(async (ctx) => await ctx.run("node -w"));
console.log("Task result:", result);

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
const result = await executor.run(async (ctx) => await ctx.run("node -v"));
console.log("Task result:", result);

await executor.end();
await executor.shutdown();
})();
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
yagnaOptions: { apiKey: "try_golem" },
});
const result = await executor.run(async (ctx) => (await ctx.run("node -v")).stdout);
await executor.end();
await executor.shutdown();

console.log("Task result:", result);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async function run() {
.run(async (ctx) => appendResults((await ctx.run("echo 'Hello World'")).stdout))
.catch((e) => logger.error(e));

await executor.end();
await executor.shutdown();
}

document.getElementById("echo").onclick = run;
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
console.log("/golem/work content: ", (await ctx.run("ls /golem/work")).stdout);
});

await executor.end();
await executor.shutdown();
})();
2 changes: 1 addition & 1 deletion examples/docs-examples/tutorials/quickstart/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

const taskResult = await executor.run(async (ctx) => (await ctx.run("node -v")).stdout);

await executor.end();
await executor.shutdown();

console.log("Task result:", taskResult);
})();
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h3>Logs</h3>
await executor
.run(async (ctx) => appendResults((await ctx.run("echo 'Hello World'")).stdout))
.catch((e) => logger.error(e));
await executor.end();
await executor.shutdown();
}
document.getElementById("echo").onclick = run;
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async function main(args) {
} catch (err) {
console.log(`Password not found`);
} finally {
await executor.end();
await executor.shutdown();
}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/external-request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ const __dirname = fileURLToPath(new URL(".", import.meta.url));
);
console.log(result.stdout);
});
await executor.end();
await executor.shutdown();
})();
2 changes: 1 addition & 1 deletion examples/fibonacci/fibonacci.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function main(fiboN = 1, tasksCount = 1, subnetTag, driver, network, debug
}

await Promise.all(runningTasks);
await executor.end();
await executor.shutdown();
}
program
.requiredOption("-n, --fibonacci-number <n>", "fibonacci number", (val) => parseInt(val))
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/hello.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
(async function main() {
const executor = await TaskExecutor.create("golem/alpine:latest");
await executor.run(async (ctx) => console.log((await ctx.run("echo 'Hello World'")).stdout));
await executor.end();
await executor.shutdown();
})();
1 change: 1 addition & 0 deletions examples/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/simple-usage/batchPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
.catch((error) => console.log(error));
res?.map(({ stdout }) => console.log(stdout));
});
await executor.end();
await executor.shutdown();
})();
2 changes: 1 addition & 1 deletion examples/simple-usage/batchStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
results.on("error", (error) => console.error(error.toString()));
results.on("close", () => console.log("END"));
});
await executor.end();
await executor.shutdown();
})();
2 changes: 1 addition & 1 deletion examples/simple-usage/fileTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ import { readFileSync } from "fs";
console.log(`Result=${res.result}`);
console.log("File new_test.json: ", readFileSync("new_test.json", "utf-8"));
});
await executor.end();
await executor.shutdown();
})();
Empty file.
2 changes: 1 addition & 1 deletion examples/simple-usage/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ function createLogger(options) {
});

await executor.run(async (ctx) => console.log((await ctx.run("echo 'Hello World'")).stdout));
await executor.end();
await executor.shutdown();
})(options);
2 changes: 1 addition & 1 deletion examples/simple-usage/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
const results = await Promise.all(futureResults);
console.log(results);

await executor.end();
await executor.shutdown();
})();
2 changes: 1 addition & 1 deletion examples/simple-usage/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import { TaskExecutor } from "@golem-sdk/golem-js";
return `${res1.stdout}${res2.stdout}`;
});
console.log(results);
await executor.end();
await executor.shutdown();
})();
Loading

0 comments on commit 1c334c6

Please sign in to comment.