【问题标题】:laravel push queued jobs from different codebaselaravel 推送来自不同代码库的排队作业
【发布时间】:2017-01-24 12:04:07
【问题描述】:

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

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 项目) 的域名,否则我可以尝试创建一条会触发命令的路由。

【问题讨论】:

    标签: laravel laravel-5.2 beanstalkd laravel-queue


    【解决方案1】:

    在这个ref的帮助下

    通过使用这些包 "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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 1970-01-01
      • 2017-09-12
      • 2016-09-12
      • 2020-01-02
      • 1970-01-01
      • 2015-11-03
      相关资源
      最近更新 更多