1
0
mirror of https://github.com/flarum/core.git synced 2025-07-30 21:20:24 +02:00

Move garbage collection into middleware

This prevents garbage collection to randomly break the installer:
before installation, the models that are being accessed have no
database connection.

Now, the middleware is only mounted into the forum's middleware
stack. I want API requests to have stable performance, and the
forum middleware stack is only mounted when Flarum is installed.
This commit is contained in:
Franz Liedke
2017-12-21 12:23:34 +01:00
parent c8a1a5fcfa
commit 56231d61be
3 changed files with 57 additions and 24 deletions

View File

@@ -0,0 +1,55 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Http\Middleware;
use Flarum\Http\AccessToken;
use Flarum\User\AuthToken;
use Flarum\User\EmailToken;
use Flarum\User\PasswordToken;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Stratigility\MiddlewareInterface;
class CollectGarbage implements MiddlewareInterface
{
/**
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
$this->collectGarbageSometimes();
return $out ? $out($request, $response) : $response;
}
private function collectGarbageSometimes()
{
// In order to save performance, we only execute this query
// from time to time (with 50% chance).
if (! $this->hit()) {
return;
}
AccessToken::whereRaw('last_activity <= ? - lifetime', [time()])->delete();
$earliestToKeep = date('Y-m-d H:i:s', time() - 24 * 60 * 60);
EmailToken::where('created_at', '<=', $earliestToKeep)->delete();
PasswordToken::where('created_at', '<=', $earliestToKeep)->delete();
AuthToken::where('created_at', '<=', $earliestToKeep)->delete();
}
private function hit()
{
return mt_rand(1, 100) <= 2;
}
}