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

Report Errors #102

Open
jsfour opened this issue Mar 22, 2023 · 11 comments
Open

Report Errors #102

jsfour opened this issue Mar 22, 2023 · 11 comments

Comments

@jsfour
Copy link
Member

jsfour commented Mar 22, 2023

When there is an error processing a request we need to retry first then report that back to the user.

This probably would need to happen here: https://github.com/octaviuslabs/walter/blob/main/src/handlers/index.ts

@jsfour
Copy link
Member Author

jsfour commented Mar 22, 2023

@imwalterbot can you take a look?

@imwalterbot
Copy link
Contributor

@imwalterbot can you take a look?

Queued for processing...

@imwalterbot
Copy link
Contributor

@imwalterbot can you take a look?

Processing this now

@imwalterbot
Copy link
Contributor

Based on the provided code, I can see that you want to implement a retry mechanism when there's an error processing a request and then report it back to the user. Here's a pseudocode solution to achieve this:

function retryOnError(attempts, processingFunction, errorCallback, successCallback) {
  // Run the processing function
  try {
    processingFunction()
    successCallback()
  } catch (error) {
    // If there's an error and we still have retry attempts left
    if (attempts > 0) {
      // Retry the processing function with one less attempt
      retryOnError(attempts - 1, processingFunction, errorCallback, successCallback)
    } else {
      // If no more attempts left, call the error callback
      errorCallback(error)
    }
  }
}

function processEventWithRetry(event) {
  retryOnError(
    3, // Number of retry attempts
    () => processEvent(event), // Processing function
    (error) => { // Error callback
      // Report the error back to the user
      gh.postIssueComment(
        event.payload.repository,
        event.payload.issue,
        [`> ${event.payload.comment.body} `, "Error processing request. Please try again later."].join("\n\n")
      )
      Log.error(`Error processing event ${event.id}: ${error}`)
    },
    () => { // Success callback
      Log.info(`Successfully processed event ${event.id}`)
    }
  )
}

Now, you can replace the call to processEvent(event) in the code with processEventWithRetry(event) to implement the retry mechanism.

This pseudocode adds a retryOnError function that takes the number of retry attempts, the processing function, an error callback, and a success callback. If there's an error and there are still retry attempts left, it will retry the processing function. If there are no more attempts left, it will call the error callback to report the error to the user. If the processing function is successful, it will call the success callback.

@jsfour
Copy link
Member Author

jsfour commented Mar 22, 2023

@imwalterbot good approach. Can you do a back off?

@imwalterbot
Copy link
Contributor

@imwalterbot good approach. Can you do a back off?

Queued for processing...

@imwalterbot
Copy link
Contributor

@imwalterbot good approach. Can you do a back off?

Processing this now

@imwalterbot
Copy link
Contributor

Based on the provided code, I can add a backoff mechanism to the retry strategy. Here's the updated pseudocode with an exponential backoff:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function retryOnError(attempts, processingFunction, errorCallback, successCallback) {
  // Run the processing function
  try {
    await processingFunction();
    successCallback();
  } catch (error) {
    // If there's an error and we still have retry attempts left
    if (attempts > 0) {
      // Calculate the backoff time (exponential backoff)
      const backoffTime = Math.pow(2, 3 - attempts) * 1000;

      // Wait for the backoff time before retrying
      await sleep(backoffTime);

      // Retry the processing function with one less attempt
      await retryOnError(attempts - 1, processingFunction, errorCallback, successCallback);
    } else {
      // If no more attempts left, call the error callback
      errorCallback(error);
    }
  }
}

async function processEventWithRetry(event) {
  await retryOnError(
    3, // Number of retry attempts
    async () => await processEvent(event), // Processing function
    (error) => { // Error callback
      // Report the error back to the user
      gh.postIssueComment(
        event.payload.repository,
        event.payload.issue,
        [`> ${event.payload.comment.body} `, "Error processing request. Please try again later."].join("\n\n")
      );
      Log.error(`Error processing event ${event.id}: ${error}`);
    },
    () => { // Success callback
      Log.info(`Successfully processed event ${event.id}`);
    }
  );
}

Now, you can replace the call to processEvent(event) in the code with processEventWithRetry(event) to implement the retry mechanism with an exponential backoff.

The updated pseudocode adds a sleep function that returns a promise that resolves after a specified number of milliseconds. This is used to introduce a delay before retrying the processing function. The backoff time is calculated using an exponential backoff strategy, where the backoff time doubles with each retry attempt.

@jsfour
Copy link
Member Author

jsfour commented Mar 22, 2023

@imwalterbot
Copy link
Contributor

@imwalterbot APPROVED for application to https://github.com/octaviuslabs/walter/blob/main/src/handlers/index.ts

Queued for processing...

@imwalterbot
Copy link
Contributor

@imwalterbot APPROVED for application to https://github.com/octaviuslabs/walter/blob/main/src/handlers/index.ts

Processing this now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants