Skip to content

Commit

Permalink
Merge pull request #2 from mdhia/master
Browse files Browse the repository at this point in the history
Add fallback for POSIX
  • Loading branch information
sydekumf authored May 23, 2018
2 parents a7b553c + 3a9a56e commit 8779850
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
13 changes: 8 additions & 5 deletions Console/Command/CronProcessCheckCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ class CronProcessCheckCommand extends Command
*
* @param ScheduleFactory $scheduleFactory
*/
public function __construct (
public function __construct(
ScheduleFactory $scheduleFactory
){
)
{
$this->scheduleFactory = $scheduleFactory;
parent::__construct();
}
Expand Down Expand Up @@ -70,15 +71,17 @@ public function configure()
public function execute(InputInterface $input, OutputInterface $output)
{
$cronList = $this->scheduleFactory->create()->getCollection()
->addFieldToFilter( 'status', Schedule::STATUS_RUNNING)
->addFieldToFilter( 'pid', ['neq' => 'NULL'])
->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) {
if ((function_exists('posix_getpgid') && posix_getpgid($cron->getPid()) === false) // try to use POSIX (Portable Operating System Interface)
|| file_exists('/proc/' . $cron->getPid()) === false // check process pid in /proc/ folder
) {
$cleanCount++;
$cron->setMessages(sprintf('Magenerds_CronProcessCheck: Process (PID: %s) is gone', $cron->getPid()));
$cron->setStatus(Schedule::STATUS_ERROR);
Expand Down
4 changes: 2 additions & 2 deletions Plugin/Model/CronSchedulePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class CronSchedulePlugin
*/
public function afterTryLockJob(Schedule $subject, $canLock)
{
if($canLock) {
if ($canLock) {
$subject->setPid(getmypid());
}

Expand All @@ -52,7 +52,7 @@ public function afterTryLockJob(Schedule $subject, $canLock)
*/
public function beforeSetData(Schedule $subject, $key, $value = null)
{
if($key === 'status' && $value !== Schedule::STATUS_RUNNING) {
if ($key === 'status' && $value !== Schedule::STATUS_RUNNING) {
$subject->setPid(NULL);
}
}
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
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`
1. Activate POSIX module in your php.ini
2. Install `Magenerds/CronProcessCheck` Module
3. Add System cronjob `* * * * * /var/www/magento/bin/magento magenerds:cronprocess:check > /dev/null 2>&1g`
1 change: 1 addition & 0 deletions registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @copyright Copyright (c) 2018 TechDivision GmbH (http://www.techdivision.com)
* @link http://www.techdivision.com/
*/

use Magento\Framework\Component\ComponentRegistrar;

ComponentRegistrar::register(
Expand Down

0 comments on commit 8779850

Please sign in to comment.