laravel推送来自不同代码库的排队作业

时间:2016-09-16 09:47:48

标签: laravel laravel-5.2 beanstalkd laravel-queue

我试图从其他代码库中触发Jobs

MyCommand Class:

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;
use App\Jobs\MyJob;


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 App\Jobs;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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

如果我运行命令(在某些代码库中)命令:我的i&#39; m得到这样的有效负载任何成功处理。

{
    "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;}"
    }
}

现在我的问题是我需要从其他代码库触发这个Job或命令有什么办法吗?

我没有(命令/工人项目)的域名,否则我可以尝试创建一个它将触发命令的路由。

1 个答案:

答案 0 :(得分:1)

通过帮助ref

通过使用这些包&#34;照亮/排队&#34;:&#34; 5.2。&#34;,&#34; pda / pheanstalk&#34;:&#34; ~3.0& #34;,&#34;照亮/加密&#34;:&#34; 5.2。&#34;

我能够像这样将普通有效负载推送到给定的管道

use Illuminate\Queue\Capsule\Manager as Queue;

$queue = new Queue;


// Some drivers need it
$queue->getContainer()->bind('encrypter', function() {
    return new Illuminate\Encryption\Encrypter('foobar');
});

$queue->addConnection([
    'driver' => 'beanstalkd',
    'host' => 'localhost',
    'queue' => 'default',
], 'default');

$queue->push('App\Jobs\MyJob@process', ['data'=> 'something']);

//其他代码库中JobClass的第一个参数完整路径 //第二个参数Job的任何参数。

我的Job中的一些小修改,用于处理命令和普通有效负载

<?php namespace App\Jobs;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

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();
    }
}