Welcome Guest, Not a member yet? Register   Sign In
Session reverting back to previous state after slow ajax call finishes
#1

[eluser]brainer[/eluser]
I'm noticing weird behavior with the Session library.

I have a account setup wizard on my site with 5 steps. The users current position on the wizard is saved in a session item called 'wizard_level'. On the 5th step their 'wizard level' is set as 'complete' and they get redirected to their dashboard.

However, on the 4th step there is a background AJAX task (not important what it does - but it takes around 5 seconds to complete), and if the user carries on to the 5th step before this AJAX task finishes there are some unexpected results:

1. The user's 'wizard_level' is marked as 'complete' and they get sent to their dashboard (fine)
2. The original background ajax call from step 4 finally finishes - I can see this in Charles Proxy
3. The user's 'wizard_level' is now back to the 'step_4'.

It seems to me that when the AJAX call finishes, it refreshes the session to be the same as when the call started, thus ignoring the update to the session which happened on step 5.

I thought this might be a potential solution, but was wrong:
http://ellislab.com/forums/viewthread/138823/P0/

Been stuck on this four hours now. Anybody have any ideas?
#2

[eluser]toopay[/eluser]
Use some technical languange (the easy way should be speak with your code) rather than some business languange (like "I have a account setup wizard on my site with 5 steps"). It will make your issue more clear to understand.
#3

[eluser]brainer[/eluser]
Let's just say there two controller methods:

check_something()
update_session()

check_something() runs first (via AJAX) and takes around 5 seconds to complete. In the mean time update_session() (non-AJAX) runs and takes < 1 second to complete.

While running, update_session() updates an item in the session, which remains in the session until check_something() finishes running, then the session item returns to how it was before either script was called.

However if check_something() finishes before update_session() is called, everything is fine.
#4

[eluser]toopay[/eluser]
[quote author="brainer" date="1305223563"]
check_something() runs first (via AJAX) and takes around 5 seconds to complete. In the mean time update_session() (non-AJAX) runs and takes < 1 second to complete.
[/quote]
Dont really understand, and where the "5 seconds to complete" or "1 second to complete" come from? Did you benchmarking it or...
#5

[eluser]brainer[/eluser]
The exact number of seconds isn't important. What's important is:

- check_something() takes several seconds
- if update_session() gets called while check_something() is still running, the update it makes to the session will be reverted when check_something() finally finishes.

I don't think I can explain any better, sorry.

Anyone experience anything like this before?
#6

[eluser]toopay[/eluser]
hahaha, ok of course thats not the important things, just curious how that numbers calculated.

If your problems is just related with some "triggering"(since you dont include any code), and you need to synchronize your function run time, then the simple solution would be use
Code:
// Change 10 to whatever second you need to delay
sleep(10);
at function you want to delay.

If you can provide better explanation, than maybe i can suggest more things.
#7

[eluser]brainer[/eluser]
Smile Nah, sleep() won't work in this case. As I mentioned above this is all part of a 'wizard' section...

check_something() is an AJAX call which runs in the background of the 2nd last step of the wizard
update_session() is the last step of the wizard.

The problem is; if you go to the final step of the wizard before the AJAX background call finishes on the previous step - the session update which happens on the final step gets reverted when the AJAX call which started on the previous page finally finishes.
#8

[eluser]toopay[/eluser]
[quote author="brainer" date="1305229128"]The problem is; if you go to the final step of the wizard before the AJAX background call finishes on the previous step - the session update which happens on the final step gets reverted when the AJAX call which started on the previous page finally finishes.[/quote]

Thats sound like a "logical" mistake for me. I mean, how its possible ? You should, give some flag on function which called by ajax, then inspect is there any "success" flag from previous step before running the next step (we started sound confusing, right? thats why i suggest you to share your code at the first place :-S )
#9

[eluser]brainer[/eluser]
toopay, thanks for trying.

Anyone else?
#10

[eluser]toopay[/eluser]
[quote author="brainer" date="1305229775"]toopay, thanks for trying.

Anyone else?[/quote]

Hahaha. Seriously, i never ever get a such thing as a response in this forums. Maybe i had said something that make you feel discomfort, and for that, forgive me ;-)

But before you try debug your code and working more deep on it, then consider the "logic" i suggest at my previous post.

This is some example of what i'm talking about :
Code:
// ...
public function check_something()
{
   //...

   // At the end or at the success statement of this function, put some flag
   $this->session->set_userdata('success', TRUE);
}
// ...
public function update_session()
{
   // Before any single line executed, inspect if the previous step is succed
   if($this->session->userdata('success') == TRUE)
   {
      // Then you can execute something...
   }
   else
   {
      // Redirect to previous step or whatever you need
   }
}
// ...




Theme © iAndrew 2016 - Forum software by © MyBB