Allow 'Cron run completed.' log message to be skipped

Created on 27 June 2022, about 3 years ago
Updated 26 May 2023, about 2 years ago

Problem/Motivation

Every time cron has ran, an info log message 'Cron run completed.' is added to the watchdog. Similar to the issue problem/motivation in #2823543: Make cron execution logging optional , this is to prevent log pollution. System processes typically produce no output when running without issues. On a production environment running cron every minute, this message alone adds 1440 log entries that have no particular value

Steps to reproduce

Proposed resolution

Allow 'Cron run completed.' log message to be skipped

Remaining tasks

  1. Write a patch
  2. Review
  3. Commit

User interface changes

To be determined.

API changes

To be determined.

Data model changes

To be determined.

Release notes snippet

The 'Cron run completed.' log message can now be skipped.

📌 Task
Status

Needs work

Version

11.0 🔥

Component
Cron 

Last updated 6 days ago

No maintainer
Created by

🇳🇱Netherlands idebr

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇦🇺Australia imclean Tasmania

    By default Drupal keeps the last 1000 log messages. While this can be increased, it comes with increased database size and processing time.

    Providing the option to not log "Cron run completed." events where there are no errors would free up a lot of space and any errors won't disappear from the database log so quickly.

    For example, we're running cron every minute on some sites. We use Ultimate Cron to send any queued email every minute, while everything else is hourly or 3 hourly.

    The reason for this is to improve the user experience. Users fill out a form which is submitted instantly when they click "Submit", with an email sent to them within a minute. Without queueing email it would take 3 or more seconds to submit the form.

  • Status changed to Needs review about 2 years ago
  • last update about 2 years ago
    Custom Commands Failed
  • I agree with @longwave, in this case there is no need to use null logger.

  • 🇦🇺Australia imclean Tasmania

    I agree with @longwave in this case, there is no need to use null logger.

    +++ b/core/lib/Drupal/Core/Cron.php
    @@ -311,8 +322,7 @@ protected function invokeCronHandlers() {
    -    $logger = $time_logging_enabled ? $this->logger : new NullLogger();
    +    $logger = $this->isDetailedLoggingEnabled() ? $this->logger : new NullLogger();
     
         // Iterate through the modules calling their cron handlers (if any):
    
  • Status changed to Needs work about 2 years ago
  • 🇺🇸United States smustgrave

    Can the tests be expanded to capture when the setting is off and on?

    Also IS still needs to be completed.

    Plus #13

  • @imclean, @smustgrave, it would be nice if you read the full version of the code, and then write comments.
    Let me clarify for you to understand.

    If you check the patch from comment 2 and comment 6 from @longwave, you will see that in the proposed solution (patch 2), null logger is redundant and it is enough to use a common if condition.

    Comment 7 mentions "pattern similar to Cron::invokeCronHandlers()" and if you look at the code it's appropriate to use a null logger to avoid adding a bunch of if conditions inside closure.

    So my comment regarding the null logger applies exclusively to the setCronLastTime method.

    @smustgrave,

    Can the tests be expanded to capture when the setting is off and on?

    \Drupal\Tests\dblog\Kernel\DbLogTest::testDbLogCron (starts on line 55) - this code covers on/off setting.

  • Updated patch included code style fixes

  • last update about 2 years ago
    29,383 pass, 5 fail
  • 🇭🇷Croatia Aporie

    Reroll for D10.0.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 2 years ago
    Custom Commands Failed
  • 🇭🇷Croatia Aporie

    Reroll for D10.0.

    Had to update my phpcs version. Though, now I have more errors than the one used by Drupal pipeline. I hope it'll make it, otherwise I'll have to call it a day.

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update about 2 years ago
    28,513 pass, 4 fail
  • 🇫🇮Finland sokru

    I agree it would be nice addition, but I don't know if we can introduce the change without a new config, the current behavior has been there for 6 years (since Drupal 8.3). At minimum it needs a change to form element descriptions for "Detailed cron logging" checkbox.

  • Status changed to Needs review almost 2 years ago
  • last update almost 2 years ago
    29,456 pass, 3 fail
  • 🇪🇸Spain psf_ Huelva

    New patch to Drupal 10.1.x.

    I used the same approach that in Cron::invokeCronHandlers().

  • last update almost 2 years ago
    30,131 pass, 3 fail
  • 🇪🇸Spain psf_ Huelva

    Same path to Drupal 11.x

  • Status changed to Needs work almost 2 years ago
  • 🇪🇸Spain psf_ Huelva

    Require fix tests.

  • 🇭🇷Croatia Aporie

    Reroll of #16 for 10.3.6 introducing this commit

  • 🇧🇪Belgium borisson_ Mechelen, 🇧🇪

    This needs to be changed to use a branch instead of just a patch.

    Looking at the implementation, I'm not sure we can add the config factory at that place in the argument list, since it will break implementations that have the other arguments already? It is a required argument, so maybe it is in the right place. I'm not sure about this.

  • Merge request !12821Cron, do not dblog when detailed loggin disabled. → (Open) created by Aporie
  • Pipeline finished with Failed
    11 days ago
    #555135
  • Pipeline finished with Failed
    11 days ago
    #555152
  • Pipeline finished with Failed
    11 days ago
    #555160
  • Pipeline finished with Failed
    11 days ago
    #555165
  • 🇬🇧United Kingdom longwave UK

    We don't need to make this so complicated. Given that we already do

        $time_logging_enabled = \Drupal::config('system.cron')->get('logging');
    

    then I think it's fine to copy-paste that to run() and be done here; we can handle injecting the config factory as a dependency in another issue.

  • 🇭🇷Croatia Aporie

    I think we shouldn't be too far from a green pipeline.

    It could require more tests though, to make sure when "logging" returns true or false.

    For now, I've been working my way around to get it green. Dunno why it failed on the last one, will check tomorrow.

  • 🇬🇧United Kingdom longwave UK

    As #26 says, you can't just add a new argument to the constructor, especially in the middle of the argument list; contrib or custom code may extend this class - for example I'm fairly sure Ultimate Cron in contrib extends this service. If we are going to do that we need to add it to the end of the list and provide backward compatibility.

  • 🇭🇷Croatia Aporie

    Yeah, it's kind of right.

    Anyone who extended the cron might struggle with this new constructor.

    So what's your stand? You think we should go for a static call?

    As per discussion the null logger is actually needed, because it prevents code from failing, in case the logger is needed.

    But I'm fine with any of the working solutions. True dat, if we implement a new constructor, anyone who extended the cron will have to make changes ....

  • 🇭🇷Croatia Aporie

    aporie changed the visibility of the branch 3292849-allow-cron-run-without-dependency-injection to hidden.

  • 🇭🇷Croatia Aporie

    aporie changed the visibility of the branch 3292849-allow-cron-run-without-dependency-injection to active.

  • Merge request !12844Cron, do not dblog when detailed logging disabled. → (Open) created by Aporie
  • Pipeline finished with Failed
    9 days ago
    Total: 119s
    #556856
  • Pipeline finished with Running
    9 days ago
    #556892
  • Pipeline finished with Success
    6 days ago
    Total: 500s
    #558849
  • 🇭🇷Croatia Aporie

    Hi @longwave,

    Sorry I overlooked your message in #30, it contained all the answers to my questions.

    So ... I've been investigating, and I didn't manage to set @trigger_error('...', E_USER_DEPRECATED), at least not without breaking ultimate_cron. I actually need this patch for a project on which I have ultimate_cron installed, so it was easy to test.

    I think the issue is that, whereas extending Cron.php constructor in ultimate_cron/src/UltimateCron.php, they alter the service provider in ultimate_cron/src/UltimateCronServiceProvider.php and add the config.factory using addMethodCall:

    /**
       * {@inheritdoc}
       */
      public function alter(ContainerBuilder $container) {
        // Overrides cron class to use our own cron manager.
        $container->getDefinition('cron')
          ->setClass('Drupal\ultimate_cron\UltimateCron')
          ->addMethodCall('setConfigFactory', [new Reference('config.factory')]);
      }
    

    I didn't really know which action to take, so for now I just implemented it using Drupal static call and without implementing a null constructor variable and adding the deprecation notice.

  • The Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".

    This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.

    Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Production build 0.71.5 2024