From bd433ae292f25abccb63bae1fb21c94cdbbf2f75 Mon Sep 17 00:00:00 2001 From: Martin Pachol Date: Fri, 16 Mar 2018 13:39:27 -0400 Subject: [PATCH] multiple buxfixs and enhancements --- .../Cron/Block/Adminhtml/Settings.php | 4 +- .../Controller/Adminhtml/Settings/Index.php | 45 +++++++++++++++---- .../Cron/Model/ResourceModel/Schedule.php | 8 ++-- app/code/MageMojo/Cron/Model/Schedule.php | 27 ++++++++--- app/code/MageMojo/Cron/Setup/InstallData.php | 4 +- .../view/adminhtml/templates/settings.phtml | 35 ++++++++++----- 6 files changed, 90 insertions(+), 33 deletions(-) diff --git a/app/code/MageMojo/Cron/Block/Adminhtml/Settings.php b/app/code/MageMojo/Cron/Block/Adminhtml/Settings.php index 7db1c07..f9a940d 100644 --- a/app/code/MageMojo/Cron/Block/Adminhtml/Settings.php +++ b/app/code/MageMojo/Cron/Block/Adminhtml/Settings.php @@ -36,9 +36,9 @@ public function checkbox($path, $name) { print '>'; } - public function textfield($path, $name) { + public function textfield($path, $name, $size, $max) { $value = $this->resourceconfig->getConfigValue($path, 'default', 0); - print ''; + print ''; } } diff --git a/app/code/MageMojo/Cron/Controller/Adminhtml/Settings/Index.php b/app/code/MageMojo/Cron/Controller/Adminhtml/Settings/Index.php index 1149cbb..2b4c422 100644 --- a/app/code/MageMojo/Cron/Controller/Adminhtml/Settings/Index.php +++ b/app/code/MageMojo/Cron/Controller/Adminhtml/Settings/Index.php @@ -1,20 +1,23 @@ resultPageFactory = $resultPageFactory; $this->resource = $resource; + $this->messageManager = $messageManager; parent::__construct($context); } /** @@ -25,19 +28,43 @@ public function __construct( public function execute() { if ($this->getRequest()->getParam('form_key')) { + $fail = false; if ($this->getRequest()->getParam('enabled')) { $this->resource->setConfigValue('magemojo/cron/enabled','default',0,1); } else { $this->resource->setConfigValue('magemojo/cron/enabled','default',0,0); } - $this->resource->setConfigValue('magemojo/cron/jobs','default',0,$this->getRequest()->getParam('maxjobs')); - $this->resource->setConfigValue('magemojo/cron/phpproc','default',0,$this->getRequest()->getParam('phpproc')); - $this->resource->setConfigValue('magemojo/cron/maxload','default',0,$this->getRequest()->getParam('maxload')); - $this->resource->setConfigValue('magemojo/cron/history','default',0,$this->getRequest()->getParam('history')); + if (is_numeric($this->getRequest()->getParam('maxjobs'))) { + $this->resource->setConfigValue('magemojo/cron/jobs','default',0,$this->getRequest()->getParam('maxjobs')); + } else { + $fail = true; + $this->messageManager->addError('Max Jobs must be numeric'); + } + if ($this->getRequest()->getParam('phpproc')) { + $this->resource->setConfigValue('magemojo/cron/phpproc','default',0,$this->getRequest()->getParam('phpproc')); + } else { + $fail = true; + $this->messageManager->addError('PHP Binary cannot by null'); + } + if (is_numeric($this->getRequest()->getParam('maxload'))) { + $this->resource->setConfigValue('magemojo/cron/maxload','default',0,$this->getRequest()->getParam('maxload')); + } else { + $fail = true; + $this->messageManager->addError('Max Load must be numeric'); + } + if (is_numeric($this->getRequest()->getParam('history'))) { + $this->resource->setConfigValue('magemojo/cron/history','default',0,$this->getRequest()->getParam('history')); + } else { + $fail = true; + $this->messageManager->addError('History must be numeric'); + } + if (!$fail) { + $this->messageManager->addSuccess('Cron Configuration Saved'); + } } - + return $this->resultPageFactory->create(); - + } } diff --git a/app/code/MageMojo/Cron/Model/ResourceModel/Schedule.php b/app/code/MageMojo/Cron/Model/ResourceModel/Schedule.php index 9389fd5..f7053b2 100644 --- a/app/code/MageMojo/Cron/Model/ResourceModel/Schedule.php +++ b/app/code/MageMojo/Cron/Model/ResourceModel/Schedule.php @@ -7,7 +7,7 @@ public function _construct() { $this->_init('cron_schedule', 'schedule_id'); } - + #making our own function for this because it doesnt't work anyplace consistantly public function getConfigValue($path,$scope,$scopeid) { $connection = $this->getConnection(); @@ -33,7 +33,7 @@ public function getLastJobTime() { $result = $connection->fetchOne($select); return $result; } - + public function saveSchedule($job, $created, $schedule) { $connection = $this->getConnection(); $insertdata = array(); @@ -90,7 +90,7 @@ public function getJobsByStatus($status) { $result = $connection->fetchAll($select); return $result; } - + public function resetSchedule() { $connection = $this->getConnection(); $message = 'Parent Cron Process Terminated Abnomally'; @@ -109,7 +109,7 @@ public function cleanSchedule($days) { } return $ids; } - + public function getSettings() { $connection = $this->getConnection(); diff --git a/app/code/MageMojo/Cron/Model/Schedule.php b/app/code/MageMojo/Cron/Model/Schedule.php index 256b815..d371116 100644 --- a/app/code/MageMojo/Cron/Model/Schedule.php +++ b/app/code/MageMojo/Cron/Model/Schedule.php @@ -9,13 +9,16 @@ class Schedule extends \Magento\Framework\Model\AbstractModel private $directorylist; private $cronschedule; private $resource; + private $maintenance; public function __construct(\Magento\Cron\Model\Config $cronconfig, \Magento\Framework\App\Filesystem\DirectoryList $directorylist, - \MageMojo\Cron\Model\ResourceModel\Schedule $resource) { + \MageMojo\Cron\Model\ResourceModel\Schedule $resource, + \Magento\Framework\App\MaintenanceMode $maintenance) { $this->cronconfig = $cronconfig; $this->directorylist = $directorylist; $this->resource = $resource; + $this->maintenance = $maintenance; } @@ -81,7 +84,7 @@ public function checkProcess($pid) { } public function getJobOutput($scheduleid) { - $file = $this->basedir.'/var/cron/schedule'.$scheduleid; + $file = $this->basedir.'/var/cron/schedule.'.$scheduleid; if (file_exists($file)){ return trim(file_get_contents($file)); } @@ -183,9 +186,21 @@ public function prepareStub($jobconfig, $stub) { } public function canRunJobs($jobcount, $pending) { + $cpunum = exec('cat /proc/cpuinfo | grep processor | wc -l'); + if (!$cpunum) { + $cpunum = 1; + } + if ((sys_getloadavg()[0] / $cpunum) > $this->maxload) { + print "Crons suspended due to high load average: ".(sys_getloadavg()[0] / $cpunum)."\n"; + } + $maint = $this->maintenance->isOn(); + if ($maint) { + print "Crons suspended due to maintenance mode being enabled \n"; + } if (($jobcount < $this->simultaniousJobs) and (count($pending) > 0) - and (sys_getloadavg()[0] < $this->maxload)) { + and ((sys_getloadavg()[0] / $cpunum) < $this->maxload) + and (!$maint)) { return true; } return false; @@ -212,7 +227,7 @@ public function service() { foreach ($running as $pid=>$scheduleid) { if (!$this->checkProcess($pid)) { $output = $this->getJobOutput($scheduleid); - if (strpos(strtolower($output),'error') === true) { + if (strpos(strtolower($output),'error') > 0) { $this->resource->setJobStatus($scheduleid,'error',$output); } else { $this->resource->setJobStatus($scheduleid,'success',$output); @@ -226,13 +241,13 @@ public function service() { print "Getting pending jobs\n"; $pending = $this->resource->getPendingJobs(); while ($this->canRunJobs($jobcount, $pending)) { - print "In job run loop\n"; + #print "In job run loop\n"; $job = array_pop($pending); $runcheck = $this->resource->getJobByStatus($job["job_code"],'running'); if (count($runcheck) == 0) { $jobconfig = $this->getJobConfig($job["job_code"]); $runtime = $this->prepareStub($jobconfig,$stub); - $cmd = $this->phpproc." -r '".$runtime."' & > ".$this->basedir."/var/cron/schedule.".$job["schedule_id"]." 2>&1 & echo $!;"; + $cmd = $this->phpproc." -r '".$runtime."' &> ".$this->basedir."/var/cron/schedule.".$job["schedule_id"]." & echo $!"; $pid = exec($cmd); $this->setPid('cron.'.$pid,$job["schedule_id"]); $this->resource->setJobStatus($job["schedule_id"],'running',NULL); diff --git a/app/code/MageMojo/Cron/Setup/InstallData.php b/app/code/MageMojo/Cron/Setup/InstallData.php index 2e3c47c..21f0531 100644 --- a/app/code/MageMojo/Cron/Setup/InstallData.php +++ b/app/code/MageMojo/Cron/Setup/InstallData.php @@ -38,10 +38,10 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface if (count($result) == 0) { $insertData = array(); array_push($insertData,array('scope' => 'default', 'scope_id' => 0, 'path' => 'magemojo/cron/enabled', 'value' => '1')); - array_push($insertData,array('scope' => 'default', 'scope_id' => 0, 'path' => 'magemojo/cron/jobs', 'value' => '5')); + array_push($insertData,array('scope' => 'default', 'scope_id' => 0, 'path' => 'magemojo/cron/jobs', 'value' => '3')); array_push($insertData,array('scope' => 'default', 'scope_id' => 0, 'path' => 'magemojo/cron/phpproc', 'value' => 'php')); array_push($insertData,array('scope' => 'default', 'scope_id' => 0, 'path' => 'magemojo/cron/history', 'value' => '1')); - array_push($insertData,array('scope' => 'default', 'scope_id' => 0, 'path' => 'magemojo/cron/maxload', 'value' => '20')); + array_push($insertData,array('scope' => 'default', 'scope_id' => 0, 'path' => 'magemojo/cron/maxload', 'value' => '.75')); $connection->insertMultiple($setup->getTable('core_config_data'), $insertData); } diff --git a/app/code/MageMojo/Cron/view/adminhtml/templates/settings.phtml b/app/code/MageMojo/Cron/view/adminhtml/templates/settings.phtml index 6aa46d1..3d1ca07 100644 --- a/app/code/MageMojo/Cron/view/adminhtml/templates/settings.phtml +++ b/app/code/MageMojo/Cron/view/adminhtml/templates/settings.phtml @@ -10,24 +10,39 @@
- - + + + + + - - + + + + + - - + + + + + - - + + + + + - - + + + + +
Cron Enabledcheckbox('magemojo/cron/enabled','enabled'); ?>Cron Enabled:checkbox('magemojo/cron/enabled','enabled'); ?>
Turn the cron on/off.
Maximun Cron Processestextfield('magemojo/cron/jobs','maxjobs'); ?>Maximum Cron Processes:textfield('magemojo/cron/jobs','maxjobs',2,2); ?>
The number of cron threads running in parallel. This option is the sum of all defined jobs. Example: If you have 5 jobs set to run at midnight, Maximum Cron Processes set to 1, only 1 job will execute sequentially until all 5 are completed. Default 3.
PHP Processtextfield('magemojo/cron/phpproc','phpproc'); ?>PHP Binary Naqme / Path:textfield('magemojo/cron/phpproc','phpproc',10,100); ?>
The name of your php binary you run from the shell. Usually php or php70. You can optionally include the full path to the binary. Default php.
Max Load Averagetextfield('magemojo/cron/maxload','maxload'); ?>Max Load Average:textfield('magemojo/cron/maxload','maxload',2,2); ?>
Defined by the php function sys.getloadavg() / number of cpu cores. The function sys.getloadavg() is reported 1.0 for each core in use, just like the load average reported in top. The number of cpu cores is pulled from /proc/cpuinfo and load average is divided by this number. If load average exceeds this amount cron processes will be temporarily suspended until load decreases.
History Retentiontextfield('magemojo/cron/history','history'); ?>History Retention:textfield('magemojo/cron/history','history',4,4); ?>
The number of days history to keep in the cron_schedule table. Default 1 (1 day).