Преглед на файлове

chmod script tasklogger. symfony/process

Guillermo Espinoza преди 8 години
родител
ревизия
6a00624fc3
променени са 1 файла, в които са добавени 47 реда и са изтрити 10 реда
  1. 47 10
      Services/TaskLoggerService.php

+ 47 - 10
Services/TaskLoggerService.php

@@ -4,6 +4,8 @@ namespace WorkflowBundle\Services;
 
 use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
 use PhpAmqpLib\Message\AMQPMessage;
+use Symfony\Component\Process\Process;
+use Symfony\Component\Process\Exception\ProcessFailedException;
 
 class TaskLoggerService implements ConsumerInterface
 {
@@ -20,18 +22,53 @@ class TaskLoggerService implements ConsumerInterface
         if (isset($data['id']) && isset($data['cmd'])) {
             $taskloggerId = $data['id'];
             $cmd = $data['cmd'];
-            $taskloggerDir = "/tmp/tasklogger/{$taskloggerId}/";
-            try {
-                if (!file_exists($taskloggerDir)) {
-                    mkdir($taskloggerDir, 0777, true);
-                }
-                file_put_contents($taskloggerDir.'cmd.sh', $cmd);
-            } catch (\Exception $ex) {
-                throw $ex;
-            }
+                        
+            $file_name = $this->createTaskLoggerCmdFile($taskloggerId, $cmd);
+            $output = $this->runFileProcess($file_name);
+
+            return true;
+        }
+        
+        return false;
+    }
+    
+    /**
+     * @param string $taskloggerId
+     * 
+     * @return string
+     */
+    public function createTaskLoggerCmdFile($taskloggerId, $cmd)
+    {
+        $mode = 0777;
+        $tasklogger_dir = "/tmp/tasklogger/{$taskloggerId}/";
+        if (!file_exists($tasklogger_dir)) {
+            mkdir($tasklogger_dir, $mode, true);
+        }
+        
+        $file_name = $tasklogger_dir.'cmd.sh';
+        file_put_contents($file_name, $cmd);
+        chmod($file_name, $mode);
+        
+        return $file_name;
+    }
+    
+    /**
+     * @param string $filename
+     * 
+     * @return string
+     */
+    public function runFileProcess($filename)
+    {
+        $process = new Process($filename);
+        $output = null;
+        try {
+            $process->mustRun();
+            $output = $process->getOutput();
+        } catch (ProcessFailedException $e) {
+            $output = $e->getMessage();
         }
         
-        return true;
+        return $output;
     }
 
 }