Example #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());
 }
 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 run()
 {
     //Do not run this job if ReadPermissionSubscriptionUpdateComplete is already running
     try {
         JobInProcess::getByType('ReadPermissionSubscriptionComprehensiveUpdate');
     } catch (NotFoundException $e) {
         ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables(true);
     }
     return true;
 }
 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());
 }
Example #5
0
 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();
 }
Example #6
0
 public function actionResetJob($type)
 {
     assert('is_string($type) && $type != ""');
     $jobClassName = $type . 'Job';
     try {
         $jobInProcess = JobInProcess::getByType($type);
         $jobInProcess->delete();
         $messageBoxContent = HtmlNotifyUtil::renderHighlightBoxByMessage(Zurmo::t('JobsManagerModule', 'The job {jobName} has been reset.', array('{jobName}' => $jobClassName::getDisplayName())));
         $this->processListAction($messageBoxContent);
     } catch (NotFoundException $e) {
         $messageBoxContent = HtmlNotifyUtil::renderHighlightBoxByMessage(Zurmo::t('JobsManagerModule', 'The job {jobName} was not found to be stuck and therefore was not reset.', array('{jobName}' => $jobClassName::getDisplayName())));
         $this->processListAction($messageBoxContent);
     }
 }
 public function run()
 {
     $counter = 0;
     try {
         while (JobInProcess::getByType('ReadPermissionSubscriptionQuickUpdate') instanceof RedBeanModel && $counter <= self::MAX_NUMBER_OF_TRIES) {
             sleep(30);
             $counter++;
         }
     } catch (NotFoundException $e) {
         ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables(false);
         return true;
     }
     return false;
 }
Example #8
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()));
 }
 protected static function getNonMonitorJobsInProcessModels()
 {
     $searchAttributeData = array();
     $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'type', 'operatorType' => 'doesNotEqual', 'value' => 'Monitor'));
     $searchAttributeData['structure'] = '1';
     $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter('JobInProcess');
     $where = RedBeanModelDataProvider::makeWhere('JobInProcess', $searchAttributeData, $joinTablesAdapter);
     return JobInProcess::getSubset($joinTablesAdapter, null, null, $where, null);
 }
 protected static function getIfJobIsInProcessOtherwiseReturnNullByType($type)
 {
     assert('is_string($type) && $type != ""');
     try {
         $jobInProcess = JobInProcess::getByType($type);
     } catch (NotFoundException $e) {
         $jobInProcess = null;
     }
     return $jobInProcess;
 }
 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();
         }
     }
 }
 /**
  * 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.");
             }
         }
     }
 }
 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;
 }