Skip to content

Commit

Permalink
Add periodic logging every 100th file iterated
Browse files Browse the repository at this point in the history
  • Loading branch information
bergice committed Apr 15, 2019
1 parent eff4efd commit 7aebc14
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions src/FileMigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SilverStripe\Assets;

use Psr\Log\LoggerInterface;
use SilverStripe\Assets\Flysystem\FlysystemAssetStore;
use SilverStripe\Assets\Storage\AssetStore;
use SilverStripe\Core\Config\Config;
Expand Down Expand Up @@ -34,6 +35,21 @@ class FileMigrationHelper
*/
private static $delete_invalid_files = true;

private static $dependencies = [
'logger' => '%$' . LoggerInterface::class,
];

/** @var LoggerInterface|null */
private $logger;

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

/**
* Perform migration
*
Expand All @@ -57,26 +73,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 % 100 === 0) {
if ($this->logger) {
$this->logger->info("Iterated $processedCount out of $total files. Migrated $migratedCount files.");
}
}

$processedCount++;
if ($success) {
$count++;
$migratedCount++;
}
}
if (class_exists(Versioned::class)) {
Versioned::set_reading_mode($originalState);
}
return $count;
return $migratedCount;
}

/**
Expand All @@ -94,6 +120,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 +133,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 +183,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

0 comments on commit 7aebc14

Please sign in to comment.