Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Schedule cron job laravel

I will like to know how to schedule a cron job to run everyday at 00:01.

I have created JOB in App/Jobs folder

<?php
namespace App\Jobs;
use App\Models\Result;
use App\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use DB;

set_time_limit(0);

class UpdateActive extends Job implements SelfHandling
{
    public static function ActiveUpdate()
    {
        Result::update(['draw_id' => 1, 
                        'isactive' => 0
                       ]);
     }

   public static function downGrade()
   {
   try {
        UserRole::update(['permission' => 1,
                         'isactive' => 2    
        ]);
   } catch (QueryException $e ) {
     //handle error
   }
   }

   public static function handle() 
   {
     self::ActiveUpdate();
     self::downGrade();
   }
 }

in App/Console/Kernel.php I have added this link to the schedule method

protected function schedule(Schedule $schedule)
    {
        /*$schedule->command('inspire')
                 ->hourly(); */
        $schedule->call(function () {
            $check_draw = \App\Jobs\UpdateActive::ActiveUpdate();

        })->everyMinute();
    }

Please note I have used everyMinute for test purpose

In crontab -e I then added

* * * * * php /home/vagrant/Code/projects/artisan schedule:run 1>> /dev/null 2>&1

but the schedule doesn't seem to run i think because when i check my results table the isactive field hasn't changed.

I am wondering where I am going wrong please. If anyone has done this in L5. What am I missing?

like image 207
Baako Avatar asked Jul 21 '15 23:07

Baako


1 Answers

I will like to know how to schedule a cron job to run everyday at 00:01.

So you want it to run daily at 00:01?

Answer:

protected function schedule(Schedule $schedule)
{
    $schedule->call(function () {
        $check_draw = \App\Jobs\UpdateActive::ActiveUpdate();
    })->dailyAt("00:01");
}

Aside: (edited in response to your comments)

This is how I would do it:

The command:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class UpdateActiveCommand extends Command
{
    protected $signature = 'update-active';

    protected $description = 'Update something?';

    public function handle()
    {
        try {
            $this->comment("Update active...");
            $this->updateActive();

            $this->comment("Downgrade...");
            $this->downGrade();

            $this->info("Done!");
        } catch (QueryException $e ) {
            $this->error($e->getMessage());
        }
    }

     private function updateActive()
     {
        Result::update([
            'draw_id'  => 1, 
            'isactive' => 0,
        ]);
     }

     private function downGrade()
     {
        UserRole::update([
            'permission' => 1,
            'isactive'   => 2,
        ]);
     }
}

The scheduler:

<?php

namespace App\Console;

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

class Kernel extends ConsoleKernel
{
    protected $commands = [
        \App\Console\Commands\UpdateActiveCommand::class,
    ];

    protected function schedule(Schedule $schedule)
    {
        $schedule->command('update-active')
                 ->dailyAt('00:01')
                 ->sendOutputTo(storage_path('logs/update-active.log'))
                 ->emailOutputTo('[email protected]');
    }
}

If you did it this way you could also run it from the command line with php artisan update-active and see the output.

like image 85
ryanwinchester Avatar answered Oct 13 '22 12:10

ryanwinchester