-
-
Notifications
You must be signed in to change notification settings - Fork 263
feat: added task scheduler #3976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+512
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #!/usr/bin/env php | ||
| <?php | ||
|
|
||
| /** | ||
| * phpMyFAQ task scheduler. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| * v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
| * obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * @package phpMyFAQ | ||
| * @author Thorsten Rinne <thorsten@phpmyfaq.de> | ||
| * @copyright 2026 phpMyFAQ Team | ||
| * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 | ||
| * @link https://www.phpmyfaq.de | ||
| * @since 2026-02-13 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Symfony\Component\Config\FileLocator; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; | ||
|
|
||
| require __DIR__ . '/../phpmyfaq/src/Bootstrap.php'; | ||
| require __DIR__ . '/../phpmyfaq/src/autoload.php'; | ||
|
|
||
| $command = $argv[1] ?? ''; | ||
| if ($command !== 'run') { | ||
| fwrite(STDERR, "Usage: php bin/scheduler.php run\n"); | ||
| exit(1); | ||
| } | ||
|
|
||
| $container = new ContainerBuilder(); | ||
| $loader = new PhpFileLoader($container, new FileLocator(__DIR__)); | ||
| $loader->load('../phpmyfaq/src/services.php'); | ||
| $container->compile(); | ||
|
|
||
| $scheduler = $container->get('phpmyfaq.scheduler.task-scheduler'); | ||
| $results = $scheduler->run(); | ||
|
|
||
| echo "Task scheduler finished.\n"; | ||
| echo json_encode($results, JSON_PRETTY_PRINT) . PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * Scheduled background tasks for phpMyFAQ. | ||
| * | ||
| * This Source Code Form is subject to the terms of the Mozilla Public License, | ||
| * v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
| * obtain one at https://mozilla.org/MPL/2.0/. | ||
| * | ||
| * @package phpMyFAQ | ||
| * @author Thorsten Rinne <thorsten@phpmyfaq.de> | ||
| * @copyright 2026 phpMyFAQ Team | ||
| * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 | ||
| * @link https://www.phpmyfaq.de | ||
| * @since 2026-02-13 | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace phpMyFAQ\Scheduler; | ||
|
|
||
| use phpMyFAQ\Administration\Backup; | ||
| use phpMyFAQ\Administration\Session as AdminSession; | ||
| use phpMyFAQ\Configuration; | ||
| use phpMyFAQ\Enums\BackupType; | ||
| use phpMyFAQ\Faq\Statistics; | ||
| use Throwable; | ||
|
|
||
| class TaskScheduler | ||
| { | ||
| private const int DEFAULT_SESSION_RETENTION_SECONDS = 86400; | ||
|
|
||
| public function __construct( | ||
| private readonly Configuration $configuration, | ||
| private readonly AdminSession $adminSession, | ||
| private readonly Backup $backup, | ||
| private readonly Statistics $statistics, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Runs all configured scheduler tasks. | ||
| * | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function run(): array | ||
| { | ||
| $results = []; | ||
|
|
||
| try { | ||
| $results['sessionCleanup'] = $this->cleanupSessions(); | ||
| } catch (Throwable $throwable) { | ||
| $this->configuration->getLogger()->error('Scheduled session cleanup threw an exception.', [ | ||
| 'message' => $throwable->getMessage(), | ||
| 'trace' => $throwable->getTraceAsString(), | ||
| ]); | ||
| $results['sessionCleanup'] = null; | ||
| } | ||
|
|
||
| try { | ||
| $results['searchOptimization'] = $this->optimizeSearchIndex(); | ||
| } catch (Throwable $throwable) { | ||
| $this->configuration->getLogger()->error('Scheduled search optimization threw an exception.', [ | ||
| 'message' => $throwable->getMessage(), | ||
| 'trace' => $throwable->getTraceAsString(), | ||
| ]); | ||
| $results['searchOptimization'] = null; | ||
| } | ||
|
|
||
| try { | ||
| $results['statisticsAggregation'] = $this->aggregateStatistics(); | ||
| } catch (Throwable $throwable) { | ||
| $this->configuration->getLogger()->error('Scheduled statistics aggregation threw an exception.', [ | ||
| 'message' => $throwable->getMessage(), | ||
| 'trace' => $throwable->getTraceAsString(), | ||
| ]); | ||
| $results['statisticsAggregation'] = null; | ||
| } | ||
|
|
||
| try { | ||
| $results['backupCreation'] = $this->createBackup(); | ||
| } catch (Throwable $throwable) { | ||
| $this->configuration->getLogger()->error('Scheduled backup creation threw an exception.', [ | ||
| 'message' => $throwable->getMessage(), | ||
| 'trace' => $throwable->getTraceAsString(), | ||
| ]); | ||
| $results['backupCreation'] = null; | ||
| } | ||
|
|
||
| return $results; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{success: bool, cutoffTimestamp: int, retentionSeconds: int} | ||
| */ | ||
| public function cleanupSessions(): array | ||
| { | ||
| $configuredRetention = (int) ($this->configuration->get('session.scheduler.retentionSeconds') ?? 0); | ||
| $retentionSeconds = $configuredRetention > 0 ? $configuredRetention : self::DEFAULT_SESSION_RETENTION_SECONDS; | ||
| $cutoffTimestamp = time() - $retentionSeconds; | ||
|
|
||
| try { | ||
| $success = $this->adminSession->deleteSessions(0, $cutoffTimestamp); | ||
| } catch (Throwable $throwable) { | ||
| $this->configuration->getLogger()->error('Scheduled session cleanup threw an exception.', [ | ||
| 'message' => $throwable->getMessage(), | ||
| 'trace' => $throwable->getTraceAsString(), | ||
| 'cutoffTimestamp' => $cutoffTimestamp, | ||
| 'retentionSeconds' => $retentionSeconds, | ||
| ]); | ||
| $success = false; | ||
| } | ||
|
|
||
| if (!$success) { | ||
| $this->configuration->getLogger()->warning('Scheduled session cleanup failed.', [ | ||
| 'cutoffTimestamp' => $cutoffTimestamp, | ||
| 'retentionSeconds' => $retentionSeconds, | ||
| ]); | ||
| } | ||
|
|
||
| return [ | ||
| 'success' => $success, | ||
| 'cutoffTimestamp' => $cutoffTimestamp, | ||
| 'retentionSeconds' => $retentionSeconds, | ||
| ]; | ||
| } | ||
thorsten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @return array{success: bool, skipped: bool, elasticsearch: bool|null, opensearch: bool|null} | ||
| */ | ||
| public function optimizeSearchIndex(): array | ||
| { | ||
| $elasticsearchResult = null; | ||
| $openSearchResult = null; | ||
|
|
||
| if ($this->configuration->get('search.enableElasticsearch')) { | ||
| try { | ||
| $this->configuration | ||
| ->getElasticsearch() | ||
| ->indices() | ||
| ->forcemerge([ | ||
| 'index' => $this->configuration->getElasticsearchConfig()->getIndex(), | ||
| 'max_num_segments' => 1, | ||
| ]); | ||
| $elasticsearchResult = true; | ||
| } catch (Throwable $throwable) { | ||
| $elasticsearchResult = false; | ||
| $this->configuration->getLogger()->error('Scheduled Elasticsearch optimization failed.', [ | ||
| 'message' => $throwable->getMessage(), | ||
| 'trace' => $throwable->getTraceAsString(), | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| if ($this->configuration->get('search.enableOpenSearch')) { | ||
| try { | ||
| $this->configuration | ||
| ->getOpenSearch() | ||
| ->indices() | ||
| ->forcemerge([ | ||
| 'index' => $this->configuration->getOpenSearchConfig()->getIndex(), | ||
| 'max_num_segments' => 1, | ||
| ]); | ||
| $openSearchResult = true; | ||
| } catch (Throwable $throwable) { | ||
| $openSearchResult = false; | ||
| $this->configuration->getLogger()->error('Scheduled OpenSearch optimization failed.', [ | ||
| 'message' => $throwable->getMessage(), | ||
| 'trace' => $throwable->getTraceAsString(), | ||
| ]); | ||
| } | ||
| } | ||
|
|
||
| $skipped = $elasticsearchResult === null && $openSearchResult === null; | ||
| $success = !$skipped && $elasticsearchResult !== false && $openSearchResult !== false; | ||
|
|
||
| return [ | ||
| 'success' => $success, | ||
| 'skipped' => $skipped, | ||
| 'elasticsearch' => $elasticsearchResult, | ||
| 'opensearch' => $openSearchResult, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @return array{success: bool, generatedAt: int, totalFaqs: int|null, totalSessions: int|null, error: string|null} | ||
| */ | ||
| public function aggregateStatistics(): array | ||
| { | ||
| try { | ||
| return [ | ||
| 'success' => true, | ||
| 'generatedAt' => time(), | ||
| 'totalFaqs' => $this->statistics->totalFaqs(), | ||
| 'totalSessions' => $this->adminSession->getNumberOfSessions(), | ||
| 'error' => null, | ||
| ]; | ||
| } catch (Throwable $throwable) { | ||
| $this->configuration->getLogger()->error('Scheduled statistics aggregation failed.', [ | ||
| 'message' => $throwable->getMessage(), | ||
| 'trace' => $throwable->getTraceAsString(), | ||
| ]); | ||
|
|
||
| return [ | ||
| 'success' => false, | ||
| 'generatedAt' => time(), | ||
| 'totalFaqs' => null, | ||
| 'totalSessions' => null, | ||
| 'error' => $throwable->getMessage(), | ||
| ]; | ||
| } | ||
| } | ||
thorsten marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @return array{success: bool, fileName: string|null} | ||
| */ | ||
| public function createBackup(): array | ||
| { | ||
| try { | ||
| $backupResult = $this->backup->export(BackupType::BACKUP_TYPE_DATA); | ||
|
|
||
| return [ | ||
| 'success' => true, | ||
| 'fileName' => $backupResult->fileName, | ||
| ]; | ||
| } catch (Throwable $throwable) { | ||
| $this->configuration->getLogger()->error('Scheduled backup creation failed.', [ | ||
| 'message' => $throwable->getMessage(), | ||
| 'trace' => $throwable->getTraceAsString(), | ||
| ]); | ||
|
|
||
| return [ | ||
| 'success' => false, | ||
| 'fileName' => null, | ||
| ]; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.