Back

one instance function

  • 0
  • Self Hosted
  • Functions
antonio84
11 Oct, 2023, 18:19

Hello, I want to check if a cloud function is running and wait the end of the execution before to run a new one. How can I achieve that?

TL;DR
The user wants to check if a cloud function is running and wait for its execution to finish before running a new one. One solution is to create a document with a specific ID when the function runs, then continue processing and delete the document when done executing. Any other function that tries to run would get an error because no two documents can have the same ID. This solution ensures thread safety. An example code snippet in Dart is provided in the thread.
D5
13 Oct, 2023, 17:55

Hello. In your code you should get a response by default when you trigger it client sided

D5
13 Oct, 2023, 18:00

Example in Dart (tell me your preferred lang and will write one if needed):

TypeScript
try {
    final execution = await functions.createExecution(
        functionId: '[FUNCTION_ID]',
        body: json.encode({ 'foo': 'bar' }),
        async: false,
        path: '/',
        method: 'GET',
        headers: {
            'X-Custom-Header': '123'
        }
    );

//you can trigger here anything since the function executed successfully
    print("The function got executed correctly. Response:")
    print(execution.toMap());

} catch (e) {

//this will be triggered if the function execution fails or there is a network/appwrite issue
    print("An error ocurred:")
    print(e.message);
}
antonio84
14 Oct, 2023, 06:31

thank you for your response, I have multiple clients that access that function. I need to check if the function is running, it should be thread safe.

D5
14 Oct, 2023, 08:54

Note that doesn't goes in the function, but client side code

D5
14 Oct, 2023, 08:54

Could change depending on your appwrite version. The code I sent is for latest appwrite version

Drake
15 Oct, 2023, 03:55

I would do it by creating a locks collection.

When the function runs, it'll create a document with a specific id. If it can create the document, it should continue processing and then delete the document when done executing. Any other function that tries to run would get an error because no 2 documents can have the same ID

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