沃梦达 / 编程问答 / php问题 / 正文

配置和测试 Laravel 任务调度

Configure and test Laravel Task Scheduling(配置和测试 Laravel 任务调度)

本文介绍了配置和测试 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 任务调度

基础教程推荐