@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | // Check if there is an activity name |
50 | 50 | if (!isset($params["name"]) || !$params["name"]) |
51 | 51 | throw new CpeSdk\CpeException("Can't instantiate BasicActivity: 'name' is not provided or empty !\n", |
52 | - self::NO_ACTIVITY_NAME); |
|
52 | + self::NO_ACTIVITY_NAME); |
|
53 | 53 | |
54 | 54 | if (!$cpeLogger) |
55 | 55 | $this->cpeLogger = new CpeSdk\CpeLogger(null, $params["name"]); |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | // Check if there is a version name |
62 | 62 | if (!isset($params["version"]) || !$params["version"]) |
63 | 63 | throw new CpeSdk\CpeException("Can't instantiate BasicActivity: 'version' is not provided or empty !\n", |
64 | - self::NO_ACTIVITY_VERSION); |
|
64 | + self::NO_ACTIVITY_VERSION); |
|
65 | 65 | |
66 | 66 | // Initialize the activity in SWF if necessary |
67 | 67 | $this->init_activity(); |
@@ -106,12 +106,12 @@ discard block |
||
106 | 106 | { |
107 | 107 | if (!$task) |
108 | 108 | throw new CpeSdk\CpeException("Activity Task empty !", |
109 | - self::ACTIVITY_TASK_EMPTY); |
|
109 | + self::ACTIVITY_TASK_EMPTY); |
|
110 | 110 | |
111 | 111 | if (!isset($task["input"]) || !$task["input"] || |
112 | 112 | $task["input"] == "") |
113 | 113 | throw new CpeSdk\CpeException("No input provided to 'Activity'", |
114 | - self::NO_INPUT); |
|
114 | + self::NO_INPUT); |
|
115 | 115 | |
116 | 116 | // Save input string |
117 | 117 | $this->input_str = $task["input"]; |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | // Check JSON input |
136 | 136 | if (!($this->input = json_decode($this->input_str))) |
137 | 137 | throw new CpeSdk\CpeException("JSON input is invalid !", |
138 | - self::INPUT_INVALID); |
|
138 | + self::INPUT_INVALID); |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | /** |
@@ -11,20 +11,20 @@ discard block |
||
11 | 11 | |
12 | 12 | class CpeActivity |
13 | 13 | { |
14 | - public $params; // Activity params coming from ActivityPoller |
|
15 | - public $debug; // Debug flag |
|
14 | + public $params; // Activity params coming from ActivityPoller |
|
15 | + public $debug; // Debug flag |
|
16 | 16 | |
17 | - public $activityId; // ID of the activity |
|
18 | - public $activityType; // Type of activity |
|
17 | + public $activityId; // ID of the activity |
|
18 | + public $activityType; // Type of activity |
|
19 | 19 | |
20 | - public $cpeLogger; // Logger |
|
21 | - public $cpeSqsWriter; // Used to write messages in SQS |
|
22 | - public $cpeSwfHandler; // USed to control SWF |
|
23 | - public $cpeJsonValidator;// Run JSON schemas validation |
|
20 | + public $cpeLogger; // Logger |
|
21 | + public $cpeSqsWriter; // Used to write messages in SQS |
|
22 | + public $cpeSwfHandler; // USed to control SWF |
|
23 | + public $cpeJsonValidator; // Run JSON schemas validation |
|
24 | 24 | |
25 | - public $input_str; // Complete activity input string |
|
26 | - public $input; // Complete activity input JSON object |
|
27 | - public $activityLogKey; // Create a key workflowId:activityId to put in logs |
|
25 | + public $input_str; // Complete activity input string |
|
26 | + public $input; // Complete activity input JSON object |
|
27 | + public $activityLogKey; // Create a key workflowId:activityId to put in logs |
|
28 | 28 | |
29 | 29 | const HEARTBEAT_FAILED = "HEARTBEAT_FAILED"; |
30 | 30 | const NO_ACTIVITY_NAME = "NO_ACTIVITY_NAME"; |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | // For listening to the Input SQS queue |
45 | 45 | $this->cpeJsonValidator = new CpeSdk\CpeJsonValidator(); |
46 | 46 | // Save activity params |
47 | - $this->params = $params; |
|
47 | + $this->params = $params; |
|
48 | 48 | |
49 | 49 | // Check if there is an activity name |
50 | 50 | if (!isset($params["name"]) || !$params["name"]) |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | return true; |
91 | 91 | } catch (\Aws\Swf\Exception\UnknownResourceException $e) { |
92 | 92 | $this->cpeLogger->log_out("ERROR", basename(__FILE__), |
93 | - "Activity '" . $this->params["name"] . "' doesn't exists. Creating it ...\n"); |
|
93 | + "Activity '".$this->params["name"]."' doesn't exists. Creating it ...\n"); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | // Register activites if doesn't exists in SWF |
@@ -170,7 +170,7 @@ discard block |
||
170 | 170 | )); |
171 | 171 | } catch (\Exception $e) { |
172 | 172 | $this->cpeLogger->log_out("ERROR", basename(__FILE__), |
173 | - "Unable to send 'Task Failed' response ! " . $e->getMessage(), |
|
173 | + "Unable to send 'Task Failed' response ! ".$e->getMessage(), |
|
174 | 174 | $this->activityLogKey); |
175 | 175 | return false; |
176 | 176 | } |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | )); |
196 | 196 | } catch (\Exception $e) { |
197 | 197 | $this->cpeLogger->log_out("ERROR", basename(__FILE__), |
198 | - "Unable to send 'Task Completed' response ! " . $e->getMessage(), |
|
198 | + "Unable to send 'Task Completed' response ! ".$e->getMessage(), |
|
199 | 199 | $this->activityLogKey); |
200 | 200 | return false; |
201 | 201 | } |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | if ($info->get("cancelRequested") == true) |
224 | 224 | { |
225 | 225 | $this->cpeLogger->log_out("WARNING", basename(__FILE__), |
226 | - "Cancel has been requested for this task '" . $task->get("activityId") . "' ! Killing task ...", |
|
226 | + "Cancel has been requested for this task '".$task->get("activityId")."' ! Killing task ...", |
|
227 | 227 | $this->activityLogKey); |
228 | 228 | throw new CpeSdk\CpeException("Cancel request. No heartbeat, leaving!", |
229 | 229 | self::HEARTBEAT_FAILED); |
@@ -47,21 +47,24 @@ discard block |
||
47 | 47 | $this->params = $params; |
48 | 48 | |
49 | 49 | // Check if there is an activity name |
50 | - if (!isset($params["name"]) || !$params["name"]) |
|
51 | - throw new CpeSdk\CpeException("Can't instantiate BasicActivity: 'name' is not provided or empty !\n", |
|
50 | + if (!isset($params["name"]) || !$params["name"]) { |
|
51 | + throw new CpeSdk\CpeException("Can't instantiate BasicActivity: 'name' is not provided or empty !\n", |
|
52 | 52 | self::NO_ACTIVITY_NAME); |
53 | + } |
|
53 | 54 | |
54 | - if (!$cpeLogger) |
|
55 | - $this->cpeLogger = new CpeSdk\CpeLogger(null, $params["name"], $debug); |
|
56 | - else |
|
57 | - $this->cpeLogger = $cpeLogger; |
|
55 | + if (!$cpeLogger) { |
|
56 | + $this->cpeLogger = new CpeSdk\CpeLogger(null, $params["name"], $debug); |
|
57 | + } else { |
|
58 | + $this->cpeLogger = $cpeLogger; |
|
59 | + } |
|
58 | 60 | |
59 | 61 | // Create logger object. Use activity name for logger |
60 | 62 | |
61 | 63 | // Check if there is a version name |
62 | - if (!isset($params["version"]) || !$params["version"]) |
|
63 | - throw new CpeSdk\CpeException("Can't instantiate BasicActivity: 'version' is not provided or empty !\n", |
|
64 | + if (!isset($params["version"]) || !$params["version"]) { |
|
65 | + throw new CpeSdk\CpeException("Can't instantiate BasicActivity: 'version' is not provided or empty !\n", |
|
64 | 66 | self::NO_ACTIVITY_VERSION); |
67 | + } |
|
65 | 68 | |
66 | 69 | // Initialize the activity in SWF if necessary |
67 | 70 | $this->init_activity(); |
@@ -104,14 +107,16 @@ discard block |
||
104 | 107 | */ |
105 | 108 | public function do_task_check($task) |
106 | 109 | { |
107 | - if (!$task) |
|
108 | - throw new CpeSdk\CpeException("Activity Task empty !", |
|
109 | - self::ACTIVITY_TASK_EMPTY); |
|
110 | + if (!$task) { |
|
111 | + throw new CpeSdk\CpeException("Activity Task empty !", |
|
112 | + self::ACTIVITY_TASK_EMPTY); |
|
113 | + } |
|
110 | 114 | |
111 | 115 | if (!isset($task["input"]) || !$task["input"] || |
112 | - $task["input"] == "") |
|
113 | - throw new CpeSdk\CpeException("No input provided to 'Activity'", |
|
116 | + $task["input"] == "") { |
|
117 | + throw new CpeSdk\CpeException("No input provided to 'Activity'", |
|
114 | 118 | self::NO_INPUT); |
119 | + } |
|
115 | 120 | |
116 | 121 | // Save input string |
117 | 122 | $this->input_str = $task["input"]; |
@@ -133,9 +138,10 @@ discard block |
||
133 | 138 | public function do_input_validation() |
134 | 139 | { |
135 | 140 | // Check JSON input |
136 | - if (!($this->input = json_decode($this->input_str))) |
|
137 | - throw new CpeSdk\CpeException("JSON input is invalid !", |
|
141 | + if (!($this->input = json_decode($this->input_str))) { |
|
142 | + throw new CpeSdk\CpeException("JSON input is invalid !", |
|
138 | 143 | self::INPUT_INVALID); |
144 | + } |
|
139 | 145 | } |
140 | 146 | |
141 | 147 | /** |
@@ -16,6 +16,6 @@ |
||
16 | 16 | |
17 | 17 | // custom string representation of object |
18 | 18 | public function __toString() { |
19 | - return __CLASS__ . ": [{$this->ref}]: {$this->message}\n"; |
|
19 | + return __CLASS__.": [{$this->ref}]: {$this->message}\n"; |
|
20 | 20 | } |
21 | 21 | } |
@@ -10,10 +10,10 @@ |
||
10 | 10 | $schemas_path) |
11 | 11 | { |
12 | 12 | $retriever = new JsonSchema\Uri\UriRetriever; |
13 | - $json_schemas = $retriever->retrieve('file://' . $schemas_path . "/$schemas_name"); |
|
13 | + $json_schemas = $retriever->retrieve('file://'.$schemas_path."/$schemas_name"); |
|
14 | 14 | |
15 | 15 | $refResolver = new JsonSchema\RefResolver($retriever); |
16 | - $refResolver->resolve($json_schemas, 'file://' . $schemas_path . "/"); |
|
16 | + $refResolver->resolve($json_schemas, 'file://'.$schemas_path."/"); |
|
17 | 17 | |
18 | 18 | $validator = new JsonSchema\Validator(); |
19 | 19 | $validator->check($json, $json_schemas); |
@@ -18,12 +18,14 @@ |
||
18 | 18 | $validator = new JsonSchema\Validator(); |
19 | 19 | $validator->check($json, $json_schemas); |
20 | 20 | |
21 | - if ($validator->isValid()) |
|
22 | - return false; |
|
21 | + if ($validator->isValid()) { |
|
22 | + return false; |
|
23 | + } |
|
23 | 24 | |
24 | 25 | $details = ""; |
25 | - foreach ($validator->getErrors() as $error) |
|
26 | - $details .= sprintf("[%s] %s\n", $error['property'], $error['message']); |
|
26 | + foreach ($validator->getErrors() as $error) { |
|
27 | + $details .= sprintf("[%s] %s\n", $error['property'], $error['message']); |
|
28 | + } |
|
27 | 29 | |
28 | 30 | return $details; |
29 | 31 | } |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | $log["workflowId"] = $workflowId; |
55 | 55 | |
56 | 56 | // Open Syslog. Use programe name as key |
57 | - if (!openlog (__FILE__, LOG_CONS|LOG_PID, LOG_LOCAL1)) |
|
57 | + if (!openlog(__FILE__, LOG_CONS|LOG_PID, LOG_LOCAL1)) |
|
58 | 58 | throw new CpeException("Unable to connect to Syslog!", |
59 | 59 | OPENLOG_ERROR); |
60 | 60 | |
@@ -96,11 +96,11 @@ discard block |
||
96 | 96 | if (!is_string($log['message'])) |
97 | 97 | $log['message'] = json_encode($log['message']); |
98 | 98 | |
99 | - $toPrint = $log['time'] . " [" . $log['type'] . "] [" . $log['source'] . "] "; |
|
99 | + $toPrint = $log['time']." [".$log['type']."] [".$log['source']."] "; |
|
100 | 100 | // If there is a workflow ID. We append it. |
101 | 101 | if ($workflowId) |
102 | 102 | $toPrint .= "[$workflowId] "; |
103 | - $toPrint .= $log['message'] . "\n"; |
|
103 | + $toPrint .= $log['message']."\n"; |
|
104 | 104 | |
105 | 105 | if (file_put_contents( |
106 | 106 | $this->logPath, |
@@ -28,15 +28,18 @@ discard block |
||
28 | 28 | $this->printout = $printout; |
29 | 29 | $this->logPath = "/var/tmp/logs/cpe/"; |
30 | 30 | |
31 | - if ($logPath) |
|
32 | - $this->logPath = $logPath; |
|
31 | + if ($logPath) { |
|
32 | + $this->logPath = $logPath; |
|
33 | + } |
|
33 | 34 | |
34 | - if (!file_exists($this->logPath)) |
|
35 | - mkdir($this->logPath, 0755, true); |
|
35 | + if (!file_exists($this->logPath)) { |
|
36 | + mkdir($this->logPath, 0755, true); |
|
37 | + } |
|
36 | 38 | |
37 | 39 | $file = basename($argv[0]); |
38 | - if ($suffix) |
|
39 | - $file .= "-".$suffix; |
|
40 | + if ($suffix) { |
|
41 | + $file .= "-".$suffix; |
|
42 | + } |
|
40 | 43 | // Append progname to the path |
41 | 44 | $this->logPath .= "/".$file.".log"; |
42 | 45 | |
@@ -60,13 +63,15 @@ discard block |
||
60 | 63 | "message" => $message |
61 | 64 | ]; |
62 | 65 | |
63 | - if ($workflowId) |
|
64 | - $log["workflowId"] = $workflowId; |
|
66 | + if ($workflowId) { |
|
67 | + $log["workflowId"] = $workflowId; |
|
68 | + } |
|
65 | 69 | |
66 | 70 | // Open Syslog. Use programe name as key |
67 | - if (!openlog (__FILE__, LOG_CONS|LOG_PID, LOG_LOCAL1)) |
|
68 | - throw new CpeException("Unable to connect to Syslog!", |
|
71 | + if (!openlog (__FILE__, LOG_CONS|LOG_PID, LOG_LOCAL1)) { |
|
72 | + throw new CpeException("Unable to connect to Syslog!", |
|
69 | 73 | OPENLOG_ERROR); |
74 | + } |
|
70 | 75 | |
71 | 76 | // Change Syslog priority level |
72 | 77 | switch ($type) |
@@ -103,22 +108,26 @@ discard block |
||
103 | 108 | // Write log in file |
104 | 109 | private function print_to_file($log, $workflowId) |
105 | 110 | { |
106 | - if (!is_string($log['message'])) |
|
107 | - $log['message'] = json_encode($log['message']); |
|
111 | + if (!is_string($log['message'])) { |
|
112 | + $log['message'] = json_encode($log['message']); |
|
113 | + } |
|
108 | 114 | |
109 | 115 | $toPrint = $log['time'] . " [" . $log['type'] . "] [" . $log['source'] . "] "; |
110 | 116 | // If there is a workflow ID. We append it. |
111 | - if ($workflowId) |
|
112 | - $toPrint .= "[$workflowId] "; |
|
117 | + if ($workflowId) { |
|
118 | + $toPrint .= "[$workflowId] "; |
|
119 | + } |
|
113 | 120 | $toPrint .= $log['message'] . "\n"; |
114 | 121 | |
115 | - if ($this->printout) |
|
116 | - print $toPrint; |
|
122 | + if ($this->printout) { |
|
123 | + print $toPrint; |
|
124 | + } |
|
117 | 125 | |
118 | 126 | if (file_put_contents( |
119 | 127 | $this->logPath, |
120 | 128 | $toPrint, |
121 | - FILE_APPEND) === false) |
|
122 | - print "ERROR: Can't write into log file!\n"; |
|
129 | + FILE_APPEND) === false) { |
|
130 | + print "ERROR: Can't write into log file!\n"; |
|
131 | + } |
|
123 | 132 | } |
124 | 133 | } |
125 | 134 | \ No newline at end of file |
@@ -31,10 +31,11 @@ discard block |
||
31 | 31 | $this->sqs = $aws->get('Sqs'); |
32 | 32 | |
33 | 33 | // Logger |
34 | - if (!$cpeLogger) |
|
35 | - $this->cpeLogger = new CpeSdk\CpeLogger(); |
|
36 | - else |
|
37 | - $this->cpeLogger = $cpeLogger; |
|
34 | + if (!$cpeLogger) { |
|
35 | + $this->cpeLogger = new CpeSdk\CpeLogger(); |
|
36 | + } else { |
|
37 | + $this->cpeLogger = $cpeLogger; |
|
38 | + } |
|
38 | 39 | } |
39 | 40 | |
40 | 41 | /** |
@@ -45,12 +46,13 @@ discard block |
||
45 | 46 | // Poll one message at a time from the provided SQS queue |
46 | 47 | public function receive_message($queue, $timeout) |
47 | 48 | { |
48 | - if ($this->debug) |
|
49 | - $this->cpeLogger->log_out( |
|
49 | + if ($this->debug) { |
|
50 | + $this->cpeLogger->log_out( |
|
50 | 51 | "DEBUG", |
51 | 52 | basename(__FILE__), |
52 | 53 | "Polling from '$queue' ..." |
53 | 54 | ); |
55 | + } |
|
54 | 56 | |
55 | 57 | // Poll from SQS to check for new message |
56 | 58 | $result = $this->sqs->receiveMessage(array( |
@@ -62,12 +64,13 @@ discard block |
||
62 | 64 | if (($messages = $result->get('Messages')) && |
63 | 65 | count($messages)) |
64 | 66 | { |
65 | - if ($this->debug) |
|
66 | - $this->cpeLogger->log_out( |
|
67 | + if ($this->debug) { |
|
68 | + $this->cpeLogger->log_out( |
|
67 | 69 | "DEBUG", |
68 | 70 | basename(__FILE__), |
69 | 71 | "New messages recieved in queue: '$queue'" |
70 | 72 | ); |
73 | + } |
|
71 | 74 | |
72 | 75 | return $messages[0]; |
73 | 76 | } |
@@ -175,9 +175,10 @@ discard block |
||
175 | 175 | $sendInput = null, |
176 | 176 | $result = null) |
177 | 177 | { |
178 | - if (!($input = json_decode($activityTask->get('input')))) |
|
179 | - throw new CpeException("Task input JSON is invalid!\n".$activityTask->get('input'), |
|
178 | + if (!($input = json_decode($activityTask->get('input')))) { |
|
179 | + throw new CpeException("Task input JSON is invalid!\n".$activityTask->get('input'), |
|
180 | 180 | INVALID_JSON); |
181 | + } |
|
181 | 182 | |
182 | 183 | $activity = [ |
183 | 184 | 'activityId' => $activityTask->get('activityId'), |
@@ -185,12 +186,14 @@ discard block |
||
185 | 186 | ]; |
186 | 187 | |
187 | 188 | // Want to send back the input data ? |
188 | - if ($sendInput) |
|
189 | - $activity['input'] = $input; |
|
189 | + if ($sendInput) { |
|
190 | + $activity['input'] = $input; |
|
191 | + } |
|
190 | 192 | |
191 | 193 | // Extra data? Concat to data array. |
192 | - if ($result) |
|
193 | - $activity['result'] = $result; |
|
194 | + if ($result) { |
|
195 | + $activity['result'] = $result; |
|
196 | + } |
|
194 | 197 | |
195 | 198 | // Initial data structure |
196 | 199 | $data = array( |
@@ -98,6 +98,10 @@ discard block |
||
98 | 98 | ); |
99 | 99 | } |
100 | 100 | |
101 | + /** |
|
102 | + * @param string $reason |
|
103 | + * @param string $details |
|
104 | + */ |
|
101 | 105 | public function activity_failed($task, $reason, $details) |
102 | 106 | { |
103 | 107 | $this->send_activity_msg( |
@@ -169,6 +173,11 @@ discard block |
||
169 | 173 | } |
170 | 174 | |
171 | 175 | // Send a message to SQS output queue |
176 | + |
|
177 | + /** |
|
178 | + * @param string $eventType |
|
179 | + * @param boolean $sendInput |
|
180 | + */ |
|
172 | 181 | private function send_activity_msg( |
173 | 182 | $activityTask, |
174 | 183 | $eventType, |
@@ -18,8 +18,9 @@ |
||
18 | 18 | public function __construct() |
19 | 19 | { |
20 | 20 | # Check if preper env vars are setup |
21 | - if (!($region = getenv("AWS_DEFAULT_REGION"))) |
|
22 | - throw new CpeSdk\CpeException("Set 'AWS_DEFAULT_REGION' environment variable!"); |
|
21 | + if (!($region = getenv("AWS_DEFAULT_REGION"))) { |
|
22 | + throw new CpeSdk\CpeException("Set 'AWS_DEFAULT_REGION' environment variable!"); |
|
23 | + } |
|
23 | 24 | |
24 | 25 | // SWF client |
25 | 26 | $this->swf = SwfClient::factory(array( |