xref: /webtrees/app/Http/Middleware/DoHousekeeping.php (revision d812eb6b3dd6c33a96d07cd964cb4c3b88cec447)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Http\Middleware;
20
21use Fig\Http\Message\RequestMethodInterface;
22use Fisharebest\Webtrees\Services\HousekeepingService;
23use Fisharebest\Webtrees\Webtrees;
24use League\Flysystem\Adapter\Local;
25use League\Flysystem\Filesystem;
26use League\Flysystem\FilesystemInterface;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\MiddlewareInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32/**
33 * Run the housekeeping service at irregular intervals.
34 */
35class DoHousekeeping implements MiddlewareInterface
36{
37    // Delete cache files after 1 hour.
38    private const MAX_CACHE_AGE = 60 * 60;
39
40    // Delete thumnnails after 90 days.
41    private const MAX_THUMBNAIL_AGE = 60 * 60 * 24 * 90;
42
43    // Delete files in /data/tmp after 1 hour.
44    private const TMP_DIR          = 'data/tmp';
45    private const MAX_TMP_FILE_AGE = 60 * 60;
46
47    // Delete error logs after 90 days.
48    private const MAX_LOG_AGE = 60 * 60 * 24 * 90;
49
50    // Delete inactive sessions after 1 day.
51    private const MAX_SESSION_AGE = 60 * 60 * 24;
52
53    // Run the cleanup every 100 requests.
54    private const PROBABILITY = 100;
55
56    /** @var HousekeepingService */
57    private $housekeeping_service;
58
59    /**
60     * Housekeeping constructor.
61     *
62     * @param HousekeepingService $housekeeping_service
63     */
64    public function __construct(HousekeepingService $housekeeping_service)
65    {
66        $this->housekeeping_service = $housekeeping_service;
67    }
68
69    /**
70     * @param ServerRequestInterface  $request
71     * @param RequestHandlerInterface $handler
72     *
73     * @return ResponseInterface
74     */
75    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
76    {
77        $response = $handler->handle($request);
78
79        // Run the cleanup after random page requests.
80        if ($request->getMethod() === RequestMethodInterface::METHOD_GET && random_int(1, self::PROBABILITY) === 1) {
81            $this->runHousekeeping();
82        }
83
84        return $response;
85    }
86
87    /**
88     * Run the various housekeeping services.
89     *
90     * @return void
91     */
92    private function runHousekeeping(): void
93    {
94        $data_filesystem = app(FilesystemInterface::class);
95        $root_filesystem = new Filesystem(new Local(Webtrees::ROOT_DIR));
96
97        // Clear files in the (user-specified) data folder - which might not be local files
98        $this->housekeeping_service->deleteOldFiles($data_filesystem, 'cache', self::MAX_CACHE_AGE);
99
100        $this->housekeeping_service->deleteOldFiles($data_filesystem, 'thumbnail-cache', self::MAX_THUMBNAIL_AGE);
101
102        // Clear files in /data - which need to be local files
103        $this->housekeeping_service->deleteOldFiles($root_filesystem, self::TMP_DIR, self::MAX_TMP_FILE_AGE);
104
105        // Clear entries in database tables
106        $this->housekeeping_service->deleteOldLogs(self::MAX_LOG_AGE);
107
108        $this->housekeeping_service->deleteOldSessions(self::MAX_SESSION_AGE);
109    }
110}
111