Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle "ReplyError: ERR Unknown worker" from faktory server #117

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/__tests__/_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ mocked.beat = (state?: string) => ({ socket }: ServerControl) => {
socket.write(`$${json.length}\r\n${json}\r\n`);
}
};

mocked.beatErr = (errString?: string) => ({ socket }: ServerControl) => {
if (!errString) {
socket.write("-ERR \r\n");
} else {
socket.write(`-ERR ${errString}\r\n`);
}
};

mocked.fetch = (job: JobPayload | null) => ({ socket }: ServerControl) => {
if (job) {
const string = JSON.stringify(job);
Expand Down
13 changes: 13 additions & 0 deletions src/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,19 @@ test("#beat: returns a signal from the server", async (t) => {
});
});

test("#beat: returns unknown worker err", async (t) => {
await mocked(async (server, port) => {

const message = "Unknown worker";
server.on("BEAT", mocked.beatErr(message));

const client = new Client({ port });
const err = await client.beat();
t.is(err, `ERR ${message}`);

});
});

test.skip("#connect: rejects connect when connection cannot be established", async (t) => {
const client = new Client({ url: "tcp://localhost:7488" });

Expand Down
12 changes: 8 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,15 @@ export class Client {
*/
async beat(): Promise<string> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This this method is a bit low-level, I would prefer for it to reject the promise when it fails sending the beat to the server. Callers of Client#beat could then catch that error where necessary. As it's written, a caller of Client#beat would have no way of knowing whether it succeeded or not, because it returns a truthy value either way.

Since beat is already wrapped in a try/catch in Worker#work as of 4.1.0, do you think it will be okay if we don't try/catch here?

Copy link
Author

@Laksh47 Laksh47 Mar 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your inputs, I will upgrade the package and test this try/catch in Worker#work and provide an update

And I think stopping the worker during this error seems to be the best thing to do @jbielick

I can update the PR later today!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey Josh, @jbielick

4.1.4 seems to handle the error signal in the catch block!

heartDebug("BEAT");
const response = await this.send(["BEAT", encode({ wid: this.wid })]);
if (response[0] === "{") {
return JSON.parse(response).state;
try {
const response = await this.send(["BEAT", encode({ wid: this.wid })]);
if (response[0] === "{") {
return JSON.parse(response).state;
}
return response;
} catch(error) {
return error.message;
}
return response;
}

/**
Expand Down