Replies: 1 comment 1 reply
-
I copy-paste your description in Cursor Composer and less than a minute, it provides a solution. <?php
/**
* OpenMage
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available at https://opensource.org/license/osl-3-0-php
*
* @category Mage
* @package Mage_Core
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://www.magento.com)
* @copyright Copyright (c) 2020-2024 The OpenMage Contributors (https://www.openmage.org)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Deferred task execution manager
*
* @category Mage
* @package Mage_Core
*/
class Mage_Core_Model_Deferred
{
/**
* @var array
*/
protected $_tasks = [];
/**
* @var bool
*/
protected $_isRegistered = false;
/**
* Add a task to be executed after response is sent
*
* @param callable $callback
* @param array $params
* @return $this
*/
public function addTask($callback, array $params = [])
{
$this->_tasks[] = ['callback' => $callback, 'params' => $params];
if (!$this->_isRegistered) {
register_shutdown_function([$this, 'executeTasks']);
$this->_isRegistered = true;
}
return $this;
}
/**
* Execute all registered tasks
* Called automatically on shutdown
*/
public function executeTasks()
{
if (connection_status() !== CONNECTION_NORMAL) {
return; // Client disconnected, skip deferred tasks
}
// Ensure output buffer is flushed
while (ob_get_level()) {
ob_end_flush();
}
flush();
// Execute tasks
foreach ($this->_tasks as $task) {
try {
call_user_func_array($task['callback'], $task['params']);
} catch (Exception $e) {
Mage::logException($e);
}
}
}
} Not sure if it's any good. There is a GitHub Copilot on top of this page, you can try that as well. Often time, the AI misses what you intended, you just need to explain again, may be a few iterations later, you'll get a workable solution. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Laravel offers the functionality to defer tasks until the response has been sent to the client.
It allows to spend a couple of seconds on a task which doesn't influence the response. One common usage example is server-side analytics. Compose and send a message about the user's visit to the analytics platform after the response to the visitor has been sent.
It works by collecting all deferred calls. Then after the response has been sent, the FPM process keeps running and executes all collected calls.
How can this be implemented in OpenMage?
Beta Was this translation helpful? Give feedback.
All reactions