/**
  * Tests running a background process.
  *
  * @covers Cocur\BackgroundProcess\BackgroundProcess::__construct()
  * @covers Cocur\BackgroundProcess\BackgroundProcess::run()
  * @covers Cocur\BackgroundProcess\BackgroundProcess::isRunning()
  * @covers Cocur\BackgroundProcess\BackgroundProcess::getPid()
  * @covers Cocur\BackgroundProcess\BackgroundProcess::stop()
  */
 public function testRun()
 {
     $process = new BackgroundProcess('sleep 5');
     $this->assertFalse($process->isRunning(), 'process should not run');
     $process->run();
     $this->assertNotNull($process->getPid(), 'process should have a pid');
     $this->assertTrue($process->isRunning(), 'process should run');
     $this->assertTrue($process->stop(), 'stop process');
     $this->assertFalse($process->isRunning(), 'processes should not run anymore');
     $this->assertFalse($process->stop(), 'cannot stop process that is not running');
 }
 /**
  * @test
  * @covers Cocur\BackgroundProcess\BackgroundProcess::stop()
  * @covers Cocur\BackgroundProcess\BackgroundProcess::getOS()
  */
 public function stopShouldStopRunningProcess()
 {
     if (preg_match('/^WIN/', PHP_OS)) {
         $this->markTestSkipped('Cocur\\BackgroundProcess\\BackgroundProcess::stop() is not supported on Windows.');
         return;
     }
     $process = new BackgroundProcess('sleep 5');
     $process->run();
     $this->assertTrue($process->stop());
     $this->assertFalse($process->isRunning());
 }
예제 #3
0
 /**
  * Run the PDF Processor utility.
  * @return  mixed   Total pages or false if failed processing.
  */
 protected function runPdfProcessor($localPDF)
 {
     // Get Chaucer PDF converter
     $ChaucerPDF = Configure::read('processor.ChaucerPDF');
     if (strlen($ChaucerPDF) > 0) {
         if (!strlen($localPDF)) {
             throw new exception("[PdfProcessor::runPdfProcessor] PDF Processing executable not configured");
         }
         if (!file_exists($localPDF)) {
             throw new exception("[PdfProcessor::runPdfProcessor] PDF Processor {$ChaucerPDF} not found");
         }
         // Get book dimensions
         $bookSize = $this->getBookDimensions($localPDF);
         // Get book processing options
         $options = $this->getPdfProcessorSwitches($bookSize['width'], $bookSize['height']);
         // Assemble PDF converter command
         $cmd = "{$ChaucerPDF} {$options} {$localPDF}";
         CakeLog::debug('[PdfProcessor::runPdfProcessor] Executing: ' . $cmd);
         // Set progress and error log files for PDF converter
         $progressFile = Folder::addPathElement($this->workingDir, "progress.txt");
         $errorLogFile = Folder::addPathElement($this->workingDir, "ErrorLog.txt");
         // Create and run new background process
         $pdfProc = new BackgroundProcess($cmd);
         $endTime = time() + PROCESS_TIMEOUT;
         $pdfProc->run($cmd);
         // Preset progress vars
         $maxSteps = 0;
         $lastPage = 0;
         $totalPages = 0;
         // Get and check running process
         $bRunning = true;
         while ($bRunning && time() < $endTime) {
             // Get the progress
             if (file_exists($progressFile)) {
                 // Get contents of the progress file
                 $content = file_get_contents($progressFile);
                 // Check for the string 10/1020. <page>/<page_count>
                 if (strpos($content, '/') !== false) {
                     // Get page info
                     $pageInfo = explode('/', $content);
                     // Check to stat the progress
                     if (!$maxSteps) {
                         $totalPages = (int) $pageInfo[1];
                         if ($totalPages > 0) {
                             $maxSteps = $this->getMaxProgressSteps($totalPages);
                             $this->progress->startProgress($maxSteps);
                         }
                     }
                     // Update step
                     $this->progress->setCurrentStep($pageInfo[0]);
                     if ($lastPage != $pageInfo[0]) {
                         $endTime = time() + PROCESS_TIMEOUT;
                         // reset the timeout window for the pdf processor
                         $lastPage = $pageInfo[0];
                     }
                 } else {
                     // Check if process is running
                     $bRunning = $this->isPdfProcessorRunning($progressFile, $errorLogFile);
                 }
             }
             sleep(2);
         }
         clearstatcache();
         // Check if there were any errors
         if (file_exists($errorLogFile)) {
             throw new Exception('PDF Processor Failed: ' . file_get_contents($errorLogFile));
         }
         CakeLog::debug('[PdfProcessor::runPdfProcessor] Complete: ' . $cmd);
         // Return total pages
         return $totalPages;
     }
     // Return false if PDF Converter was not detected
     return FALSE;
 }