Skip to content
Back

Webhook timing out

  • 0
  • Functions
  • Webhooks
Asdshadow
16 Feb, 2024, 16:15

I have a webhook set up to execute some code on my function. More than hafl of the executions give me this error : Operation timed out after 30000 milliseconds with 0 bytes received with status code 0\nError Code: 0.

I read somewhere that this could be cuz the function executes synchronously but i am using a domain and I dont know how to make the function run async.

The weird thing is that my webhook retrys sending the exact same response 5 times when it gets an error, and sometimes it works. And even when the webhook times out the data in my database is changed.

TL;DR
Developers are experiencing a timeout error with their webhook executions. They have tried running the functions async in the past but are currently using a domain. They have checked the logs and found that there are no logs when the execution fails. Suggestions given by other developers include adding logging to see where the function is getting stuck and checking for multiple executions in Appwrite. It is also mentioned that the function does not need to await JSON.parse(). The developer shares their code and mentions that the webhook retries sending the exact same response 5 times when it gets an error. They also notice that sometimes it works and sometimes it times out, but the
Asdshadow
16 Feb, 2024, 16:16

This is my code :

Asdshadow
16 Feb, 2024, 16:17
TypeScript
  const client = new Client()
    .setEndpoint(process.env.APPWRITE_API_ENDPOINT)
    .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID)
    .setKey(process.env.APPWRITE_API_KEY);

  if (req.method == "GET") {
    return res.empty();
  }

  const databases = new Databases(client);
  const users = new Users(client);

  const body = await JSON.parse(req.bodyRaw);
  if (body == undefined) {
    return res.empty();
  }
  const entitlementId = body.event.entitlement_ids[0];
  const purchasedAtTime = body.event.purchased_at_ms;
  const expirationAtTime = body.event.expiration_at_ms;
  const userId = body.event.app_user_id;
  const type = body.event.type;

  const teamMembership = await users.listMemberships(userId);
  if (type == "RENEWAL" || type == "INITIAL_PURCHASE") {
    const data = {
      maxMembers: plans[entitlementId].members,
      paymentDate: new Date(purchasedAtTime),
      paymentDueDate: new Date(expirationAtTime),
      status: "active",
      ownerEmail: teamMembership.memberships[0].userEmail,
      ownerName: teamMembership.memberships[0].userName,
    };

    await databases.updateDocument(
      "DBID",
      "ColledtionID",
      teamMembership.memberships[0].teamId,
      data
    );

    return res.empty();
  } else if (type == "EXPIRATION") {
    await databases.updateDocument(
      "DBID",
      "CollectionID",
      teamMembership.memberships[0].teamId,
      {
        status: "inactive",
        maxMembers: 1,
      }
    );
    return res.empty();
  }
};```
Steven
16 Feb, 2024, 16:38

FYI, it's best to wrap code in backticks to format a bit nicer. You can use 1 backtick for inline code (https://www.markdownguide.org/basic-syntax/#code) and 3 backticks for multiline code (https://www.markdownguide.org/extended-syntax/#syntax-highlighting.

Steven
16 Feb, 2024, 16:39

yes, when you execution a function via the function domain, it is executed synchronously.

The weird thing is that my webhook retrys sending the exact same response 5 times when it gets an error,

What do you mean by this?

Steven
16 Feb, 2024, 16:40

btw you don't need to await JSON.parse()

Steven
16 Feb, 2024, 16:42

you're not covering all cases here so there's a chance the function is not returning a response and maybe Appwrite is still waiting for that response

Asdshadow
16 Feb, 2024, 16:43

Thanks I fixed the backticks. What i mean is when the webhook gets a failed response it retrys sending the webhook and sometimes it gets a secessful response but sometimes it still fails with the same data being send to the function.

Steven
16 Feb, 2024, 16:43

Maybe the provider is making the additional requests. Do you see multiple executions in Appwrite?

Asdshadow
16 Feb, 2024, 16:44

yes I do. and even appwrite has some successful executions and sometimes it fails. same data being passed.

Steven
16 Feb, 2024, 16:45

if you see additional executions, it means the provider is making additional requests

Asdshadow
16 Feb, 2024, 16:45

the i have covered the only two cases that would be send though

Steven
16 Feb, 2024, 16:47

maybe you can add some logging to see where it's getting stuck

Asdshadow
16 Feb, 2024, 16:47

Ok so revenue cat sends a webhook and it fails so it sends another in like 5 min and it fails again sometimes it is successfull but when it is revenuecat stops sending. I have checked and it does not send any after a successfull response

Asdshadow
16 Feb, 2024, 16:47

I had logging and no logs were executed when it failed.

Steven
16 Feb, 2024, 16:47

ok so that would explain the multiple requests

Asdshadow
16 Feb, 2024, 16:51

This is not the first time i am experiencing this error. Whenever i try to run a function sync it is a 50/50 chance the function executes. When they do they execute in like 1s or less but when they fail they timeout cuz of the 30s. Before i have fixed this by running the functions async. But this time i am running the functions through a domain.

Steven
16 Feb, 2024, 16:51

can you share when your executions happen?

Asdshadow
16 Feb, 2024, 16:52

i dont understand

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more