/**
  * Runs task
  */
 protected function _run()
 {
     $this->_Process = new TaskProcess($this->_task['command'] . $this->_argsToString($this->_task['arguments']), $this->_task['path']);
     $this->_Process->setTimeout($this->_task['timeout']);
     try {
         $this->_Process->start(function ($type, $buffer) {
             if ('err' === $type) {
                 $this->_Shell->err($buffer);
                 $this->_task['stderr'] .= $buffer;
             } else {
                 $this->_Shell->out($buffer);
                 $this->_task['stdout'] .= $buffer;
             }
             $this->_TaskServer->updated($this->_task);
         });
         while ($this->_Process->isRunning()) {
             $this->_task['process_id'] = (int) $this->_Process->getPid();
             $this->_TaskServer->updateStatistics($this->_task);
             $this->_Process->checkTimeout();
             sleep(Configure::read('Task.checkInterval'));
             if ($this->_TaskServer->mustStop($this->_task['id'])) {
                 $this->_Process->stop(Configure::read('Task.stopTimeout'));
                 $this->_task['code'] = 143;
                 $this->_task['code_string'] = TaskProcess::$exitCodes[143];
                 return $this->_stopped(true);
             }
         }
         $this->_task['code'] = $this->_Process->getExitCode();
         $this->_task['code_string'] = $this->_Process->getExitCodeText();
     } catch (Exception $Exception) {
         $this->_task['code'] = 134;
         $this->_task['code_string'] = $Exception->getMessage();
     }
     $this->_stopped(false);
 }
 /**
  * {@inheritdoc}
  */
 public function importAction()
 {
     set_time_limit(1200);
     $this->getLockingAccessDenied();
     $import_counts = array();
     $messages = array();
     if (!empty($_FILES) && !empty($_FILES["inputFile"])) {
         $file = TMP_UPLOAD_PATH . '/' . $_FILES["inputFile"]["name"];
         $tmpFile = $_FILES["inputFile"]["tmp_name"];
         $inputFile = $_FILES['inputFile'];
         $error_counts = 0;
         if ($this->importFileValidate($inputFile)) {
             if (move_uploaded_file($tmpFile, $file)) {
                 $objReader = \PHPExcel_IOFactory::createReaderForFile($file);
                 if (get_class($objReader) == 'PHPExcel_Reader_CSV') {
                     unset($objReader);
                     $this->get('session')->setFlash('sonata_flash_error', $this->admin->trans('Fichier non lisible'));
                     return $this->render(':redirects:back.html.twig');
                 }
                 $_FILES = array();
                 //create fake process id
                 $pid = time();
                 $user = $this->get('security.context')->getToken()->getUser();
                 $kernel = $this->get('kernel');
                 $command = 'php ' . $kernel->getRootDir() . '/console clientoperationsbundle:import:excel ' . $user->getId() . ' ' . $this->client_id . ' application.sonata.admin.v01tva ' . $file . ' ' . $inputFile['name'] . ' ' . $this->getLocking() . ' ' . $this->_year . ' ' . $this->_month . ' ' . $pid . ' --env=' . $kernel->getEnvironment() . ' --no-debug ';
                 /* var_dump($command);
                   	exit; */
                 $process = new \Symfony\Component\Process\Process($command);
                 $process->setTimeout(3600);
                 $process->start();
                 $start = microtime(true);
                 while ($process->isRunning()) {
                     $total = microtime(true) - $start;
                     if ($total / 60 >= 2) {
                         // if process is too long (2 minutes)
                         //var_dump(($total/60));
                         $em = $this->getDoctrine()->getManager();
                         $importNotif = new ImportNotification();
                         $importNotif->setPid($pid)->setUser($user)->setClientId($this->client_id);
                         $em->persist($importNotif);
                         $em->flush();
                         $this->_hasImportErrors = true;
                         $this->get('session')->setFlash('sonata_flash_error', $this->admin->trans('There are too many data to be processed. We will just notify you once it\'s done. Check your email (' . $user->getEmail() . ') within few hours.'));
                         break;
                         return $this->render(':redirects:back.html.twig');
                     }
                 }
                 $output = unserialize($process->getOutput());
                 $messages = $output['messages'];
                 $import_counts = $output['import_counts'];
                 $error_counts = $output['error_counts'];
             }
         }
     }
     if (!empty($messages) || $error_counts) {
         $this->get('session')->setFlash('sonata_flash_info|raw', implode("<br/>", $messages));
     } else {
         $message = trim($this->get('session')->getFlash('sonata_flash_info|raw'));
         if ($message == '') {
             $this->get('session')->setFlash('sonata_flash_info|raw', $this->admin->trans('Imported : %count%', array('%count%' => 0)));
         } else {
             $this->get('session')->setFlash('sonata_flash_info|raw', $message);
         }
     }
     if (isset($import_counts['rows']['errors']) && !empty($import_counts['rows']['errors']) || $this->_hasImportErrors) {
         return $this->render(':redirects:back.html.twig');
     } else {
         return $this->redirect($this->generateUrl('rapprochement_index', array('client_id' => $this->client_id, 'month' => $this->_query_month, 'fromImport' => 1)));
     }
 }
Esempio n. 3
0
 /**
  * @param string      $command
  * @param bool        $async
  * @param string|null $cwd
  *
  * @return \Symfony\Component\Process\Process
  */
 private static function _runProcess($command, $async = false, $cwd = null)
 {
     $process = new \Symfony\Component\Process\Process($command, $cwd);
     self::_log(sprintf('Running command: %s', $command));
     if ($async) {
         $process->start();
         sleep(1);
         if (!$process->isRunning()) {
             throw new RuntimeException(sprintf('Failed to start %s: %s', $command, $process->getErrorOutput()));
         }
     } else {
         $process->run();
         if (!$process->isSuccessful()) {
             throw new RuntimeException(sprintf('Failed to run %s: %s', $command, $process->getErrorOutput()));
         }
     }
     return $process;
 }
 public function clientAction()
 {
     set_time_limit(0);
     //ini_set('max_execution_time', 0);
     //ini_set('memory_limit', '256M');
     if ($_SERVER['REQUEST_METHOD'] == 'POST') {
         if (!empty($_FILES) && !empty($_FILES["inputFile"]["name"])) {
             $file = TMP_UPLOAD_PATH . '/' . str_replace(array(' ', '+', '(', ')'), '', $_FILES["inputFile"]["name"]);
             $tmpFile = $_FILES["inputFile"]["tmp_name"];
             $inputFile = $_FILES['inputFile'];
             if (move_uploaded_file($tmpFile, $file)) {
                 /* @var $objReader \PHPExcel_Reader_Excel2007 */
                 $objReader = \PHPExcel_IOFactory::createReaderForFile($file);
                 if (get_class($objReader) == 'PHPExcel_Reader_CSV') {
                     $this->get('session')->setFlash('sonata_flash_error', $this->admin->trans('Fichier non lisible'));
                     return $this->render(':redirects:back.html.twig');
                 }
                 $_FILES = array();
                 //create fake process id
                 $pid = time();
                 $user = $this->get('security.context')->getToken()->getUser();
                 $kernel = $this->get('kernel');
                 $command = 'php ' . $kernel->getRootDir() . '/console clientbundle:import:initial client ' . $user->getId() . ' ' . realpath($file) . ' ' . $pid . ' --env=' . $kernel->getEnvironment();
                 // . ' --no-debug ';
                 /* var_dump($command);
                 			exit; */
                 $process = new \Symfony\Component\Process\Process($command);
                 $process->setTimeout(3600);
                 $process->start();
                 $start = microtime(true);
                 while ($process->isRunning()) {
                     $total = microtime(true) - $start;
                     if ($total / 60 >= 2) {
                         // if process is too long (2 minutes)
                         //var_dump(($total/60));
                         $em = $this->getDoctrine()->getManager();
                         $importNotif = new ImportNotification();
                         $importNotif->setPid($pid)->setUser($user)->setClientId(0);
                         $em->persist($importNotif);
                         $em->flush();
                         $this->_hasImportErrors = true;
                         $this->get('session')->setFlash('sonata_flash_error', \AppKernel::getStaticContainer()->get('translator')->trans('There are too many data to be processed. We will just notify you once it\'s done. Check your email (' . $user->getEmail() . ') within few hours.'));
                         break;
                         return $this->render(':redirects:back.html.twig');
                     }
                 }
                 $output = unserialize($process->getOutput());
                 $messages = $output['messages'];
                 $import_counts = $output['import_counts'];
             }
         } else {
             $this->get('session')->setFlash('sonata_flash_error|raw', 'Please upload a file');
         }
         //$messages = $this->getCountMessageImports();
         if (!empty($messages)) {
             $this->get('session')->setFlash('sonata_flash_info|raw', implode("<br />", $messages));
         }
         return $this->render(':redirects:back.html.twig');
     }
     return $this->render('ApplicationSonataClientBundle:InitialImport:client.html.twig');
 }