Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 public function apply(base $appbox, Application $app)
 {
     $sql = 'DELETE FROM Tasks';
     $stmt = $app['phraseanet.appbox']->get_connection()->prepare($sql);
     $stmt->execute();
     $stmt->closeCursor();
     $sql = 'SELECT task_id, active, crashed, name, class, settings FROM task2';
     $stmt = $appbox->get_connection()->prepare($sql);
     $stmt->execute();
     $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
     $stmt->closeCursor();
     foreach ($rs as $row) {
         try {
             $job = $this->createJob($app, $row['class']);
         } catch (\RuntimeException $e) {
             continue;
         }
         $settings = simplexml_load_string($row['settings']);
         $period = $job->getEditor()->getDefaultPeriod();
         if ($settings->period) {
             $period = (int) $settings->period;
             unset($settings->period);
             $row['settings'] = $settings->asXML();
         }
         $task = new Task();
         $task->setCrashed($row['crashed'])->setJobId($job->getJobId())->setName($row['name'])->setPeriod($period)->setSettings($row['settings'])->setStatus($row['active'] ? Task::STATUS_STARTED : Task::STATUS_STOPPED);
         $app['EM']->persist($task);
     }
     $app['EM']->flush();
 }
Ejemplo n.º 2
0
 public function doTestRun()
 {
     $job = $this->getJob();
     $task = new Task();
     $task->setSettings($job->getEditor()->getDefaultSettings());
     $job->singleRun(new JobData(self::$DI['app'], $task));
 }
Ejemplo n.º 3
0
 public function getTaskList()
 {
     $task3 = new Task();
     $task3->setName('task 3')->setStatus(Task::STATUS_STOPPED)->setJobId('Alchemy\\Phrasea\\TaskManager\\Job\\NullJob');
     self::$DI['app']['orm.em']->persist($task3);
     self::$DI['app']['orm.em']->flush();
     return new TaskList(self::$DI['app']['orm.em']->getRepository('Phraseanet:Task'), self::$DI['app']['root.path'], '/path/to/php', '/path/to/php-conf');
 }
Ejemplo n.º 4
0
 /**
  * Returns live informations about the given task.
  *
  * @return array
  */
 public function getTask(Task $task, $throwException = false)
 {
     try {
         $data = $this->notifier->notify(Notifier::MESSAGE_INFORMATIONS, 2);
     } catch (RuntimeException $e) {
         if ($throwException) {
             throw $e;
         }
         $data = [];
     }
     $taskData = isset($data['jobs']) && isset($data['jobs'][$task->getId()]) ? $data['jobs'][$task->getId()] : [];
     return ['configuration' => $task->getStatus(), 'actual' => isset($taskData['status']) ? $taskData['status'] : Task::STATUS_STOPPED, 'process-id' => isset($taskData['process-id']) ? $taskData['process-id'] : null];
 }
Ejemplo n.º 5
0
 public function entityToTask(TaskEntity $task)
 {
     $name = $task->getId();
     $arguments = ['exec', $this->phpExec];
     if ($this->phpConf) {
         $arguments[] = '-c';
         $arguments[] = $this->phpConf;
     }
     $arguments[] = '-f';
     $arguments[] = $this->root . '/bin/console';
     $arguments[] = '--';
     $arguments[] = '-q';
     $arguments[] = 'task-manager:task:run';
     $arguments[] = $task->getId();
     $arguments[] = '--listen-signal';
     $arguments[] = '--max-duration';
     $arguments[] = '1800';
     $arguments[] = '--max-memory';
     $arguments[] = 128 << 20;
     $builder = ProcessBuilder::create($arguments);
     $builder->setTimeout(0);
     return new Task($task, $name, $task->isSingleRun() ? 1 : INF, $builder->getProcess());
 }
 private function insertTwoTasks(EntityManager $em)
 {
     $task1 = new Task();
     $task1->setName('task 1')->setJobId('Null');
     $task2 = new Task();
     $task2->setName('task 2')->setJobId('Null');
     $em->persist($task1);
     $em->persist($task2);
 }
 /**
  * {@inheritDoc}
  */
 public function setPeriod($period)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setPeriod', array($period));
     return parent::setPeriod($period);
 }
Ejemplo n.º 8
0
 private function formatTask(Task $task, $data)
 {
     $taskData = isset($data['jobs']) && isset($data['jobs'][$task->getId()]) ? $data['jobs'][$task->getId()] : [];
     return ['configuration' => $task->getStatus(), 'actual' => isset($taskData['status']) ? $taskData['status'] : Task::STATUS_STOPPED, 'process-id' => isset($taskData['process-id']) ? $taskData['process-id'] : null];
 }
Ejemplo n.º 9
0
 /**
  * {@inheritdoc}
  */
 public function getPath()
 {
     return sprintf('%s/task_%d.log', $this->root, $this->task->getId());
 }
Ejemplo n.º 10
0
 private function getCommandline($indexerPath, Application $app, Task $task)
 {
     $cmd = [$indexerPath, '-o'];
     $settings = simplexml_load_string($task->getSettings());
     $host = trim($settings->host);
     $port = (int) $settings->port;
     $base = trim($settings->base);
     $user = trim($settings->user);
     $password = trim($settings->password);
     $socket = (int) $settings->socket;
     $charset = trim($settings->charset);
     $stem = trim($settings->stem);
     $sortempty = trim($settings->sortempty);
     $debugmask = (int) $settings->debugmask;
     $nolog = \p4field::isyes(trim($settings->nolog));
     $winsvc_run = \p4field::isyes(trim($settings->winsvc_run));
     if ('' !== $host) {
         $cmd[] = '--host';
         $cmd[] = $host;
     }
     if (0 < $port) {
         $cmd[] = '--port';
         $cmd[] = $port;
     }
     if ('' !== $base) {
         $cmd[] = '--base';
         $cmd[] = $base;
     }
     if ('' !== $user) {
         $cmd[] = '--user';
         $cmd[] = $user;
     }
     if ('' !== $password) {
         $cmd[] = '--password';
         $cmd[] = $password;
     }
     if (0 < $socket) {
         $cmd[] = '--socket';
         $cmd[] = $socket;
     }
     if ('' !== $charset) {
         $cmd[] = '--default-character-set';
         $cmd[] = $charset;
     }
     if ('' !== $stem) {
         $cmd[] = '--stem';
         $cmd[] = $stem;
     }
     if ('' !== $sortempty) {
         $cmd[] = '--sort-empty';
         $cmd[] = $sortempty;
     }
     if (0 < $debugmask) {
         $cmd[] = '--debug';
         $cmd[] = $debugmask;
     }
     if ($nolog) {
         $cmd[] = '--nolog';
     }
     if ($winsvc_run) {
         $cmd[] = '--run';
     }
     return $cmd;
 }
Ejemplo n.º 11
0
 public function getTask(Request $request, Task $task)
 {
     if (false === $this->app['phraseanet.configuration']['main']['task-manager']['enabled']) {
         throw new RuntimeException('The use of the task manager is disabled on this instance.');
     }
     if ('json' === $request->getContentType()) {
         return $this->app->json(array_replace(['id' => $task->getId(), 'name' => $task->getName(), 'urls' => $this->getTaskResourceUrls($task->getId())], $this->getLiveInformationRequest()->getTask($task)));
     }
     $editor = $this->getJobFactory()->create($task->getJobId())->getEditor();
     $form = $this->app->form(new TaskForm());
     $form->setData($task);
     return $this->render($editor->getTemplatePath(), ['task' => $task, 'form' => $form->createView(), 'view' => 'XML']);
 }
Ejemplo n.º 12
0
 /**
  * Update a task property
  *  - name
  *  - autostart
  *
  * @param  \Silex\Application           $app  Silex application
  * @param  Task                         $task The task
  * @return \API_V1_result
  * @throws \API_V1_exception_badrequest
  */
 public function set_task_property(Application $app, $task)
 {
     $result = new API_V1_result($app, $app['request'], $this);
     $title = $app['request']->get('title');
     $autostart = $app['request']->get('autostart');
     if (null === $title && null === $autostart) {
         throw new \API_V1_exception_badrequest();
     }
     if ($title) {
         $task->setName($title);
     }
     if ($autostart) {
         $task->setStatus(Task::STATUS_STARTED);
     }
     $result->set_datas(['task' => $this->list_task($app, $task)]);
     return $result;
 }
Ejemplo n.º 13
0
 /**
  * transform tasks 'workflow 01' to 'RecordMover'
  * will group tasks(01) with same period to a single task(02)
  *
  * @param base        $appbox
  * @param Application $app
  *
  * @return boolean
  */
 public function apply(base $appbox, Application $app)
 {
     $ttasks = [];
     $conn = $appbox->get_connection();
     $sql = 'SELECT task_id, active, name, class, settings
             FROM task2
             WHERE class=\'task_period_workflow01\'';
     if (($stmt = $conn->prepare($sql)) !== FALSE) {
         $stmt->execute();
         $ttasks = $stmt->fetchAll();
     }
     $stmt->closeCursor();
     $tdom = [];
     // key = period
     $taskstodel = [];
     foreach ($ttasks as $task) {
         $active = true;
         $warning = [];
         /*
          * migrating task 'workflow01'
          */
         $x = $task['settings'];
         if (false !== ($sx = simplexml_load_string($x))) {
             $period = (int) $sx->period;
             if (!array_key_exists('_' . $period, $tdom)) {
                 $dom = new DOMDocument('1.0', 'UTF-8');
                 $dom->formatOutput = true;
                 $dom->preserveWhiteSpace = false;
                 $ts = $dom->appendChild($dom->createElement('tasksettings'));
                 $ts->appendChild($dom->createElement('period'))->appendChild($dom->createTextNode(60 * $period));
                 $ts->appendChild($dom->createElement('logsql'))->appendChild($dom->createTextNode('1'));
                 $tasks = $ts->appendChild($dom->createElement('tasks'));
                 $tdom['_' . $period] = ['dom' => $dom, 'tasks' => $tasks];
             } else {
                 $dom =& $tdom['_' . $period]['dom'];
                 $tasks =& $tdom['_' . $period]['tasks'];
             }
             /*
              * migrating task 'workflow01'
              */
             if ($task['class'] === 'task_period_workflow01') {
                 $t = $tasks->appendChild($dom->createElement('task'));
                 $t->setAttribute('active', '0');
                 $t->setAttribute('name', 'imported from \'' . $task['name'] . '\'');
                 $t->setAttribute('action', 'update');
                 if ($sx->sbas_id) {
                     $sbas_id = trim($sx->sbas_id);
                     if ($sbas_id != '' && is_numeric($sbas_id)) {
                         $t->setAttribute('sbas_id', $sx->sbas_id);
                     } else {
                         $warning[] = sprintf("Bad sbas_id '%s'", $sbas_id);
                         $active = false;
                     }
                 } else {
                     $warning[] = sprintf("missing sbas_id");
                     $active = false;
                 }
                 // 'from' section
                 $from = $t->appendChild($dom->createElement('from'));
                 if ($sx->coll0) {
                     if (($coll0 = trim($sx->coll0)) != '') {
                         if (is_numeric($coll0)) {
                             $n = $from->appendChild($dom->createElement('coll'));
                             $n->setAttribute('compare', '=');
                             $n->setAttribute('id', $coll0);
                         } else {
                             $warning[] = sprintf("Bad (from) coll_id '%s'", $coll0);
                             $active = false;
                         }
                     }
                 }
                 if ($sx->status0 && trim($sx->status0) != '') {
                     $st = explode('_', trim($sx->status0));
                     if (count($st) == 2) {
                         $bit = (int) $st[0];
                         if ($bit >= 0 && $bit < 32 && ($st[1] == '0' || $st[1] == '1')) {
                             $from->appendChild($dom->createElement('status'))->setAttribute('mask', $st[1] . str_repeat('x', $bit - 1));
                         } else {
                             $warning[] = sprintf("Bad (from) status '%s'", trim($sx->status0));
                             $active = false;
                         }
                     } else {
                         $warning[] = sprintf("Bad (from) status '%s'", trim($sx->status0));
                         $active = false;
                     }
                 }
                 // 'to' section
                 $to = $t->appendChild($dom->createElement('to'));
                 if ($sx->coll1) {
                     if (($coll1 = trim($sx->coll1)) != '') {
                         if (is_numeric($coll1)) {
                             $n = $to->appendChild($dom->createElement('coll'));
                             $n->setAttribute('id', $coll1);
                         } else {
                             $warning[] = sprintf("Bad (to) coll_id '%s'", $coll1);
                             $active = false;
                         }
                     }
                 }
                 if ($sx->status1 && trim($sx->status1) != '') {
                     $st = explode('_', trim($sx->status1));
                     if (count($st) == 2) {
                         $bit = (int) $st[0];
                         if ($bit >= 0 && $bit < 32 && ($st[1] == '0' || $st[1] == '1')) {
                             $to->appendChild($dom->createElement('status'))->setAttribute('mask', $st[1] . str_repeat('x', $bit - 1));
                         } else {
                             $warning[] = sprintf("Bad (to) status '%s'", trim($sx->status1));
                             $active = false;
                         }
                     } else {
                         $warning[] = sprintf("Bad (to) status '%s'", trim($sx->status1));
                         $active = false;
                     }
                 }
                 if ($active && $task['active'] == '1') {
                     $t->setAttribute('active', '1');
                 }
                 foreach ($warning as $w) {
                     $t->appendChild($dom->createComment($w));
                 }
                 $taskstodel[] = $task['task_id'];
             }
         }
         if (count($taskstodel) > 0) {
             $conn->exec('DELETE FROM task2 WHERE task_id IN(' . implode(',', $taskstodel) . ')');
         }
     }
     /*
      * save new tasks
      */
     foreach ($tdom as $newtask) {
         $settings = $newtask['dom']->saveXML();
         $sxml = simplexml_load_string($settings);
         $period = $sxml->period ? (int) $sxml->period : 300;
         $task = new Task();
         $task->setName('Record mover')->setJobId('RecordMover')->setSettings($settings)->setPeriod($period)->setStatus(Task::STATUS_STARTED);
         $app['orm.em']->persist($task);
     }
     $app['orm.em']->flush();
     return true;
 }
Ejemplo n.º 14
0
 /**
  * Update a task property
  *  - name
  *  - autostart
  *
  * @param  \Silex\Application $app  Silex application
  * @param  Task               $task The task
  *
  * @return Response
  */
 public function set_task_property(Application $app, Request $request, $task)
 {
     $title = $app['request']->get('title');
     $autostart = $app['request']->get('autostart');
     if (null === $title && null === $autostart) {
         return $this->getBadRequest($app, $request);
     }
     if ($title) {
         $task->setName($title);
     }
     if ($autostart) {
         $task->setStatus(Task::STATUS_STARTED);
     }
     return Result::create($request, ['task' => $this->list_task($app, $task)])->createResponse();
 }
Ejemplo n.º 15
0
 protected function doExport(Application $app, Task $task, FtpExport $export)
 {
     $settings = simplexml_load_string($task->getSettings());
     $proxy = (string) $settings->proxy;
     $proxyport = (string) $settings->proxyport;
     $state = "";
     $ftp_server = $export->getAddr();
     $ftp_user_name = $export->getLogin();
     $ftp_user_pass = $export->getPwd();
     $ftpLog = $ftp_user_name . "@" . \p4string::addEndSlash($ftp_server) . $export->getDestfolder();
     if ($export->getCrash() == 0) {
         $line = $this->translator->trans('task::ftp:Etat d\'envoi FTP vers le serveur "%server%" avec le compte "%username%" et pour destination le dossier : "%directory%"', ['%server%' => $ftp_server, '%username%' => $ftp_user_name, '%directory%' => $export->getDestfolder()]) . PHP_EOL;
         $state .= $line;
         $this->log('debug', $line);
     }
     $state .= $line = $this->translator->trans("task::ftp:TENTATIVE no %number%, %date%", ['%number%' => $export->getCrash() + 1, '%date%' => "  (" . date('r') . ")"]) . PHP_EOL;
     $this->log('debug', $line);
     try {
         $ssl = $export->isSsl();
         $ftp_client = $app['phraseanet.ftp.client']($ftp_server, 21, 300, $ssl, $proxy, $proxyport);
         $ftp_client->login($ftp_user_name, $ftp_user_pass);
         if ($export->isPassif()) {
             try {
                 $ftp_client->passive(true);
             } catch (\Exception $e) {
                 $this->log('debug', $e->getMessage());
             }
         }
         if (trim($export->getDestfolder()) != '') {
             try {
                 $ftp_client->chdir($export->getDestFolder());
                 $export->setDestfolder('/' . $export->getDestfolder());
             } catch (\Exception $e) {
                 $this->log('debug', $e->getMessage());
             }
         } else {
             $export->setDestfolder('/');
         }
         if (trim($export->getFoldertocreate()) != '') {
             try {
                 $ftp_client->mkdir($export->getFoldertocreate());
             } catch (\Exception $e) {
                 $this->log('debug', $e->getMessage());
             }
             try {
                 $new_dir = $ftp_client->add_end_slash($export->getDestfolder()) . $export->getFoldertocreate();
                 $ftp_client->chdir($new_dir);
             } catch (\Exception $e) {
                 $this->log('debug', $e->getMessage());
             }
         }
         $obj = [];
         $basefolder = '';
         if (!in_array(trim($export->getDestfolder()), ['.', './', ''])) {
             $basefolder = \p4string::addEndSlash($export->getDestfolder());
         }
         $basefolder .= $export->getFoldertocreate();
         if (in_array(trim($basefolder), ['.', './', ''])) {
             $basefolder = '/';
         }
         foreach ($export->getElements() as $exportElement) {
             if ($exportElement->isDone()) {
                 continue;
             }
             $base_id = $exportElement->getBaseId();
             $record_id = $exportElement->getRecordId();
             $subdef = $exportElement->getSubdef();
             $localfile = null;
             try {
                 $sbas_id = \phrasea::sbasFromBas($app, $base_id);
                 $record = new \record_adapter($app, $sbas_id, $record_id);
                 $sdcaption = $app['serializer.caption']->serialize($record->get_caption(), CaptionSerializer::SERIALIZE_XML, $exportElement->isBusinessfields());
                 $remotefile = $exportElement->getFilename();
                 if ($subdef == 'caption') {
                     $desc = $app['serializer.caption']->serialize($record->get_caption(), CaptionSerializer::SERIALIZE_XML, $exportElement->isBusinessfields());
                     $localfile = sys_get_temp_dir() . '/' . md5($desc . time() . mt_rand());
                     if (file_put_contents($localfile, $desc) === false) {
                         throw new \Exception('Impossible de creer un fichier temporaire');
                     }
                 } elseif ($subdef == 'caption-yaml') {
                     $desc = $app['serializer.caption']->serialize($record->get_caption(), CaptionSerializer::SERIALIZE_YAML, $exportElement->isBusinessfields());
                     $localfile = sys_get_temp_dir() . '/' . md5($desc . time() . mt_rand());
                     if (file_put_contents($localfile, $desc) === false) {
                         throw new \Exception('Impossible de creer un fichier temporaire');
                     }
                 } else {
                     $sd = $record->get_subdefs();
                     if (!$sd || !isset($sd[$subdef])) {
                         continue;
                     }
                     $localfile = $sd[$subdef]->get_pathfile();
                     if (!file_exists($localfile)) {
                         throw new \Exception('Le fichier local n\'existe pas');
                     }
                 }
                 $current_folder = rtrim(str_replace('//', '/', $basefolder . $exportElement->getFolder()), '/');
                 if ($ftp_client->pwd() != $current_folder) {
                     try {
                         $ftp_client->chdir($current_folder);
                     } catch (\Exception $e) {
                         $this->log('debug', $e->getMessage());
                     }
                 }
                 $ftp_client->put($remotefile, $localfile);
                 $obj[] = ["name" => $subdef, "size" => filesize($localfile), "shortXml" => $sdcaption ? $sdcaption : ''];
                 if ($subdef == 'caption') {
                     unlink($localfile);
                 }
                 $exportElement->setDone(true)->setError(false);
                 $app['EM']->persist($exportElement);
                 $app['EM']->flush();
                 $this->logexport($app, $record, $obj, $ftpLog);
             } catch (\Exception $e) {
                 $state .= $line = $this->translator->trans('task::ftp:File "%file%" (record %record_id%) de la base "%basename%" (Export du Document) : Transfert cancelled (le document n\'existe plus)', ['%file%' => basename($localfile), '%record_id%' => $record_id, '%basename%' => \phrasea::sbas_labels(\phrasea::sbasFromBas($app, $base_id), $app)]) . "\n<br/>";
                 $this->log('debug', $line);
                 // One failure max
                 $exportElement->setDone($exportElement->isError())->setError(true);
                 $app['EM']->persist($exportElement);
                 $app['EM']->flush();
             }
         }
         if ($export->isLogfile()) {
             $this->log('debug', "logfile ");
             $date = new DateTime();
             $buffer = '#transfert finished ' . $date->format(DATE_ATOM) . "\n\n";
             foreach ($export->getElements() as $exportElement) {
                 if (!$exportElement->isDone() || $exportElement->isError()) {
                     continue;
                 }
                 $filename = $exportElement->getFilename();
                 $folder = $exportElement->getFilename();
                 $root = $export->getFoldertocreate();
                 $buffer .= $root . '/' . $folder . $filename . "\n";
             }
             $tmpfile = sys_get_temp_dir() . '/tmpftpbuffer' . $date->format('U') . '.txt';
             file_put_contents($tmpfile, $buffer);
             $remotefile = $date->format('U') . '-transfert.log';
             $ftp_client->chdir($export->getDestFolder());
             $ftp_client->put($remotefile, $tmpfile);
             unlink($tmpfile);
         }
         $ftp_client->close();
     } catch (\Exception $e) {
         $state .= $line = $e . "\n";
         $this->log('debug', $line);
         $export->incrementCrash();
         $app['EM']->persist($export);
         $app['EM']->flush();
     }
     $this->finalize($app, $export);
 }
Ejemplo n.º 16
0
 public function setTaskPropertyAction(Request $request, Task $task)
 {
     $title = $request->get('title');
     $autostart = $request->get('autostart');
     if (null === $title && null === $autostart) {
         return $this->getBadRequestAction($request);
     }
     if ($title) {
         $task->setName($title);
     }
     if ($autostart) {
         $task->setStatus(Task::STATUS_STARTED);
     }
     return $this->showTaskAction($request, $task);
 }
Ejemplo n.º 17
0
 /**
  * Reset the number of crashes of the task.
  *
  * @param Task $task
  *
  * @return Task
  */
 public function resetCrashes(Task $task)
 {
     $task->setCrashed(0);
     $this->om->persist($task);
     $this->om->flush();
     $this->notify(NotifierInterface::MESSAGE_UPDATE);
     return $task;
 }
Ejemplo n.º 18
0
 public function getTask(Application $app, Request $request, Task $task)
 {
     if ('json' === $request->getContentType()) {
         return $app->json(array_replace(['id' => $task->getId(), 'name' => $task->getName(), 'urls' => $this->getTaskResourceUrls($app, $task->getId())], $app['task-manager.live-information']->getTask($task)));
     }
     $editor = $app['task-manager.job-factory']->create($task->getJobId())->getEditor();
     $form = $app->form(new TaskForm());
     $form->setData($task);
     return $app['twig']->render($editor->getTemplatePath(), ['task' => $task, 'form' => $form->createView(), 'view' => 'XML']);
 }