• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to delay?

#1
I have a function that both adds a row to mySQL and also registers a user in FusionAuth which is an authentication package I am using.  The FusionAuth call that registers the user returns an ID number that I need to keep in the row in mySQL. But I am finding a race condition where FusionAuth is returning the ID number before mySQL has finished creating the record. 

So I think that I need to insert a delay in my code prior to updating the ID field in the mySQL table. If I don't, I will sometimes get an error because I am trying to update a record that doesn't exist yet. 

Is there some type of CI delay I can use or perhaps another way to deal with this race condition? Perhaps the append record in mySQL is being cached? It is being created by Grocery Crud. Is there a way to force the cache to purge before I try to update the ID number?
proof that an old dog can learn new tricks
Reply

#2
Adding a delay is rarely a good solution. I don't know FusionAuth. Is there some ajax call involved? If not, I don't get how you can have this problem if everything is done in the same MySQL session.
Reply

#3
ajax, yes.
proof that an old dog can learn new tricks
Reply

#4
(11-16-2020, 07:07 AM)richb201 Wrote: ajax, yes.

Then you should wait until the ajax request is completed before you continue. By waiting I mean use a callback function that will be called when the ajax request is completed. That will guarantee that you can proceed with what you need to do next. Do not use a real waiting function with a fixed number of milliseconds to wait. It's never reliable and also bad user experience if it freeze the page.
Reply

#5
The ajax is embedded in the Grocery Crud code which I don't have access to. I agree with you, a 1 second delay is not the best thing in the world, but it surely is better than an error. Both the mySQL and the Fusion Auth are running on the server. How about my CI code? Is the php being converted into something that is running in the client's browser? If the delay is on the user's PC, no biggie. If it is on the server, that would not be good.
proof that an old dog can learn new tricks
Reply

#6
You do now.

GitHub - Grocery CRUD
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#7
(11-16-2020, 08:28 AM)richb201 Wrote: The ajax is embedded in the Grocery Crud code which I don't have access to. I agree with you, a 1 second delay is not the best thing in the world, but it surely is better than an error.

Maybe there's a way to define a callback function that Grocery Crud can call when the request is finished. But I don't know. I never used that.

(11-16-2020, 08:28 AM)richb201 Wrote: How about my CI code? Is the php being converted into something that is running in the client's browser?

No, all CI and PHP in general is running on the server. Only JS run in the browser.

(11-16-2020, 08:28 AM)richb201 Wrote: If the delay is on the user's PC, no biggie. If it is on the server, that would not be good.

If you can't find a way to define a callback function, you can always set a timer to call the backend again if it's not working the first time. Maybe try 3 times at a 500ms interval then stop and display an error. It's dirty but it's better than a 1 second delay.

Edit: I forgot, do this in JS, that will run in the browser.

(11-16-2020, 12:44 PM)InsiteFX Wrote: You do now.

GitHub - Grocery CRUD

Editing the code of third party libraries is rarely a good a idea.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.