Question

I have never setup up a queueing system before. I decided to give it a shot. It seems the queueing system is working perfectly. However, it doesn't seem the data is being sent correctly. Here is my code.

...
$comment = new Comment(Input::all());
$comment->user_id = $user->id;
$comment->save();

if ($comment->isSaved())
{
    $voters = $comment->argument->voters->unique()->toArray();
    Queue::push('Queues\NewComment',
        [
        'comment' => $comment->load('argument', 'user')->toArray(),
        'voters' => $voters
        ]
    );
    return Response::json(['success' => true, 'comment' => $comment->load('user')->toArray()]);
}
...

The class that handles this looks like this:

class NewComment {

    public function fire($job, $data)
    {
        $comment = $data['comment'];
        $voters = $data['voters'];

        Log::info($data);

        foreach ($voters as $voter)
        {
            if ($voter['id'] != $comment['user_id'])
            {
                $mailer = new NewCommentMailer($voter, $comment);
                $mailer->send();
            }
        }

        $job->delete();
    }

}

This works beautifully on my local server using the synchronous queue driver. However, on my production server, I'm using Beanstalkd. The queue is firing like it is supposed to. However, I'm getting an error like this:

[2013-12-19 10:25:02] production.ERROR: exception 'ErrorException' with message 'Undefined index: voters' in /var/www/mywebsite/app/queues/NewComment.php:10

If I print out the $data variable passed into the NewComment queue handler, I get this:

[2013-12-19 10:28:05] production.INFO: {"comment":{"incrementing":true,"timestamps":true,"exists":true}} [] []

I have no clue why this is happening. Anyone have an idea how to fix this.

Was it helpful?

Solution 2

What I ended up doing was sticking with simple numbers. I only stored the comment's ID on the queue and then did all the processing in my queue handler class. That was the easiest way to do it.

OTHER TIPS

So $voters apparently isn't being put into the queue as part of the payload. I'd build the payload array outside of the Queue::push() function, log the contents, and see exactly what is being put in.

I've found if you aren't getting something out that you expect, chances are, it's not being put in like you expect either.

While you are at it, make sure that the beanstalkd system hasn't got old data stuck in it that is incorrect. You could add a timestamp into the payload to help make sure it's the latest data, and arrange to delete or bury any jobs that don't have the appropriate information - checked before you start to process them. Just looking at a count of items in the beanstalkd tubes should make it plain if there are stuck jobs.

I've not done anything with Laravel, but I have written many tasks for other Beanstalkd and SQS-backed systems, and the hard part is when the job fails, and you have to figure out what went wrong, and how to avoid just redoing the same failure over and over again.

You will get data as expected in the handler by wrapping the data in an array:

array(
  array('comment' => $comment->load('argument', 'user')->toArray(),
        'voters' => $voters
       )
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top