Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

thsteinmetz's avatar

[L5][Beanstalkd] Dispatch command onto specific tube

Right now I am using

Queue::pushOn('tube_name', new SomeCommand($data)); 

but I'd love to move over to using the dispatch trait but is there a way to specify the tube name that I want this command to run on? I've been looking through the src but I've come up empty thus far.

0 likes
8 replies
usman's avatar
usman
Best Answer
Level 27

Hi, When we dispatch a command, the command bus checks if the command is queue-able. In case it ShoulBeQueued it calls the dispatchToQueue method.

If we define a method named as queue inside our queued command the dispatchToQueue will prefer this method to use for the queuing of the Command.

Here is an example that you can use as a reference:


<?php namespace App\Commands;

use App\Commands\Command;

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

class QueuedCommand extends Command implements ShouldBeQueued {

    use InteractsWithQueue, SerializesModels;


    public $message;
    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    public function queue($queue, $command)
    {

        $queue->pushOn('my_queue',$command);

    }
}

Now your command will be queued on 'my_queue`. you can work it out using the:

./artisan queue:work --queue="my_queue"

I have tested it using Beanstalkd driver and works perfectly :)

I hope this will help you. Usman.

5 likes
jonnywilliamson's avatar

@usman - thank you so much for that wonder clear example. This should have been in the docs I think, but anyway, it works perfectly!

johnpaulmedina's avatar

I tried to implement this on a command which has a separate handler on L/5.0

Any reason why this method won't execute a pushOn tube?

@usman

UPDATE ** DISREGARD MY LAST COMMENT... YOU CAN JUST USE Queue::laterOn( $tube, $time, $job );

jonnywilliamson's avatar

Laravel 5.1 Docs now have a much clearer and simpler example for this:

Specifying The Queue For A Job

You may also specify the queue a job should be sent to.

By pushing jobs to different queues, you may "categorize" your queued jobs, and even prioritize how many workers you assign to various queues. This does not push jobs to different queue "connections" as defined by your queue configuration file, but only to specific queues within a single connection. To specify the queue, use the onQueue method on the job instance. The onQueue method is provided by the base App\Jobs\Job class included with Laravel:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use App\Jobs\SendReminderEmail;
use App\Http\Controllers\Controller;

class UserController extends Controller
{
    /**
     * Send a reminder e-mail to a given user.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function sendReminderEmail(Request $request, $id)
    {
        $user = User::findOrFail($id);

        $job = (new SendReminderEmail($user))->onQueue('emails');

        $this->dispatch($job);
    }
}
tomcoonen's avatar

An update for Laravel 5.1:

    public function queue($queue, $command, $data)
    {
        $queue->pushOn('your-queue', $command, $data);
    }
1 like

Please or to participate in this conversation.