Skip to content

Instantly share code, notes, and snippets.

@SchumacherFM
Last active April 20, 2023 10:58
Show Gist options
  • Save SchumacherFM/d1ddcb9e2740d8930b9e to your computer and use it in GitHub Desktop.
Save SchumacherFM/d1ddcb9e2740d8930b9e to your computer and use it in GitHub Desktop.
Magento Backend Reindex and fastcgi_finish_request()

Backend Section: Index Management

  • Nginx 1.4
  • PHP 5.5 FPM

Doc: fastcgi_finish_request

Clicking on one of the Reindex Data links executes the reindex process and immediately shows the URL from action preReindexProcess() but clicking on that link to initiate a new request takes as long as it takes to wait for the reindexing.

So something is still using the same connection and waits to finish the previous request. How to initiate a new request without waiting for the previous one?

Also the postReindexProcess() action will never be executed it only shows in var/report/ the error:

a:5:{i:0;s:144:"Cannot send headers; headers already sent in Zookal/Index/Model/Observer.php, line 37";i:1;s:1566:"#0 lib/Zend/Controller/Response/Abstract.php(148): Zend_Controller_Response_Abstract->canSendHeaders(true)

If this is not working properly you cannot implement it in checkout->saveOrder action ...

<?xml version="1.0"?>
<config>
<modules>
<Zookal_Index>
<version>0.0.1</version>
</Zookal_Index>
</modules>
<global>
<models>
<zookalindex>
<class>Zookal_Index_Model</class>
</zookalindex>
</models>
<helpers>
<zookalindex>
<class>Zookal_Index_Helper</class>
</zookalindex>
</helpers>
</global>
<adminhtml>
<events>
<controller_action_predispatch_adminhtml_process_reindexProcess>
<observers>
<zookal_index>
<class>zookalindex/observer</class>
<method>preReindexProcess</method>
</zookal_index>
</observers>
</controller_action_predispatch_adminhtml_process_reindexProcess>
<controller_action_postdispatch_adminhtml_process_reindexProcess>
<observers>
<zookal_index>
<class>zookalindex/observer</class>
<method>postReindexProcess</method>
</zookal_index>
</observers>
</controller_action_postdispatch_adminhtml_process_reindexProcess>
</events>
</adminhtml>
</config>
<?php
class Zookal_Index_Model_Observer
{
private $_isFinishingRequest = false;
/**
* fastcgi_finish_request implementation to run reindexing in the background
*
* @fire controller_action_predispatch_adminhtml_process_reindexProcess
*
* @param Varien_Event_Observer $observer
*
* @return $this
*/
public function preReindexProcess(Varien_Event_Observer $observer)
{
/** @var Mage_Index_Adminhtml_ProcessController $controller */
$controller = $observer->getEvent()->getControllerAction();
if (function_exists('fastcgi_finish_request')) {
$msg = Mage::helper('zookalindex')->__('Reindexing process has been initialized and runs in the background!');
// $this->_getSession()->addSuccess($msg);
//$this->_redirect($controller, '*/*/list');
$url = $controller->getUrl('*/*/list');
$controller->getResponse()->setBody('<a href="' . $url . '">' . $url . '</a><hr>' . $msg . '');
$controller->getResponse()->sendResponse();
$this->_isFinishingRequest = fastcgi_finish_request();
}
}
/**
*
* @fire controller_action_postdispatch_adminhtml_process_reindexProcess
*
* @return $this
*/
public function postReindexProcess()
{
if (true === $this->_isFinishingRequest) {
$messages = $this->_getSession()->getMessages(true);
Mage::log('fastcgi_finish_request: ' . $messages->toString());
exit;
}
}
/**
* Retrieve adminhtml session model object
*
* @return Mage_Adminhtml_Model_Session
*/
protected function _getSession()
{
return Mage::getSingleton('adminhtml/session');
}
/**
* @param Mage_Index_Adminhtml_ProcessController $controller
* @param $path
* @param array $arguments
*
* @return $this
*/
protected function _redirect(Mage_Index_Adminhtml_ProcessController $controller, $path, $arguments = array())
{
$this->_getSession()->setIsUrlNotice($controller->getFlag('', Mage_Index_Adminhtml_ProcessController::FLAG_IS_URLS_CHECKED));
$controller->getResponse()->setRedirect($controller->getUrl($path, $arguments));
return $this;
}
}
@SchumacherFM
Copy link
Author

Sending the header Connection: close in Zookal_Index_Model_Observer::preReindexProcess () does also not work. The click on the link forces the user to wait 20-30 seconds and then loads the indexer list but the indexer is still running in the background.

Nginx config says to add fastcgi_pass_header ... which isn't working so I've disabled every keep-alive request that nginx will always send Connection: close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment