Skip to content

Commit 2dae56b

Browse files
Merge pull request #45 from wunderio/feature/44-Better-handling-for-Memcache
Feature/44 better handling for memcache
2 parents e35989b + e39ff85 commit 2dae56b

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

.lando/run

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export PATH="$PATH:/app/drupal/vendor/bin"
1010
cd "/app/drupal"
1111

1212
set +e
13-
drush status bootstrap | grep -q Successful
13+
drush status | grep -q -e 'Drupal bootstrap *: *Successful'
1414
status="$?"
1515
set -e
1616
if [[ "$status" -ne 0 ]]; then

_ping.php

+37-9
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ public function run(): void {
5252
* Setup
5353
*/
5454

55+
// Start output buffering as any PHP notice would return 503 error code
56+
// if printed before setting headers.
57+
ob_start();
58+
5559
// Start profiling as early as possible.
5660
$this->profile = new Profile();
5761
$this->status = new Status();
@@ -101,15 +105,20 @@ public function run(): void {
101105

102106
$slows = $this->profile->getByDuration(1000, NULL);
103107
$payloads = $this->profile2logs($slows, 'slow');
104-
$this->logErrors($payloads);
108+
$this->logErrors($payloads, 'notice');
105109

106110
$payloads = $this->status->getBySeverity('warning');
107111
$payloads = $this->status2logs($payloads, 'warning');
108-
$this->logErrors($payloads);
112+
$this->logErrors($payloads, 'warning');
109113

110114
$payloads = $this->status->getBySeverity('error');
111115
$payloads = $this->status2logs($payloads, 'error');
112-
$this->logErrors($payloads);
116+
$this->logErrors($payloads, 'error');
117+
118+
// Stop buffering before setting headers. After that it doesn't matter
119+
// if there's any output as script is not going to give 503 error code
120+
// anymore.
121+
$buffered_output = ob_get_clean();
113122

114123
if (!empty($payloads)) {
115124
$code = 500;
@@ -142,6 +151,10 @@ public function run(): void {
142151
$profiling_tbl = $this->profile->getTextTable(PHP_EOL);
143152
print <<<TXT
144153
154+
<pre style="color:red">
155+
$buffered_output
156+
</pre>
157+
145158
<pre>
146159
$status_tbl
147160
</pre>
@@ -279,14 +292,17 @@ public function profile2logs(array $slows, string $status): array {
279292
* Log errors according to the environment.
280293
*
281294
* We recognize following envs:
295+
* - drupal -> Drupal logger.
282296
* - silta -> stderr.
283297
* - lando -> stderr.
284298
* - the rest -> syslog().
285299
*
286300
* @param array $payloads
287301
* Array of payload arrays, containing error message and additional info.
302+
* @param string $severity
303+
* The severity of Drupal logger (method name).
288304
*/
289-
public function logErrors(array $payloads): void {
305+
public function logErrors(array $payloads, string $severity): void {
290306

291307
if (!empty(getenv('TESTING'))) {
292308
$logger = function (string $msg) {
@@ -297,20 +313,32 @@ public function logErrors(array $payloads): void {
297313
$_logs[] = $msg;
298314
};
299315
}
316+
elseif (method_exists('\Drupal', 'logger')) {
317+
$logger = function (string $msg) use ($severity) {
318+
try {
319+
// Replace curly braces with double parentheses because Drupal logging
320+
// is unable to handle JSON.
321+
$msg = str_replace(['{', '}'], ['((', '))'], $msg);
322+
\Drupal::logger('drupal_ping')->{$severity}($msg);
323+
}
324+
catch (\Exception $e) {
325+
}
326+
};
327+
}
300328
elseif (!empty(getenv('SILTA_CLUSTER')) || !empty(getenv('LANDO'))) {
301329
$logger = function (string $msg) {
302-
error_log($msg);
330+
error_log("ping: $msg");
303331
};
304332
}
305333
else {
306334
$logger = function (string $msg) {
307-
syslog(LOG_ERR | LOG_LOCAL6, $msg);
335+
syslog(LOG_ERR | LOG_LOCAL6, "ping: $msg");
308336
};
309337
}
310338

311339
foreach ($payloads as $payload) {
312340
$payload = json_encode($payload);
313-
$logger("ping: $payload");
341+
$logger($payload);
314342
}
315343
}
316344

@@ -709,7 +737,7 @@ protected function check2(): void {
709737

710738
// We are not relying on Memcache or Memcached classes.
711739
// For speed and simplicity we use just basic networking.
712-
$socket = fsockopen($s['host'], $s['port'], $errno, $errstr, 1);
740+
$socket = @fsockopen($s['host'], $s['port'], $errno, $errstr, 1);
713741
if (!empty($errstr)) {
714742
$msgs[] = [
715743
'host' => $s['host'],
@@ -750,7 +778,7 @@ protected function check2(): void {
750778
}
751779

752780
if ($good_count < 1 && $bad_count > 0) {
753-
$this->setStatus('error', 'Connection errors.', ['errors' => $msgs]);
781+
$this->setStatus('warning', 'Connection errors.', ['errors' => $msgs]);
754782
return;
755783
}
756784

tests/AppTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ public function testlogErrors(): void {
4141
$a->logErrors([
4242
['check1' => 'msg1'],
4343
['check2' => 'msg2'],
44-
]);
44+
], 'notice');
4545
$expected = [
46-
'ping: {"check1":"msg1"}',
47-
'ping: {"check2":"msg2"}',
46+
'{"check1":"msg1"}',
47+
'{"check2":"msg2"}',
4848
];
4949
$this->assertEquals($expected, $_logs);
5050
}

tests/MemcacheCheckerTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function testCheckErrorPort(): void {
108108
'error' => 'Connection refused',
109109
]],
110110
];
111-
$this->assertEquals(['error', $data], $status);
111+
$this->assertEquals(['warning', $data], $status);
112112
}
113113

114114
/**
@@ -133,7 +133,7 @@ public function testCheckErrorHost(): void {
133133
'error' => 'Cannot assign requested address',
134134
]],
135135
];
136-
$this->assertEquals(['error', $data], $status);
136+
$this->assertEquals(['warning', $data], $status);
137137
}
138138

139139
/**
@@ -227,7 +227,7 @@ public function testCheckErrorMulti(): void {
227227
],
228228
],
229229
];
230-
$this->assertEquals(['error', $data], $status);
230+
$this->assertEquals(['warning', $data], $status);
231231
}
232232

233233
}

0 commit comments

Comments
 (0)