Laravel Scheduler
Created 6 years ago by jcastillotx

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
<?php namespace App\Console\Commands;

    use Illuminate\Console\Command;
    use Illuminate\Foundation\Bus\DispatchesJobs;
    use Modules\SponsorshipsModule\Sponsor\Contract\SponsorRepositoryInterface;
    use Modules\SponsorshipsModule\Sponsorship\Command\ChargeCard;
    use Modules\SponsorshipsModule\Sponsorship\Contract\SponsorshipRepositoryInterface;

    class NewCharge extends Command {

        use DispatchesJobs;

        /**
         * The name and signature of the console 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
<?php namespace App\Console;

    use Illuminate\Console\Scheduling\Schedule;
    use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

    class Kernel extends ConsoleKernel {

        /**
         * The Artisan commands provided by your application.
         *
         * @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
<?php protected $plugins = [];

    /**
     * The addon Artisan commands.
     *
     * @type array|null
     */
    protected $commands = [
        \App\Console\Commands\NewCharge::class
    ];

    protected $schedules = [
        'daily' => [
            \App\Console\Commands\NewCharge::class
        ]
    ];
?>
    
ryanthompson  —  6 years 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  —  6 years ago

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

emergingdzns  —  6 years 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  —  6 years ago

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

ryanthompson  —  6 years ago

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

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

    protected $schedules = [
        'hourly' => [
            ImportCheck::class,
        ],
    ];
jcastillotx  —  6 years ago

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

ryanthompson  —  6 years 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  —  6 years 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  —  6 years 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?

jcastillotx  —  5 years ago

I still have not been able to get the scheduler to work correctly.