Skip to content

Schedule gets registered with every artisan call #51354

Closed
@Plytas

Description

@Plytas

Laravel Version

11.7.0

PHP Version

8.3.6

Database Driver & Version

No response

Description

When running any artisan command (just php artisan does the same), the code inside console.php route file and/or ->withSchedule() bootstrap method gets executed. Usually this wouldn't be a big deal, because the code should only register cron events. However, sometimes dynamic scheduling might involve database queries which can inflict unwanted performance penalty or in worst case scenario fail in CI setup.

We noticed this in our CI while upgrading from Laravel 10 to Laravel 11 (with slimmed down app structure). Our scheduler needs to be dynamic and it makes a DB query for that reason. When CI is setting up the application it runs composer install which then runs php artisan package:discover --ansi as a composer post-autoload-dump script. This is standard in every Laravel project. Since package:discover is an artisan command it boots up the scheduler as well. At this stage in CI database is not yet available thus we get a QueryException: SQLSTATE[HY000] [2002] Connection refused.

I have checked and this wasn't the case with Laravel 10. I'm not sure if this is now intended or not, but I would expect scheduler code to only execute when calling php artisan schedule:run and php artisan schedule:work.

Steps To Reproduce

  • Set up new Laravel 11 project.
  • Add dd(); inside console.php route file or inside ->withSchedule() app bootstrap method.
  • Run php artisan and observe the command to fail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions