Fix: Move unused functions from main module file to class files and resolve critical and major issues
This commit is contained in:
@@ -45,7 +45,7 @@ class QctmCronTask extends ObjectModel
|
||||
'id_module' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
|
||||
'task_name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 128, 'required' => true),
|
||||
'description' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 255, 'required' => true),
|
||||
'cron_expression' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 64, 'required' => true),
|
||||
'cron_expression' => array('type' => self::TYPE_STRING, 'validate' => 'isString', 'size' => 64, 'required' => true),
|
||||
'callback' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 128, 'required' => true),
|
||||
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
|
||||
'is_system' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
|
||||
@@ -64,6 +64,166 @@ class QctmCronTask extends ObjectModel
|
||||
);
|
||||
}
|
||||
|
||||
public function dispatch()
|
||||
{
|
||||
$tasks = self::getActiveTasks();
|
||||
if (empty($tasks)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$now = new DateTime('now', new DateTimeZone(Configuration::get('PS_TIMEZONE') ?: date_default_timezone_get()));
|
||||
$moduleCache = [];
|
||||
foreach ($tasks as $task) {
|
||||
if (!\Cron\CronExpression::isValidExpression($task['cron_expression'])) {
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_ERROR,
|
||||
'Invalid cron expression "' . $task['cron_expression'] . '"'
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$cron = \Cron\CronExpression::factory($task['cron_expression']);
|
||||
|
||||
if (!$cron->isDue($now)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$idModule = (int) $task['id_module'];
|
||||
if (!array_key_exists($idModule, $moduleCache)) {
|
||||
$moduleCache[$idModule] = Module::getInstanceById($idModule);
|
||||
}
|
||||
$module = $moduleCache[$idModule];
|
||||
|
||||
if (!$module) {
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_ERROR,
|
||||
'Module with ID "' . (int) $task['id_module'] . '" could not be loaded'
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$module->active) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$callback = $task['callback'];
|
||||
|
||||
if (!method_exists($module, $callback)) {
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_ERROR,
|
||||
'Callback method "' . $callback . '" not found on module "' . $module->name . '"'
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$startTime = microtime(true);
|
||||
|
||||
try {
|
||||
$module->{$callback}();
|
||||
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_SUCCESS,
|
||||
null,
|
||||
round(microtime(true) - $startTime, 4)
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_ERROR,
|
||||
$e->getMessage(),
|
||||
round(microtime(true) - $startTime, 4)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function syncAllExistingModules()
|
||||
{
|
||||
$allTasks = Hook::exec('registerCronTasks', array(), null, true);
|
||||
|
||||
if (!empty($allTasks) && is_array($allTasks)) {
|
||||
foreach ($allTasks as $moduleName => $tasks) {
|
||||
if (!empty($tasks) && is_array($tasks)) {
|
||||
$module = Module::getInstanceByName($moduleName);
|
||||
if($module){
|
||||
$this->registerTasksForModule($module->id, $tasks);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register tasks for a specific module after validation
|
||||
*
|
||||
* @param int $idModule
|
||||
* @param array $tasks
|
||||
*/
|
||||
public function registerTasksForModule($idModule, $tasks)
|
||||
{
|
||||
$module = Module::getInstanceById($idModule);
|
||||
if (!$module) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
if (!$this->validateTask($module, $task)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (QctmCronTask::getByModuleAndName($idModule, $task['name'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$cronTask = new QctmCronTask();
|
||||
$cronTask->id_module = $idModule;
|
||||
$cronTask->task_name = $task['name'];
|
||||
$cronTask->description = $task['description'];
|
||||
$cronTask->cron_expression = $task['cron'];
|
||||
$cronTask->callback = $task['callback'];
|
||||
$cronTask->active = 1;
|
||||
$cronTask->is_system = 0;
|
||||
if (!$cronTask->save()) {
|
||||
PrestaShopLogger::addLog(
|
||||
'QloCronTaskManager: failed to register task "' . $task['name'] . '" for module "' . $module->name . '"',
|
||||
3
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a task definition array
|
||||
*
|
||||
* @param object $module
|
||||
* @param array $task
|
||||
* @return bool
|
||||
*/
|
||||
public function validateTask(Module $module, $task)
|
||||
{
|
||||
if (!Validate::isGenericName($task['name']) || !Validate::isGenericName($task['description'])
|
||||
|| !Validate::isString($task['cron']) || !Validate::isString($task['callback'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!\Cron\CronExpression::isValidExpression($task['cron'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!method_exists($module, $task['callback'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $idModule
|
||||
* @param string $taskName
|
||||
|
||||
@@ -372,6 +372,11 @@ class AdminCronTaskManagerController extends ModuleAdminController
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
if ($task['name'] === $cronTask->task_name) {
|
||||
if (!$cronTask->validateTask($module, $task)) {
|
||||
$this->errors[] = $this->l('Module returned an invalid task definition (bad cron expression or missing callback).');
|
||||
return false;
|
||||
}
|
||||
|
||||
$cronTask->cron_expression = $task['cron'];
|
||||
$cronTask->description = $task['description'];
|
||||
$cronTask->callback = $task['callback'];
|
||||
|
||||
@@ -35,7 +35,9 @@ if (!$token || $token !== $expectedToken) {
|
||||
$module = Module::getInstanceByName('qlocrontaskmanager');
|
||||
|
||||
if (!$module || !$module->active) {
|
||||
die('Module not active');
|
||||
die('Cron module not active');
|
||||
}
|
||||
|
||||
$module->dispatch();
|
||||
require_once dirname(__FILE__) . '/classes/QctmCronTask.php';
|
||||
$objCronTask = new QctmCronTask();
|
||||
$objCronTask->dispatch();
|
||||
|
||||
@@ -174,7 +174,8 @@ class QloCronTaskManager extends Module
|
||||
if (method_exists($module, 'hookRegisterCronTasks')) {
|
||||
$tasks = $module->hookRegisterCronTasks();
|
||||
if (!empty($tasks) && is_array($tasks)) {
|
||||
$this->registerTasksForModule($module->id, $tasks);
|
||||
$objCronTask = new QctmCronTask();
|
||||
$objCronTask->registerTasksForModule($module->id, $tasks);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -188,164 +189,6 @@ class QloCronTaskManager extends Module
|
||||
QctmCronTask::deleteByModule($params['object']->id);
|
||||
}
|
||||
|
||||
protected function syncAllExistingModules()
|
||||
{
|
||||
$allTasks = Hook::exec('registerCronTasks', array(), null, true);
|
||||
|
||||
if (!empty($allTasks) && is_array($allTasks)) {
|
||||
foreach ($allTasks as $moduleName => $tasks) {
|
||||
if (!empty($tasks) && is_array($tasks)) {
|
||||
$module = Module::getInstanceByName($moduleName);
|
||||
if($module){
|
||||
$this->registerTasksForModule($module->id, $tasks);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register tasks for a specific module after validation
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @param array $tasks
|
||||
*/
|
||||
protected function registerTasksForModule($idModule, $tasks)
|
||||
{
|
||||
$module = Module::getInstanceById($idModule);
|
||||
if (!$module) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
if (!$this->validateTask($module, $task)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (QctmCronTask::getByModuleAndName($idModule, $task['name'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$cronTask = new QctmCronTask();
|
||||
$cronTask->id_module = $idModule;
|
||||
$cronTask->task_name = $task['name'];
|
||||
$cronTask->description = $task['description'];
|
||||
$cronTask->cron_expression = $task['cron'];
|
||||
$cronTask->callback = $task['callback'];
|
||||
$cronTask->active = 1;
|
||||
$cronTask->is_system = 0;
|
||||
if (!$cronTask->save()) {
|
||||
PrestaShopLogger::addLog(
|
||||
'QloCronTaskManager: failed to register task "' . $task['name'] . '" for module "' . $module->name . '"',
|
||||
3
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a task definition array
|
||||
*
|
||||
* @param string $moduleName
|
||||
* @param array $task
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateTask(Module $module, $task)
|
||||
{
|
||||
if (!Validate::isGenericName($task['name']) || !Validate::isGenericName($task['description'])
|
||||
|| !Validate::isString($task['cron']) || !Validate::isString($task['callback'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!\Cron\CronExpression::isValidExpression($task['cron'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!method_exists($module, $task['callback'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function dispatch()
|
||||
{
|
||||
if (!$this->active) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tasks = QctmCronTask::getActiveTasks();
|
||||
if (empty($tasks)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$now = new DateTime();
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
if (!\Cron\CronExpression::isValidExpression($task['cron_expression'])) {
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_ERROR,
|
||||
'Invalid cron expression "' . $task['cron_expression'] . '"'
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$cron = \Cron\CronExpression::factory($task['cron_expression']);
|
||||
|
||||
if (!$cron->isDue($now)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$module = Module::getInstanceById($task['id_module']);
|
||||
|
||||
if (!$module) {
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_ERROR,
|
||||
'Module with ID "' . (int) $task['id_module'] . '" could not be loaded'
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$module->active) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$callback = $task['callback'];
|
||||
|
||||
if (!method_exists($module, $callback)) {
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_ERROR,
|
||||
'Callback method "' . $callback . '" not found on module "' . $module->name . '"'
|
||||
);
|
||||
continue;
|
||||
}
|
||||
|
||||
$startTime = microtime(true);
|
||||
|
||||
try {
|
||||
$module->{$callback}();
|
||||
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_SUCCESS,
|
||||
null,
|
||||
round(microtime(true) - $startTime, 4)
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
QctmCronTaskLog::addLog(
|
||||
$task['id_cron_task'],
|
||||
QctmCronTaskLog::QCTM_CRON_TASK_LOG_STATUS_ERROR,
|
||||
$e->getMessage(),
|
||||
round(microtime(true) - $startTime, 4)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the system log-cleanup cron task (daily at midnight).
|
||||
*/
|
||||
@@ -361,6 +204,7 @@ class QloCronTaskManager extends Module
|
||||
public function install()
|
||||
{
|
||||
$db = new QctmCronTaskManagerDb();
|
||||
$objCronTask = new QctmCronTask();
|
||||
|
||||
if (!parent::install()
|
||||
|| !$this->registerHooks()
|
||||
@@ -372,7 +216,7 @@ class QloCronTaskManager extends Module
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->syncAllExistingModules();
|
||||
$objCronTask->syncAllExistingModules();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
{*
|
||||
* NOTICE OF LICENSE
|
||||
*
|
||||
* This source file is subject to the Academic Free License (AFL 3.0)
|
||||
* that is bundled with this package in the file LICENSE.md
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* https://opensource.org/licenses/afl-3.0.php
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to support@qloapps.com so we can send you a copy immediately.
|
||||
*
|
||||
* DISCLAIMER
|
||||
*
|
||||
* Do not edit or add to this file if you wish to upgrade this module to a newer
|
||||
* versions in the future. If you wish to customize this module for your needs
|
||||
* please refer to https://store.webkul.com/customisation-guidelines for more information.
|
||||
*
|
||||
* @author Webkul IN
|
||||
* @copyright Since 2010 Webkul
|
||||
* @license https://opensource.org/licenses/afl-3.0.php Academic Free License 3.0
|
||||
*}
|
||||
|
||||
<div class="btn-group pull-right">
|
||||
<a href="{$logLink}" class="btn btn-default" title="View">
|
||||
<i class="icon-search-plus"></i> View
|
||||
</a>
|
||||
</div>
|
||||
Reference in New Issue
Block a user