Every entry you add is converted into an instance of Illuminate\Console\Scheduling\Event and stored in an $events class property of the Scheduler, an Event object consists of the following:

  • Command to run
  • CRON Expression
  • Timezone to be used to evaluate the time
  • Operating System User the command should run as
  • The list of Environments the command should run under
  • Maintenance mode configuration
  • Event Overlapping configuration
  • Command Foreground/Background running configuration
  • A list of checks to decide if the command should run or not
  • Configuration on how to handle the output
  • Callbacks to run after the command runs
  • Callbacks to run before the command runs
  • Description for the command
  • A unique Mutex for the command

The command to run could be one of the following:

  • A callback
  • A command to run on the operating system
  • An artisan command
  • A job to be dispatched

Using a callback

In case of a callback, the Container::call() method is used to run the value we pass which means we can pass a callable or a string representing a method on a class:

protected function schedule(Schedule $schedule)
{
$schedule->call(function () {
DB::table('recent_users')->delete();
})->daily();
}

Or:

protected function schedule(Schedule $schedule)
{
$schedule->call('MetricsRepository@cleanRecentUsers')->daily();
}

Passing a command for the operating system

If you would like to pass a command for the operating system to run you can use exec():

$schedule->exec('php /home/sendmail.php --user=10 --attachInvoice')->monthly();

You can also pass the parameters as an array:

$schedule->exec('php /home/sendmail.php', [
'--user=10',
'--subject' => 'Reminder',
'--attachInvoice'
])->monthly();

Passing an artisan command

$schedule->command('mail:send --user=10')->monthly();

You can also pass the class name:

$schedule->command('App\Console\Commands\EmailCommand', ['user' => 10])->monthly();

The values you pass are converted under the hood to an actual shell command and passed to exec() to run it on the operating system.

Dispatching a Job

You may dispatch a job to queue using the Job class name or an actual object:

$schedule->job('App\Jobs\SendOffer')->monthly();

$schedule->job(new SendOffer(10))->monthly();

Under the hood Laravel will create a callback that calls the dispatch() helper method to dispatch your command.

So the two actual methods of creating an event here is by calling exec() or call(), the first one submits an instance of Illuminate\Console\Scheduling\Event and the latter submits Illuminate\Console\Scheduling\CallbackEvent which has some special handling.

Building the cron expression

Using the timing method of the Scheduled Event, laravel builds a CRON expression for that event under the hood, by default the expression is set to run the command every minute:

* * * * * *

But when you call hourly() for example the expression will be updated to:

0 * * * * *

If you call dailyAt('13:30') for example the expression will be updated to:

30 13 * * * *

If you call twiceDaily(5, 14) for example the expression will be updated to:

0 5,14 * * * *

A very smart abstraction layer that saves you tons of research to find the right cron expression, however you can pass your own expression if you want as well:

$schedule->command('mail:send')->cron('0 * * * * *');

How about timezones?

If you want the CRON expression to be evaluated with respect to a specific timezone you can do that using:

->timezone('Europe/London')

Under the hood Laravel checks the timezone value you set and update the Carbon date instance to reflect that.

So laravel checks if the command is due using the CRON expression?

Exactly, Laravel uses the mtdowling/cron-expression library to determine if the command is due based on the current system time (with respect to the timezone we set).

Adding Constraints on running the command

Duration constraints

For example if you want the command to run daily but only between two specific dates:

->between('2017-05-27', '2017-06-26')->daily();

And if you want to prevent it from running during a specific period:

->unlessBetween('2017-05-27', '2017-06-26')->daily();

Environment constraints

You can use the environments() method to pass the list of environments the command is allowed to run under:

->environments('staging', 'production');

Maintenance Mode

By default scheduled commands won't run when the application is in maintenance mode, however you can change that by using:

->evenInMaintenanceMode()

OS User

You can set the Operating System user that'll run the command using:

->user('forge')

Under the hood Laravel will use sudo -u forge to set the user on the operating system.

Custom Constraints

You can define your own custom constraint using the when() and skip() methods:

// Runs the command only when the user count is greater than 1000
->when(function(){
return User::count() > 1000;
}); // Runs the command unless the user count is greater than 1000
->skip(function(){
return User::count() > 1000;
});

Before and After callbacks

Using the before() and then() methods you can register callbacks that'll run before or after the command finishes execution:

->before(function(){
Mail::to('myself@Mail.com', new CommandStarted());
})
->then(function(){
Mail::to('myself@Mail.com', new CommandFinished());
});

You can also ping URLs or webhooks using the pingBefore() and thenPing() methods:

->ping('https://my-webhook.com/start')->thenPing('https://my-webhook.com/finish')

Using these commands laravel registers a before/after callbacks under the hood and uses Guzzle to send a GET HTTP request:

return $this->before(function () use ($url) {
(new HttpClient)->get($url);
});

Properties of a scheduled job in Laravel的更多相关文章

  1. KBMMW 4.92.00 发布

    We are happy to announce the release of kbmMW Professional and Enterprise Edition. Yet again kbmMW c ...

  2. laravel/lumen 单元测试

    Testing Introduction Application Testing Interacting With Your Application Testing JSON APIs Session ...

  3. Laravel之Service Container服务容器

    managing class dependencies and performing dependency injection. Dependency injection is a fancy phr ...

  4. Laravel系列 目录结构

    Where Is The Models Directory? app directory by default 其中 app:,core code of your application, almos ...

  5. Laravel系列2入门使用

    最好的教程是官方文档! homestead安装好,就可以使用了. 安装Laravel composer create-project --prefer-dist laravel/laravel blo ...

  6. 50分钟学会Laravel 50个小技巧

    50分钟学会Laravel 50个小技巧 时间 2015-12-09 17:13:45  Yuansir-web菜鸟 原文  http://www.yuansir-web.com/2015/12/09 ...

  7. Quartz的配置文件quartz.properties详解

    配置 quartz.properties 文件 文件 quartz.properties 定义了 Quartz 应用运行时行为,还包含了许多能控制 Quartz 运转的属性.这个文件应该放在class ...

  8. Spring注解@Scheduled定时任务

    一.首先配置applicationContext-task.xml (1)添加 xmlns:task="http://www.springframework.org/schema/task& ...

  9. laravel 5.0 artisan 命令列表(中文简体)

    #php artisan list Laravel Framework version Usage: [options] command [arguments] Options(选项): --help ...

随机推荐

  1. pipy国内镜像的网址

    pipy国内镜像目前有:豆瓣 http://pypi.douban.com/simple/阿里云 http://mirrors.aliyun.com/pypi/simple/中国科技大学 https: ...

  2. @JsonInclude(JsonInclude.Include.NON_NULL) 加在对象上

    @JsonInclude(JsonInclude.Include.NON_NULL) public class ViewWorkermessage implements Serializable { ...

  3. 第七篇:Jmeter连接MySQL的测试

    .准备一个有数据表格的MySQL数据库: 2.在测试计划面板上点击浏览按钮,把你的JDBC驱动添加进来: mysql-connector-java-5.1.26-bin.jar 3.添加一个线程组-- ...

  4. w3af安装

    基于python开发,由于kali自带的w3af无法正常使用 安装新的w3af apt-get update apt-get install kali-linux-all安装所有工具包 apt-get ...

  5. python全栈开发 什么是python python命名及循环

    python全栈 一.  python介绍: 1.    python起源 2.    主要应用领域; web,人工智能,云计算,系统运维. 1.1   python是一门什么语言? python是一 ...

  6. Android Studio 中的FindBugs插件使用,轻松帮你发现Bug (转)

    在日常开发过程中难免会因为一时疏忽而留下一些Bug,这些Bug就是埋在程序里的定时炸弹,如果不能及时铲除就会导致程序的不稳定,异常或闪退的现象,从而导致用户的体验的下降.那么怎么才能找出这些埋在程序里 ...

  7. Android笔记:intent

    一.显式intent如下:(1)在intent构造函数传入两个activity文件名Intent intent = new Intent(FirstActivity.this, SecondActiv ...

  8. 2019年华南理工大学程序设计竞赛(春季赛)-H-Parco_Love_GCD

    题目链接:https://ac.nowcoder.com/acm/contest/625/H 题意:给定n个数(<=1e9)的序列,其中n<=5e5,求该序列所有子序列的对应的gcd对1e ...

  9. 使用mybatis-generator-core工具自动生成mybatis实体

    我们可以使用mybatis-generator-core这个工具将数据库对象转换成mybatis对象,具体步骤如下. 1.mybatis-generator-core下载 下载地址:http://do ...

  10. 关于vue搭建项目运行出行的错误问题,简直是大坑啊

    解决方法简单粗暴,非常简单粗暴 直接在根目录新建一个test文件夹就可以搞定,用来放置配置文件的 折腾了我一上午啊