# Hooks: Handling hook failures and the time limit for exponential backoff

**URL:** <https://community.auth0.com/t/hooks-handling-hook-failures-and-the-time-limit-for-exponential-backoff/103578>\
**Category:** Get Help\
**Tags:** extensibility, hooks-extensibility\
**Created:** [March 28, 2023, 8:12pm UTC](https://community.auth0.com/t/hooks-handling-hook-failures-and-the-time-limit-for-exponential-backoff/103578 "2023-03-28T20:12:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![brandon.zeal](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/brandon.zeal/32/45862_2.png) [@brandon.zeal](https://community.auth0.com/u/brandon.zeal)\
**Post date:** [March 28, 2023, 8:12pm UTC](https://community.auth0.com/t/hooks-handling-hook-failures-and-the-time-limit-for-exponential-backoff/103578/1 "2023-03-28T20:12:53Z")

</div>

I have created a hook that runs a process on my server to onboard a user to my own database in addition. I wanted to use the approach to use exponential backoff in case of maybe the server being unreachable for any scenario of which the request might fail.

However, there’s a limit to how long a hook can run in Auth0. If a hook exceeds this time limit, it will generate an unhandled asynchronous exception and return a 500 error with the message “Script execution time exceeded”.

This creates a dilemma for hooks that use exponential backoff, as it’s possible for the delay between retries to exceed the time limit, resulting in a failure that cannot be handled by the hook itself.

I am not sure how to protect from this as this data ideal would be captured on user registration.

Here is the code for my hook for context:

```auto
module.exports = function (user, context, callback) {
  const baseURL = 'https://{hidden}.ngrok.io';
  const route = '/api/db/user';

  // Define the base delay and maximum delay in milliseconds
  const baseDelay = 2000; // 1 second
  const maxDelay = 86400000; // 1 day in milliseconds

  // Define the current delay, starting at the base delay
  let currentDelay = baseDelay;

  // Define the function to make the API call with exponential backoff
  function makeAPICall() {
    const options = {
      url: baseURL + route,
      method: 'POST',
      data: {
        user: user
      }
    };

    axios(options)
      .then(response => {
        console.log('User created successfully');
        callback(null, user, context);
      })
      .catch(error => {
        console.log('Error creating user', user.id, error);

        // Increase the delay exponentially, up to the maximum delay
        currentDelay = Math.min(currentDelay * 2, maxDelay);

        // Retry the API call after the current delay
        setTimeout(makeAPICall, currentDelay);
      });
  }

  makeAPICall();
};
`
```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 29, 2023, 8:27pm UTC](https://community.auth0.com/t/hooks-handling-hook-failures-and-the-time-limit-for-exponential-backoff/103578/3 "2023-03-29T20:27:28Z")

</div>

Hi @brandon.zeal,

First, I would suggest using [Actions](https://auth0.com/docs/customize/actions) unless you have an explicit reason to use the legacy feature, Hooks.

As for the timeout, these post-registration extensibility points aren’t meant to be long-running jobs. You could either deal with the user creation in your DB another way – for example: Add the user to your DB when you see a token with a new user on your backend/API, or attempt to add the user on each login if a flag isn’t present in the user’s app\_metadata – or retry until you hit the timeout.

Hope this helps

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [June 22, 2023, 6:38pm UTC](https://community.auth0.com/t/hooks-handling-hook-failures-and-the-time-limit-for-exponential-backoff/103578/4 "2023-06-22T18:38:22Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
