/**
  * @param $cronJob
  *
  * @return bool
  */
 public function getDateLastCronRun($cronJob)
 {
     $collection = $this->schelduleFactory->create()->getCollection()->addFieldToFilter('status', \Magento\Cron\Model\Schedule::STATUS_SUCCESS)->addFieldToFilter('job_code', $cronJob);
     //limit and order the results
     $collection->getSelect()->limit(1)->order('executed_at DESC');
     if ($collection->getSize() == 0) {
         return false;
     }
     //@codingStandardsIgnoreStart
     $executedAt = $collection->getFirstItem()->getExecutedAt();
     //@codingStandardsIgnoreEnd
     return $executedAt;
 }
 /**
  * @return \Magento\Backend\Model\View\Result\Redirect
  */
 public function execute()
 {
     $ids = $this->getRequest()->getParam('selected');
     if (!is_array($ids)) {
         $this->messageManager->addErrorMessage(__('Please select cron.'));
     } else {
         try {
             //@codingStandardsIgnoreStart
             foreach ($ids as $id) {
                 $model = $this->scheduleFactory->create()->setId($id);
                 $model->delete();
             }
             //@codingStandardsIgnoreEnd
             $this->messageManager->addSuccessMessage(__('Total of %1 record(s) were deleted.', count($ids)));
         } catch (\Exception $e) {
             $this->messageManager->addErrorMessage($e->getMessage());
         }
     }
     /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
     $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
     $resultRedirect->setPath('*/*/');
     return $resultRedirect;
 }
 public function testMissedJobsCleanedInTime()
 {
     /* 1. Initialize dependencies of _generate() method which is called first */
     $jobConfig = ['test_group' => ['test_job1' => ['instance' => 'CronJob', 'method' => 'execute']]];
     // This item was scheduled 2 days ago
     /** @var \Magento\Cron\Model\Schedule|\PHPUnit_Framework_MockObject_MockObject $schedule1 */
     $schedule1 = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->setMethods(['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup'])->getMock();
     $schedule1->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
     $schedule1->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-2 day -2 hour'));
     $schedule1->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED));
     //we expect this job be deleted from the list
     $schedule1->expects($this->once())->method('delete')->will($this->returnValue(true));
     // This item was scheduled 1 day ago
     $schedule2 = $this->getMockBuilder('Magento\\Cron\\Model\\Schedule')->disableOriginalConstructor()->setMethods(['getExecutedAt', 'getScheduledAt', 'getStatus', 'delete', '__wakeup'])->getMock();
     $schedule2->expects($this->any())->method('getExecutedAt')->will($this->returnValue(null));
     $schedule2->expects($this->any())->method('getScheduledAt')->will($this->returnValue('-1 day'));
     $schedule2->expects($this->any())->method('getStatus')->will($this->returnValue(Schedule::STATUS_MISSED));
     //we don't expect this job be deleted from the list
     $schedule2->expects($this->never())->method('delete');
     $this->_collection->addItem($schedule1);
     $this->_config->expects($this->once())->method('getJobs')->will($this->returnValue($jobConfig));
     //get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_generate()"
     $this->_cache->expects($this->at(0))->method('load')->will($this->returnValue(time() + 10000000));
     //get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_cleanup()"
     $this->_cache->expects($this->at(1))->method('load')->will($this->returnValue(time() - 10000000));
     $this->_scopeConfig->expects($this->at(0))->method('getValue')->with($this->equalTo('system/cron/test_group/use_separate_process'))->will($this->returnValue(0));
     $this->_scopeConfig->expects($this->at(1))->method('getValue')->with($this->equalTo('system/cron/test_group/schedule_generate_every'))->will($this->returnValue(0));
     $this->_scopeConfig->expects($this->at(2))->method('getValue')->with($this->equalTo('system/cron/test_group/history_cleanup_every'))->will($this->returnValue(0));
     $this->_scopeConfig->expects($this->at(3))->method('getValue')->with($this->equalTo('system/cron/test_group/schedule_lifetime'))->will($this->returnValue(2 * 24 * 60));
     $this->_scopeConfig->expects($this->at(4))->method('getValue')->with($this->equalTo('system/cron/test_group/history_success_lifetime'))->will($this->returnValue(0));
     $this->_scopeConfig->expects($this->at(5))->method('getValue')->with($this->equalTo('system/cron/test_group/history_failure_lifetime'))->will($this->returnValue(0));
     /* 2. Initialize dependencies of _cleanup() method which is called second */
     $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($schedule1);
     $collection->addItem($schedule2);
     $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);
 }
 /**
  * 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->_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\\Resource\\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->dispatch('');
 }
Exemple #5
0
 /**
  * @param string $jobCode
  * @param string $cronExpression
  * @param int $time
  * @return Schedule
  */
 protected function generateSchedule($jobCode, $cronExpression, $time)
 {
     $schedule = $this->_scheduleFactory->create()->setCronExpr($cronExpression)->setJobCode($jobCode)->setStatus(Schedule::STATUS_PENDING)->setCreatedAt(strftime('%Y-%m-%d %H:%M:%S', $this->timezone->scopeTimeStamp()))->setScheduledAt(strftime('%Y-%m-%d %H:%M', $time));
     return $schedule;
 }