A short period of the year left, and I needed data that was taking way to long to load. Of course, one can assume that your query’s or datamodel are not good enough, but

Assumptions are the mother or all **** ups!

In this case the assumption maybe right, but none the less…….

I decided it was time for nightly Cron Jobs. As we also implemented Elastic Search it was a nice to have because we can build our search index at night as well.

I will try to take you trough the steps of this (easy!) process to get it up and running.

Step 1; Create your Class!

Start with opening your preferred IDE (and that of course is Jetbrains PHPStorm ;-) ) Navigate to the app/Console/Commands folder and create a new file. Name it “MyFirstCronClass.php” and open it up.

1. Set your namespace (eg. app\Console\Commands).
2. Use proper whitespace.
3. Use the Illuminate\Console\Command; Facade
4. Start your class by extending Command. (eg. class MyFirstCronClass extends Command );
Your code by now:

<?php

namespace app\Console\Commands;

use Illuminate\Console\Command;

class MyFirstCronClass extends Command
{

Step 2; Fill with appropriate methods and parameters

1. Create a protected parameter $name = “MyFirstCronClass”;. This is used to call your Class in the Console.
2. A protected parameter $description = “Do something with my first Cron Class”;. This one is just a description.
3. And a public function handle(). The actual function. Lets give it a comment with some nonsense information.
Your code:

<?php

namespace app\Console\Commands;

use Illuminate\Console\Command;

class MyFirstCronClass extends Command
{
    /**
     * The console command name.
     *
     * @var string
     */
    protected $name = 'MyFirstCronClass';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Do something with my First Cron Class.';

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

Step 3; let it do something

Well, right now you have a loose class that doesn’t do anything yet, lets fill’er up!
Change your handle function;

 public function handle()
    {
        //Well yes something now!
        dd("Hello awesome great Master of California that shines trough the curtains of Artisan in the way of Dzjengis Kahn when he was visiting Hawaii");
    }

(Yes, indeed, that doesn’t make any sense at all.)

Save it, and open up the following file: app\Console\Kernel.php
In the $commands array, add your class and in the Schedule function, let us add the Cron for lets say, every hour?
Then your code will look like this:

<?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\MyFirstCronClass',
    ];

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

Well, every hour something will be printed (I hope not my line of text…. Be original!)

Let us test if our Class works! Open up your terminal, navigate to the main folder of your project and type in:

php artisan MyFirstCronClass

If everything went right, you will see the beautiful text on your screen ;-)

Step 4; make it an actual Cron Job

Because Laravel allows us to create our own Cron Schedules we don’t need Crontab in our server anymore. Well, at least not as much as normally. We need to call 1 line and Laravel (Artisan) will do the rest.
Open up your console and openup your crontab

sudo crontab -e

Add the following line at the end of the file:

* * * * * php /var/www/artisan schedule:run >> /dev/null 2>&1

Done!

Think of the possibilities, for instance; all your CronJobs in Version Control. This code will ofcourse be commited to your VCS repo, in other words, all your co-workers can view, alter and learn from it.