Completed
Push — master ( 72bd53...6289d3 )
by Nicolas
01:58
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/InputPoller.php 1 patch
Braces   +35 added lines, -25 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(
@@ -192,9 +197,10 @@  discard block
 block discarded – undo
192 197
                 "taskList"     => array("name" => $message->{'data'}->{'workflow'}->{'taskList'}),
193 198
                 "input"        => json_encode($message->{'data'})
194 199
             );
195
-            if (isset($message->{'data'}->{'workflow'}->{'executionStartToCloseTimeout'})) 
196
-                $payload["executionStartToCloseTimeout"] =
200
+            if (isset($message->{'data'}->{'workflow'}->{'executionStartToCloseTimeout'})) {
201
+                            $payload["executionStartToCloseTimeout"] =
197 202
                     $message->{'data'}->{'workflow'}->{'executionStartToCloseTimeout'};
203
+            }
198 204
             $workflowRunId = $this->cpeSwfHandler->swf->startWorkflowExecution($payload);
199 205
 
200 206
             $this->cpeLogger->log_out(
@@ -229,13 +235,15 @@  discard block
 block discarded – undo
229 235
             !isset($message->{"time"})   || $message->{"time"} == "" || 
230 236
             !isset($message->{"jobId"})  || $message->{"jobId"} == "" || 
231 237
             !isset($message->{"type"})   || $message->{"type"} == "" || 
232
-            !isset($message->{"data"})   || $message->{"data"} == "")
233
-            throw new CpeSdk\CpeException("'time', 'type', 'jobId' or 'data' fields missing in JSON message file!",
238
+            !isset($message->{"data"})   || $message->{"data"} == "") {
239
+                    throw new CpeSdk\CpeException("'time', 'type', 'jobId' or 'data' fields missing in JSON message file!",
234 240
                 self::INVALID_JSON);
241
+        }
235 242
         
236
-        if (!isset($message->{'data'}->{'workflow'}))
237
-            throw new CpeSdk\CpeException("Input doesn't contain any workflow information. You must provide the workflow you want to send this job to!",
243
+        if (!isset($message->{'data'}->{'workflow'})) {
244
+                    throw new CpeSdk\CpeException("Input doesn't contain any workflow information. You must provide the workflow you want to send this job to!",
238 245
                 self::INVALID_JSON);
246
+        }
239 247
     }
240 248
 }
241 249
 
@@ -266,11 +274,13 @@  discard block
 block discarded – undo
266 274
     // Handle input parameters
267 275
     $options = getopt("l:hdn:");
268 276
 
269
-    if (isset($options['h']))
270
-        usage();
277
+    if (isset($options['h'])) {
278
+            usage();
279
+    }
271 280
     
272
-    if (isset($options['d'])) 
273
-        $debug = true;
281
+    if (isset($options['d'])) {
282
+            $debug = true;
283
+    }
274 284
         
275 285
     $logPath = null;
276 286
     if (isset($options['l']))
@@ -308,8 +318,7 @@  discard block
 block discarded – undo
308 318
 // Create InputPoller object
309 319
 try {
310 320
     $inputPoller = new InputPoller($config);
311
-}
312
-catch (CpeSdk\CpeException $e) {
321
+} catch (CpeSdk\CpeException $e) {
313 322
     echo $e->getMessage();
314 323
     $cpeLogger->log_out(
315 324
         "FATAL", 
@@ -323,5 +332,6 @@  discard block
 block discarded – undo
323 332
 print "Start polling ...\n";
324 333
 
325 334
 // Start polling loop to get incoming commands from SQS input queues
326
-while (42)
335
+while (42) {
327 336
     $inputPoller->poll_SQS_queues();
337
+}
Please login to merge, or discard this patch.
pollers/src/ActivityPoller.php 1 patch
Braces   +40 added lines, -29 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,26 +220,31 @@  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
                 $params = [
223 228
                     "domain"  => $this->domain,
224 229
                     "name"    => $activityToHandle->{"name"},
225 230
                     "version" => $activityToHandle->{"version"}
226 231
                 ];
227
-                if (isset($activityToHandle->{"defaultTaskStartToCloseTimeout"}))
228
-                    $params["defaultTaskStartToCloseTimeout"] =
232
+                if (isset($activityToHandle->{"defaultTaskStartToCloseTimeout"})) {
233
+                                    $params["defaultTaskStartToCloseTimeout"] =
229 234
                         $activityToHandle->{"defaultTaskStartToCloseTimeout"};
230
-                if (isset($activityToHandle->{"defaultTaskHeartbeatTimeout"}))
231
-                    $params["defaultTaskHeartbeatTimeout"] =
235
+                }
236
+                if (isset($activityToHandle->{"defaultTaskHeartbeatTimeout"})) {
237
+                                    $params["defaultTaskHeartbeatTimeout"] =
232 238
                         $activityToHandle->{"defaultTaskHeartbeatTimeout"};
233
-                if (isset($activityToHandle->{"defaultTaskScheduleToStartTimeout"}))
234
-                    $params["defaultTaskScheduleToStartTimeout"] =
239
+                }
240
+                if (isset($activityToHandle->{"defaultTaskScheduleToStartTimeout"})) {
241
+                                    $params["defaultTaskScheduleToStartTimeout"] =
235 242
                         $activityToHandle->{"defaultTaskScheduleToStartTimeout"};
236
-                if (isset($activityToHandle->{"defaultTaskScheduleToCloseTimeout"}))
237
-                    $params["defaultTaskScheduleToCloseTimeout"] =
243
+                }
244
+                if (isset($activityToHandle->{"defaultTaskScheduleToCloseTimeout"})) {
245
+                                    $params["defaultTaskScheduleToCloseTimeout"] =
238 246
                         $activityToHandle->{"defaultTaskScheduleToCloseTimeout"};
247
+                }
239 248
                 
240 249
                 $this->activityHandler = 
241 250
                     new $activityToHandle->{"class"}(
@@ -301,15 +310,18 @@  discard block
 block discarded – undo
301 310
     global $activityVersion;
302 311
     
303 312
     // Handle input parameters
304
-    if (!($options = getopt("D:T:A:V:c:l:hd")))
305
-        usage($defaultConfigFile);
313
+    if (!($options = getopt("D:T:A:V:c:l:hd"))) {
314
+            usage($defaultConfigFile);
315
+    }
306 316
     
307
-    if (!count($options) || isset($options['h']))
308
-        usage($defaultConfigFile);
317
+    if (!count($options) || isset($options['h'])) {
318
+            usage($defaultConfigFile);
319
+    }
309 320
 
310 321
     // Debug
311
-    if (isset($options['d']))
312
-        $debug = true;
322
+    if (isset($options['d'])) {
323
+            $debug = true;
324
+    }
313 325
 
314 326
     // Domain
315 327
     if (!isset($options['D']))
@@ -367,8 +379,7 @@  discard block
 block discarded – undo
367 379
             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";
368 380
             exit(1);
369 381
         }
370
-    }
371
-    catch (Exception $e) {
382
+    } catch (Exception $e) {
372 383
         print $e;
373 384
     }
374 385
             
@@ -388,8 +399,7 @@  discard block
 block discarded – undo
388 399
 // Instantiate ActivityPoller
389 400
 try {
390 401
     $activityPoller = new ActivityPoller($config);
391
-} 
392
-catch (CpeSdk\CpeException $e) {
402
+} catch (CpeSdk\CpeException $e) {
393 403
     $cpeLogger->log_out("FATAL",
394 404
         basename(__FILE__), $e->getMessage());
395 405
     exit(1);
@@ -398,6 +408,7 @@  discard block
 block discarded – undo
398 408
 $cpeLogger->log_out("INFO", basename(__FILE__), "Starting activity tasks polling");
399 409
 
400 410
 // Start polling loop
401
-while (42)
411
+while (42) {
402 412
     $activityPoller->poll_for_activities();
413
+}
403 414
 
Please login to merge, or discard this patch.