xref: /webtrees/app/Http/Middleware/DoHousekeeping.php (revision 5fb61fc65b99b2d532a8e4bc9f5ba7068636203f)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\Middleware;
21
22use Fig\Http\Message\RequestMethodInterface;
23use Fisharebest\Webtrees\Registry;
24use Fisharebest\Webtrees\Services\HousekeepingService;
25use League\Flysystem\FilesystemOperator;
26use Psr\Http\Message\ResponseInterface;
27use Psr\Http\Message\ServerRequestInterface;
28use Psr\Http\Server\MiddlewareInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30
31/**
32 * Run the housekeeping service at irregular intervals.
33 */
34class DoHousekeeping implements MiddlewareInterface
35{
36    // Delete old thumbnails after 90 days.
37    private const THUMBNAIL_DIR     = 'thumbnail-cache';
38    private const MAX_THUMBNAIL_AGE = 60 * 60 * 24 * 90;
39
40    // Delete files in /data/tmp after 1 hour.
41    private const TMP_DIR          = 'data/tmp';
42    private const MAX_TMP_FILE_AGE = 60 * 60;
43
44    // Delete error logs after 90 days.
45    private const MAX_LOG_AGE = 60 * 60 * 24 * 90;
46
47    // Delete inactive sessions after 1 day.
48    private const MAX_SESSION_AGE = 60 * 60 * 24;
49
50    // Run the cleanup every N requests.
51    private const PROBABILITY = 250;
52
53    /** @var HousekeepingService */
54    private $housekeeping_service;
55
56    /**
57     * Housekeeping constructor.
58     *
59     * @param HousekeepingService $housekeeping_service
60     */
61    public function __construct(HousekeepingService $housekeeping_service)
62    {
63        $this->housekeeping_service = $housekeeping_service;
64    }
65
66    /**
67     * @param ServerRequestInterface  $request
68     * @param RequestHandlerInterface $handler
69     *
70     * @return ResponseInterface
71     */
72    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
73    {
74        $response = $handler->handle($request);
75
76        // Run the cleanup after random page requests.
77        if ($request->getMethod() === RequestMethodInterface::METHOD_GET && random_int(1, self::PROBABILITY) === 1) {
78            $this->runHousekeeping(Registry::filesystem()->data(), Registry::filesystem()->root());
79        }
80
81        return $response;
82    }
83
84    /**
85     * Run the various housekeeping services.
86     *
87     * @param FilesystemOperator $data_filesystem
88     * @param FilesystemOperator $root_filesystem
89     *
90     * @return void
91     */
92    private function runHousekeeping(FilesystemOperator $data_filesystem, FilesystemOperator $root_filesystem): void
93    {
94        // Clear old thumbnails
95        $this->housekeeping_service->deleteOldFiles($data_filesystem, self::THUMBNAIL_DIR, self::MAX_THUMBNAIL_AGE);
96
97        // Clear temporary files
98        $this->housekeeping_service->deleteOldFiles($root_filesystem, self::TMP_DIR, self::MAX_TMP_FILE_AGE);
99
100        // Clear entries in database tables
101        $this->housekeeping_service->deleteOldLogs(self::MAX_LOG_AGE);
102
103        $this->housekeeping_service->deleteOldSessions(self::MAX_SESSION_AGE);
104    }
105}
106