Completed
Push — master ( e411bc...5768ca )
by Nicolas
02:04
created
client_example/ClientCommander.php 1 patch
Braces   +50 added lines, -41 removed lines patch added patch discarded remove patch
@@ -38,8 +38,7 @@  discard block
 block discarded – undo
38 38
         $decodedClient = json_decode($clientInfo);
39 39
         
40 40
         $CpeClientSdk->start_job($decodedClient, $content);
41
-    }
42
-    catch (Exception $e) {
41
+    } catch (Exception $e) {
43 42
         print("[ERROR] " . $e->getMessage() . "\n");
44 43
     }
45 44
 }
@@ -68,42 +67,51 @@  discard block
 block discarded – undo
68 67
     global $argv;
69 68
         
70 69
     // Handle input parameters
71
-    if (!($options = getopt("c:k::s::r::hd")))
72
-        usage();
73
-    if (isset($options['h']))
74
-        usage();
70
+    if (!($options = getopt("c:k::s::r::hd"))) {
71
+            usage();
72
+    }
73
+    if (isset($options['h'])) {
74
+            usage();
75
+    }
75 76
     
76
-    if (isset($options['d']))
77
-        $debug = true;
77
+    if (isset($options['d'])) {
78
+            $debug = true;
79
+    }
78 80
   
79 81
     if (isset($options['c']))
80 82
     {
81 83
         $clientConfFile = $options['c'];
82
-        if (!file_exists($clientConfFile))
83
-            throw new Exception("The client config file is not valid!");
84
-        if (!($clientInfo = file_get_contents($clientConfFile)))
85
-            throw new Exception("Unable to read the file");
86
-    }
87
-    else
88
-        throw new Exception("Please provide the client config file!");
89
-
90
-    if (isset($options['k']))
91
-        $key = $options['k'];
92
-    else
93
-        $key = getenv("AWS_ACCESS_KEY_ID");
84
+        if (!file_exists($clientConfFile)) {
85
+                    throw new Exception("The client config file is not valid!");
86
+        }
87
+        if (!($clientInfo = file_get_contents($clientConfFile))) {
88
+                    throw new Exception("Unable to read the file");
89
+        }
90
+    } else {
91
+            throw new Exception("Please provide the client config file!");
92
+    }
93
+
94
+    if (isset($options['k'])) {
95
+            $key = $options['k'];
96
+    } else {
97
+            $key = getenv("AWS_ACCESS_KEY_ID");
98
+    }
94 99
     
95
-    if (isset($options['s']))
96
-        $secret = $options['s'];
97
-    else
98
-        $secret = getenv("AWS_SECRET_KEY");
99
-
100
-    if (isset($options['r']))
101
-        $region = $options['r'];
102
-    else 
103
-        $region = getenv("AWS_DEFAULT_REGION");
104
-    if (!$region)
105
-        throw new Exception("Please provide your AWS region as parameter or using AWS_DEFAULT_REGION env var !");
106
-}
100
+    if (isset($options['s'])) {
101
+            $secret = $options['s'];
102
+    } else {
103
+            $secret = getenv("AWS_SECRET_KEY");
104
+    }
105
+
106
+    if (isset($options['r'])) {
107
+            $region = $options['r'];
108
+    } else {
109
+            $region = getenv("AWS_DEFAULT_REGION");
110
+    }
111
+    if (!$region) {
112
+            throw new Exception("Please provide your AWS region as parameter or using AWS_DEFAULT_REGION env var !");
113
+    }
114
+    }
107 115
 
108 116
     function usage()
109 117
     {
@@ -123,8 +131,7 @@  discard block
 block discarded – undo
123 131
 
124 132
 try {
125 133
     check_input_parameters();
126
-} 
127
-catch (Exception $e) {
134
+} catch (Exception $e) {
128 135
     print "[ERROR] " . $e->getMessage() . "\n";
129 136
     exit(2);
130 137
 }
@@ -147,14 +154,16 @@  discard block
 block discarded – undo
147 154
 {
148 155
     // Prompt (<3 php)
149 156
     $line = readline("Command [enter]: ");
150
-    if (!$line)
151
-        continue;
157
+    if (!$line) {
158
+            continue;
159
+    }
152 160
     readline_add_history($line);
153 161
 
154 162
     // Process user input
155 163
     $args = explode(" ", $line);
156
-    if (!isset($commandMap[$args[0]]))
157
-        print "[ERROR] Command not found!\n";
158
-    else 
159
-        $commandMap[$args[0]]($args);
160
-}
161 164
\ No newline at end of file
165
+    if (!isset($commandMap[$args[0]])) {
166
+            print "[ERROR] Command not found!\n";
167
+    } else {
168
+            $commandMap[$args[0]]($args);
169
+    }
170
+    }
162 171
\ No newline at end of file
Please login to merge, or discard this patch.
pollers/src/ActivityPoller.php 1 patch
Braces   +28 added lines, -21 removed lines patch added patch discarded remove patch
@@ -98,9 +98,10 @@  discard block
 block discarded – undo
98 98
     public function poll_for_activities()
99 99
     {
100 100
         // Poll from all the taskList registered for each activities 
101
-        if ($this->debug)
102
-            $this->cpeLogger->log_out("DEBUG", basename(__FILE__),
101
+        if ($this->debug) {
102
+                    $this->cpeLogger->log_out("DEBUG", basename(__FILE__),
103 103
                 "Polling activity taskList '" . $this->taskList  . "' ... ");
104
+        }
104 105
             
105 106
         try {
106 107
             // Call SWF and poll for incoming tasks
@@ -124,8 +125,9 @@  discard block
 block discarded – undo
124 125
     {
125 126
         // Get activityType and WorkflowExecution info
126 127
         if (!($activityType      = $activityTask->get("activityType")) ||
127
-            !($workflowExecution = $activityTask->get("workflowExecution")))
128
-            return false;
128
+            !($workflowExecution = $activityTask->get("workflowExecution"))) {
129
+                    return false;
130
+        }
129 131
         
130 132
         $this->cpeLogger->log_out("INFO",
131 133
             basename(__FILE__),
@@ -152,16 +154,18 @@  discard block
 block discarded – undo
152 154
             // Perform input validation
153 155
             $this->activityHandler->do_input_validation();
154 156
 
155
-            if ($this->debug)
156
-                $this->cpeLogger->log_out("DEBUG", basename(__FILE__), 
157
+            if ($this->debug) {
158
+                            $this->cpeLogger->log_out("DEBUG", basename(__FILE__), 
157 159
                     "Activity input:\n" . print_r($this->activityHandler->input, true));
160
+            }
158 161
         
159 162
             // Run activity task
160 163
             $result = $this->activityHandler->do_activity($activityTask);
161 164
 
162
-            if ($this->debug && $result)
163
-                $this->cpeLogger->log_out("DEBUG", basename(__FILE__), 
165
+            if ($this->debug && $result) {
166
+                            $this->cpeLogger->log_out("DEBUG", basename(__FILE__), 
164 167
                     "Activity output:\n" . print_r($result, true));
168
+            }
165 169
         
166 170
         } catch (CpeSdk\CpeException $e) {
167 171
             $reason  = $e->ref;
@@ -216,8 +220,9 @@  discard block
 block discarded – undo
216 220
                 // Instantiate the Activity class that will process Tasks
217 221
 
218 222
                 if (!isset($this->cpeLogger) ||
219
-                    !$this->cpeLogger)
220
-                    print "EMPTY !!!\n";
223
+                    !$this->cpeLogger) {
224
+                                    print "EMPTY !!!\n";
225
+                }
221 226
                 
222 227
                 $this->activityHandler = 
223 228
                     new $activityToHandle->{"class"}(
@@ -287,15 +292,18 @@  discard block
 block discarded – undo
287 292
     global $activityVersion;
288 293
     
289 294
     // Handle input parameters
290
-    if (!($options = getopt("D:T:A:V:c:l:hd")))
291
-        usage($defaultConfigFile);
295
+    if (!($options = getopt("D:T:A:V:c:l:hd"))) {
296
+            usage($defaultConfigFile);
297
+    }
292 298
     
293
-    if (!count($options) || isset($options['h']))
294
-        usage($defaultConfigFile);
299
+    if (!count($options) || isset($options['h'])) {
300
+            usage($defaultConfigFile);
301
+    }
295 302
 
296 303
     // Debug
297
-    if (isset($options['d']))
298
-        $debug = true;
304
+    if (isset($options['d'])) {
305
+            $debug = true;
306
+    }
299 307
 
300 308
     // Domain
301 309
     if (!isset($options['D']))
@@ -353,8 +361,7 @@  discard block
 block discarded – undo
353 361
             print "\n[CONFIG ISSUE]\nConfiguration file '$defaultConfigFile' invalid or non-existant.\n\n[EASY FIX]\nGo to the directory mentioned in the error above and rename the template file 'cpeConfigTemplate.json' to 'cpeConfig.json'. Configure your Activities. As example you have Activities for CloudTranscode already setup in the template. You can declare your Activities and start executing tasks in an SWF workflow.\n";
354 362
             exit(1);
355 363
         }
356
-    }
357
-    catch (Exception $e) {
364
+    } catch (Exception $e) {
358 365
         print $e;
359 366
     }
360 367
             
@@ -374,8 +381,7 @@  discard block
 block discarded – undo
374 381
 // Instantiate ActivityPoller
375 382
 try {
376 383
     $activityPoller = new ActivityPoller($config);
377
-} 
378
-catch (CpeSdk\CpeException $e) {
384
+} catch (CpeSdk\CpeException $e) {
379 385
     $cpeLogger->log_out("FATAL",
380 386
         basename(__FILE__), $e->getMessage());
381 387
     exit(1);
@@ -384,6 +390,7 @@  discard block
 block discarded – undo
384 390
 $cpeLogger->log_out("INFO", basename(__FILE__), "Starting activity tasks polling");
385 391
 
386 392
 // Start polling loop
387
-while (42)
393
+while (42) {
388 394
     $activityPoller->poll_for_activities();
395
+}
389 396
 
Please login to merge, or discard this patch.
pollers/src/InputPoller.php 1 patch
Braces   +32 added lines, -23 removed lines patch added patch discarded remove patch
@@ -86,9 +86,10 @@  discard block
 block discarded – undo
86 86
     // If a msg is received, we pass it to 'handle_input' for processing
87 87
     public function poll_SQS_queues()
88 88
     {
89
-        if (!isset($this->config->{'clients'}))
90
-            throw new CpeSdk\CpeException("Clients configuration invalid. Check the config file or your parameters.",
89
+        if (!isset($this->config->{'clients'})) {
90
+                    throw new CpeSdk\CpeException("Clients configuration invalid. Check the config file or your parameters.",
91 91
                 self::INVALID_CONFIG);
92
+        }
92 93
         
93 94
         // For all clients in config files
94 95
         // We poll from queues
@@ -102,13 +103,14 @@  discard block
 block discarded – undo
102 103
             try {
103 104
                 if ($msg = $this->cpeSqsListener->receive_message($queue, 10))
104 105
                 {
105
-                    if (!($decoded = json_decode($msg['Body'])))
106
-                        $this->cpeLogger->log_out(
106
+                    if (!($decoded = json_decode($msg['Body']))) {
107
+                                            $this->cpeLogger->log_out(
107 108
                             "ERROR", 
108 109
                             basename(__FILE__), 
109 110
                             "JSON data invalid in queue: '$queue'");
110
-                    else                    
111
-                        $this->handle_message($decoded);
111
+                    } else {
112
+                                            $this->handle_message($decoded);
113
+                    }
112 114
                 }
113 115
             } catch (CpeSdk\CpeException $e) {
114 116
                 $this->cpeLogger->log_out(
@@ -123,8 +125,9 @@  discard block
 block discarded – undo
123 125
             }
124 126
             
125 127
             // Message polled. Valid or not, we delete it from SQS
126
-            if ($msg)
127
-                $this->cpeSqsListener->delete_message($queue, $msg);
128
+            if ($msg) {
129
+                            $this->cpeSqsListener->delete_message($queue, $msg);
130
+            }
128 131
         }
129 132
     }
130 133
 
@@ -149,12 +152,13 @@  discard block
 block discarded – undo
149 152
             basename(__FILE__), 
150 153
             "Received message '" . $message->{"type"}  . "'"
151 154
         );
152
-        if ($this->debug)
153
-            $this->cpeLogger->log_out(
155
+        if ($this->debug) {
156
+                    $this->cpeLogger->log_out(
154 157
                 "DEBUG", 
155 158
                 basename(__FILE__), 
156 159
                 "Details:\n" . json_encode($message, JSON_PRETTY_PRINT)
157 160
             );
161
+        }
158 162
 
159 163
         // We call the callback function that handles this message  
160 164
         $this->{$this->typesMap[$message->{"type"}]}($message);
@@ -168,12 +172,13 @@  discard block
 block discarded – undo
168 172
     // Start a new workflow in SWF to initiate new transcoding job
169 173
     private function start_job($message)
170 174
     {
171
-        if ($this->debug)
172
-            $this->cpeLogger->log_out(
175
+        if ($this->debug) {
176
+                    $this->cpeLogger->log_out(
173 177
                 "DEBUG",
174 178
                 basename(__FILE__),
175 179
                 "Starting new workflow!"
176 180
             );
181
+        }
177 182
         
178 183
         // Workflow info
179 184
         $workflowType = array(
@@ -225,13 +230,15 @@  discard block
 block discarded – undo
225 230
             !isset($message->{"time"})   || $message->{"time"} == "" || 
226 231
             !isset($message->{"jobId"})  || $message->{"jobId"} == "" || 
227 232
             !isset($message->{"type"})   || $message->{"type"} == "" || 
228
-            !isset($message->{"data"})   || $message->{"data"} == "")
229
-            throw new CpeSdk\CpeException("'time', 'type', 'jobId' or 'data' fields missing in JSON message file!",
233
+            !isset($message->{"data"})   || $message->{"data"} == "") {
234
+                    throw new CpeSdk\CpeException("'time', 'type', 'jobId' or 'data' fields missing in JSON message file!",
230 235
                 self::INVALID_JSON);
236
+        }
231 237
         
232
-        if (!isset($message->{'data'}->{'workflow'}))
233
-            throw new CpeSdk\CpeException("Input doesn't contain any workflow information. You must provide the workflow you want to send this job to!",
238
+        if (!isset($message->{'data'}->{'workflow'})) {
239
+                    throw new CpeSdk\CpeException("Input doesn't contain any workflow information. You must provide the workflow you want to send this job to!",
234 240
                 self::INVALID_JSON);
241
+        }
235 242
     }
236 243
 }
237 244
 
@@ -262,11 +269,13 @@  discard block
 block discarded – undo
262 269
     // Handle input parameters
263 270
     $options = getopt("l:hdn:");
264 271
 
265
-    if (isset($options['h']))
266
-        usage();
272
+    if (isset($options['h'])) {
273
+            usage();
274
+    }
267 275
     
268
-    if (isset($options['d'])) 
269
-        $debug = true;
276
+    if (isset($options['d'])) {
277
+            $debug = true;
278
+    }
270 279
         
271 280
     $logPath = null;
272 281
     if (isset($options['l']))
@@ -304,8 +313,7 @@  discard block
 block discarded – undo
304 313
 // Create InputPoller object
305 314
 try {
306 315
     $inputPoller = new InputPoller($config);
307
-}
308
-catch (CpeSdk\CpeException $e) {
316
+} catch (CpeSdk\CpeException $e) {
309 317
     echo $e->getMessage();
310 318
     $cpeLogger->log_out(
311 319
         "FATAL", 
@@ -319,5 +327,6 @@  discard block
 block discarded – undo
319 327
 print "Start polling ...\n";
320 328
 
321 329
 // Start polling loop to get incoming commands from SQS input queues
322
-while (42)
330
+while (42) {
323 331
     $inputPoller->poll_SQS_queues();
332
+}
Please login to merge, or discard this patch.