joomla / joomla-cms

Home of the Joomla! Content Management System
https://www.joomla.org
GNU General Public License v2.0
4.79k stars 3.66k forks source link

Task Scheduler Cronjob schedule type ran on the wrong day #42459

Open bascherz opened 1 year ago

bascherz commented 1 year ago

Steps to reproduce the issue

I do not know if this is reproducible. It happened on a live site and I don't have time to mess with it. But this is what happened.

I had the task setup using the "Cron Expression" interface to run at 10 after midnight (5:10am UTC) on the 20th of every month (all weekdays selected). The job executed on Dec 02, 2023. The task is a Community Builder unconstrained task that runs a custom php script that has been thoroughly tested using the Task Scheduler's "Run Test". When run manually, there has never been an error like this one.

Screenshot

Expected result

I expected the task to run on the 20th, not the 2nd, without errors.

Actual result

Here is the log file from the erroneous run:

#Date: 2023-12-02 05:10:04 UTC
#Software: Joomla! 4.4.1 Stable [ Pamoja ] 28-November-2023 16:00 GMT

#Fields: date   time    priority    message
2023-12-02  05:10:04    INFO    Running task#01 'Email the latest newsletter'.
2023-12-02  05:10:04    ERROR   Task> Action failed! Error: :: Action 121 :: Code failed. Error: Failed opening required '[ROOT]/components/com_comprofiler/scripts/emailnewsletter.php' (include_path='.:/opt/alt/php81/usr/share/pear:/opt/alt/php81/usr/share/php:/usr/share/pear:/usr/share/php')
2023-12-02  05:10:09    WARNING Task#01 was unlocked.

System information (as much as possible)

Software: Joomla! 4.4.1 Stable [ Pamoja ] 28-November-2023 16:00 GMT PHP Built On: Linux 4.18.0-477.10.1.lve.el8.x86_64 #1 SMP Wed May 17 07:12:55 EDT 2023 x86_64 Database Type: mysql Database Version: 10.6.16-MariaDB PHP Version 8.1.25 Server API: LiteSpeed V8.1 Cloudlinux 1.3 Community Builder: v2.9.0, Auto Actions: v9.1.0+build.2022.10.14.14.57.50.7b1989310

Additional comments

I am now trying the Interval scheduling method. The task has not yet executed. I expect it to run on 2023-12-23.

bascherz commented 10 months ago

I switched to using a monthly interval for this particular case. It turns out the basic issue is that when scheduling, say, a monthly interval, a specific day of the month is entered. But instead of using that information to determine the next scheduled execution time, it is using the last time the task ran (e.g., Test Run) plus the selected interval (a month in this case).

The code below is from ExecRuleHelper.php. Based on the comments, it appears to be unfinished. Clearly in the case for 'interval', no attempt to use the specified day-of-month is made. The $nextExec is clearly only ever based on $lastExec.

/**
 * @param   boolean  $string    If true, an SQL formatted string is returned.
 * @param   boolean  $basisNow  If true, the current date-time is used as the basis for projecting the next
 *                              execution.
 *
 * @return ?Date|string
 *
 * @since  4.1.0
 * @throws \Exception
 */
public function nextExec(bool $string = true, bool $basisNow = false)
{
    // Exception handling here
    switch ($this->type) {
        case 'interval':
            $lastExec = Factory::getDate($basisNow ? 'now' : $this->getFromTask('last_execution'), 'UTC');
            $interval = new \DateInterval($this->rule->exp);
            $nextExec = $lastExec->add($interval);
            $nextExec = $string ? $nextExec->toSql() : $nextExec;
            break;
        case 'cron-expression':
            // @todo: testing
            $cExp     = new CronExpression((string) $this->rule->exp);
            $nextExec = $cExp->getNextRunDate('now', 0, false, 'UTC');
            $nextExec = $string ? $this->dateTimeToSql($nextExec) : $nextExec;
            break;
        default:
            // 'manual' execution is handled here.
            $nextExec = null;
    }

    return $nextExec;
}

The workaround until the above code is completed is to edit the database table #__schedulertasks and set next_execution to the date/time you want after any test run.

As for the cron-expression case, I am not sure what the issue was there but it is likely it's similar to the interval case. This code just does not appear to be finished yet.

rdeutz commented 2 months ago

I have made a fix (#44061) for the interval, would be great if you have time to test it. I have also looked at the cron-execution and it seems to me doing it in the right way, not tested all possibilities. There are some things I would do different, but that is more the UI that functionality.

bascherz commented 2 months ago

I think the cron-execution may have been worked on since I created my ticket.

I'll give it a workout this weekend and get back to you. It looks like I have the ability to approve the change, but I have never done so. Please advise on next steps after testing.

Thanks! Bruce


*Bruce Scherzinger b @.>@. @.>*

On Thu, Sep 12, 2024 at 7:18 AM Robert Deutz @.***> wrote:

I have made a fix (#44061 https://github.com/joomla/joomla-cms/pull/44061) for the interval, would be great if you have time to test it. I have also looked at the cron-execution and it seems to me doing it in the right way, not tested all possibilities. There are some things I would do different, but that is more the UI that functionality.

— Reply to this email directly, view it on GitHub https://github.com/joomla/joomla-cms/issues/42459#issuecomment-2346015054, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD3BBERKIVPAC3SO4A5L4H3ZWFZ7FAVCNFSM6AAAAABAFEUSYGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBWGAYTKMBVGQ . You are receiving this because you authored the thread.Message ID: @.***>

bascherz commented 2 months ago

Hey Robert, here are my observations.

It has been my observation that the next execution time is honored by the task scheduler. So my testing here was focused on making sure that time is set correctly in each case. It seems we still have an issue with the cron scheduler (seems like a UI issue) that may already be fixed in 5.2. But the fact the interval-months computation is going a month past the next occurrence is an issue with these changes.

Hope this helps.

Regards, Bruce


*Bruce Scherzinger b @.>@. @.>*

On Thu, Sep 12, 2024 at 5:33 PM Bruce Scherzinger < @.***> wrote:

I think the cron-execution may have been worked on since I created my ticket.

I'll give it a workout this weekend and get back to you. It looks like I have the ability to approve the change, but I have never done so. Please advise on next steps after testing.

Thanks! Bruce


*Bruce Scherzinger b @.>@. @.>*

On Thu, Sep 12, 2024 at 7:18 AM Robert Deutz @.***> wrote:

I have made a fix (#44061 https://github.com/joomla/joomla-cms/pull/44061) for the interval, would be great if you have time to test it. I have also looked at the cron-execution and it seems to me doing it in the right way, not tested all possibilities. There are some things I would do different, but that is more the UI that functionality.

— Reply to this email directly, view it on GitHub https://github.com/joomla/joomla-cms/issues/42459#issuecomment-2346015054, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD3BBERKIVPAC3SO4A5L4H3ZWFZ7FAVCNFSM6AAAAABAFEUSYGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBWGAYTKMBVGQ . You are receiving this because you authored the thread.Message ID: @.***>

bascherz commented 2 months ago

Oh, I forgot something. When I do a Run Test, that resets the next execution time for the minutes and hours cases, but not for the others. IMHO, Run Test should NEVER change the next execution time.


*Bruce Scherzinger b @.>@. @.>*

On Fri, Sep 13, 2024 at 7:06 PM Bruce Scherzinger < @.***> wrote:

Hey Robert, here are my observations.

  • On Sep 13, saved an existing monthly task that is scheduled to run on the 20th of every month. Now it shows the next execution will be on Oct 20, not Sep 20 as it should be. But it did get the time correct.
    • Maybe if one could edit the Next Scheduled Execution, this would provide a temporary workaround. Currently, this requires a database modification.
  • The cron scheduler UI does not allow selections in multiple interval groups. For instance, if I select day 20, when I try to select any month, the day becomes unselected. This is clearly a UI issue and it prevents me from testing cron scheduling. Note that I started with J! 5.1.4 and simply replaced the code you modified.
  • The interval-days option computes next execution time as expected.
  • The interval-minutes and -hours seem to work fine. But it would be nice to be able to specify the minute within the hour for interval-hours, similar to how you can specify the hour for interval-days. I suppose this is really what cron scheduling is for, but that's not usable in J! 5.1.4.

It has been my observation that the next execution time is honored by the task scheduler. So my testing here was focused on making sure that time is set correctly in each case. It seems we still have an issue with the cron scheduler (seems like a UI issue) that may already be fixed in 5.2. But the fact the interval-months computation is going a month past the next occurrence is an issue with these changes.

Hope this helps.

Regards, Bruce


*Bruce Scherzinger b @.>@. @.>*

On Thu, Sep 12, 2024 at 5:33 PM Bruce Scherzinger < @.***> wrote:

I think the cron-execution may have been worked on since I created my ticket.

I'll give it a workout this weekend and get back to you. It looks like I have the ability to approve the change, but I have never done so. Please advise on next steps after testing.

Thanks! Bruce


*Bruce Scherzinger b @.>@. @.>*

On Thu, Sep 12, 2024 at 7:18 AM Robert Deutz @.***> wrote:

I have made a fix (#44061 https://github.com/joomla/joomla-cms/pull/44061) for the interval, would be great if you have time to test it. I have also looked at the cron-execution and it seems to me doing it in the right way, not tested all possibilities. There are some things I would do different, but that is more the UI that functionality.

— Reply to this email directly, view it on GitHub https://github.com/joomla/joomla-cms/issues/42459#issuecomment-2346015054, or unsubscribe https://github.com/notifications/unsubscribe-auth/AD3BBERKIVPAC3SO4A5L4H3ZWFZ7FAVCNFSM6AAAAABAFEUSYGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNBWGAYTKMBVGQ . You are receiving this because you authored the thread.Message ID: @.***>