Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do you use laravel scheduler instead of just cron job?

I am sending birthday mail to users every day at 15:00. I created a command that does the job. Sending mail like below

<?php

namespace App\Console\Commands;

use App\Notifications\BirthdayMessage;
use App\User;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Notification;

class BirthdayNotification extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cron:birthday';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'This command sends users birthday mail';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        // get users who was born today
        $users = User::whereMonth('birthdate', '=', Carbon::now()
            ->format('m'))
            ->whereDay('birthdate', '=', Carbon::now()->format('d'))->get();

        // notify users
        foreach ($users as $user)
        {
            $user->notify(new BirthdayMessage());
        }
    }
}

It scheduled as below,

$schedule->command("cron:birthday")->dailyAt("15:55");

I added this to my task scheduler like below. https://gist.github.com/Splode/94bfa9071625e38f7fd76ae210520d94

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My question here why I don’t simply add C:\php\php.exe C:\iis\artisan cron:birthday instead of C:\php\php.exe C:\iis\artisan schedule:run

I couldn’t spot the difference of doing schedule:run. Thank you for answers

>Solution :

What if you want to run a command for their birthday, maybe a reminder a few days early, a birthday reminder to their friends, a marriage reminder, a marriage greeting? You could have hundreds of scheduled commands. You want these to be controlled and changed through code, not configuration.

The scheduler allows you to have a single place in your kernel where all your scheduled commands are. It gives you an excellent overview and will enable you to add an unlimited amount while organizing them in the code. You only need to run one scheduler and not edit your crontab for every schedule you add or modify.

The documentation explains this pretty eloquently, https://laravel.com/docs/9.x/scheduling#introduction:

In the past, you may have written a cron configuration entry for each
task you needed to schedule on your server. However, this can quickly
become a pain because your task schedule is no longer in source
control and you must SSH into your server to view your existing cron
entries or add additional entries.

Laravel’s command scheduler offers a fresh approach to managing
scheduled tasks on your server. The scheduler allows you to fluently
and expressively define your command schedule within your Laravel
application only. Only a single cron entry
is needed on your when using the scheduler server. Your task schedule is defined in the
app/Console/Kernel.php file’s schedule method. To help you get
started, a simple example is defined within the method.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading