Búsquedas recientes


No hay búsquedas recientes

How to deal with asynchrony in API calls

Respondidas


Publicado 28 sept 2022

Hello All,

I am writing an app to bulk delete users. However, users that have un-closed tickets cannot be deleted. I can run an API call to close their tickets, then run the call to delete the user, but because the calls run asynchronously the second call executes before the first has finished -- so the users are not deleted (as the tickets have not yet been closed).

To mitigate this, I nested one API call within the other -- something like this:

function closeTicketsAndDeleteUsers(arrayTicketIDs,arrayUserIDs)
{
  // close tickets
   API = "/api/v2/tickets/update_many.json?ids=" + arrayTicketIDs;
   myURL = encodeURI(API); 
   Var settings = {
        type: 'PUT',
        url: myURL
   };
  client.request(settings).then(
            function(data) {    
                              // success close tickets -- now delete users
                API = "/api/v2/users/destroy_many.json?ids=" + arrayUserIDs;
                               myURL = encodeURI(API); 
                              Var settings = {
                             type: 'DELETE',
                             url: myURL
                                };
                               client.request(settings)
            },
            function(response) {
                showError(response);
            },
        );
}

However, this still does not work. It successfully deletes the tickets but does not delete the user. I think this may be because the 'delete tickets' call puts the tickets in a queue, and they are still not deleted by the time that the 'delete users' call is executed. The users are successfully deleted only if there are no tickets for them.

Can anyone advise how to achieve my aim of both closing tickets and deleting users within a single process?

Thanks,

Simon.


0

3

3 comentarios

Just want to add:

I utilised an asynchronous function to get the status of the delete, as suggested by Jack above.

I added an asynchronous 'sleep' function to pause between calls to the status url, so that the rate limiter should not be triggered.

This solved the problem.

1


Hi Jack,

Thanks for your suggestion. I think this could fix my issue.

I wondered about defining async functions (particularly as there is an 'await' command) but it looked like these were more to allow asynchronous execution rather than the sequential execution that I am after.

Thanks,

Simon.

0


So the Update Many Tickets endpoint actually returns a job status object, so you are sort of right in thinking the call to that endpoint will put the tickets in a "queue" as it will delete them one by one and the job status will update. 

You'll want to do something like this:

  • Make the API call to Update Many Tickets
  • Get the initial response from that API call (the job status) - This response will include a "url" property - this is important
  • Make a GET request to that URL from the job status - you'll want to repeat this step so you can check to see when the job is finished, you can also use this as a basis to log out the progress maybe, i.e. 'Closing user's ticket 1/100', Just make sure you introduce a pause or put this step in a set timeout when looping it so as to not hammer the API and get annoyed at rate limits. Ideally you can poll the job status every 5 seconds for best results I find. 
  • When the job is finished (the "status" property in the response will change to "completed") you know it will be safe to delete the user.

It may also be beneficial to look into async functions.

1


Iniciar sesión para dejar un comentario.

¿No encontró lo que buscaba?

Nueva publicación