public function testRunNonMonitorJob() { Yii::app()->user->userModel = User::getByUsername('super'); //Test running a TestJob that it creates a JobLog and does not leave a JobInProcess $this->assertEquals(0, JobInProcess::getCount()); $this->assertEquals(0, JobLog::getCount()); $isJobInProgress = false; JobsManagerUtil::runNonMonitorJob('Test', new MessageLogger(), $isJobInProgress); $this->assertFalse($isJobInProgress); $this->assertEquals(0, JobInProcess::getCount()); $jobLogs = JobLog::getAll(); $this->assertEquals(1, count($jobLogs)); $this->assertEquals('Test', $jobLogs[0]->type); $this->assertEquals(JobLog::STATUS_COMPLETE_WITHOUT_ERROR, $jobLogs[0]->status); $this->assertEquals(0, $jobLogs[0]->isProcessed); //Now test a job that always fails $isJobInProgress = false; JobsManagerUtil::runNonMonitorJob('TestAlwaysFails', new MessageLogger(), $isJobInProgress); $this->assertFalse($isJobInProgress); $this->assertEquals(0, JobInProcess::getCount()); $jobLogs = JobLog::getAll(); $this->assertEquals(2, count($jobLogs)); $this->assertEquals('TestAlwaysFails', $jobLogs[1]->type); $this->assertEquals(JobLog::STATUS_COMPLETE_WITH_ERROR, $jobLogs[1]->status); $this->assertEquals('The test job failed', $jobLogs[1]->message); $this->assertEquals(0, $jobLogs[1]->isProcessed); }
public function testJobLog() { Yii::app()->user->userModel = User::getByUsername('super'); $jobLog = new JobLog(); $jobLog->type = 'Monitor'; $jobLog->startDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $jobLog->endDateTime = DateTimeUtil::convertTimestampToDbFormatDateTime(time()); $jobLog->status = JobLog::STATUS_COMPLETE_WITHOUT_ERROR; //Should fail to save because isProcessed is not specified. $this->assertFalse($jobLog->save()); $jobLog->isProcessed = false; $this->assertTrue($jobLog->save()); $id = $jobLog->id; $jobLog = JobLog::getById($id); $jobLog->delete(); $this->assertEquals(0, JobInProcess::getCount()); }
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()); }