Status message

Maintenant, vous regardez: Task Scheduling in Laravel/Lumen

Task Scheduling in Laravel/Lumen

The Laravel framework allows you to set up scheduled tasks so that you don't have to worry about setting them up at the system level. You can get rid of that complex cron syntax while setting up scheduled tasks since Laravel allows you to define them in a user-friendly way.

Here’s a text representation that should clarify how cron works

  1. # Use the hash sign to prefix a comment
  2. # +---------------- minute (0 - 59)
  3. # | +------------- hour (0 - 23)
  4. # | | +---------- day of month (1 - 31)
  5. # | | | +------- month (1 - 12)
  6. # | | | | +---- day of week (0 - 7) (Sunday=0 or 7)
  7. # | | | | |
  8. # * * * * * command to be executed
  9. #-----------------------------------------------------------

1) let's create a command:
app/Console/Commands

  1. namespace App\Console\Commands;
  2.  
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Carbon;
  5. use Illuminate\Support\Facades\Mail;
  6.  
  7. class CheckLogFileSize extends Command
  8. {
  9. /**
  10.   * The name and signature of the console command.
  11.   *
  12.   * @var string
  13.   */
  14. protected $signature = 'logfilesize:check';
  15.  
  16. /**
  17.   * The console command description.
  18.   *
  19.   * @var string
  20.   */
  21. protected $description = 'Check the log file size daily and send email if filesize grows fast';
  22.  
  23. /**
  24.   * Create a new command instance.
  25.   *
  26.   * @return void
  27.   */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32.  
  33. /**
  34.   * Execute the console command.
  35.   *
  36.   * @return mixed
  37.   */
  38. public function handle()
  39. {
  40. //2019-06-05
  41. $today = Carbon::today()->format('Y-m-d');
  42. $file = storage_path('logs'). '/lumen-' . $today . '.log';
  43.  
  44. if(!file_exists($file)) exit();
  45.  
  46. $size = filesize($file);
  47. $logsize = round($size / 1024 / 1024, 1);// in mb
  48.  
  49. if($logsize >= 100){
  50.  
  51. $content = $file . ' size is growing very fast, please deal with it as soon as possible.';
  52. $toMail = '<a href="mailto:546131320@qq.com">546131320@qq.com</a>';
  53.  
  54. Mail::raw($content, function ($message) use ($toMail, $logsize) {
  55. $message->subject('Warning!!!, Log file size now is ' . $logsize . 'MB');
  56. $message->to($toMail);
  57. });
  58.  
  59. }
  60. }
  61. }

2) Edit Kernel.php file

  1. namespace App\Console;
  2.  
  3. use Illuminate\Console\Scheduling\Schedule;
  4. use Laravel\Lumen\Console\Kernel as ConsoleKernel;
  5.  
  6. class Kernel extends ConsoleKernel
  7. {
  8. /**
  9.   * The Artisan commands provided by your application.
  10.   *
  11.   * @var array
  12.   */
  13. protected $commands = [
  14. //mail test
  15. Commands\SendMailCommand::class,
  16. Commands\CheckLogFileSize::class,
  17. ];
  18.  
  19. /**
  20.   * Define the application's command schedule.
  21.   *
  22.   * @param \Illuminate\Console\Scheduling\Schedule $schedule
  23.   * @return void
  24.   */
  25. protected function schedule(Schedule $schedule)
  26. {
  27. //
  28. $schedule->command('logfilesize:check')
  29. ->everyThirtyMinutes();
  30. }
  31. }

For testing:

  1. php artisan logfilesize:check

3) Add scheduler to cron by running crontab -e

  1. crontab -e

Add the following code to cron editor:

  1. * * * * * cd /path-to-your-project && php artisan schedule:run >> /dev/null 2>&1