Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit 76bbd82

Browse files
committed
Fix ShutdownScheduler trigger, do not use output_buffering anymore.
1 parent cdacb3d commit 76bbd82

File tree

3 files changed

+24
-16
lines changed

3 files changed

+24
-16
lines changed

core/src/core/src/pydio/Core/Controller/ShutdownScheduler.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
*/
2121
namespace Pydio\Core\Controller;
2222

23+
use Pydio\Core\Exception\PydioException;
24+
use Pydio\Core\Services\ApplicationState;
2325
use Pydio\Log\Core\Logger;
26+
use Symfony\Component\Console\Output\OutputInterface;
2427

2528
defined('AJXP_EXEC') or die('Access not allowed');
2629
/**
@@ -53,23 +56,23 @@ public static function getInstance()
5356
public function __construct()
5457
{
5558
$this->callbacks = array();
56-
register_shutdown_function(array($this, 'callRegisteredShutdown'));
57-
ob_start();
59+
// register_shutdown_function(array($this, 'callRegisteredShutdown'));
60+
// ob_start();
5861
}
5962

6063
/**
6164
* @return bool
62-
* @throws \Exception
65+
* @throws PydioException
6366
*/
6467
public function registerShutdownEventArray()
6568
{
6669
$callback = func_get_args();
6770

6871
if (empty($callback)) {
69-
throw new \Exception('No callback passed to '.__FUNCTION__.' method');
72+
throw new PydioException('No callback passed to '.__FUNCTION__.' method');
7073
}
7174
if (!is_callable($callback[0])) {
72-
throw new \Exception('Invalid callback ('.$callback[0].') passed to the '.__FUNCTION__.' method');
75+
throw new PydioException('Invalid callback ('.$callback[0].') passed to the '.__FUNCTION__.' method');
7376
}
7477
$flattenArray = array();
7578
$flattenArray[0] = $callback[0];
@@ -82,43 +85,41 @@ public function registerShutdownEventArray()
8285

8386
/**
8487
* @return bool
85-
* @throws \Exception
88+
* @throws PydioException
8689
*/
8790
public function registerShutdownEvent()
8891
{
8992
$callback = func_get_args();
9093

9194
if (empty($callback)) {
92-
throw new \Exception('No callback passed to '.__FUNCTION__.' method');
95+
throw new PydioException('No callback passed to '.__FUNCTION__.' method');
9396
}
9497
if (!is_callable($callback[0])) {
95-
throw new \Exception('Invalid callback ('.$callback[0].') passed to the '.__FUNCTION__.' method');
98+
throw new PydioException('Invalid callback ('.$callback[0].') passed to the '.__FUNCTION__.' method');
9699
}
97100
$this->callbacks[] = $callback;
98101
return true;
99102
}
100103

101104
/**
102105
* Trigger the schedulers
106+
* @param OutputInterface $cliOutput
103107
*/
104-
public function callRegisteredShutdown()
108+
public function callRegisteredShutdown($cliOutput = null)
105109
{
106110
session_write_close();
107-
if (!headers_sent()) {
108-
$size = ob_get_length();
109-
header("Connection: close\r\n");
110-
//header("Content-Encoding: none\r\n");
111-
header("Content-Length: $size");
112-
}
113111
ob_end_flush();
114112
flush();
115113
$index = 0;
116114
while (count($this->callbacks)) {
117115
$arguments = array_shift($this->callbacks);
118116
$callback = array_shift($arguments);
119117
try {
118+
if($cliOutput !== null){
119+
$cliOutput->writeln("<comment>--> Applying Shutdown Hook: ". get_class($callback[0]) ."::".$callback[1]."</comment>");
120+
}
120121
call_user_func_array($callback, $arguments);
121-
} catch (\Exception $e) {
122+
} catch (PydioException $e) {
122123
Logger::error(__CLASS__, __FUNCTION__, array("context" => "Applying hook " . get_class($callback[0]) . "::" . $callback[1], "message" => $e->getMessage()));
123124
}
124125
$index++;

core/src/core/src/pydio/Core/Http/Cli/CliMiddleware.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
use Psr\Http\Message\ResponseInterface;
2424
use Psr\Http\Message\ServerRequestInterface;
25+
use Pydio\Core\Controller\ShutdownScheduler;
2526
use Pydio\Core\Exception\AuthRequiredException;
2627
use Pydio\Core\Exception\PydioException;
2728
use Pydio\Core\Exception\WorkspaceNotFoundException;
@@ -64,6 +65,9 @@ public function handleRequest(ServerRequestInterface $requestInterface, Response
6465

6566
$this->emitResponse($requestInterface, $responseInterface);
6667

68+
$logHooks = isSet($requestInterface->getParsedBody()["cli-show-hooks"]) ? $output: null;
69+
ShutdownScheduler::getInstance()->callRegisteredShutdown($logHooks);
70+
6771
} catch (AuthRequiredException $e){
6872

6973
$output->writeln("<error>Authentication Failed</error>");

core/src/core/src/pydio/Core/Http/Middleware/SapiMiddleware.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
use \Psr\Http\Message\ServerRequestInterface;
2424
use \Psr\Http\Message\ResponseInterface;
25+
use Pydio\Core\Controller\ShutdownScheduler;
2526
use Pydio\Core\Exception\PydioException;
2627

2728
use Pydio\Core\Http\Response\SerializableResponseStream;
@@ -125,7 +126,9 @@ public function emitResponse(ServerRequestInterface $request, ResponseInterface
125126

126127
if($response !== false && ($response->getBody()->getSize() || $response instanceof \Zend\Diactoros\Response\EmptyResponse) || $response->getStatusCode() != 200) {
127128
$emitter = new \Zend\Diactoros\Response\SapiEmitter();
129+
$response = $response->withHeader("Connection", "close");
128130
$emitter->emit($response);
131+
ShutdownScheduler::getInstance()->callRegisteredShutdown();
129132
}
130133
}
131134

0 commit comments

Comments
 (0)