Problem/Motivation

Notify loads all nodes/comments in database using

  • $this->entityTypeManager->getStorage('node')->loadMultiple()
  • $this->entityTypeManager->getStorage('comment')->loadMultiple();

in \Drupal\notify\Notify::send().

When the site has many nodes (like 50,000+ nodes), PHP uses up memory / execution time with those lines.

Proposed resolution

Do not use loadMultiple(), use entityQuery or SelectQuery instead and gather entity-id / created / changed used later.

Attached is the patch.

Comments

hidehisa created an issue.

elisseck’s picture

Agreed this is a problem and that there is something to be done in the `selectContent()` method.

We patched it like this, because it seemed like one just shouldn't load all the nodes in all node types, even if notify isn't enabled for those types. That cut way down on memory usage during cron runs:

  public function selectContent(): array {

...

    $config = \Drupal::config('notify.settings');
    $nodetypes = $config->get('notify_nodetypes');
    $all = NodeType::loadMultiple($nodetypes);
    $ntype = [];
    foreach ($all as $type => $object) {
      $ntype[] = $type;
    }
    $query = \Drupal::entityQuery('node')
      ->condition('type', $ntype, 'IN')
      ->accessCheck(FALSE);
    $nids = $query->execute();
    // Fetch new published nodes.
    $nodes = $this->entityTypeManager->getStorage('node')->loadMultiple($nids);

...