Configure and test Laravel Task Scheduling(配置和测试 Laravel 任务调度)
问题描述
Laravel 版本:5.1.45 (LTS)
PHP 版本:5.6.1
我尝试使用 Laravel 每 1 分钟运行一次命令任务调度一>.
I'm trying to run a command every 1 minute using Laravel Task Scheduling.
我已将此行添加到我的 cron 选项卡文件中
I've added this line to my cron tab file
* * * * * php artisan schedule:run >>/dev/null 2>&1
这是我的/app/Console/Kernel.php
<?php
namespace AppConsole;
use IlluminateConsoleSchedulingSchedule;
use IlluminateFoundationConsoleKernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* The Artisan commands provided by your application.
*
* @var array
*/
protected $commands = [
AppConsoleCommandsInspire::class,
];
/**
* Define the application's command schedule.
*
* @param IlluminateConsoleSchedulingSchedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->hourly();
$schedule->command('echo "Happy New Year!" ')->everyMinute(); //<---- ADD HERE }
}
我添加了这一行 $schedule->command('echo "Happy New Year!"')->everyMinute();
我该如何测试?
如何触发我的回声显示?
How do I trigger my echo to display ?
我怎么知道我做的事情没有错?
How do I know if what I did is not wrong ?
推荐答案
command()
运行工匠命令.您要实现的目标 - 向操作系统发出命令 - 由 exec('echo "Happy New Year!"')
command()
runs an artisan command. What you're trying to achieve - issuing a command to the OS - is done by exec('echo "Happy New Year!"')
测试取决于您要测试的内容:
Testing depends on what you want to test:
- 调度程序(每分钟)是否在工作?
在这种情况下,您不必这样做.它在原始框架代码中进行了测试.
In this case, you don't have to. It is tested in the original framework code.
- 命令是否成功?
好吧,您可以手动运行 php artisan schedule:run
并查看输出.
Well, you can manually run php artisan schedule:run
and see the output.
调度程序在默认情况下不产生任何输出(>>/dev/null 2>&1
).但是,您可以通过链接 writeOutputTo()
或 appendOutputTo()
(https://laravel.com/docs/5.1/scheduling#task-output).
The scheduler does not produce any output on default (>> /dev/null 2>&1
). You can, however, redirect the output of the runned scripts to any file by chaining writeOutputTo()
or appendOutputTo()
(https://laravel.com/docs/5.1/scheduling#task-output).
对于更复杂的逻辑,请改为编写控制台命令(https://laravel.com/docs/5.1/artisan#writing-commands) 并使用 command()
- 这样你就可以写出漂亮的、可测试的代码.
For more complex logic, write a console command instead (https://laravel.com/docs/5.1/artisan#writing-commands) and use command()
- this way you can write nice, testable code.
这篇关于配置和测试 Laravel 任务调度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:配置和测试 Laravel 任务调度
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01