public function testRunAndProcessStuckJobs() { Yii::app()->user->userModel = User::getByUsername('super'); $emailAddress = new Email(); $emailAddress->emailAddress = '*****@*****.**'; Yii::app()->user->userModel->primaryEmail = $emailAddress; $saved = Yii::app()->user->userModel->save(); $this->assertTrue($saved); $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount()); $this->assertEquals(0, Yii::app()->emailHelper->getSentCount()); $monitorJob = new MonitorJob(); $this->assertEquals(0, count(JobInProcess::getAll())); $this->assertEquals(0, count(Notification::getAll())); $jobInProcess = new JobInProcess(); $jobInProcess->type = 'Test'; $this->assertTrue($jobInProcess->save()); //Should make createdDateTime long enough in past to trigger as stuck. $createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 1000); $sql = "Update item set createddatetime = '" . $createdDateTime . "' where id = " . $jobInProcess->getClassId('Item'); R::exec($sql); $jobInProcess->forget(); $monitorJob->run(); $this->assertEquals(1, count(Notification::getAll())); //Confirm an email was sent $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount()); $this->assertEquals(1, Yii::app()->emailHelper->getSentCount()); }
public function testSuperUserResetStuckJobInProcess() { $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super'); //Test when the job is not stuck $this->setGetArray(array('type' => 'Monitor')); $content = $this->runControllerWithNoExceptionsAndGetContent('jobsManager/default/resetJob'); $this->assertTrue(strpos($content, 'The job Monitor Job was not found to be stuck and therefore was not reset.') !== false); //Test when the job is stuck (Just having a jobInProcess is enough to trigger it. $jobInProcess = new JobInProcess(); $jobInProcess->type = 'Monitor'; $this->assertTrue($jobInProcess->save()); $this->setGetArray(array('type' => 'Monitor')); $content = $this->runControllerWithNoExceptionsAndGetContent('jobsManager/default/resetJob'); $this->assertTrue(strpos($content, 'The job Monitor Job has been reset.') !== false); }
public function testJobInProcess() { Yii::app()->user->userModel = User::getByUsername('super'); $jobInProcess = new JobInProcess(); $jobInProcess->type = 'Monitor'; $this->assertTrue($jobInProcess->save()); $id = $jobInProcess->id; try { $jobInProcess = JobInProcess::getByType('SomethingElse'); $this->fail(); } catch (NotFoundException $e) { //nothing. passes. } $jobInProcess = JobInProcess::getByType('Monitor'); $this->assertEquals(1, count($jobInProcess)); $this->assertEquals($id, $jobInProcess->id); $jobInProcess->delete(); $this->assertEquals(0, count(JobInProcess::getAll())); }
public function testIsJobInProcessOverThreashold() { Yii::app()->user->userModel = User::getByUsername('super'); $jobInProcess = new JobInProcess(); $jobInProcess->type = 'Test'; $this->assertTrue($jobInProcess->save()); //Set the createdDateTime as way in the past, so that it is over the threshold $sql = "update " . Item::getTableName('Item') . " set createddatetime = '1980-06-03 18:33:03' where id = " . $jobInProcess->getClassId('Item'); R::exec($sql); $jobInProcessId = $jobInProcess->id; $jobInProcess->forget(); $jobInProcess = JobInProcess::getById($jobInProcessId); $this->assertTrue(JobsManagerUtil::isJobInProcessOverThreshold($jobInProcess, $jobInProcess->type)); $jobInProcess->delete(); //Test when a job is not over the threshold. $jobInProcess = new JobInProcess(); $jobInProcess->type = 'Test'; $this->assertTrue($jobInProcess->save()); $this->assertFalse(JobsManagerUtil::isJobInProcessOverThreshold($jobInProcess, $jobInProcess->type)); $jobInProcess->delete(); }
public function testRunAndProcessStuckJobs() { Yii::app()->user->userModel = User::getByUsername('super'); $emailAddress = new Email(); $emailAddress->emailAddress = '*****@*****.**'; Yii::app()->user->userModel->primaryEmail = $emailAddress; $saved = Yii::app()->user->userModel->save(); $this->assertTrue($saved); $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount()); $this->assertEquals(0, Yii::app()->emailHelper->getSentCount()); $monitorJob = new MonitorJob(); $this->assertEquals(0, JobInProcess::getCount()); $this->assertEquals(0, StuckJob::getCount()); $this->assertEquals(0, Notification::getCount()); $this->assertEquals(0, EmailMessage::getCount()); $jobInProcess = new JobInProcess(); $jobInProcess->type = 'Test'; $this->assertTrue($jobInProcess->save()); //Should make createdDateTime long enough in past to trigger as stuck. $createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 10000); $sql = "Update item set createddatetime = '" . $createdDateTime . "' where id = " . $jobInProcess->getClassId('Item'); ZurmoRedBean::exec($sql); $jobInProcess->forget(); $monitorJob->run(); $this->assertEquals(0, JobInProcess::getCount()); //should still be 0 but the quantity should increase $this->assertEquals(0, Notification::getCount()); //There should now be one stuck job with quantity 1 $stuckJobs = StuckJob::getAll(); $this->assertEquals(1, count($stuckJobs)); $this->assertEquals('Test', $stuckJobs[0]->type); $this->assertEquals(1, $stuckJobs[0]->quantity); //Now it should increase to 2 $jobInProcess = new JobInProcess(); $jobInProcess->type = 'Test'; $this->assertTrue($jobInProcess->save()); //Should make createdDateTime long enough in past to trigger as stuck. $createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 10000); $sql = "Update item set createddatetime = '" . $createdDateTime . "' where id = " . $jobInProcess->getClassId('Item'); ZurmoRedBean::exec($sql); $jobInProcess->forget(); $monitorJob->run(); $this->assertEquals(0, JobInProcess::getCount()); //should still be 0 but the quantity should increase $this->assertEquals(0, Notification::getCount()); //There should now be one stuck job with quantity 1 $stuckJobs = StuckJob::getAll(); $this->assertEquals(1, count($stuckJobs)); $this->assertEquals('Test', $stuckJobs[0]->type); $this->assertEquals(2, $stuckJobs[0]->quantity); //Set quantity to 3, then run monitor again and notification should go out. $stuckJobs[0]->quantity = 3; $this->assertTrue($stuckJobs[0]->save()); $jobInProcess = new JobInProcess(); $jobInProcess->type = 'Test'; $this->assertTrue($jobInProcess->save()); //Should make createdDateTime long enough in past to trigger as stuck. $createdDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time() - 10000); $sql = "Update item set createddatetime = '" . $createdDateTime . "' where id = " . $jobInProcess->getClassId('Item'); ZurmoRedBean::exec($sql); $jobInProcess->forget(); //Now the threshold of 4 should be reached and we should send a notification $monitorJob->run(); ForgetAllCacheUtil::forgetAllCaches(); $stuckJobs = StuckJob::getAll(); $this->assertEquals(1, count($stuckJobs)); $this->assertEquals('Test', $stuckJobs[0]->type); $this->assertEquals(4, $stuckJobs[0]->quantity); $this->assertEquals(1, Notification::getCount()); //Confirm an email was sent $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount()); $this->assertEquals(1, EmailMessage::getCount()); $this->assertEquals(1, Yii::app()->emailHelper->getSentCount()); }
/** * Given a 'type' of job, run the job. This is for non-monitor jobs only. * @param $type * @param MessageLogger $messageLogger * @param $isJobInProgress * @throws FailedToSaveModelException */ public static function runNonMonitorJob($type, MessageLogger $messageLogger, &$isJobInProgress) { assert('is_string($type) && $type != "Monitor"'); assert('is_bool($isJobInProgress)'); try { JobInProcess::getByType($type); $messageLogger->addInfoMessage("Existing job detected"); $isJobInProgress = true; } catch (NotFoundException $e) { $jobInProcess = new JobInProcess(); $jobInProcess->type = $type; $jobInProcess->save(); $startDateTime = $jobInProcess->createdDateTime; $jobClassName = $type . 'Job'; $job = new $jobClassName(); $job->setMessageLogger($messageLogger); $ranSuccessfully = $job->run(); $errorMessage = $job->getErrorMessage(); $jobInProcess->delete(); $jobLog = new JobLog(); $jobLog->type = $type; $jobLog->startDateTime = $startDateTime; $jobLog->endDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); if ($ranSuccessfully) { $messageLogger->addInfoMessage("Job completed successfully"); $jobLog->status = JobLog::STATUS_COMPLETE_WITHOUT_ERROR; } else { $messageLogger->addInfoMessage("Job completed with errors"); $jobLog->status = JobLog::STATUS_COMPLETE_WITH_ERROR; $jobLog->message = $errorMessage; } $jobLog->isProcessed = false; if (!$jobLog->save()) { throw new FailedToSaveModelException(); } $stuckJob = StuckJob::getByType($type); $stuckJob->quantity = 0; if (!$stuckJob->save()) { throw new FailedToSaveModelException(); } } }
public function testRun() { chdir(COMMON_ROOT . DIRECTORY_SEPARATOR . 'protected' . DIRECTORY_SEPARATOR . 'commands'); $command = "php zurmocTest.php resetStuckJobs super NonExistingJobClass"; if (!IS_WINNT) { $command .= ' 2>&1'; } exec($command, $output); //$this->assertEquals($output[1], 'Error! The NonExistingJobClassJob does not exist.'); $command = "php zurmocTest.php resetStuckJobs super JobLogCleanup"; if (!IS_WINNT) { $command .= ' 2>&1'; } unset($output); exec($command, $output); $this->assertEquals($output[1], 'The job JobLogCleanupJob was not found to be stuck and therefore was not reset.'); // Now to test case when job log exist in jonInProcess table, we need to insert it manually $jobInProcess = new JobInProcess(); $jobInProcess->type = 'JobLogCleanup'; $this->assertTrue($jobInProcess->save()); $command = "php zurmocTest.php resetStuckJobs super JobLogCleanup"; if (!IS_WINNT) { $command .= ' 2>&1'; } unset($output); exec($command, $output); $this->assertEquals($output[1], 'The job JobLogCleanupJob has been reset.'); $this->assertEmpty(JobInProcess::getAll()); // Test with no items in JobInProcess $command = "php zurmocTest.php resetStuckJobs super All"; if (!IS_WINNT) { $command .= ' 2>&1'; } unset($output); exec($command, $output); $this->assertEquals($output[1], 'Reset all jobs.'); $this->assertEquals($output[2], 'There are no jobs in process to be reset.'); // Now test with some items in JobInProcess table and 'All' parameter $jobInProcess = new JobInProcess(); $jobInProcess->type = 'JobLogCleanup'; $this->assertTrue($jobInProcess->save()); $jobInProcess2 = new JobInProcess(); $jobInProcess2->type = 'Monitor'; $this->assertTrue($jobInProcess2->save()); $command = "php zurmocTest.php resetStuckJobs super All"; if (!IS_WINNT) { $command .= ' 2>&1'; } unset($output); exec($command, $output); $this->assertEquals($output[1], 'Reset all jobs.'); $this->assertEquals($output[2], 'The job JobLogCleanup has been reset.'); $this->assertEquals($output[3], 'The job Monitor has been reset.'); $this->assertEmpty(JobInProcess::getAll()); }