migration is ok again (remains probleme of ram consuming > 4Go for users

This commit is contained in:
2020-11-17 11:23:03 +01:00
parent ee5f880f2c
commit 8db06621ce
57 changed files with 801 additions and 162 deletions

View File

@@ -0,0 +1,43 @@
Copied from https://www.drupal.org/sandbox/onkeltem/2828817 and added the patch from https://www.drupal.org/project/2828817/issues/2919993
# About
On Drupal 7 we could disable hooks while running migrations:
https://www.drupal.org/node/2136601
This module adds a similar feature. You can disable hooks
in settings.php or by editing configuratoin object `migrate_booster.settings`.
There are two ways to disable hooks:
1) Disable specific hooks and modules:
```
$config['migrate_booster.settings']['hooks'] = [
# Entity insert
'entity_insert' => [
'workbench_moderation',
'pathauto',
'xmlsitemap',
],
# Entity presave
'entity_presave' => [
'xmlsitemap',
],
# Entity predelete
'entity_predelete' => [
'flag',
],
];
```
2) Disable all hooks of specific modules:
```
$config['migrate_booster.settings']['modules'] = [
'workbench_moderation',
'pathauto',
'xmlsitemap',
];
```

View File

@@ -0,0 +1,3 @@
commands:
- migrate:import
- migrate:rollback

View File

@@ -0,0 +1,6 @@
services:
migrate_booster.commands:
class: \Drupal\migrate_booster\Commands\MigrateBoosterCommands
arguments: ['@plugin.manager.migration', '@date.formatter', '@entity_type.manager', '@keyvalue']
tags:
- { name: drush.command }

View File

@@ -0,0 +1,30 @@
<?php
use Drupal\migrate_booster\MigrateBooster;
/**
* Implements hook_drush_init().
*/
function migrate_booster_drush_init() {
// Skip when bootstrap level is low (e.g. drush cr)
if (!\Drupal::hasContainer()) {
return;
}
MigrateBooster::bootDrush();
}
/**
* Implements hook_drush_command().
*/
function migrate_booster_drush_command() {
$items['migrate-booster-reset'] = [
'description' => 'Resets migrate booster and implementation cache.',
'aliases' => ['mbr'],
'drupal dependencies' => ['migrate_booster'],
];
return $items;
}
function drush_migrate_booster_reset() {
MigrateBooster::disable();
}

View File

@@ -0,0 +1,7 @@
name: Migrate booster
type: module
description: Hooks disabler
core: 8.x
package: Migration
dependencies:
- migrate

View File

@@ -0,0 +1,15 @@
<?php
use Drupal\migrate_booster\MigrateBooster;
/** @noinspection PhpInconsistentReturnPointsInspection */
/**
* Implements hook_module_implements_alter().
* @param $implementations
* @param $hook
* @return null
*/
function migrate_booster_module_implements_alter(&$implementations, $hook) {
MigrateBooster::alter($implementations, $hook);
}

View File

@@ -0,0 +1,5 @@
services:
migrate_booster.hooks_enabler_subscriber:
class: Drupal\migrate_booster\HooksEnablerSubscriber
tags:
- { name: 'event_subscriber' }

View File

@@ -0,0 +1,10 @@
<?php
use Drupal\migrate_booster\MigrateBooster;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;
/** @noinspection PhpUnusedParameterInspection */
$GLOBALS['dispatcher']->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
MigrateBooster::bootDrupal();
});

View File

@@ -0,0 +1,61 @@
<?php
namespace Drupal\migrate_booster\Commands;
use Consolidation\AnnotatedCommand\AnnotationData;
use Drupal\migrate_booster\MigrateBooster;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Input\InputInterface;
/**
*
* In addition to a commandfile like this one, you need a drush.services.yml
* in root of your module.
*
* See these files for an example of injecting Drupal services:
* - http://cgit.drupalcode.org/devel/tree/src/Commands/DevelCommands.php
* - http://cgit.drupalcode.org/devel/tree/drush.services.yml
*/
class MigrateBoosterCommands extends DrushCommands {
/**
* Resets migrate booster and implementation cache.
*
* @command migrate:booster:reset
*
* @validate-module-enabled migrate_booster
* @aliases mbr,migrate-booster-reset
*/
public function boosterReset()
{
// See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a
// legacy command.
MigrateBooster::reset();
}
/**
* Enables migrate booster and implementation cache.
*
* @command migrate:booster:enable
*
* @validate-module-enabled migrate_booster
* @aliases mbe,migrate-booster-enable
*/
public function boosterEnable()
{
// See bottom of https://weitzman.github.io/blog/port-to-drush9 for details on what to change when porting a
// legacy command.
MigrateBooster::enable();
}
/**
* @hook init *
*/
public function initCommand(InputInterface $input, AnnotationData $annotationData) {
// Skip when bootstrap level is low (e.g. drush cr)
if (!\Drupal::hasContainer()) {
return;
}
MigrateBooster::bootDrush($input, $annotationData);
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Drupal\migrate_booster;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Provides a MyModuleSubscriber.
*/
class HooksEnablerSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('ensureHooksEnabled', 20);
return $events;
}
/**
* Triggers on 'kernel.request' event which occurs when Drupal
* bootstraps (but not when Drush or Drupal console command runs).
*/
public function ensureHooksEnabled() {
MigrateBooster::bootDrupal();
}
}

View File

@@ -0,0 +1,115 @@
<?php
namespace Drupal\migrate_booster;
use Consolidation\AnnotatedCommand\AnnotationData;
use Symfony\Component\Console\Input\InputInterface;
class MigrateBooster {
protected static $alterActive;
protected static $config;
const CID = 'migrate_booster_enabled';
// Startup hooks
/**
* Reacts on HOOK_drush_init().
*
* Enables/disables booster depending on a drush command invoked.
*/
public static function bootDrush(InputInterface $input, AnnotationData $annotationData) {
if (in_array($annotationData['command'], static::getConfig('commands'))) {
static::enable();
}
else {
static::disable();
}
}
/**
* Disables booster on Drupal and Drupal console boots.
*/
public static function bootDrupal() {
static::disable();
}
/**
* Enables booster.
*
* Resets implementation cache and sets $alterActive class variable.
*
*/
public static function enable() {
static::$alterActive = TRUE;
static::reset();
}
/**
* Disables booster.
*
* Resets implementation cache.
*/
public static function disable() {
static::reset();
}
/**
* Resets implementations cache.
*/
public static function reset() {
$module_handler = \Drupal::moduleHandler();
$module_handler->resetImplementations();
}
/** @noinspection PhpInconsistentReturnPointsInspection */
/**
* Implements hook_module_implementation_alter().
*
* Disables configured hooks.
* @param $implementations
* @param $hook
* @return null
*/
public static function alter(&$implementations, $hook) {
if (!static::$alterActive) {
return NULL;
}
if (!$implementations) {
return NULL;
}
$hooks = static::getConfig('hooks');
$modules = static::getConfig('modules');
$disabled = [];
// Disable by hook + module
if (array_key_exists($hook, $hooks)) {
$disabled = array_intersect_key($implementations, array_flip($hooks[$hook]));
}
// Disable by module
$disabled += array_intersect_key($implementations, array_flip($modules));
$implementations = array_diff_key($implementations, $disabled);
/** @noinspection PhpUnusedParameterInspection */
array_walk($disabled, function ($el, $key) use ($hook) {
error_log('DISABLED: ' . $key . '_' . $hook);
});
}
/**
* Helper functions
*
* @param $key
* @return array
*/
protected static function getConfig($key) {
if (!static::$config) {
static::$config = \Drupal::config('migrate_booster.settings')->get();
}
if ($key && isset(static::$config[$key])) {
return static::$config[$key];
}
return [];
}
}