API Add extension points to MigrateFileTask (#8994)

* API Add extension points to MigrateFileTask

* Apply suggestions from code review

Co-Authored-By: Guy Marriott <guy@scopey.co.nz>
This commit is contained in:
Maxime Rainville 2019-05-28 09:19:05 +12:00 committed by Ingo Schommer
parent f97ca26e76
commit 9bfce8c1a4
2 changed files with 134 additions and 49 deletions

View File

@ -55,7 +55,7 @@ but will no longer be attached to a dataobject anymore, and should be cleaned up
To disable this, set the following config:
```yaml
SilverStripe\Assets\FileMigrationHelper:
SilverStripe\Assets\Dev\Tasks\FileMigrationHelper:
delete_invalid_files: false
```
@ -113,3 +113,71 @@ Use the following estimates to decide how you will run your file migration:
| 10000+ | Command Line or contact support | n/a | n/a |
Your exact experience will vary based on your host server, the size of your files and other conditions. If your site is hosted on a managed environement (e.g.: [Common Web Platform](https://www.cwp.govt.nz/service-desk) or [SilverStripe Platform](https://docs.platform.silverstripe.com/support/)), you may not have access to the command line to manually run the migration task. Contact your hosting provider's helpdesk if that's your case.
## Customise the File Migration Task (Advanced)
In some context, you may want to disable some other process when the file migration is running. For example, if you have a module that indexes files when they get modified, you'll probably want to wait until the file migration is done to reindex.
The `MigrateFileTask` exposes 4 extension point that can be use to detect the progress of the migration.
* `preFileMigration` that gets fired at the start of the task
* `postFileMigration` that gets fired at the end of the task
* `preFileMigrationSubtask` that gets fired at the start of each subtasks
* `postFileMigrationSubtask` that gets fired at the end of each subtasks.
`preFileMigrationSubtask` and `postFileMigrationSubtask` will provide a single string parameter matching the name of the subtask (e.g.: `move-files`)
### Example migrate file task extension
```php
<?php
use Psr\Log\LoggerInterface;
use SilverStripe\Core\Extension;
class MigrateFileTaskExtension extends Extension
{
private static $dependencies = [
'logger' => '%$' . LoggerInterface::class . '.quiet',
];
/** @var LoggerInterface */
private $logger;
/**
* @param LoggerInterface $logger
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function preFileMigration()
{
$this->logger->info('Run some extension code BEFORE the Migrate File Task');
}
public function postFileMigration()
{
$this->logger->info('Run some extension code AFTER the Migrate File Task');
}
public function preFileMigrationSubtask($subtaskName)
{
$this->logger->info(sprintf('Run some extension code BEFORE the %s subtask', $subtaskName));
}
public function postFileMigrationSubtask($subtaskName)
{
$this->logger->info(sprintf('Run some extension code AFTER the %s subtask', $subtaskName));
}
}
```
Add the following snippet to your YML config to enable the extension.
```yaml
SilverStripe\Dev\Tasks\MigrateFileTask:
extensions:
- MigrateFileTaskExtension
```

View File

@ -46,85 +46,102 @@ class MigrateFileTask extends BuildTask
$args = $request->getVars();
$this->validateArgs($args);
$this->extend('preFileMigration');
$subtasks = !empty($args['only']) ? explode(',', $args['only']) : $this->defaultSubtasks;
if (in_array('move-files', $subtasks)) {
$subtask = 'move-files';
if (in_array($subtask, $subtasks)) {
if (!class_exists(FileMigrationHelper::class)) {
$this->logger->error("No file migration helper detected");
return;
}
$this->logger->info('### Migrating filesystem and database records (move-files)');
$this->logger->info('If the task fails or times out, run it again and it will start where it left off.');
$migrated = FileMigrationHelper::singleton()->run();
if ($migrated) {
$this->logger->info("{$migrated} File DataObjects upgraded");
} else {
$this->logger->info("No File DataObjects need upgrading");
$this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Migrating filesystem and database records ({$subtask})");
$this->logger->info('If the task fails or times out, run it again and it will start where it left off.');
$migrated = FileMigrationHelper::singleton()->run();
if ($migrated) {
$this->logger->info("{$migrated} File DataObjects upgraded");
} else {
$this->logger->info("No File DataObjects need upgrading");
}
$this->extend('postFileMigrationSubtask', $subtask);
}
}
if (in_array('move-thumbnails', $subtasks)) {
$subtask = 'move-thumbnails';
if (in_array($subtask, $subtasks)) {
if (!class_exists(LegacyThumbnailMigrationHelper::class)) {
$this->logger->error("LegacyThumbnailMigrationHelper not found");
return;
}
$this->logger->info('### Migrating existing thumbnails (move-thumbnails)');
$moved = LegacyThumbnailMigrationHelper::singleton()
->setLogger($this->logger)
->run($this->getStore());
if ($moved) {
$this->logger->info(sprintf("%d thumbnails moved", count($moved)));
} else {
$this->logger->info("No thumbnails moved");
$this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Migrating existing thumbnails ({$subtask})");
$moved = LegacyThumbnailMigrationHelper::singleton()
->setLogger($this->logger)
->run($this->getStore());
if ($moved) {
$this->logger->info(sprintf("%d thumbnails moved", count($moved)));
} else {
$this->logger->info("No thumbnails moved");
}
$this->extend('postFileMigrationSubtask', $subtask);
}
}
if (in_array('generate-cms-thumbnails', $subtasks)) {
$subtask = 'generate-cms-thumbnails';
if (in_array($subtask, $subtasks)) {
if (!class_exists(ImageThumbnailHelper::class)) {
$this->logger->error("ImageThumbnailHelper not found");
return;
} else {
$this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Generating new CMS UI thumbnails ({$subtask})");
ImageThumbnailHelper::singleton()->run();
$this->extend('postFileMigrationSubtask', $subtask);
}
$this->logger->info('### Generating new CMS UI thumbnails (generate-cms-thumbnails)');
ImageThumbnailHelper::singleton()->run();
}
if (in_array('fix-folder-permissions', $subtasks)) {
$subtask = 'fix-folder-permissions';
if (in_array($subtask, $subtasks)) {
if (!class_exists(FixFolderPermissionsHelper::class)) {
$this->logger->error("FixFolderPermissionsHelper not found");
return;
}
$this->logger->info('### Fixing folder permissions (fix-folder-permissions)');
$updated = FixFolderPermissionsHelper::singleton()->run();
if ($updated > 0) {
$this->logger->info("Repaired {$updated} folders with broken CanViewType settings");
} else {
$this->logger->info("No folders required fixes");
$this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Fixing folder permissions ({$subtask})");
$updated = FixFolderPermissionsHelper::singleton()->run();
if ($updated > 0) {
$this->logger->info("Repaired {$updated} folders with broken CanViewType settings");
} else {
$this->logger->info("No folders required fixes");
}
$this->extend('postFileMigrationSubtask', $subtask);
}
}
if (in_array('fix-secureassets', $subtasks)) {
$subtask = 'fix-secureassets';
if (in_array($subtask, $subtasks)) {
if (!class_exists(SecureAssetsMigrationHelper::class)) {
$this->logger->error("SecureAssetsMigrationHelper not found");
return;
} else {
$this->extend('preFileMigrationSubtask', $subtask);
$this->logger->info("### Fixing secure-assets ({$subtask})");
$moved = SecureAssetsMigrationHelper::singleton()
->setLogger($this->logger)
->run($this->getStore());
$this->extend('postFileMigrationSubtask', $subtask);
}
$this->logger->info('### Fixing secure-assets (fix-secureassets)');
$moved = SecureAssetsMigrationHelper::singleton()
->setLogger($this->logger)
->run($this->getStore());
}
$this->extend('postFileMigration');
}
public function getDescription()