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.
Something like:
> php MyJob:run
what command should I use to run a method in this file or the handle?
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.
Something like:
> php MyJob:run
what command should I use to run a method in this file or the handle?
UPDATE
我创建了mxl/laravel-job composer包,提供了Laravel命令,用于从命令行分派作业(job):
$ 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 Job
class and has FromParameters
interface that allows to implement command line parameters parsing and use job from PHP code and command line simultaneously.
Read more about its features at the package GitHub page.
OLD ANSWER
跑
php artisan make:command DispatchJob
创建运行作业(job)的特殊artisan命令.
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
之后,您可以从命令行运行作业(job):
php artisan job:dispatch YourJobNameHere