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 3, 2019
1 parent eff4efd commit df3eb86
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/FileMigrationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace SilverStripe\Assets;

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

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

/**
* Perform migration
*
Expand All @@ -56,27 +61,40 @@ public function run($base = null)
Environment::increaseTimeLimitTo();
Environment::increaseMemoryLimitTo();

$this->logger = Injector::inst()->get(LoggerInterface::class);

// Loop over all files
$count = 0;
$i = $migrated = 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 ($i % 100 == 0) {
/** @var LoggerInterface $logger */
if ($this->logger != null) {
$this->logger->info("Iterated $i out of $total files. Migrated $migrated files.");
}
}

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

/**
Expand Down

0 comments on commit df3eb86

Please sign in to comment.