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

Add periodic logging every 100th file iterated #228

Merged
merged 8 commits into from
Apr 18, 2019
66 changes: 61 additions & 5 deletions src/FileMigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace SilverStripe\Assets;

use Monolog\Logger;
use Psr\Log\LoggerInterface;
use SilverStripe\Assets\Flysystem\FlysystemAssetStore;
use SilverStripe\Assets\Storage\AssetStore;
use SilverStripe\Control\Director;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Environment;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Logging\PreformattedEchoHandler;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\DataQuery;
Expand All @@ -34,6 +38,29 @@ class FileMigrationHelper
*/
private static $delete_invalid_files = true;

/**
* Specifies the interval for every Nth file looped at which output will be logged.
*
* @config
* @var bool
bergice marked this conversation as resolved.
Show resolved Hide resolved
*/
private static $log_interval = 100;

private static $dependencies = [
bergice marked this conversation as resolved.
Show resolved Hide resolved
'logger' => '%$' . LoggerInterface::class,
];

/** @var LoggerInterface|null */
private $logger;
bergice marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* Perform migration
*
Expand All @@ -42,6 +69,13 @@ class FileMigrationHelper
*/
public function run($base = null)
{
if (Director::is_cli()) {
// Inject real-time log handler
if ($this->logger instanceof Logger) {
$this->logger->pushHandler(new PreformattedEchoHandler());
}
}

if (empty($base)) {
$base = PUBLIC_PATH;
}
Expand All @@ -57,26 +91,36 @@ public function run($base = null)
Environment::increaseMemoryLimitTo();

// Loop over all files
$count = 0;
$processedCount = $migratedCount = 0;
$originalState = null;
if (class_exists(Versioned::class)) {
$originalState = Versioned::get_reading_mode();
Versioned::set_stage(Versioned::DRAFT);
}

foreach ($this->getFileQuery() as $file) {
$query = $this->getFileQuery();
$total = $query->count();
foreach ($query as $file) {
// Bypass the accessor and the filename from the column
$filename = $file->getField('Filename');

$success = $this->migrateFile($base, $file, $filename);

if ($processedCount % static::$log_interval === 0) {
bergice marked this conversation as resolved.
Show resolved Hide resolved
if ($this->logger) {
$this->logger->info("Iterated $processedCount out of $total files. Migrated $migratedCount files.");
}
}

$processedCount++;
if ($success) {
$count++;
$migratedCount++;
}
bergice marked this conversation as resolved.
Show resolved Hide resolved
}
if (class_exists(Versioned::class)) {
Versioned::set_reading_mode($originalState);
}
return $count;
return $migratedCount;
}

/**
Expand All @@ -94,6 +138,9 @@ protected function migrateFile($base, File $file, $legacyFilename)
// Make sure this legacy file actually exists
$path = $base . '/' . $legacyFilename;
if (!file_exists($path)) {
if ($this->logger) {
$this->logger->warning("$legacyFilename not migrated because the file does not exist ($path)");
}
return false;
}

Expand All @@ -104,6 +151,9 @@ protected function migrateFile($base, File $file, $legacyFilename)
if ($this->config()->get('delete_invalid_files')) {
$file->delete();
}
if ($this->logger) {
$this->logger->warning("$legacyFilename not migrated because the extension $extension is not a valid extension");
}
return false;
}

Expand Down Expand Up @@ -151,7 +201,13 @@ protected function migrateFile($base, File $file, $legacyFilename)

if (!$useLegacyFilenames) {
// removing the legacy file since it has been migrated now and not using legacy filenames
return unlink($path);
$removed = unlink($path);
if (!$removed) {
if ($this->logger) {
$this->logger->warning("$legacyFilename was migrated, but failed to remove the legacy file ($path)");
}
}
return $removed;
}
return true;
}
Expand Down