0

我正在尝试从其他代码库触发作业

MyCommand 类:

<?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

如果我运行命令(在一些代码库中)命令:我得到这样的有效负载,任何处理成功。

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

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

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

4

1 回答 1

1

在这个参考的帮助下

通过使用这些包 "illuminate/queue": "5.2. ", "pda/pheanstalk": "~3.0", "illuminate/encryption": "5.2. "

我可以像这样将普通的有效载荷推到给定的管子上

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的任何参数。

我的作业中的小修改以处理两个命令和普通有效负载

<?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();
    }
}
于 2016-09-20T10:18:48.897 回答