Skip to content

Commit

Permalink
Merge pull request #1 from mdhia/master
Browse files Browse the repository at this point in the history
Init Module
  • Loading branch information
sydekumf authored May 18, 2018
2 parents 6f89892 + f5ca02c commit a7b553c
Show file tree
Hide file tree
Showing 9 changed files with 342 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Build
/build
/build.properties
/instance-src
/target
/output

# Build - SCSS
.sass-cache
.sass-build
.tmp

# NPM / Bower
node_modules
bower_components

# Composer
/vendor

# Eclipse IDE
/.buildpath
/.project
/.settings

# PhpStorm IDE
/.idea

# Hidden files
.DS_Store
._.DS_Store
92 changes: 92 additions & 0 deletions Console/Command/CronProcessCheckCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Magenerds\CronProcessCheck\Console\Command\CronProcessCheckCommand
*
* Copyright (c) 2018 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* [email protected]
*/

namespace Magenerds\CronProcessCheck\Console\Command;

use Magento\Cron\Model\Schedule;
use Magento\Cron\Model\ScheduleFactory;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* @category Magenerds
* @package Magenerds_CronProcessCheck
* @subpackage Console
* @copyright Copyright (c) 2018 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Mahmood Dhia <[email protected]>
*/
class CronProcessCheckCommand extends Command
{
/**
* @var ScheduleFactory
*/
private $scheduleFactory;

/**
* class constructor
*
* @param ScheduleFactory $scheduleFactory
*/
public function __construct (
ScheduleFactory $scheduleFactory
){
$this->scheduleFactory = $scheduleFactory;
parent::__construct();
}

/**
* Configures the current command.
*/
public function configure()
{
$this->setName('magenerds:cronprocess:check');
$this->setDescription('Check running cronjobs');

parent::configure();
}

/**
* Check running cronjobs
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*
* @return null|int null or 0 if everything went fine, or an error code
* @throws \Exception
*/
public function execute(InputInterface $input, OutputInterface $output)
{
$cronList = $this->scheduleFactory->create()->getCollection()
->addFieldToFilter( 'status', Schedule::STATUS_RUNNING)
->addFieldToFilter( 'pid', ['neq' => 'NULL'])
->load();

$cleanCount = 0;

/** @var Schedule $cron */
foreach ($cronList as $cron) {
if(posix_getpgid($cron->getPid()) === false) {
$cleanCount++;
$cron->setMessages(sprintf('Magenerds_CronProcessCheck: Process (PID: %s) is gone', $cron->getPid()));
$cron->setStatus(Schedule::STATUS_ERROR);
$cron->save();
}
}

$output->writeln(sprintf('Cleaned %s cronjobs', $cleanCount));
return true;
}
}
59 changes: 59 additions & 0 deletions Plugin/Model/CronSchedulePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* Magenerds\CronProcessCheck\Plugin\Model\CronSchedulePlugin
*
* Copyright (c) 2018 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* [email protected]
*/

namespace Magenerds\CronProcessCheck\Plugin\Model;

use Magento\Cron\Model\Schedule;

/**
* @category Magenerds
* @package Magenerds_CronProcessCheck
* @subpackage Model
* @copyright Copyright (c) 2018 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Mahmood Dhia <[email protected]>
*/
class CronSchedulePlugin
{
/**
* Set the job PID if can lock.
*
* @param Schedule $subject
* @param bool $canLock
* @return bool
*/
public function afterTryLockJob(Schedule $subject, $canLock)
{
if($canLock) {
$subject->setPid(getmypid());
}

return $canLock;
}

/**
* Remove PID if status is not running
*
* @param Schedule $subject
* @param $key
* @param $value
* @return void
*/
public function beforeSetData(Schedule $subject, $key, $value = null)
{
if($key === 'status' && $value !== Schedule::STATUS_RUNNING) {
$subject->setPid(NULL);
}
}
}
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
# CronProcessCheck
Cron Process check for Magento 2. This modules checks if a cron job process crashed and sets its status to error.

### Setup
1. Install `Magenerds/CronProcessCheck` Module
2. Add System cronjob `* * * * * /var/www/magento/bin/magento magenerds:cronprocess:check > /dev/null 2>&1g`
75 changes: 75 additions & 0 deletions Setup/UpgradeSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Magenerds\CronProcessCheck\Setup
*
* Copyright (c) 2018 TechDivision GmbH
* All rights reserved
*
* This product includes proprietary software developed at TechDivision GmbH, Germany
* For more information see http://www.techdivision.com/
*
* To obtain a valid license for using this software please contact us at
* [email protected]
*/

namespace Magenerds\CronProcessCheck\Setup;

use Magento\Framework\DB\Ddl\Table;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;

/**
* @category Magenerds
* @package Magenerds_CronProcessCheck
* @subpackage Setup
* @copyright Copyright (c) 2018 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
* @author Mahmood Dhia <[email protected]>
*/
class UpgradeSchema implements UpgradeSchemaInterface
{
/**
* Upgrades DB schema
*
* @param SchemaSetupInterface $setup
* @param ModuleContextInterface $context
* @return void
*/
public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();

if (version_compare($context->getVersion(), '1.0.0') < 0) {
$this->alterCronScheduleTable($setup);
}

$setup->endSetup();
}

/**
* Alter cron_schedule table
*
* @param SchemaSetupInterface $setup
* @return void
*/
public function alterCronScheduleTable($setup)
{
$cronSchedule = $setup->getTable('cron_schedule');

$columns = [
'pid' => [
'type' => Table::TYPE_INTEGER,
'nullable' => true,
'unsigned' => true,
'default' => NULL,
'comment' => 'Contains the process id of running php instance',
]
];

$connection = $setup->getConnection();
foreach ($columns as $name => $definition) {
$connection->addColumn($cronSchedule, $name, $definition);
}
}
}
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "magenerds/cronprocesscheck",
"description": "Cron Process check for Magento 2. This modules checks if a cron job process crashed and sets its status to error.",
"require": {
"magento/framework": "^100.0.0|^101.0.0",
"magenerds/dashboard": "^1.0"
},
"repositories": {
"magento": {
"type": "composer",
"url": "https://repo.magento.com/"
}
},
"type": "magento2-module",
"license": "OSL-3.0",
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Magenerds\\CronProcessCheck\\": ""
}
}
}
23 changes: 23 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0"?>
<!--
/**
* @category Magenerds
* @package Magenerds_CronProcessCheck
* @author Mahmood Dhia <[email protected]>
* @copyright Copyright (c) 2018 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<type name="Magento\Framework\Console\CommandListInterface">
<arguments>
<argument name="commands" xsi:type="array">
<item name="cronProcessCheck" xsi:type="object">Magenerds\CronProcessCheck\Console\Command\CronProcessCheckCommand</item>
</argument>
</arguments>
</type>

<type name="Magento\Cron\Model\Schedule">
<plugin name="cronModelSchedulePlugin" type="Magenerds\CronProcessCheck\Plugin\Model\CronSchedulePlugin" sortOrder="10"/>
</type>
</config>
18 changes: 18 additions & 0 deletions etc/module.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<!--
/**
* @category Magenerds
* @package Magenerds_CronProcessCheck
* @author Mahmood Dhia <[email protected]>
* @copyright Copyright (c) 2018 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
*/
-->

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Magenerds_CronProcessCheck" setup_version="1.0.0">
<sequence>
<module name="Magento_Cron"/>
</sequence>
</module>
</config>
16 changes: 16 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* @category Magenerds
* @package Magenerds_CronProcessCheck
* @subpackage etc
* @author Mahmood Dhia <[email protected]>
* @copyright Copyright (c) 2018 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
*/
use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
ComponentRegistrar::MODULE,
'Magenerds_CronProcessCheck',
__DIR__
);

0 comments on commit a7b553c

Please sign in to comment.