Laravel Scheduler

jcastillotx - 4 weeks ago

Ok I have followed the laravel documentation and what was explained to me about the laravel scheduler. However, it does not work.

  1. In app/Console/Commands/ I have created a command
@var string
         */
        protected $signature = 'charge:new';

        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Scheduled Card Charger';

        // Sponsorship setup
        private $sponsorships = null;
        private $sponsors     = null;

        /**
         * RecurringCharges constructor.
         *
         * @param \Modules\SponsorshipsModule\Sponsorship\Contract\SponsorshipRepositoryInterface $sponsorships
         */
        public function __construct( SponsorshipRepositoryInterface $sponsorships, SponsorRepositoryInterface $sponsors ) {

            parent::__construct();

            $this->sponsorships = $sponsorships;
            $this->sponsors     = $sponsors;
        }

        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle() {

            $this->dispatch( new ChargeCard( $this->sponsorships, $this->sponsors ) );

        }
    }
?>
  1. I added the command to the console kernel
@var array
         */
        protected $commands = [
            'App\Console\Commands\NewCharge'
        ];

        /**
         * Define the application's command schedule.
         *
         * @param  \Illuminate\Console\Scheduling\Schedule $schedule
         *
         * @return void
         */
        protected function schedule( Schedule $schedule ) {

            $schedule->command( 'charge:new' )->timezone('America/Chicago')->daily();
        }

        /**
         * Register the Closure based commands for the application.
         *
         * @return void
         */
        protected function commands() {
            require base_path( 'routes/console.php' );

        }
    }?>
  1. I created a command within my module. I also added the command to the service provider
@type array|null
     */
    protected $commands = [
        \App\Console\Commands\NewCharge::class
    ];

    protected $schedules = [
        'daily' => [
            \App\Console\Commands\NewCharge::class
        ]
    ];
?>
    

ryanthompson - 4 weeks ago

I wouldn't mess with the kernels. Just add your schedule and commands to you addon service provider. Perhaps revert the kernels to how they are in Pyro's branch.

jcastillotx - 4 weeks ago

Ryan do you have an example of something you did that is scheduled?

emergingdzns - 4 weeks ago

@jcastillotx are you certain that the schedule is NOT getting fired? You may want to check your laravel logs as it's possible that it's getting run but hitting an error. Those scheduled tasks are often hard to debug. Before I found out that you could put them in the service provider (as suggested by @ryanthompson ) I had built my own console commands as you did but I had to then also add the trigger to the routes\console.php file. Otherwise it would not trigger.

jcastillotx - 4 weeks ago

What the logs told me was nothing scheduled to run. Do you have an example of your schedule code?

ryanthompson - 4 weeks ago

Here's a working example from https://zimmermanhonda.com

    protected $commands = [
        ImportCheck::class,
        ImportVehicles::class,
    ];

    protected $schedules = [
        'hourly' => [
            ImportCheck::class,
        ],
    ];

jcastillotx - 4 weeks ago

what does Importcheck look like? I appreciate the help Ryan. This last thing I need to be complete with the sponsorship system

ryanthompson - 4 weeks ago

Just following up on this - Scheduler will only run daily at midnight which is why you're not seeing things fire off. If you instead try everyMinute it'll fire off all the time. Or use a cron expression!

jcastillotx - 4 weeks ago

I have a cron scheduler ** but it says no command scheduled. Ill go through everything again to verify. Are the ImportCheck:class within the module Console or Command folder?

emergingdzns - 3 weeks ago

@ryanthompson should it be able to use any cron syntax? I have mine set to * 6 * * *. Shouldn't that fire at 6 am every day?