我正在尝试从其他代码库触发Jobs
MyCommand类:
<?php
namespace AppConsoleCommands;
use IlluminateConsoleCommand;
use IlluminateFoundationBusDispatchesJobs;
use AppJobsMyJob;
class EncodeTvVideos extends Command
{
use DispatchesJobs;
protected $signature = 'command:my';
protected $description = '';
public function handle()
{
$job = (new MyJob($this->argument()))
->onConnection('beanstalkd')
->onQueue('cron-default'));
$this->dispatch($job);
}
}
和MyJob类:
<?php namespace AppJobs;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
class MyJob extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
private $payload;
public function __construct($payload = null)
{
$this->payload = $payload;
}
public function handle()
{
echo "Job processed here";
$this->job->delete();
}
}
我通过使用queue:listen命令来监听我的队列,如
php artisan queue:listen --queue=cron-default
如果我运行命令(在一些代码库中)命令:my,我得到这样的有效载荷,任何处理成功。
{
"job":"Illuminate\\Queue\\CallQueuedHandler@call",
"data":{
"command":"O:29:\"Acme\Jobs\FooJob\":4:{s:11:\"fooBar\";s:7:\"abc-123\";s:5:\"queue\";N;s:5:\"delay\";N;s:6:\"\u0000*\u0000job\";N;}"
}
}
现在我的问题是我需要触发这个作业或命令从其他代码库是有任何方法吗?
我没有(命令/工人项目)的域名,否则我可以尝试创建一个路由,它将触发命令。
By the help this ref
通过使用这些包"illuminate/queue": ""、"pda/pheanstalk":"~ 3.0"、"照亮/加密":"5.2。"
我能够像这样将普通有效载荷推到给定的管中
use IlluminateQueueCapsuleManager as Queue;
$queue = new Queue;
// Some drivers need it
$queue->getContainer()->bind('encrypter', function() {
return new IlluminateEncryptionEncrypter('foobar');
});
$queue->addConnection([
'driver' => 'beanstalkd',
'host' => 'localhost',
'queue' => 'default',
], 'default');
$queue->push('AppJobsMyJob@process', ['data'=> 'something']);
//JobClass在其他代码库中的第一个参数完整路径//第二个参数为Job的任意参数
对my Job进行了小修改,以处理两个命令和普通有效负载
<?php namespace AppJobs;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
class MyJob extends Job implements ShouldQueue
{
use InteractsWithQueue, SerializesModels;
private $payload;
public function __construct($payload = null)
{
$this->payload = $payload;
}
public function handle()
{
echo "Job processed here";
$this->job->delete();
}
public function process($job, $payload)
{
echo "Job processed from plain payload";
$job->delete();
}
}