如何使用命令行手动 运行 一项 laravel/lumen 作业
How to manually run a laravel/lumen job using command line
我在文件夹 app/Jobs/MyJob.php
中创建了一个作业文件,如果可能的话,我想 运行 使用命令行。
类似于:
> php MyJob:run
我应该对 运行 此文件或句柄中的方法使用什么命令?
更新
我创建了 mxl/laravel-job composer 包,提供 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
软件包还提供了一种通过使用基础 Job
class 来减少样板代码的方法,并且具有 FromParameters
接口,允许实现命令行参数解析和使用来自 [=45= 的作业] 代码和命令行同时进行。
在 the package GitHub page 阅读有关其功能的更多信息。
旧答案
运行
php artisan make:command DispatchJob
创建特殊的 artisan 命令,运行 的工作。
打开创建的 DispatchJob.php
文件并像这样定义 DispatchJob
class:
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());
}
}
现在你应该启动队列工作器:
php artisan queue:work
然后您可以从命令行 运行 作业:
php artisan job:dispatch YourJobNameHere
如果您使用的 QUEUE_DRIVER
与 sync 不同,并且您想要从您的项目文件夹 运行 调度您之前创建的队列命令:
php artisan queue:work --queue=MyQueueName
勾选这个配置一个数据库QUEUE_DRIVER
for Job With Parameter Optional
试试这个
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} ");
}
}
}
现在你可以在终端中这样尝试了
1.Without 文件夹
php artisan job:dispatch appJob
2.With 文件夹
php artisan job:dispatch folderName/appJob
3.Folder 带参数
php artisan job:dispatch folderName/appJob 12
最简单的方法就是用Tinker调用
它是用于调试的 Laravel 命令,通过来自项目根目录
的 运行 下面的命令使用它
php artisan tinker
从 tinker 调度特定队列上的作业
\Queue::pushON('rms', new App\Jobs\UpdateRMS());
first parameter - Queue name
second parameter - job name
一次将多个作业分派到特定队列
\Queue::bulk([new App\Jobs\UpdateRMS(), new App\Jobs\UpdateRMS()], null, 'rms');
Demo(product-service是我的项目名)照着做就行了
在队列中找到一个调度的作业)我的案例队列配置为Redis
我在文件夹 app/Jobs/MyJob.php
中创建了一个作业文件,如果可能的话,我想 运行 使用命令行。
类似于:
> php MyJob:run
我应该对 运行 此文件或句柄中的方法使用什么命令?
更新
我创建了 mxl/laravel-job composer 包,提供 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
软件包还提供了一种通过使用基础 Job
class 来减少样板代码的方法,并且具有 FromParameters
接口,允许实现命令行参数解析和使用来自 [=45= 的作业] 代码和命令行同时进行。
在 the package GitHub page 阅读有关其功能的更多信息。
旧答案
运行
php artisan make:command DispatchJob
创建特殊的 artisan 命令,运行 的工作。
打开创建的 DispatchJob.php
文件并像这样定义 DispatchJob
class:
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());
}
}
现在你应该启动队列工作器:
php artisan queue:work
然后您可以从命令行 运行 作业:
php artisan job:dispatch YourJobNameHere
如果您使用的 QUEUE_DRIVER
与 sync 不同,并且您想要从您的项目文件夹 运行 调度您之前创建的队列命令:
php artisan queue:work --queue=MyQueueName
勾选这个QUEUE_DRIVER
for Job With Parameter Optional
试试这个
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} ");
}
}
}
现在你可以在终端中这样尝试了
1.Without 文件夹
php artisan job:dispatch appJob
2.With 文件夹
php artisan job:dispatch folderName/appJob
3.Folder 带参数
php artisan job:dispatch folderName/appJob 12
最简单的方法就是用Tinker调用
它是用于调试的 Laravel 命令,通过来自项目根目录
的 运行 下面的命令使用它php artisan tinker
从 tinker 调度特定队列上的作业
\Queue::pushON('rms', new App\Jobs\UpdateRMS());
first parameter - Queue name
second parameter - job name
一次将多个作业分派到特定队列
\Queue::bulk([new App\Jobs\UpdateRMS(), new App\Jobs\UpdateRMS()], null, 'rms');
Demo(product-service是我的项目名)照着做就行了