如何使用命令行手动运行 laravel/lumen 作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43357472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to manually run a laravel/lumen job using command line
提问by Oscar David
I have created a Job file in the folder app/Jobs/MyJob.phpand i would like to run it just once, if its possible using command line.
我在文件夹中创建了一个 Job 文件app/Jobs/MyJob.php,如果可能,我想使用命令行运行它一次。
Something like:
就像是:
> php MyJob:run
> php MyJob:run
what command should I use to run a method in this file or the handle?
我应该使用什么命令来运行此文件或句柄中的方法?
回答by mixel
UPDATE
更新
I created mxl/laravel-jobcomposer package providing Laravel command for dispatching jobs from command line:
我创建了mxl/laravel-jobcomposer 包,提供 Laravel 命令以从命令行分派作业:
$ 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 also provides a way to reduce boilerplate by using base Jobclass and has FromParametersinterface that allows to implement command line parameters parsing and use job from PHP code and command line simultaneously.
包还提供了一种通过使用基Job类来减少样板文件的方法,并且具有FromParameters允许同时实现命令行参数解析和使用来自 PHP 代码和命令行的作业的接口。
Read more about its features at the package GitHub page.
在包 GitHub 页面上阅读有关其功能的更多信息。
OLD ANSWER
旧答案
Run
跑
php artisan make:command DispatchJob
to create special artisan command that runs jobs.
创建运行作业的特殊工匠命令。
Open created DispatchJob.phpfile and define DispatchJobclass like this:
打开创建的DispatchJob.php文件并DispatchJob像这样定义类:
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
回答by J.C. Gras
If you are using a QUEUE_DRIVERdiferent to syncand you want to dispatch a queue that you have created before, from your project folder run the command:
如果您使用QUEUE_DRIVER不同的同步方式,并且想要调度您之前创建的队列,请从您的项目文件夹中运行以下命令:
php artisan queue:work --queue=MyQueueName
Check this linkto configure a databaseQUEUE_DRIVER
检查此链接以配置数据库QUEUE_DRIVER
回答by Jignesh Joisar
for Job With Parameter Optional
用于带有可选参数的作业
try this one
试试这个
class DispatchJob extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'job:dispatch {job} {parameter?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Dispatch job';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$prefix = '\App\Jobs\';
$jobClassName = trim($this->argument('job'));
if(stripos($jobClassName,"/")){
$jobClassName = str_replace('/','\',$jobClassName);
}
$class = '\App\Jobs\' . $jobClassName;
if(!class_exists($class)){
$this->error("{$class} class Not exists");
}else {
if ($this->argument('parameter')) {
$job = new $class($this->argument('parameter'));
} else {
$job = new $class();
}
dispatch($job);
$this->info("Successfully Dispatch {$class} ");
}
}
}
now U can try like this in terminal
现在你可以在终端尝试这样
1.Without Folder
1.无文件夹
php artisan job:dispatch appJob
2.With Folder
2.带文件夹
php artisan job:dispatch folderName/appJob
3.Folder With Parameter
3.带参数的文件夹
php artisan job:dispatch folderName/appJob 12
回答by Amitesh
The most simple way is to call with the use of Tinker
最简单的方法就是用Tinker调用
Pre-requisite:You should know the basic of tinker CLI
先决条件:您应该了解 tinker CLI 的基础知识
php artisan tinker (from project root)
To dispatch job on a specific queue
在特定队列上分派作业
\Queue::pushON('rms', new App\Jobs\UpdateRMS());first parameter - Queue name
second parameter - job name
\Queue::pushON('rms', new App\Jobs\UpdateRMS());第一个参数 - 队列名称
第二个参数 - 作业名称
Dispatch multiple jobs at once to a specific queue
一次将多个作业分派到特定队列
\Queue::bulk([new App\Jobs\UpdateRMS(), new App\Jobs\UpdateRMS()], null, 'rms');
Demo(product-service is my project name)
Find a dispatched job in the queue)my case queue is configured as Redis

