Skip to content

Commit

Permalink
multiple buxfixs and enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
gnuzealot committed Mar 16, 2018
1 parent 8d40ca5 commit bd433ae
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 33 deletions.
4 changes: 2 additions & 2 deletions app/code/MageMojo/Cron/Block/Adminhtml/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<input type="text" name="'.$name.'" value="'.$value.'">';
print '<input type="text" name="'.$name.'" size="'.$size.'" maxchar="'.$max.'" value="'.$value.'">';
}

}
45 changes: 36 additions & 9 deletions app/code/MageMojo/Cron/Controller/Adminhtml/Settings/Index.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<?php

namespace MageMojo\Cron\Controller\Adminhtml\Settings;
use Magento\Backend\App\Action;
use Magento\Backend\App\Action;

class Index extends \Magento\Backend\App\Action
{
/** @var \Magento\Framework\View\Result\PageFactory */
protected $resultPageFactory;
protected $resource;
protected $messageManager;
public function __construct(
\Magento\Backend\App\Action\Context $context,
\Magento\Framework\View\Result\PageFactory $resultPageFactory,
\MageMojo\Cron\Model\ResourceModel\Schedule $resource
\MageMojo\Cron\Model\ResourceModel\Schedule $resource,
\Magento\Framework\Message\ManagerInterface $messageManager
) {
$this->resultPageFactory = $resultPageFactory;
$this->resource = $resource;
$this->messageManager = $messageManager;
parent::__construct($context);
}
/**
Expand All @@ -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();

}
}
8 changes: 4 additions & 4 deletions app/code/MageMojo/Cron/Model/ResourceModel/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -33,7 +33,7 @@ public function getLastJobTime() {
$result = $connection->fetchOne($select);
return $result;
}

public function saveSchedule($job, $created, $schedule) {
$connection = $this->getConnection();
$insertdata = array();
Expand Down Expand Up @@ -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';
Expand All @@ -109,7 +109,7 @@ public function cleanSchedule($days) {
}
return $ids;
}

public function getSettings() {
$connection = $this->getConnection();

Expand Down
27 changes: 21 additions & 6 deletions app/code/MageMojo/Cron/Model/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions app/code/MageMojo/Cron/Setup/InstallData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
35 changes: 25 additions & 10 deletions app/code/MageMojo/Cron/view/adminhtml/templates/settings.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,39 @@
<div class="section-config">
<table cellspacing="0" class="form-list"><colgroup class="label" /><colgroup class="value" /><colgroup class="scope-label" /><colgroup class="" /><tbody>
<tr id="row_general_country_default">
<td class="label"><span data-config-scope="[STORE VIEW]">Cron Enabled</span></label></td>
<td class="value"><?php $block->checkbox('magemojo/cron/enabled','enabled'); ?></td>
<td class="label" width="250"><span data-config-scope="[STORE VIEW]"><b>Cron Enabled:</b></span></label></td>
<td class="value" align="left"><?php $block->checkbox('magemojo/cron/enabled','enabled'); ?></td>
</tr>
<tr>
<td colspan="2" style="padding-bottom: 10px;">Turn the cron on/off.</td>
</tr>
<tr id="row_general_country_default">
<td class="label"><span data-config-scope="[STORE VIEW]">Maximun Cron Processes</span></label></td>
<td class="value"><?php $block->textfield('magemojo/cron/jobs','maxjobs'); ?></td>
<td class="label" width="250"><span data-config-scope="[STORE VIEW]"><b>Maximum Cron Processes:</b></span></label></td>
<td class="value" align="left"><?php $block->textfield('magemojo/cron/jobs','maxjobs',2,2); ?></td>
</tr>
<tr>
<td colspan="2" style="padding-bottom: 10px;">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.</td>
</tr>
<tr id="row_general_country_default">
<td class="label"><span data-config-scope="[STORE VIEW]">PHP Process</span></label></td>
<td class="value"><?php $block->textfield('magemojo/cron/phpproc','phpproc'); ?></td>
<td class="label" width="250"><span data-config-scope="[STORE VIEW]"><b>PHP Binary Naqme / Path:</b></span></label></td>
<td class="value" align="left"><?php $block->textfield('magemojo/cron/phpproc','phpproc',10,100); ?></td>
</tr>
<tr>
<td colspan="2" style="padding-bottom: 10px;">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.</td>
</tr>
<tr id="row_general_country_default">
<td class="label"><span data-config-scope="[STORE VIEW]">Max Load Average</span></label></td>
<td class="value"><?php $block->textfield('magemojo/cron/maxload','maxload'); ?></td>
<td class="label" width="250"><span data-config-scope="[STORE VIEW]"><b>Max Load Average:</b></span></label></td>
<td class="value" align="left"><?php $block->textfield('magemojo/cron/maxload','maxload',2,2); ?></td>
</tr>
<tr>
<td colspan="2" style="padding-bottom: 10px;">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.</td>
</tr>
<tr id="row_general_country_default">
<td class="label"><span data-config-scope="[STORE VIEW]">History Retention</span></label></td>
<td class="value"><?php $block->textfield('magemojo/cron/history','history'); ?></td>
<td class="label" width="250"><span data-config-scope="[STORE VIEW]"><b>History Retention:</b></span></label></td>
<td class="value" align="left"><?php $block->textfield('magemojo/cron/history','history',4,4); ?></td>
</tr>
<tr>
<td colspan="2" style="padding-bottom: 10px;">The number of days history to keep in the cron_schedule table. Default 1 (1 day).</td>
</tr>
</table>
</div>
Expand Down

0 comments on commit bd433ae

Please sign in to comment.