Commit c050640c authored by Rahul Shinde's avatar Rahul Shinde Committed by vbouchet
Browse files

Issue #3160488 by rahul.shinde: Support for drush command

parent d5053db5
Loading
Loading
Loading
Loading
+44 −0
Original line number Diff line number Diff line
CONTENTS OF THIS FILE
---------------------

 * Introduction
 * Installation
 * Configuration
 * Maintainers


INTRODUCTION
------------

Memory limit policy Drush is a module to enable constraints for drush
commands. This will allow user to override the default php memory_limit for
drush commands.


INSTALLATION
------------

 * Install as you would normally install a contributed Drupal module. Visit
   https://www.drupal.org/node/1897420 for further information.


CONFIGURATION
-------------

1. Visit /admin/config/performance/memory-limit-policy/list to configure
 policies.
1. Click on `Add Policy` button
1. Add the following details
    1. Policy Name: The name of the Policy.
    1. Memory: The limit to set for the Drush command
    1. Enabled: Mark this checkbox.
    Click on `Next` Button to add Constraint.
1. Select the `Drush` from the dropdown and click on `Configure Constraint Settings`
1. In the modal form add the drush command one command name per line.
1. Click on `Save` and then `Finish` button.


MAINTAINERS
-----------

 * rahul.shinde - https://www.drupal.org/u/rahulshinde
+6 −0
Original line number Diff line number Diff line
memory_limit_policy.constraint.plugin.drush:
  type: memory_limit_policy.constraint.plugin
  mapping:
    paths:
      type: text
      label: 'Drush Commands'
+6 −0
Original line number Diff line number Diff line
services:
  memory_limit_policy_drush.commands:
    class: \Drupal\memory_limit_policy_drush\Commands\MemoryLimitPolicyCommands
    arguments: ['@entity_type.manager']
    tags:
      - { name: drush.command }
+8 −0
Original line number Diff line number Diff line
name: Memory Limit Policy Drush
description: A plugin to set memory limit policy based on the drush commands.
package: Performance
type: module
core: 8.x
core_version_requirement: ^8 || ^9
dependencies:
  - memory_limit_policy:memory_limit_policy
+92 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\memory_limit_policy_drush\Commands;

use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\memory_limit_policy\Entity\MemoryLimitPolicy;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Event\ConsoleCommandEvent;

/**
 * Class MemoryLimitPolicyDrushCommands.
 */
class MemoryLimitPolicyCommands extends DrushCommands {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  private $entityTypeManager;

  /**
   * Constructs a new MemoryLimitPolicySubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity type manager service.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Sets the memory limit for cli.
   *
   * This pre-command-event will set the php memory limit for drush command,
   * if there any policy configured for the same.
   *
   * @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event
   *   The event.
   *
   * @hook command-event *
   * @throws \Exception
   */
  public function preCommandEvent(ConsoleCommandEvent $event) {
    // Get the command in context.
    $command = $event->getCommand();

    // Get the all aliases for the commands in context. This will be used to
    // validate against configured constraints.
    $command_alias = $command->getAliases();
    $command_alias[] = $command->getName();

    // Fetch all policies.
    $policies = $this->entityTypeManager->getStorage('memory_limit_policy')->loadByProperties(['status' => TRUE]);

    // Sort policies by weight.
    uasort($policies, function (MemoryLimitPolicy $a, MemoryLimitPolicy $b) {
      if ($a->getWeight() == $b->getWeight()) {
        return 0;
      }
      return ($a->getWeight() < $b->getWeight()) ? -1 : 1;
    });

    /** @var \Drupal\memory_limit_policy\Entity\MemoryLimitPolicy $policy */
    foreach ($policies as $policy) {
      foreach ($policy->getConstraints() as $constraint) {
        // If the constraint is other than drush, skip to next.
        if ($constraint['id'] !== 'drush') {
          continue;
        }

        // Get the configured drush commands to validate.
        $configured_drush_commands = explode(PHP_EOL, $constraint['drush_commands']);
        array_walk($configured_drush_commands, function (&$drush_command) {
          $drush_command = rtrim(trim($drush_command, "\r"), '/');
        });

        // Intersecting will help us validate the current command or its any
        // one of aliases are into configuration or not.
        $intersect = array_intersect($command_alias, $configured_drush_commands);

        // If the current command is in the policy constraint, then get the
        // configured memory and set it.
        if (!empty($intersect)) {
          ini_set('memory_limit', $policy->getMemory());
        }
      }
    }

  }

}
Loading