Problem/Motivation

As discussed in #2771129: "Field field_last_password_reset is unknown" while importing configuration on site without password_policy activated, it would be beneficial to move the initial processing of all user accounts into a queue in order to prevent memory time outs when there are large numbers of users needing to be updated. This issue doesn't appear when the password policy module is installed at the beginning of a site build, but if a site has thousands of users, the current process of adding the password policy setting onto each user at install is untenable.

Proposed resolution

Update the hook_install function to instead use Drupal's queue system to process necessary password updates.

Suggested alternate resolution #2983448: Refactor hook_install logic to expire passwords for existing users

Remaining tasks

  1. Add password policy updates into queue system.
  2. Add drush command to speed the processing of queued password policy updates.
  3. Add queue clean up to hook_uninstall.
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

merauluka created an issue. See original summary.

joachim’s picture

Priority: Normal » Critical

I'd say this is critical -- it makes it impossible to install the module on a site with a large number of existing users.

kmoll’s picture

I agree it is critical. I am experiencing this on a site I am trying to update. I will create a patch and post shortly.

kmoll’s picture

StatusFileSize
new8.79 KB

Here is my first implementation of the installation queue. I've created a base class for the queue and extended it with a cron queue task. This way if someone needs to run it manually, and not on cron, it can be done. I will have a need for this. As of right now I replaced the user loop with the queuing process entirely. But I think we may want to count the number of users possibly and run the install function as it was originally written. I don't know what that number would be 1000, 5000, more less? I'm not sure, but I feel like there should be some number where we just run it on the install and some number where we queue. I'd like to hear what you guys think about that. I need to test it a bit more, but my first run through it worked. Although I don't think its complete I'm going to put this in needs review now so someone can take a look and provide some feedback on the overall implementation.

kmoll’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 4: 2897274-install-queue-4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

kmoll’s picture

Status: Needs work » Needs review

One thing I noticed when going through this is that it sets the users password to NOT expire. I am wondering with the default for that field set to 0, why its needed to go through and add the fields to the database. Could we not just leave the defaults, and if they are forced to reset the password, then it would go through and add the correct data to the user entity?

kmoll’s picture

Status: Needs review » Needs work
kalpaitch’s picture

Hi, I didn't see this issue when I created #2983448: Refactor hook_install logic to expire passwords for existing users.

Despite this I still think #2983448: Refactor hook_install logic to expire passwords for existing users is a more ideal solution, there doesn't seem to be a good reason why these fields should need to be set at all on users that existed before the module was installed.

kalpaitch’s picture

Issue summary: View changes
bisonbleu’s picture

I just ran into this issue - "Field field_last_password_reset is unknown"

Plus, on a large website with many users, I noticed that simply enabling password_policy locally takes FOREVER to complete and can lead to a WSOD. The patch in #4 fixes both of these issues (what it was intended for + the memory issue).

On the other hand, the latest patch in #2771129: "Field field_last_password_reset is unknown" while importing configuration on site without password_policy activated does not address/fix the memory issue. I've set back that RTBC issue to Needs review to get some feedback/guidance.

gambry’s picture

Hey @kalpaitch I see your point, however having all the users standardised by applying default values to the fields looks to me a better solution than storing the install_time and processing it everytime you need to query users from password_policy point of view.
It can bring technical debt - and so bugs - if we forget about it.

I feel queuing would improve performances enough to stop WSOD?

  1. +++ b/password_policy.install
    @@ -9,19 +9,18 @@
    +    $queue_factory = \Drupal::service('queue');
    +    /** @var \Drupal\Core\Queue\QueueInterface $queue */
    +    $queue = $queue_factory->get('password_policy_update_existing_users');
    

    Should this be outside the foreach? The 'queue' service and the queue itself don't need to be loaded on each loop.

  2. +++ b/password_policy_history/password_policy_history.install
    @@ -5,31 +5,36 @@
    +    /** @var \Drupal\Core\Queue\QueueFactory $queue_factory */
    +    $queue_factory = \Drupal::service('queue');
    +    /** @var \Drupal\Core\Queue\QueueInterface $queue */
    +    $queue = $queue_factory->get('password_policy_history_create_existing_user_data');
    

    Same as above

  3. +++ b/src/Plugin/QueueWorker/PasswordPolicyQueueWorkerBase.php
    @@ -0,0 +1,53 @@
    +    $timestamp = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, REQUEST_TIME);
    

    Inject 'datetime.time' service and use $time->getRequestTime() instead of REQUEST_TIME.

gambry’s picture

I've been thinking about this for a bit, and I have few questions:

  1. Why queueing and not - for example - a batch process?
    If the users are - let's say - 1milion, how long will it take for the queue to clean, having 30sec (according to plugin definition) cycles? Is there any possibility for user data to be overridden if user updates his/her password_policy fields while user's queued item still needs to be processed?
  2. Is setting this values strongly necessary?
    Don't fields default values do the same, without all users to be processed?
mxh’s picture

Why queueing and not - for example - a batch process?
If the users are - let's say - 1milion, how long will it take for the queue to clean, having 30sec (according to plugin definition) cycles?

In the past, I tried to run long, batched updates via drush updb which then resulted in connection timeouts (MySQL is gone) because the main Drush process did not communicate with MySQL in that time period.

mxh’s picture

$user_ids = \Drupal::entityTypeManager()->getStorage('user')->getQuery()->execute();

This approach neither solves the problem, as the returned array can be extremely big too. A generator might help at this place, alternatively create a queue item which itself throws the next item to process into the queue.

navneet0693’s picture

StatusFileSize
new9.13 KB

Re-rolled against latest version.

navneet0693’s picture

Status: Needs work » Needs review
navneet0693’s picture

StatusFileSize
new8.83 KB

that was a bad patch.

kristen pol’s picture

Version: 8.x-3.x-dev » 4.0.x-dev
Priority: Critical » Major

Critical errors should be reserved for fatal errors and similar. Moving this to major.

kristen pol’s picture

Priority: Major » Normal
Issue tags: +Needs reroll

Given this issue has been quiet a few years, I'm moving it to normal priority. And it needs a reroll.

kristen pol’s picture

Status: Needs review » Needs work

Fixing status.

littlepixiez made their first commit to this issue’s fork.

littlepixiez’s picture

I made a fork here but I believe the issue I am experiencing is slightly different - still getting an issue with [error] InvalidArgumentException: Field field_last_password_reset is unknown. in Drupal\Core\Entity\ContentEntityBase->getTranslatedField() (line 616 of /var/www/html/web/core/lib/Drupal/Core/Entity/ContentEntityBase.php). on a fresh config import (from existing) on 4.0.x-dev.

The greater issue underlying seems to be more involved, in terms of what's the best approach going forward. I would potentially say a batch process in post_update (as mentioned Comment#13), as queues may well take a long time to process?