Search code examples
laravellaravel-5beanstalkdlaravel-queue

laravel push queued jobs from different codebase


I'm trying to trigger Jobs from other codebase

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

And MyJob Class:

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

I'm listen my queue by using queue:listen command like

php artisan queue:listen --queue=cron-default

If i run command (inside some codebase) command:my i'm getting payload like this any processed successfully.

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

Now my problem is i need to trigger this Job or command from other codebase is there any way?

I don't have domain name for the (commands/workers project) or else i could try create one route it will trigger command.


Solution

  • By the help this ref

    By using these packages "illuminate/queue": "5.2.", "pda/pheanstalk": "~3.0", "illuminate/encryption": "5.2."

    I'm able to push plain payload to the given tube like this

    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']);
    

    //First parameter complete path of the JobClass in other codebase //Second parameter any parameter to the Job.

    Minor modification in my Job to handle both commands, and plain payload

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