Skip to content
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

BUGFIX: Stabilise file monitor #3304

Draft
wants to merge 2 commits into
base: 9.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* A change detection strategy based on modification times
*/
class ModificationTimeStrategy implements ChangeDetectionStrategyInterface, StrategyWithMarkDeletedInterface
class ModificationTimeStrategy implements ChangeDetectionStrategyInterface, StrategyWithMarkDeletedInterface, StrategyWithFlushDeletedOnPathInterface
{
/**
* @var \Neos\Flow\Monitor\FileMonitor
Expand Down Expand Up @@ -64,6 +64,32 @@ public function setFileMonitor(FileMonitor $fileMonitor)
$this->filesAndModificationTimes = json_decode($this->cache->get($this->fileMonitor->getIdentifier() . '_filesAndModificationTimes'), true);
}

/**
* @param string $onPath
* @param array<string,1> $filesIgnoreMask files to ignore as we are sure they exist
* @return array<string, ChangeDetectionStrategyInterface::STATUS_DELETED>
*/
public function flushDeletedOnPath(string $onPath, array $filesIgnoreMask): array
{
$deletedFiles = [];
foreach ($this->filesAndModificationTimes as $pathAndFilename => $modificationTime) {
if (!str_starts_with($pathAndFilename, $onPath)) {
continue;
}
if (isset($filesIgnoreMask[$pathAndFilename])) {
continue;
}
if (file_exists($pathAndFilename)) {
// should not happen?
continue;
}
$this->modificationTimesChanged = true;
unset($this->filesAndModificationTimes[$pathAndFilename]);
$deletedFiles[$pathAndFilename] = ChangeDetectionStrategyInterface::STATUS_DELETED;
}
return $deletedFiles;
}

/**
* Checks if the specified file has changed
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php
namespace Neos\Flow\Monitor\ChangeDetectionStrategy;

/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

/**
* Contract for a change detection strategy that allows the FileMonitor to flush all removals directly.
*
* @api
*/
interface StrategyWithFlushDeletedOnPathInterface
{
/**
* @param string $onPath
* @param array<string,1> $filesIgnoreMask files to ignore as we are sure they exist
* @return array<string, ChangeDetectionStrategyInterface::STATUS_DELETED>
*/
public function flushDeletedOnPath(string $onPath, array $filesIgnoreMask): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
/**
* Contract for a change detection strategy that allows the FileMonitor to mark a file deleted directly.
*
* @deprecated in favour of more reliable {@see StrategyWithFlushDeletedOnPathInterface}
* @api
*/
interface StrategyWithMarkDeletedInterface
Expand Down
33 changes: 23 additions & 10 deletions Neos.Flow/Classes/Monitor/FileMonitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Neos\Flow\Log\PsrLoggerFactoryInterface;
use Neos\Flow\Log\Utility\LogEnvironment;
use Neos\Flow\Monitor\ChangeDetectionStrategy\ChangeDetectionStrategyInterface;
use Neos\Flow\Monitor\ChangeDetectionStrategy\StrategyWithFlushDeletedOnPathInterface;
use Neos\Flow\Monitor\ChangeDetectionStrategy\StrategyWithMarkDeletedInterface;
use Neos\Flow\SignalSlot\Dispatcher;
use Neos\Utility\Files;
Expand Down Expand Up @@ -81,6 +82,7 @@ class FileMonitor
/**
* Array of directories and files that were cached on the last run.
*
* @deprecated to be replaced by only supporting {@see StrategyWithFlushDeletedOnPathInterface}
* @var array
*/
protected $directoriesAndFiles = null;
Expand Down Expand Up @@ -303,7 +305,9 @@ protected function detectChangesOnPath($path, $filenamePattern)
$this->changedPaths[$path] = ChangeDetectionStrategyInterface::STATUS_CREATED;
}

$currentSubDirectoriesAndFilesMask = [];
foreach ($currentSubDirectoriesAndFiles as $pathAndFilename) {
$currentSubDirectoriesAndFilesMask[$pathAndFilename] = 1;
$status = $this->changeDetectionStrategy->getFileStatus($pathAndFilename);
if ($status !== ChangeDetectionStrategyInterface::STATUS_UNCHANGED) {
$this->changedFiles[$pathAndFilename] = $status;
Expand All @@ -316,18 +320,27 @@ protected function detectChangesOnPath($path, $filenamePattern)
$nowDetectedFilesAndDirectories[$pathAndFilename] = 1;
}

if ($this->directoriesAndFiles[$path] !== []) {
foreach (array_keys($this->directoriesAndFiles[$path]) as $pathAndFilename) {
$this->changedFiles[$pathAndFilename] = ChangeDetectionStrategyInterface::STATUS_DELETED;
if ($this->changeDetectionStrategy instanceof StrategyWithMarkDeletedInterface) {
$this->changeDetectionStrategy->setFileDeleted($pathAndFilename);
} else {
// This call is needed to mark the file deleted in any possibly existing caches of the strategy.
// The return value is not important as we know this file doesn't exist so we set the status to DELETED anyway.
$this->changeDetectionStrategy->getFileStatus($pathAndFilename);
if ($this->changeDetectionStrategy instanceof StrategyWithFlushDeletedOnPathInterface) {
$deletedFiles = $this->changeDetectionStrategy->flushDeletedOnPath($path, $currentSubDirectoriesAndFilesMask);
if ($deletedFiles) {
$this->changedFiles = [...$this->changedFiles, ...$deletedFiles];
$currentDirectoryChanged = true;
}
} else {
// legacy deletion detection
if ($this->directoriesAndFiles[$path] !== []) {
foreach (array_keys($this->directoriesAndFiles[$path]) as $pathAndFilename) {
$this->changedFiles[$pathAndFilename] = ChangeDetectionStrategyInterface::STATUS_DELETED;
if ($this->changeDetectionStrategy instanceof StrategyWithMarkDeletedInterface) {
$this->changeDetectionStrategy->setFileDeleted($pathAndFilename);
} else {
// This call is needed to mark the file deleted in any possibly existing caches of the strategy.
// The return value is not important as we know this file doesn't exist so we set the status to DELETED anyway.
$this->changeDetectionStrategy->getFileStatus($pathAndFilename);
}
}
$currentDirectoryChanged = true;
}
$currentDirectoryChanged = true;
}

if ($currentDirectoryChanged) {
Expand Down
Loading