如何使用命令行手动运行laravel/lumen作业 [英] How to manually run a laravel/lumen job using command line

查看:200
本文介绍了如何使用命令行手动运行laravel/lumen作业的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在文件夹 app/Jobs/MyJob.php 中创建了一个Job文件,如果可以使用命令行,我想只运行一次.

I have created a Job file in the folder app/Jobs/MyJob.php and i would like to run it just once, if its possible using command line.

类似的东西:

>php MyJob:run

在该文件或句柄中应该使用什么命令来运行方法?

what command should I use to run a method in this file or the handle?

推荐答案

更新

我创建了 mxl/laravel-job 组合程序包,其中提供了Laravel命令,用于从命令行分派作业:

I created mxl/laravel-job composer package providing Laravel command for dispatching jobs from command line:

$ composer require mxl/laravel-job
$ php artisan job:dispatch YourJob # for jobs in app/Jobs directory (App\Jobs namespace)
$ php artisan job:dispatch '\Path\To\YourJob' # dispatch job by its full class name
$ php artisan job:dispatchNow YourJob # dispatch immediately
$ php artisan job:dispatch YourJob John 1990-01-01 # dispatch with parameters

Package还提供了一种使用基础 Job 类来减少样板的方法,并且具有 FromParameters 接口,该接口允许实现命令行参数解析并使用PHP代码和命令中的job同时在线.

Package also provides a way to reduce boilerplate by using base Job class and has FromParameters interface that allows to implement command line parameters parsing and use job from PHP code and command line simultaneously.

请访问包GitHub页面,详细了解其功能.

Read more about its features at the package GitHub page.

旧答案

运行

php artisan make:command DispatchJob

创建运行作业的特殊工匠命令.

to create special artisan command that runs jobs.

打开创建的 DispatchJob.php 文件,并定义 DispatchJob 类,如下所示:

Open created DispatchJob.php file and define DispatchJob class like this:

class DispatchJob extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'job:dispatch {job}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Dispatch job';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $class = '\\App\\Jobs\\' . $this->argument('job');
        dispatch(new $class());
    }
}

现在您应该启动队列工作器:

Now you should start queue worker:

php artisan queue:work

之后,您可以从命令行运行作业:

and after that you can run jobs from command line:

php artisan job:dispatch YourJobNameHere

这篇关于如何使用命令行手动运行laravel/lumen作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆