From b0baab6374bcce6f7eeb70bfbd80cad9570a95d4 Mon Sep 17 00:00:00 2001 From: Mahmood Dhia Date: Wed, 23 May 2018 08:51:01 +0200 Subject: [PATCH 1/2] Add fallback for POSIX --- Console/Command/CronProcessCheckCommand.php | 5 ++++- README.md | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Console/Command/CronProcessCheckCommand.php b/Console/Command/CronProcessCheckCommand.php index 6985c3a..c5ce6e7 100644 --- a/Console/Command/CronProcessCheckCommand.php +++ b/Console/Command/CronProcessCheckCommand.php @@ -78,7 +78,10 @@ public function execute(InputInterface $input, OutputInterface $output) /** @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); diff --git a/README.md b/README.md index d82b30b..1244eab 100644 --- a/README.md +++ b/README.md @@ -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` From 3a9a56e4cc1b551e1f04dc03e2b7e18d5e10fe67 Mon Sep 17 00:00:00 2001 From: Mahmood Dhia Date: Wed, 23 May 2018 09:15:54 +0200 Subject: [PATCH 2/2] Fix code style --- Console/Command/CronProcessCheckCommand.php | 16 ++++++++-------- Plugin/Model/CronSchedulePlugin.php | 4 ++-- registration.php | 1 + 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Console/Command/CronProcessCheckCommand.php b/Console/Command/CronProcessCheckCommand.php index c5ce6e7..686c488 100644 --- a/Console/Command/CronProcessCheckCommand.php +++ b/Console/Command/CronProcessCheckCommand.php @@ -40,9 +40,10 @@ class CronProcessCheckCommand extends Command * * @param ScheduleFactory $scheduleFactory */ - public function __construct ( + public function __construct( ScheduleFactory $scheduleFactory - ){ + ) + { $this->scheduleFactory = $scheduleFactory; parent::__construct(); } @@ -70,18 +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( - (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 - { + 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); diff --git a/Plugin/Model/CronSchedulePlugin.php b/Plugin/Model/CronSchedulePlugin.php index 2181afb..fac941e 100644 --- a/Plugin/Model/CronSchedulePlugin.php +++ b/Plugin/Model/CronSchedulePlugin.php @@ -35,7 +35,7 @@ class CronSchedulePlugin */ public function afterTryLockJob(Schedule $subject, $canLock) { - if($canLock) { + if ($canLock) { $subject->setPid(getmypid()); } @@ -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); } } diff --git a/registration.php b/registration.php index a694587..d3bdca9 100644 --- a/registration.php +++ b/registration.php @@ -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(