问题
I am studying Laravel framework and I am facing some problems with queues. Laravel provides a unified API to work with queues and I am taking a look into it. One of the methods that Laravel provides is Queue::later(DateTime|int $delay, string $job, mixed $data = '', string $queue = null);
So, I implemented my job class:
<?php
class SendEmail {
public function send($job, $data) {
Log::info('JOB: ' . $job->getName());
Log::info('DATA: ' . $data['message']);
}
}
Above, I log on a file, the parameters received. Just to know if it's working or not. My controller calls the job this way using Queue API:
<?php
class MyControllerController extends BaseController {
function index() {
LOG::debug('Index action STARTED');
$date = Carbon::now()->addMinutes(2);
Queue::later($date, 'SendEmail@send', array('message' => 'MY MESSAGE!'));
$view = View::make('index');
LOG::debug('Index action FINISHED');
return $view;
}
}
Take a look that I am using Queue::later()
. I expected that method send()
of class SendEmail
would be executed after 2 minutes.
Well, I finished to implement a blank view and set up the route. Then I made a GET request to my controller and I went to check the log file.
I saw this when I opened the log file:
[2014-10-02 16:23:11] production.DEBUG: Index action STARTED [] []
[2014-10-02 16:23:11] production.INFO: JOB: [] []
[2014-10-02 16:23:11] production.INFO: DATA: MY MESSAGE! [] []
[2014-10-02 16:23:11] production.DEBUG: Index action FINISHED [] []
Everything executed at same time, inclusive the job. The Queue::later()
is not delaying the execution. I think my log file should be:
[2014-10-02 16:23:11] production.DEBUG: Index action STARTED [] []
[2014-10-02 16:23:11] production.DEBUG: Index action FINISHED [] []
[2014-10-02 16:25:11] production.INFO: JOB: [] []
[2014-10-02 16:25:11] production.INFO: DATA: MY MESSAGE! [] []
What's wrong with my code?
回答1:
Sorry, just found my mistake. I am using sync
driver, which means the job is executed when the job is created. I opened my /app/config/queue.php
and found:
'sync' => array(
'driver' => 'sync',
),
I will try with beanstalkd
.
来源:https://stackoverflow.com/questions/26168350/queuelater-not-working-on-laravel