I enabled the update feature that emails me whenever the site has an update available. I noticed that the time the email gets sent is always incremented by one cron run. For example, I have cron running hourly, so if there are updates needed it will email me at 8am, the next day at 9am, the next day at 10am, etc. But, based on the math, whatever interval cron runs at the email will likely always be one run later.

In hook_cron, the if statement requires the current time to be greater than interval (in my case, 24hrs). If it is exactly 24hrs, it will not pass.

function update_cron() {
  $frequency = variable_get('update_check_frequency', 1);
  $interval = 60 * 60 * 24 * $frequency;
  // Cron should check for updates if there is no update data cached or if the
  // configured update interval has elapsed.
  if (!_update_cache_get('update_available_releases') || ((time() - variable_get('update_last_check', 0)) > $interval)) {
    update_refresh();
    _update_cron_notify();
  }
}

Also, 'update_last_check' is set in _update_refresh(), after some work is done. You only need enough time between a cron run and the setting of 'update_last_check' for a second to turn over to guarantee this not running at the same time.

In my install, I moved the variable_set for 'update_last_check' from _update_refresh() to the first thing inside this if, and change the operator to >= (as the difference more often ends up exactly zero). It has been working for several days, sending the update email at the same time.

Also, it would be nice if there was a setting to say exactly when to send email updates: #3389183: Allow defining when to check for updates and send alert emails.

Comments

damien tournoud’s picture

Version: 6.20 » 8.x-dev

Makes sense. I guess 7.x and 8.x are affected, so moving there for study.

davidhernandez’s picture

I haven't tested 7/8, but did look over the code. Much of the code has changed, but the variable_set is still towards the end of the process, so I would also suspect 7/8 has the same issue.

bfroehle’s picture

wouldn't it be easier to just change the interval to something like $interval = 60 * 60 * 24 * $frequency - 10; where the constant was chosen to be 10 seconds or whatever.

davidhernandez’s picture

I've been testing this over the last couple weeks, and I'm getting mixed results. Sometimes it works perfectly, and some times it still misses (it depends on the consistency of cron, and how long it takes to run)

@bfroehle - Yes, putting in some arbitrary delay would work. That is the other solution I've been playing with. However, I think both these solutions are a bit inaccurate and inelegant. They both have a set of cases where they won't work well for some setups.

I would rather something more exact, like specifying the time to send. Speaking from a sys admin/site maintainer point of view, I like to control when the emails go out. Most people have a work flow where they prefer to receive daily statuses within certain time frames, like in the morning. That might just be me though. I'm assuming the update is based on whenever the project version is committed? So this could be any time during the day?

Would setting a variable for the delay be acceptable? In the testing I've done, some of my sites would be fine with 10 seconds, some would need more than 30. I'm sure other people vary even more.

bfroehle’s picture

The other thing to consider here is if this is even worthwhile... Is it in any way important that the email gets sent at a particular time every day?

davidhernandez’s picture

Scratching my own itch, as it were, it is worthwhile. As a sys admin, I prefer all emails to come at designated times. But, I agree that the population, in general, probably doesn't care.

NROTC_Webmaster’s picture

Status: Active » Needs review
StatusFileSize
new784 bytes

I took a little from both suggestions and this has been working for me. I think the important thing is to fix this for the majority of sites and not necessarily every single one. Making this small change now may help lead to fixing this issue completely in the future.

Since cron can only run every hour I took a minute off the interval and hopefully that will work for everyone since I can't imagine cron running for more than a minute on any site.

NROTC_Webmaster’s picture

StatusFileSize
new884 bytes

With comments this time.

kscheirer’s picture

Status: Needs review » Needs work

The last submitted patch, update-fix-cron-time-1137628-8.patch, failed testing.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

catch’s picture

Title: Update email send time always incremented by one cron run » The update module check drifts later every cron run
Issue tags: +Bug Smash Initiative

This is still valid for Drupal 9:

function update_cron() {
  $update_config = \Drupal::config('update.settings');
  $frequency = $update_config->get('check.interval_days');
  $interval = 60 * 60 * 24 * $frequency;
  $last_check = \Drupal::state()->get('update.last_check', 0);
  if ((REQUEST_TIME - $last_check) > $interval) {
 // If the configured update interval has elapsed, we want to invalidate
    // the data for all projects, attempt to re-fetch, and trigger any
    // configured notifications about the new status.
    update_refresh();
    update_fetch_data();
  }
  else {
    // Otherwise, see if any individual projects are now stale or still
    // missing data, and if so, try to fetch the data.
    update_get_available(TRUE);
  }
  $last_email_notice = \Drupal::state()->get('update.last_email_notification', 0);
  if ((REQUEST_TIME - $last_email_notice) > $interval) {
    // If configured time between notifications elapsed, send email about
    // updates possibly available.
    module_load_include('inc', 'update', 'update.fetch');
    _update_cron_notify();
  }

Making the update interval a few minutes shorter, and the e-mail interval a few minutes shorter than that, seems like it'd be sensible.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ameymudras’s picture

Version: 9.4.x-dev » 9.5.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.45 KB

Similar to #7 and #8 subtracting 60 seconds to adjust time

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kristen pol’s picture

Thanks for the patch. Tagging... I understand the issue summary but it would be good to have explicit steps to reproduce and someone needs to test the patch manually.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Also tagging for tests;

luke.stewart’s picture

My suspicion would be that we need to consider Day Light savings here.

Additionally the REQUEST_TIME global constant has also been deprecated
https://www.drupal.org/node/2785211

Currently the patch assumes each day is 24 hours long. Perhaps the use of datetime.diff would prevent shifts occurring due to day light savings.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ressa’s picture

Issue summary: View changes

I agree, it would be nice to be able to define when to send the email alert, and created #3389183: Allow defining when to check for updates and send alert emails.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.