/** * @param ManagerInterface $eventManager * @param State $state * @param Console\Request $request * @param Console\Response $response * @param array $parameters */ public function __construct(ManagerInterface $eventManager, State $state, Console\Request $request, Console\Response $response, array $parameters = []) { $this->_eventManager = $eventManager; $this->_state = $state; $this->_request = $request; $this->_request->setParams($parameters); $this->_response = $response; }
/** * Process cron queue * Generate tasks schedule * Cleanup tasks schedule * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function execute(\Magento\Framework\Event\Observer $observer) { $pendingJobs = $this->_getPendingSchedules(); $currentTime = $this->timezone->scopeTimeStamp(); $jobGroupsRoot = $this->_config->getJobs(); foreach ($jobGroupsRoot as $groupId => $jobsRoot) { if ($this->_request->getParam('group') !== null && $this->_request->getParam('group') !== '\'' . $groupId . '\'' && $this->_request->getParam('group') !== $groupId) { continue; } if ($this->_request->getParam(self::STANDALONE_PROCESS_STARTED) !== '1' && $this->_scopeConfig->getValue('system/cron/' . $groupId . '/use_separate_process', \Magento\Store\Model\ScopeInterface::SCOPE_STORE) == 1) { $this->_shell->execute('php %s cron:run --group=' . $groupId . ' --' . CLI::INPUT_KEY_BOOTSTRAP . '=' . self::STANDALONE_PROCESS_STARTED . '=1', [BP . '/bin/magento']); continue; } foreach ($pendingJobs as $schedule) { $jobConfig = isset($jobsRoot[$schedule->getJobCode()]) ? $jobsRoot[$schedule->getJobCode()] : null; if (!$jobConfig) { continue; } $scheduledTime = strtotime($schedule->getScheduledAt()); if ($scheduledTime > $currentTime) { continue; } try { if ($schedule->tryLockJob()) { $this->_runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId); } } catch (\Exception $e) { $schedule->setMessages($e->getMessage()); } $schedule->save(); } $this->_generate($groupId); $this->_cleanup($groupId); } }
/** * Process cron queue * Generate tasks schedule * Cleanup tasks schedule * * @param \Magento\Framework\Event\Observer $observer * @return void */ public function dispatch($observer) { $pendingJobs = $this->_getPendingSchedules(); $currentTime = time(); $jobGroupsRoot = $this->_config->getJobs(); foreach ($jobGroupsRoot as $groupId => $jobsRoot) { if ($this->_request->getParam('group') === null && $this->_scopeConfig->getValue('system/cron/' . $groupId . '/use_separate_process', \Magento\Store\Model\ScopeInterface::SCOPE_STORE) == 1) { $this->_shell->execute('%s -f %s -- --group=%s', array(PHP_BINARY, BP . '/' . \Magento\Framework\App\Filesystem::PUB_DIR . '/cron.php', $groupId)); continue; } if ($this->_request->getParam('group') !== null && $this->_request->getParam('group') != $groupId) { continue; } foreach ($pendingJobs as $schedule) { $jobConfig = isset($jobsRoot[$schedule->getJobCode()]) ? $jobsRoot[$schedule->getJobCode()] : null; if (!$jobConfig) { continue; } $scheduledTime = strtotime($schedule->getScheduledAt()); if ($scheduledTime > $currentTime || !$schedule->tryLockJob()) { continue; } try { $this->_runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId); } catch (\Exception $e) { $schedule->setMessages($e->getMessage()); } $schedule->save(); } $this->_generate($groupId); $this->_cleanup($groupId); } }
/** * Process cron queue * Generate tasks schedule * Cleanup tasks schedule * * @param \Magento\Framework\Event\Observer $observer * @return void * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function dispatch($observer) { $pendingJobs = $this->_getPendingSchedules(); $currentTime = $this->timezone->scopeTimeStamp(); $jobGroupsRoot = $this->_config->getJobs(); foreach ($jobGroupsRoot as $groupId => $jobsRoot) { if ($this->_request->getParam('group') !== null && $this->_request->getParam('group') != $groupId) { continue; } if ($this->_request->getParam('standaloneProcessStarted') !== '1' && $this->_scopeConfig->getValue('system/cron/' . $groupId . '/use_separate_process', \Magento\Store\Model\ScopeInterface::SCOPE_STORE) == 1) { $this->_shell->execute('php -f %s -- --group=%s --standaloneProcessStarted=%s', [BP . '/' . DirectoryList::PUB . '/cron.php', $groupId, '1']); continue; } foreach ($pendingJobs as $schedule) { $jobConfig = isset($jobsRoot[$schedule->getJobCode()]) ? $jobsRoot[$schedule->getJobCode()] : null; if (!$jobConfig) { continue; } $scheduledTime = strtotime($schedule->getScheduledAt()); if ($scheduledTime > $currentTime) { continue; } try { if ($schedule->tryLockJob()) { $this->_runJob($scheduledTime, $currentTime, $jobConfig, $schedule, $groupId); } } catch (\Exception $e) { $schedule->setMessages($e->getMessage()); } $schedule->save(); } $this->_generate($groupId); $this->_cleanup($groupId); } }
/** * Test case without saved cron jobs in data base */ public function testDispatchCleanup() { $jobConfig = ['test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']]]; $schedule = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->setMethods(['getExecutedAt', 'getStatus', 'delete', '__wakeup'])->getMock(); $schedule->expects($this->any())->method('getExecutedAt')->will($this->returnValue('-1 day')); $schedule->expects($this->any())->method('getStatus')->will($this->returnValue('success')); $this->_request->expects($this->any())->method('getParam')->will($this->returnValue('test_group')); $this->_collection->addItem($schedule); $this->_config->expects($this->once())->method('getJobs')->will($this->returnValue($jobConfig)); $this->_cache->expects($this->at(0))->method('load')->will($this->returnValue(time() + 10000000)); $this->_cache->expects($this->at(1))->method('load')->will($this->returnValue(time() - 10000000)); $this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue(0)); $scheduleMock = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->getMock(); $scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($this->_collection)); $this->_scheduleFactory->expects($this->at(0))->method('create')->will($this->returnValue($scheduleMock)); $collection = $this->getMockBuilder('Magento\\Cron\\Model\\ResourceModel\\Schedule\\Collection')->setMethods(['addFieldToFilter', 'load', '__wakeup'])->disableOriginalConstructor()->getMock(); $collection->expects($this->any())->method('addFieldToFilter')->will($this->returnSelf()); $collection->expects($this->any())->method('load')->will($this->returnSelf()); $collection->addItem($schedule); $scheduleMock = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->getMock(); $scheduleMock->expects($this->any())->method('getCollection')->will($this->returnValue($collection)); $this->_scheduleFactory->expects($this->at(1))->method('create')->will($this->returnValue($scheduleMock)); $this->_observer->execute($this->observer); }