1

What the title says. Im trying to run an import script with AJAX "call 1" and I want to keep track of the import (for feedback purposes) with AJAX "call 2". To give the end user live feedback these calls need to run simultaneously and "call 2" needs to call itself (recursive) to poll for changes.

I have the Controllers and the calls and everything works just fine, just not at the SAME time. Is it a soft lock on the database or is it something else?

Btw I am aware of the "async: true" setting for the ajax call.

[edit] It looks like Magento is preventing me from executing two controllers at the same time. Can anyone confirm this?

lloydimus
  • 109
  • 3
  • 13

2 Answers2

1

I think you cannot do two AJAX requests concurrently. This means you always needs to have a logical order, a.k. first 'call 1', then 'call 2'. If you want to make sure call 2 always fires after the call 1 just put it in the success method.

Like so:

$.ajax({
  url: "test-to-call-1",
  context: call-1-context
}).done(function() {
  $.ajax({
    url: "test-to-call-2",
    context: call-2-context
  }).done(function() {
    Now both ajax requests are done.
    And you could add the context of the first one to the second call.
  });



});

If you want to enable polling, just place a setTimeOut loop in which you do the second AJAX call :)

Like this:

function start_polling(counter){
  if(counter < 10){  // poll maximum of 10 times.
    setTimeout(function(){
      counter++;


     $.ajax({
            url: "test-to-call-2",
            context: call-2-context
          }).done(function() {
           start_polling(counter);
            Now both ajax requests are done. 
            And you could add the context of the first one to the second call.
          }).error(function(){
           start_polling(counter);
          });




    }, 1000);
  }
}
$.ajax({
  url: "test-to-call-1",
  context: call-1-context
}).done(function() {

  start_polling(0)



});
gabn88
  • 781
  • 8
  • 23
  • 1
    I appreciate your answer but it's not what I'm looking for. I want to give the end user feedback **during** the import, not afterwards. I work mainly with Symfony and within that framework it's possible to have concurrent AJAX calls, so that's why I set out to build what I had in mind, and now I'm stuck because of Mag2. It could also be related to a session lock. Ill try and figure it out. Thanks for your answer! – lloydimus Feb 22 '17 at 07:33
  • Ah, ok. That's probably a misunderstanding from my part. Don't know how it works with PHP frameworks, but in Python I would create a background task on a seperate worker. Within this task I would create a percentage indication based on the task that could be polled :) So you see, a slightly different question can lead to completely different anwers ;) – gabn88 Feb 22 '17 at 15:00
1

Well I figured it out.

All I had to do was set:

session_write_close();

In front of the method that started the import and I could start polling with a second AJAX call!

This is probably frowned upon, but it works

lloydimus
  • 109
  • 3
  • 13
  • I have written session_write_close() just above the import function but it didn't work. can you please specify your answer more in details. – shashank Jul 17 '18 at 13:43