Azure function app running indefinitely on webhook trigger

Ramji R 11 Reputation points
2023-11-06T11:50:40.67+00:00
The Below function app code should execute while a webhook is triggered. The below code runs fine. But eventhough the webhook is triggered once. The below code runs indefinitely. Is anything is wrong in the below code which cause the issue.


**Index.js**

    module.exports = async (context, req) => {
      context.log("JavaScript HTTP trigger function processed a request.");
      const contentful = require("contentful-management");
      const client = contentful.createClient({
        accessToken: "test",
      });
    
      const spaceId = "test";
      const environmentId = "test";
      try {
        const space = await client.getSpace(spaceId);
        const environment = await space.getEnvironment(environmentId);
        let response = await environment.getEntry(context.req.body.sys.id);
        const firstEntry = response;
        context.log(firstEntry);
        if (
          firstEntry.fields.previousSlug != undefined &&
          firstEntry.fields.slug != firstEntry.fields.previousSlug
        ) {
          const testingContentTypeId = "redirectManager"; // Replace with the actual content type ID
          const testingEntry = await environment.createEntry(testingContentTypeId, {
            fields: {
              id: { "en-GB": firstEntry.fields.internalDocumentId["en-GB"] },
              redirectSlug: {
                "en-GB": firstEntry.fields.previousSlug["en-GB"],
              },
              currentSlug: { "en-GB": firstEntry.fields.slug["en-GB"] },
            },
          });
          testingEntry.publish();
        }
        firstEntry.fields.previousSlug = firstEntry.fields.slug;
        var result = await firstEntry.update();
        await result.publish();
      } catch (error) {
        context.res = {
          body: error,
        };
    
        return "test";
      };
    };



**Function.json:**

    {
      "bindings": [
        {
          "authLevel": "function",
          "type": "httpTrigger",
          "direction": "in",
          "name": "req",
          "methods": [
            "get",
            "post"
          ]
        },
        {
          "type": "http",
          "direction": "out",
          "name": "res"
        }
      ]
    }


Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,004 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Mike Urnun 9,811 Reputation points Microsoft Employee
    2023-11-06T19:31:31.6533333+00:00

    Hello @Ramji R - Thanks for reaching out. The bindings look OK, and the function signature and the return also look OK to me. However, I do have the following questions for you which might help troubleshoot the root cause:

    • Does the lazyloading const contentful = require("contentful-management"); inside the function (instead of being at the top) work as intended and the client object is created successfully? Note that require() is a synchronous call and can block the rest of the execution.
    • Do you get any explicit error messages when pulling a Diagnostics report?
    • If you suspect that the issue is within the code, could you try to reproduce & debug locally?
    • If the issue occurs only on Azure, could you configure a Profiler through Application Insights and see if it provides any insights?

    Please "Accept Answer" if the answer is helpful so that others in the community may benefit from your experience.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.