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

refactor(worker): Remove redundant Bridge error handling #6875

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -193,99 +193,48 @@ export class ExecuteBridgeJob {
}: Omit<ExecuteBridgeRequestCommand, 'afterResponse' | 'action' | 'retriesLimit'> & {
job: JobEntity;
}): Promise<ExecuteOutput> {
try {
return this.executeBridgeRequest.execute({
statelessBridgeUrl,
event,
action: PostActionEnum.EXECUTE,
searchParams,
afterResponse: async (response) => {
const body = response?.body as string;

if (response.statusCode >= 400) {
let rawMessage: Record<string, unknown>;
try {
rawMessage = JSON.parse(body);
} catch {
Logger.error(`Unexpected body received from Bridge: ${body}`, LOG_CONTEXT);
rawMessage = {
error: `Unexpected body received from Bridge: ${body}`,
};
}
const createExecutionDetailsCommand: CreateExecutionDetailsCommand = {
...CreateExecutionDetailsCommand.getDetailsFromJob(job),
detail: DetailEnum.FAILED_BRIDGE_RETRY,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.WARNING,
isTest: false,
isRetry: false,
raw: JSON.stringify({
url: statelessBridgeUrl,
statusCode: response.statusCode,
retryCount: response.retryCount,
message: response.statusMessage,
...(body && body?.length > 0 ? { raw: rawMessage } : {}),
}),
return this.executeBridgeRequest.execute({
statelessBridgeUrl,
event,
action: PostActionEnum.EXECUTE,
searchParams,
afterResponse: async (response) => {
const body = response?.body as string;

if (response.statusCode >= 400) {
let rawMessage: Record<string, unknown>;
try {
rawMessage = JSON.parse(body);
} catch {
Logger.error(`Unexpected body received from Bridge: ${body}`, LOG_CONTEXT);
rawMessage = {
error: `Unexpected body received from Bridge: ${body}`,
};

await this.createExecutionDetails.execute(createExecutionDetailsCommand);
}

return response;
},
workflowOrigin,
environmentId,
}) as Promise<ExecuteOutput>;
} catch (error: any) {
Logger.error(error, 'Error sending Bridge request:', LOG_CONTEXT);

let raw: { retryCount?: number; statusCode?: number; message: string; url?: string };

if (error.response) {
rifont marked this conversation as resolved.
Show resolved Hide resolved
let rawMessage: Record<string, unknown>;
const errorResponseBody = error?.response?.body;
try {
rawMessage = JSON.parse(errorResponseBody);
} catch {
Logger.error(`Unexpected body received from Bridge: ${errorResponseBody}`, LOG_CONTEXT);
rawMessage = {
error: `Unexpected body received from Bridge: ${errorResponseBody}`,
const createExecutionDetailsCommand: CreateExecutionDetailsCommand = {
...CreateExecutionDetailsCommand.getDetailsFromJob(job),
detail: DetailEnum.FAILED_BRIDGE_EXECUTION,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: JSON.stringify({
url: statelessBridgeUrl,
statusCode: response.statusCode,
retryCount: response.retryCount,
message: response.statusMessage,
...(body && body?.length > 0 ? { raw: rawMessage } : {}),
}),
};
}

raw = {
url: statelessBridgeUrl,
statusCode: error.response?.statusCode,
message: error.response?.statusMessage,
...(error.response?.retryCount ? { retryCount: error.response?.retryCount } : {}),
...(error?.response?.body?.length > 0 ? { raw: rawMessage } : {}),
};
} else if (error.message) {
raw = {
url: statelessBridgeUrl,
message: error.message,
};
} else {
raw = {
url: statelessBridgeUrl,
message: 'An Unexpected Error Occurred',
};
}

const createExecutionDetailsCommand: CreateExecutionDetailsCommand = {
...CreateExecutionDetailsCommand.getDetailsFromJob(job),
detail: DetailEnum.FAILED_BRIDGE_EXECUTION,
source: ExecutionDetailsSourceEnum.INTERNAL,
status: ExecutionDetailsStatusEnum.FAILED,
isTest: false,
isRetry: false,
raw: JSON.stringify(raw),
};

await this.createExecutionDetails.execute(createExecutionDetailsCommand);
await this.createExecutionDetails.execute(createExecutionDetailsCommand);
}

throw error;
}
return response;
},
workflowOrigin,
environmentId,
}) as Promise<ExecuteOutput>;
}

private async mapState(job: JobEntity, payload: Record<string, unknown>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export enum DetailEnum {
MESSAGE_CONTENT_SYNTAX_ERROR = 'Message content could not be generated due to syntax error in email editor',
MESSAGE_CREATED = 'Message created',
SUCCESSFUL_BRIDGE_RESPONSE_RECEIVED = 'Successful Bridge response received',
FAILED_BRIDGE_RETRY = 'Failed to get response from Bridge, will be retried',
rifont marked this conversation as resolved.
Show resolved Hide resolved
FAILED_BRIDGE_EXECUTION = 'Bridge execution failed',
SUBSCRIBER_NO_ACTIVE_INTEGRATION = 'Subscriber does not have an active integration',
LAYOUT_NOT_FOUND = 'Layout not found ',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,17 @@ export class ExecuteBridgeRequest {
timeout: DEFAULT_TIMEOUT,
json: command.event,
retry: {
calculateDelay: ({
attemptCount,
}: {
attemptCount: number;
}): number => {
calculateDelay: ({ attemptCount, computedValue }) => {
if (computedValue === 0) {
rifont marked this conversation as resolved.
Show resolved Hide resolved
/*
* If the computed value is 0, the retry conditions were not met and we don't want to retry.
* The retry condition is only met when the response has a `statusCodes` or `errorCodes`
* that matches the supplied retry configuration values.
* @see https://github.com/sindresorhus/got/blob/3034c2fdcebdff94907a6e015a8b154e851fc343/documentation/7-retry.md?plain=1#L130
*/
return 0;
}

if (attemptCount === retriesLimit) {
return 0;
}
Expand Down
Loading