7

I am trying to run code after returning a HTTP response. I know that Laravel 5 has support for queues, but I still find them confusing. I am trying to run code after the user has registered which requires the user's password and username. This answer seems interesting, but is not directly applicable to Laravel.

  • How do I create a job in a queue?
  • How can I pass data to the new job?

I know this sounds lazy and all, but I really don't understand the documentation.

3 Answers 3

9

Setting up queues requires, as the very first step, to choose what driver you'll be using. Because it's the quickest to get running, I'll explain how to begin with the database driver, as it doesn't require any other services to be installed on the server (as it's the case for beanstalkd for example). Here's how to get this set up:

1. Set the QUEUE_DRIVER in your .env file:

QUEUE_DRIVER=database

2. Run this command to generate the migration file for the jobs table, that will be used to store job information:

php artisan queue:table

3. Now run the migration to create the table:

php artisan migrate

A jobs table was created that will store data when jobs are pushed on the queue.


You can push both commands and clojures onto queues. For the sake of brevity, I'll show an example of how to push a closure onto a queue:

$username = Request::input('username');
$password = Request::input('password');

// Do your registration stuff

// Push a job onto the queue
\Queue::push(function($job) use ($username, $password)
{
    // Do the stuff you need here with $username and $password

    // Delete the job from the queue
    $job->delete();
});

The final step to making this work is to run the queue listener. Jobs will not be processed automatically unless the queue listener is running. So run this command:

php artisan queue:listen

There are further steps you can take, such as setting up Supervisor to monitor and restart the queue listener should it crash, but this should be enough to get you started.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for the answer. I changed my code, but it fails with Class 'App\Services\Queue' not found. I tried adding use App\Services\Queue to the top of the page, but that did not fix it. What am I missing?
There are two options: either use \Queue::push() instead of Queue::push(), or add use Queue; at the top of the file.
One more thing (tell me if this should be a new question): I am trying to run the code, but get FatalErrorException in SerializableClosure.php(153) : eval()'d code line 86: Class 'App\Services\Auth' not found after around 30 seconds. Does this mean the job is never really placed in the queue?
It's the same problem as with Queue. You're in the App\Services namespace. Which means that if you use Auth, it will automatically be assumed that the class in within that namespace and will try to load App\Services\Auth. That of course is not course is not correct, because all Laravel aliases are in the global namespace. So the solution is the same: you either add use Auth; at the top of the file or prepend a backslash like so \Auth. You can read more on how namespaces work in the PHP Documentation.
@Bogdan php artisan queue:listen command gives InvalidArgumentException: The "force" option doesn't exist in Laravel 5.4
|
1

Generally we pass data on the queue like this -

On the controller we have written -

$this->dispatch(new videoToAudioConvert($video_id))

On the job section you have to write like this -

protected $video_id

public function __contructor($video_id){
  $this->video_id = $video_id
}

public function handle(){
  $this->video_id
}

You can get more idea how to create jobs in queue and how to pass variable from here.

Comments

0

what is the requirement data to store in jobs table i use this to send email with queue and i will Schedule it i do the first 3 steps. Set the QUEUE_DRIVER in your .env file:

QUEUE_DRIVER=database 2. Run this command to generate the migration file for the jobs table, that will be used to store job information:

php artisan queue:table 3. Now run the migration to create the table:

php artisan migrate A jobs table was created that will store data when jobs are pushed on the queue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.