Skip to content

Commit

Permalink
chore: fixed closing stream
Browse files Browse the repository at this point in the history
  • Loading branch information
mgordel committed Nov 6, 2023
1 parent bf67242 commit 89a5cb1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/simple-usage/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TaskExecutor } from "@golem-sdk/golem-js";

const executor = await TaskExecutor.create("golem/alpine:latest");
const finalResult = await executor.run(async (ctx) => {
const remoteProcess = await ctx.spawn("sleep 1 && echo 'Hello World' && echo 'Hello Golem'");
const remoteProcess = await ctx.spawn("sleep 1 && echo 'Hello World' && echo 'Hello Golem' >&2");
remoteProcess.stdout.on("data", (data) => console.log("stdout>", data));
remoteProcess.stderr.on("data", (data) => console.error("stderr>", data));

Expand Down
14 changes: 9 additions & 5 deletions src/task/spawn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ export class RemoteProcess {
private streamError?: Error;
private defaultTimeout = 20_000;
constructor(private streamOfActivityResults: Readable) {
this.streamOfActivityResults.on("data", (data) => (this.lastResult = data));
this.streamOfActivityResults.on("data", (data) => {
this.lastResult = data;
});
this.streamOfActivityResults.on("error", (error) => (this.streamError = error));
const { stdout, stderr } = this.transformResultsStream();
this.stdout = stdout;
Expand All @@ -21,28 +23,30 @@ export class RemoteProcess {
() => rej(new Error("The waiting time for the final result has been exceeded")),
timeout ?? this.defaultTimeout,
);
this.streamOfActivityResults.on("close", () => {
const end = () => {
clearTimeout(timeoutId);
if (this.lastResult) {
res(this.lastResult);
} else {
rej(new Error(`An error occurred while retrieving the results. ${this.streamError}`));
}
});
};
if (this.streamOfActivityResults.closed) return end();
this.streamOfActivityResults.on("close", () => end());
});
}

private transformResultsStream(): { stdout: Readable; stderr: Readable } {
const stdoutTransform = new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
callback(null, chunk?.stdout ?? null);
callback(null, chunk?.stdout);
},
});
const stderrTransform = new Transform({
objectMode: true,
transform(chunk, encoding, callback) {
callback(null, chunk?.stderr ?? null);
callback(null, chunk?.stderr);
},
});
return {
Expand Down
16 changes: 16 additions & 0 deletions src/task/work.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ describe("Work Context", () => {
});
});

describe("spawn()", () => {
it("should execute spawn command", async () => {
const expectedResult = ActivityMock.createResult({ stdout: "Ok", stderr: "Error", isBatchFinished: true });
activity.mockResults([expectedResult]);
const remoteProcess = await context.spawn("rm -rf");
for await (const result of remoteProcess.stdout) {
expect(result).toBe("Ok");
}
for await (const result of remoteProcess.stderr) {
expect(result).toBe("Error");
}
const finalResult = await remoteProcess.waitForExit();
expect(finalResult.result).toBe("Ok");
});
});

describe("transfer()", () => {
it("should execute transfer command", async () => {
const result = ActivityMock.createResult({ stdout: "Ok" });
Expand Down

0 comments on commit 89a5cb1

Please sign in to comment.