2

I am trying to import data with one ajax and trying to do live tracking of this imported data with another ajax to show progress bar(how many records have been uploaded).

checkPrgressVar = setInterval(checkPrgress, 1000);
importTaxes(importURL,cart_url,cart_token); 

/* check progress of imported data */
function checkPrgress()
{
            $.ajax({
                url: $(document).find("input[name='progressURL']").val(),
                type: 'POST',
                dataType: 'json',
               // showLoader: true,
                data: "action=import_store",
                complete: function(response) { debugger            
                    var response = response.responseJSON.output;

                },
                error: function (xhr, status, errorThrown) {
                    console.log('Error happens. Try again.');
                }
            });

}

/* import data  */
  function importTaxes(importURL,cart_url,cart_token)
  {
                $.ajax({
                    url: importURL,
                    type: 'POST',
                    dataType: 'json',
                    showLoader: true,
                    data: "cart_url="+cart_url+"&cart_token="+cart_token+"&action=import_tax",
                    complete: function(response) { debugger            
                        var response = response.responseJSON.output;
                        if(response.result == 'processing')
                        {

                        }
                        else
                        {

                        }         
                    },
                    error: function (xhr, status, errorThrown) {
                        console.log('Error happens. Try again.');
                    }
                });
 }

My checkProgress ajax returns response only after getting response from importTax ajax while this checkProgress ajax should be independent of importProgress ajax.

However both ajax is calling different controllers, it seems magento is not allowing another controller to be called until it processed another one.

I don't know why it happens and how to resolve it ?

I have tried this link but not working.

https://stackoverflow.com/questions/42274778/is-it-possible-to-fire-multiple-ajax-requests-asynchronously-in-magento-2-backen

EDIT1: what I have figured out that if I call another external URL instead of magento2 controller in checkProgress ajax call. it started working. It means magento2 is not allowing another controller to be execute while another is running via import ajax.

EDIT2- Both ajax started to work as expected if I change session storage from "files" to "db" in env.php file. It seems a session blocking issue as explained in below link-

https://stackoverflow.com/questions/15686572/two-simultaneous-ajax-requests-wont-run-in-parallel

But use DB as session storage is not preferable. So If I use files as session storage still I don't know how to resolve it.

Have any one idea about it ?

1 Answer 1

2

Finally I have figured out its solutions.

The main reason of not running both AJAX concurrently was session blocking issue as explained in below URL.

https://stackoverflow.com/questions/15686572/two-simultaneous-ajax-requests-wont-run-in-parallel

but session_write_close() didn't work for me. So after research I have found my solution and here it is -

I used Magento\Framework\Session\SaveHandler to close and open session in magento2.

I load this class in construct in one of my controller file(which handles ajax) -

/**
 * @var \Magento\Framework\Session\SaveHandler
*/
protected $sessionHandler;

public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\App\ResourceConnection $resourceConnection,
        \Magento\Framework\Session\SaveHandler $sessionHandler
    ) {
        parent::__construct($context);
        $this->resultJsonFactory = $resultJsonFactory;
        $this->resultPageFactory = $resultPageFactory;
        $this->connection = $resourceConnection;
        $this->sessionHandler = $sessionHandler;
    }

Then in execute function, I closed and open session like this-

 public function execute()
 {
        $tmp_session_dir = ini_get("session.save_path");
        $this->sessionHandler->close();
        $this->sessionHandler->open($tmp_session_dir,"admin");

        // my code here

 }

ini_get("session.save_path") gives me the path of directory on server where session files are saved.

Now my both ajax run concurrently and independent of each other.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.