0

Basically. I've created a button in M2 admin. Clicking on that button runs my custom-created indexer.

The indexer is fetching all the products and sends them to a third-party service with a POST request.

Now, the problem is 'clicking on the button syncs the products. But, during the process, the admin page keeps loading until the process is completed. The process takes time (2-3 mins).

So, Is there any method that can help me to run the sync process in the background and stop the page from continuously loading?

Thank you in advance!

1 Answer 1

0

You can run the sync process in the background and stop the page from continuously loading by using Magento's asynchronous task framework.

Create a new task using the Magento\Framework\Async\AsyncResponseInterface interface.

In the execute() method of the task, put the code that runs the sync process.

In the controller action that's triggered by the button click, instead of running the sync process directly, schedule the task using the Magento\Framework\Async\ManagerInterface.

Return a response to the user immediately after scheduling the task. This will stop the page from continuously loading.

use Magento\Framework\Async\AsyncResponseInterface;
use Magento\Framework\Async\ManagerInterface;
use Magento\Framework\Controller\ResultFactory;

class MyController extends \Magento\Backend\App\Action
{
    protected $_asyncManager;

    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        ManagerInterface $asyncManager
    ) {
        parent::__construct($context);
        $this->_asyncManager = $asyncManager;
    }

    public function execute()
    {
        $task = new class implements AsyncResponseInterface {
            public function execute()
            {
                // Put your code here to run the sync process
            }
        };

        $this->_asyncManager->launch($task);

        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }
}

In this example, we're creating a new task that implements the AsyncResponseInterface and contains the code to run the sync process. Then, we're using the _asyncManager object to launch the task in the background. Finally, we're returning a redirect response to the user, which will stop the page from continuously loading.

By using this approach, the user will be able to trigger the sync process with the click of a button, and the sync process will run in the background without interfering with the user's ability to use the admin interface.

2
  • Thank you for your answer with brief explanation. I would like to tell you that i am using magento2.4.2 and i am getting the following error during execution of bin/magento s:d:c command :- Class Magento\Framework\Async\ManagerInterface does not exist . Apr 10, 2023 at 6:55
  • There is no issue with permission. Also composer is updated to version 2.5.4. Apr 10, 2023 at 7:27

Your Answer

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

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