laravel command scheduler allows you to define clear and smooth in laravel command. And you can just use a cron on the server when using the scheduler. Scheduling method is defined in the schedule app \ Console \ kernel.php file.
Start scheduling
Only need to use the following schedule cron items added to the server.
* * * * * php /your-project/artisan schduel:run >> /dev/null 2>&1
The above cron will be called once per minute laravel command scheduler. Execution schedule: When the run command, laravel runs scheduled tasks based on your schedule.
Defining schedules
You can define all scheduled tasks app \ schedule method Console \ kernel class.
Example: We plan to call a Closure at midnight. In Closure perform database queries to clear a table
<?php
namespace App\Console;
use DB;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Artisan command provided by the application
*
* @Var array
*/
protected $commands = [
\App\Console\Commands\Inspire::class,
];
/**
* Define the application command schedule
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
DB::table('recent_users')->delete();
})->daily();
}
}
Artisan command schedule
In addition to consure scheduling, you can call Artisan commands and operating system commands. For example, you can communicate commands to the command name or the name of the class methods to schedule a Artisan command.
$schedule->command("emails:send --force")->daily();
$chedule->command(EmailsCommand::class,['--force'])->daily();
Queue scheduling tasks
The method can be used for job scheduling queue. This method provides a quick way to schedule tasks to schedule tasks without having to create a closure using the manual method call.
$schedule->job(new Hertbeat)->everyFIveMinuites();
shell command schedule
exec can be used to issue commands to the operating system
$schedule->exec('node /home/forge/script.js')->daily();
Scheduling frequency setting
You can assign a variety of scheduling your tasks
method | description |
->cron("* * * * *") | Perform this task on schedule custom cron |
->everyMinute() | Perform a task every minute |
->everyFiveMinutes() | Every five minutes to perform a task |
->everyTenMinutes() | Every ten minutes to perform a task |
->everyFiftenMinutes() | Every 15 minutes to perform a task |
->everyThirtyMinutes() | Every half hour to perform a task |
->hourly() | Perform the task once per hour |
->hourlyAt(17) | 17 minutes to perform the task once per hour |
->daily() | Every day at midnight to perform a task |
->dailyAt('13:00') | 13:00 every day to perform a task |
->twiceDaily(1,13) | 1:00 and 13:00 each day to perform a task |
->weekly() | Perform the task once a week |
->monthly() | Once a month mission |
->monthlyOn(4,'13:00') | 13:00 executed once a month on the fourth day mission |
->quarterly() | Performed once each quarter |
->yearly() | Perform once a year |
->timezone('China/Beijing') | Set the time zone |
These methods can be combined with other constraints so as to generate a more precise schedule. For example, plan a weekly schedule Monday
$schedule->call(function(){
}) -> weekly () -> mondays () -> at ('13: 00 ') // run a weekly 13:00
$schedule->commad('fool')->weekdays()
->hourly()
->timezone('Amarica/Chicago')
-> between ( '8:00', '17:00'); // Monday to Friday from 8:00 to 17:00 executed once every hour
The following additional restrictions when conditions columns
method | description |
->weekdays() | The task limit on weekdays |
->sundays() | The task limit on Sunday |
->mondays() | The task limit on Monday |
->tuesdays() | The task limit on Tuesday |
->wednesdays() | The task limit on Wednesday |
->thursdays() | The task limit on Thursday |
->fridays() | The task limit on Friday |
->saturdays() | The task limit on Saturday |
->between($start,$end) | The limits start to finish the task within the time frame |
->when(closure) | The closure function returns the task to limit the |
Time limit
between methods can be used to limit the day the task execution within a certain time frame
$schedule->command(reminds:send'')
->hourly()
->between('7:00','20:00');
Similarly, unlessbetween () method can be used to exclude task over a period of time
$schedule->command('reminds:send')
->hourly()
->unsetbetween('23:00','4:00');
Closures test limits
The method may be used to limit when performing a task according to a given test results. In other words, if a given closure returns true, if no other constraints prevent the task to run, the task will be executed.
$schedule->command('email:send')->daily()->when(function(){
return true;
});
when the reverse process when the skip can be considered. If you skip returns true, then the task is not performed.
$schedule->command('email:send')->daily()->skip(function(){
return true;
});
When the chained calls more when, scheduling command will only be executed when all the conditions are true return
Avoid duplication of tasks
By default, even if there is still perform the same task, the task is still within the schedule will be executed. To avoid this problem, you can use withoutOverlapping ()
$schedule->command('eamil:send')->withoutOverlapping();
In the example above, if there is no other command Artisan email: send in the running, this task will be executed once per minute. If you perform tasks on time and very different, you can not accurately confirm your task given how long it takes, withoutOverlapping will be of great help
Maintenance Mode
When laravel in maintenance mode, laravel scheduling will not take effect. However, if you want to enforce tasks in maintenance mode you can use evenInmentenanceMode ()
$schedule->command('Email:send')->evenInmentenanceMode();
Task Output
laravel scheduler provides several convenient output generated by the task scheduling. Firstly, but using sendOutputTo method to send output to the individual file for subsequent inspection
$schedule->command('Email:send')->daily()->sendOutputTo($filePath);
If you want to output attached to the specified file, you can use the method appendOutputTo
$schedule->command('Email:send')->appendOutputTo($filePath);
Use emailOutputTo method, the output can be sent to your specified email address via email. Before sending the task output, you need to configure laravel e-mail service
$schedule->command('Email:send')->sendOutputTo($filePath)->emailOutputTO('[email protected]');
Note: command only when emailOutputTo SendOutputTo appendOutputTo the way, does not support the use of the method call
Task hook
By methods before, and after, to specify the command executed before or after the scheduled task
$schedule->command('email:send')
->daily()
->before(function(){
Before beginning the task //
})
->after(function(){
After the task is completed //
})
Ping URL
Use pingBefore and thenPing method, you can perform tasks before and after the automatic ping the specified URL. This method of notification external services (eg Laravel Envoyer) your scheduled tasks have begun or have been completed to perform useful.
$schedule->command('email:send')->daily()
->pingbefore($url)
->thenPing($url)
Regardless pingBefore ($ url) or thenPing ($ url), needs to Guzzle Http library support. You can use the composer to add Guzzle library to your project
composer require guzzlehttp/guzzle
Source: https://learnku.com/docs/laravel/5.5/scheduling/1325