Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schedule command on last day of the month with a different timezone did not execute #49211

Closed
pascalvgemert opened this issue Dec 1, 2023 · 7 comments

Comments

@pascalvgemert
Copy link

Laravel Version

10.30.1

PHP Version

8.2.6

Database Driver & Version

No response

Description

Situation:

We have a schedule:

$schedule->command(ExampleCommand::class)->lastDayOfMonth('23:30');

We also have set the timezone to for the kernel to CET:

'schedule_timezone' => 'Europe/Amsterdam',

The server itself runs in UTC.

The lastDayOfMonth uses Carbon::now(), but I believe it didn't take the timezone into account.

Possible solution:

Add timezone when getting the last day of the month, like in the inTimeInterval method :

// In src/Illuminate/Console/Scheduling/ManagesFrequencies.php:562
public function lastDayOfMonth($time = '0:0')
{
    $this->dailyAt($time);

    return $this->spliceIntoPosition(3, Carbon::now($this->timezone)->endOfMonth()->day);
}

Steps To Reproduce

  1. Add scheduled command: $schedule->command(ExampleCommand::class)->lastDayOfMonth('23:30');
  2. Make sure the server runs in UTC
  3. Make sure the commands are scheduled in CET via config/app.php => 'schedule_timezone' => 'Europe/Amsterdam',
  4. Somehow try to trigger this schedule naturally
  5. Assert that it did not execute
@taylorotwell
Copy link
Member

Hey there - are you able to send in a fix PR for this?

Copy link

github-actions bot commented Dec 4, 2023

Thank you for reporting this issue!

As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub.

If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team.

Thank you!

@tntsoft
Copy link

tntsoft commented Dec 9, 2023

Created #49302 that implements the above, it requires a test though - I couldn't quite figure out how to mock the server timezone vs app config timezone bit in order to write it.

@pascalvgemert
Copy link
Author

@tntsoft I was struggling with the same issue.

Maybe we can make it work with (found on SO):

public function testRunsAt930()
{
    Event::fake();
    $this->travelTo(now()->startOfWeek()->setHour(9)->setMinute(30));
    $this->artisan('schedule:run');
    
    Event::assertDispatched(ScheduledTaskFinished::class, function ($event) {
        return strpos($event->task->command, 'your-command-name') !== false;
    });
}

I will try this tomorrow!

@jbrooksuk
Copy link
Member

Changing a server's timezone often requires restarting various services, cron being one of them. This would automatically resolve the issue you faced here.

@pascalvgemert
Copy link
Author

Update:
Still debugging, have created a test. The schedule did run so far as I can see now, but still at the wrong time. So I'm checking wether this is an issue in de code or server / env configuration.

@pascalvgemert
Copy link
Author

I will close the PR as the command has ran at the correct time and it was a configuration issue. My excuses for the trouble, but better safe than sorry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants