zhanghuilong

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

计划任务* * * * * php /path-to-your-project/artisan schedule:run >> /dev/null 2>&1

##########某广告项目公众号接单超过两天未申请结算修改为待审核状态#############################################
#* * * * * php /home/www/xiaoyoutui/artisan schedule:run >> /dev/null 2>&1
#* * * * * php /home/www/xiaoyoutui/artisan state:WoaReceiptSettlement  >> /dev/null 2>&1
##########某广告项目公众号接单超过两天未申请完成订单修改为完成状态#############################################
#* * * * * php /home/www/xiaoyoutui/artisan state:WoaReceiptDone  >> /dev/null 2>&1

基本用法

'star_img' => [//图片验证尺寸
'sometimes',
'required',
'image',
'dimensions:min_width=326,max_width=326,min_height=238,max_height=238'//图片尺寸限制
],
$imginfo = getimagesize($_FILES['star_img']['tmp_name']);//图片尺寸验证
if($imgInfo[0] != 326 || $imgInfo[0] != 238){
return [
'code'=> 100000,
'msg'=> '图片尺寸有误',
'data'=>null
];
}
 

验证 users 表中是否存在 email 等于指定的 email 值的记录

  • 'email' => 'exists:users'
  • Rule::exists('users')

自定义列名

验证 users 表的 email_address

  • 'email' => 'exists:users,email_address'
  • Rule::exists('users','email_address')

自定义数据库连接

验证指定数据库连接的 users 表

  • 'email' => 'exists:connection.users,email'

附加 WHERE 条件(可以有多个)

除了验证 email_address 还需要验证 role 等于 admin

  • 'email' => 'exists:users,email_address,role,admin'
  • Rule::exists('users','email_address')->where('role','admin')

! 表示不等于

  • 'email' => 'exists:users,email_address,role,!admin'
  • Rule::exists('users','email_address')->whereNot('role','admin')

NULL 在 MySQL 中是通过 IS 判断的,这里注意不用 IS

  • 'email' => 'exists:users,email,deleted_at,NULL'
  • Rule::exists('users','email_address')->whereNull('deleted_at')

对不等于 NULL 的情况不能使用 !,而是使用 NOT_NULL

  • 'email' => 'exists:users,email,deleted_at,NOT_NULL'
  • Rule::exists('users','email_address')->whereNotNull('deleted_at')

unique:table,column,except,idColumn


验证字段的值在数据库指定表的指定列中是否唯一,如果不唯一将会抛出异常,这里是关于 unique 规则的 详细说明

基本用法

验证指定的 email 值在 users 表的 email 中是否唯一

  • 'email' => 'unique:users '
  • Rule::unique('users')

自定义列名

验证 users 表的 email_address

  • 'email' => 'unique:users,email_address'
  • Rule::unique('users','email_address')

自定义数据库连接

验证指定数据库连接的 users 表

  • 'email' => 'unique:connection.users,email_address'

排除某条记录

排除 users 表中 id 等于 $userId 的记录,使用场景是用户修改 email 时,需要验证自身除外的其他用户是否已使用了该 email

  • 'email' => 'unique:users,email_address,'.$userId
  • Rule::unique('users','email_address')->ignore($userId)

排除时指定 primary_key

排除 users 表中 user_id 等于 $userId 的记录

  • 'email' => 'unique:users,email_address,'.$userId.',user_id'
  • Rule::unique('users','email_address')->ignore($userId,'user_id')

附加 WHERE 条件(和 exists 的 WHERE 条件相同)

由于排除是在 WHERE 条件之前的,如果指定了 WHERE 条件,但是没有指定排除,需要把第三个参数设为 NULL

  • 'email' => 'unique:users,email_address,NULL,id,role,admin'
  • Rule::unique('users','email_address')->where($role,'admin')

 

 

 

 

一,创建命令

版本<5.3
Php  artisan make:console command_name --command=artisan_command_name
版本>=5.3
Php artisan make:command command_name --command=artisan_command_name
 
command_name:生成的文件名
artisan_command_name: php artisan命令调度时的命令名称
结果: 在App\Console\Commands\ 下生成名为command_name.php的文件.

php artisan make:console Test --command=xzj:test

其中Test是命令名,xzj:test是控制台执行的命令,类似make:console

执行完成后,会在app/Console/Commands目录下生成一个Test.php文件:

复制代码
 1 <?php
 2 
 3 namespace App\Console\Commands;
 4 
 5 use Illuminate\Console\Command;
 6 use Illuminate\Support\Facades\Log;
 7 
 8 class Test extends Command
 9 {
10     /**
11      * The name and signature of the console command.
12      *用来描述命令的名字与参数
13      * @var string
14      */
15     protected $signature = 'xzj:test';
16 
17     /**
18      * The console command description.
19      *存储命令描述
20      * @var string
21      */
22     protected $description = 'Command 测试';
23 
24     /**
25      * Create a new command instance.
26      *
27      * @return void
28      */
29     public function __construct()
30     {
31         parent::__construct();
32     }
33 
34     /**
35      * Execute the console command.
36      *执行命令
37      * @return mixed
38      */
39     public function handle()
40     {
41         //这里做任务的具体处理,可以用模型
42         Log::info('任务调度二'.date('Y-m-d H:i:s',time()));
43     }
44 }
复制代码

二,运行命令

在运行命令前需要将其注册到App\Console\Kernel$commands属性中:

复制代码
 1 <?php
 2 
 3 namespace App\Console;
 4 
 5 use function foo\func;
 6 use Illuminate\Console\Scheduling\Schedule;
 7 use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
 8 use Illuminate\Support\Facades\Log;
 9 
10 class Kernel extends ConsoleKernel
11 {
12     /**
13      * The Artisan commands provided by your application.
14      *定义Artisan命令
15      * @var array
16      */
17     protected $commands = [
18         //
19         Commands\Test::class,
20     ];
21 
22     /**
23      * Define the application's command schedule.
24      *定义调度任务
25      * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
26      * @return void
27      */
28     protected function schedule(Schedule $schedule)
29     {
30         // $schedule->command('inspire')
31         //          ->hourly();
32         //方法一:
33 //        $schedule->call(function (){
34 //           Log::info('任务调度一:闭包形式');
35 //        })->everyMinute();
36         //方法二
37         $schedule->command('xzj:test')->everyMinute();
38     }
39 
40     /**
41      * Register the commands for the application.
42      *
43      * @return void
44      */
45     protected function commands()
46     {
47         $this->load(__DIR__.'/Commands');
48 
49         require base_path('routes/console.php');
50     }
51 }
复制代码

a.在控制台上执行Artisan命令:

php artisan xzj:test

Tip:该命令只执行一次!

b.设定定时任务

需求:有的业务需求要求在某个特定的时间段里执行某种任务,所以就用到了定时任务

输入命令

crontab -e

编写以下cron语句:

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

phpartisan都要写完整的路径

如果你不知道PHP的路劲,可以用which php   不要用whereis php 

which php

输出:

/Applications/XAMPP/xamppfiles/bin/php
path-to-your-project    就是你项目的绝对路径:根目录

添加完了之后输入以下命令可以查看:

crontab  -l

显示:

* * * * * /Applications/XAMPP/xamppfiles/bin/php  /Applications/XAMPP/xamppfiles/htdocs/blog/artisan schedul:run >> /dev/null 2>&1

 删除所有定时任务: crontab -r

crontab -r

 

schedule:run 会执行App\Console\Kernel里schedule下的注册命令

如果你想单独执行某个命令可以这样:

例如:你想执行xzj:test命令

* * * * * /Applications/XAMPP/xamppfiles/bin/php  /Applications/XAMPP/xamppfiles/htdocs/blog/artisan xzj:test >> /dev/null 2>&1

 

文件/etc/crontab中每行任务的描述格式如下: 

 
    minute  hour  day  month  dayofweek   command

```

    minute - 从0到59的整数 
    hour - 从0到23的整数 
    day - 从1到31的整数 (必须是指定月份的有效日期)
    month - 从1到12的整数 (或如Jan或Feb简写的月份)
    dayofweek - 从0到7的整数,0或7用来描述周日 (或用Sun或Mon简写来表示)
    command - 需要执行的命令(可用as ls /proc >> /tmp/proc或 执行自定义脚本的命令) 

 

```

```

分钟 (0-59)

小時(0-23)
日期(1-31)
月份(1-12)
星期(0-6) //0代表星期天
 
    除了数字还有几个个特殊的符号就是"*"、"/"和"-"、",",*代表所有的取值范围内的数字,"/"代表每的意思,"*/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。以下举几个例子说明问题:
 
每天早上6点
-----------------
0 6 * * * echo "Good morning." >> /tmp/test.txt //注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。
 
每两个小时
-----------------
0 */2 * * * echo "Have a break now." >> /tmp/test.txt
 
晚上11点到早上8点之间每两个小时
-----------------
0 23-8/2 * * * echo "Have a good dream:)" >> /tmp/test.txt
 
每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
-----------------
0 11 4 * 1-3 command line
 
1月1日早上4点
-----------------
0 4 1 1 * command line
 
    每次编辑完某个用户的cron设置后,cron自动在/var/spool/cron下生成一个与此用户同名的文件,此用户的cron信息都记录在这个文件中,这个文件是不可以直接编辑的,只可以用crontab -e 来编辑。cron启动后每过一份钟读一次这个文件,检查是否要执行里面的命令。因此此文件修改后不需要重新启动cron服务。

```

 

 

上面举例了两种实现方法,方法一是用闭包,方法二是用Artisan命令实现的。 
调度的时间可以有多种: 
->cron(‘* * * * *’); 在自定义Cron调度上运行任务 
->everyMinute(); 每分钟运行一次任务 
->everyFiveMinutes(); 每五分钟运行一次任务 
->everyTenMinutes(); 每十分钟运行一次任务 
->everyThirtyMinutes(); 每三十分钟运行一次任务 
->hourly(); 每小时运行一次任务 
->daily(); 每天凌晨零点运行任务 
->dailyAt(‘13:00’); 每天13:00运行任务 
->twiceDaily(1, 13); 每天1:00 & 13:00运行任务 
->weekly(); 每周运行一次任务 
->monthly(); 每月运行一次任务 

任务钩子:参考下面的链接
调度的时间具体的方法请参考:http://laravelacademy.org/post/9000.html

 当你不想在定时任务里执行的时候   

1,schedule里注释掉你不想执行的任务

2,在crontab里用#好注释掉定时任务

具体情况酌情处理

 

posted on 2019-07-03 17:21  孤独的英雄  阅读(224)  评论(0编辑  收藏  举报