示例#1
0
 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());
 }
 /**
  * Execute the action
  * @param array $args - command line parameters specific for this command
  * @return int|void
  */
 public function run($args)
 {
     if (!isset($args[0])) {
         $this->usageError('A username must be specified.');
     }
     try {
         Yii::app()->user->userModel = User::getByUsername($args[0]);
     } catch (NotFoundException $e) {
         $this->usageError('The specified username does not exist.');
     }
     $group = Group::getByName(Group::SUPER_ADMINISTRATORS_GROUP_NAME);
     if (!$group->users->contains(Yii::app()->user->userModel)) {
         $this->usageError('The specified user is not a super administrator.');
     }
     if (!isset($args[1])) {
         $this->usageError('JobType must be provided and must be existing jobType!');
     } else {
         $jobType = $args[1];
     }
     $template = "{message}\n";
     $messageStreamer = new MessageStreamer($template);
     $messageStreamer->setExtraRenderBytes(0);
     $messageStreamer->add('');
     if ($jobType == 'All') {
         $messageStreamer->add("Reset all jobs.");
         $jobsInProcess = JobInProcess::getAll();
         if (is_array($jobsInProcess) && count($jobsInProcess) > 0) {
             foreach ($jobsInProcess as $jobInProcess) {
                 $jobInProcess->delete();
                 $messageStreamer->add("The job {$jobInProcess->type} has been reset.");
             }
         } else {
             $messageStreamer->add("There are no jobs in process to be reset.");
         }
     } else {
         $jobClassName = $jobType . 'Job';
         if (!@class_exists($jobClassName)) {
             $messageStreamer->add("Error! The {$jobClassName} does not exist.");
         } else {
             try {
                 $jobInProcess = JobInProcess::getByType($jobType);
                 $jobInProcess->delete();
                 $messageStreamer->add("The job {$jobClassName} has been reset.");
             } catch (NotFoundException $e) {
                 $messageStreamer->add("The job {$jobClassName} was not found to be stuck and therefore was not reset.");
             }
         }
     }
 }
示例#3
0
 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, count(JobInProcess::getAll()));
 }
示例#4
0
 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, count(JobInProcess::getAll()));
     $this->assertEquals(0, count(JobLog::getAll()));
     JobsManagerUtil::runNonMonitorJob('Test', new MessageLogger());
     $this->assertEquals(0, count(JobInProcess::getAll()));
     $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
     JobsManagerUtil::runNonMonitorJob('TestAlwaysFails', new MessageLogger());
     $this->assertEquals(0, count(JobInProcess::getAll()));
     $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 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(StuckJob::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() - 10000);
     $sql = "Update item set createddatetime = '" . $createdDateTime . "' where id = " . $jobInProcess->getClassId('Item');
     ZurmoRedBean::exec($sql);
     $jobInProcess->forget();
     $monitorJob->run();
     $this->assertEquals(0, count(JobInProcess::getAll()));
     //should still be 0 but the quantity should increase
     $this->assertEquals(0, count(Notification::getAll()));
     //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, count(JobInProcess::getAll()));
     //should still be 0 but the quantity should increase
     $this->assertEquals(0, count(Notification::getAll()));
     //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, count(Notification::getAll()));
     //Confirm an email was sent
     $this->assertEquals(0, Yii::app()->emailHelper->getQueuedCount());
     $this->assertEquals(1, EmailMessage::getCount());
     $this->assertEquals(1, Yii::app()->emailHelper->getSentCount());
 }
 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());
 }
 public static function checkIfThereAreJobsInProcess()
 {
     $jobsInProcess = JobInProcess::getAll();
     if (!empty($jobsInProcess)) {
         $message = Zurmo::t('Core', 'There are jobs that are currently in progress.') . "\n";
         $message .= Zurmo::t('Core', 'Please set maintenanceMode to true and wait for these jobs to complete, and run this command again') . "\n";
         $message .= Zurmo::t('Core', 'Here is list of ative jobs:') . "\n";
         foreach ($jobsInProcess as $job) {
             $message .= Zurmo::t('Core', '{staredAt}: {type}', array('{staredAt}' => $job->createdDateTime, '{type}' => $job->type)) . "\n";
         }
         throw new NotSupportedException($message);
     }
     return true;
 }