Back

How to schedule call to appwrite function ? Call multiple time same function lead to error

  • 1
  • Databases
  • Functions
  • Tools
  • Cloud
Horsty - Cyril (he/him)
16 Jun, 2024, 22:20

Hello, first, i open this post due to this message:

TypeScript
Internal curl errors has occurred within the executor! 
Error Number: 111. 
Error Msg: Connection refused
Error Code: 500

I'm using node-appwrite and parse a json file. I have 2 appwrite function:

    1. to fetch the data and create pool of smaller data
    1. to update document with the new price

To explain, i have a file with 90_000 card price, i use functions to update price each day. I'm parsing this data set and create pool of 1_000 update (that take 4/5min).

Basically my first function triggers 90 * 1_000 update

So first, i launch a function by cron that get the updated data (fresh price) I loop on the result to create the pool and iterate over it.

I've added a wait between each call of function of 500msbut i still get this error.

What can i do ?

TL;DR
Developers are experiencing connection errors and rate limits when scheduling calls to appwrite functions in order to update prices. The issue arises when the same function is called multiple times. The proposed solution is to add additional error handling and wait times between calls to avoid rate limits.
Horsty - Cyril (he/him)
16 Jun, 2024, 22:20

There are the function for the 1) step -> fetching updated price and create small pool of data by their index

TypeScript
export default async ({ req, res, log, error }: Context) => {
  const paperPrices = await fetchPrices();
  const client = new Client();
  const functions = new Functions(client);
  client
    .setEndpoint("ENDPOINT") // Your API Endpoint
    .setProject(process.env.APPWRITE_PROJECT_NAME) // Your project ID
    .setKey(process.env.APPWRITE_API_KEY); // Your secret API key

  function createExecution(startIndex: number, endIndex: number) {
    const payload = {
      startIndex: startIndex,
      endIndex: endIndex,
    };

    return functions
      .createExecution(
        "XXXXX", // functionId
        JSON.stringify(payload), // body
        true, // async (optional)
        "/", // path (optional)
        "POST" // method (optional)
        // {} // headers (optional)
      )
      .then(function (response) {
        log("Success");
      })
      .catch(function (err) {
        error("Error");
      });
  }
  const batchSize = 1000;
  const totalSize = paperPrices.length;
  for (let startIndex = 0; startIndex < totalSize; startIndex += batchSize) {
    const endIndex = Math.min(startIndex + batchSize, totalSize);
    await createExecution(startIndex, endIndex);
    // Need to add a wait time to avoid rate limit
    await new Promise((resolve) => setTimeout(resolve, 500));
  }

  if (req.method === "GET") {
    return res.send("SUCCESS");
  }
Horsty - Cyril (he/him)
16 Jun, 2024, 22:21

There are the code for the 2) function -> Get indexes, fetch the data and get small pool of data

TypeScript
export default async ({ req, res, log, error }: Context) => {
  try {
      const { startIndex, endIndex } = JSON.parse(req.body as string);
      const allPrices: TodayPriceFormat[] = await fetchPrices(); // Data from third service
      const prices: TodayPriceFormat[] = allPrices.slice(startIndex, endIndex);
      for (const price of prices) {
        await updatePrices(price); // Launch the update document function
      }
      return res.send("Updated price");
    } catch (err) {
      error("Error");
      return res.send("Failed to update price");
    }
  };

The update document is very simple and use the sdk inside the updatePrices() method

TypeScript
return databases.updateDocument(databaseId, priceCollectionId, docId, doc);
Horsty - Cyril (he/him)
16 Jun, 2024, 22:32

How to schedule call to appwrite function ? Call multiple time same function lead to error

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