【发布时间】:2015-08-28 21:44:03
【问题描述】:
我使用 beanstalkd 作为我的队列驱动程序:
# /.env
QUEUE_DRIVER=beanstalkd
# /config/queue.php
'default' => env('QUEUE_DRIVER', 'sync'),
和一个可排队的工作
# /app/Jobs/MyJob.php
class MyJob extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
....
....
}
当我通过控制器调度作业时,这很有效,但我希望在调度时使用 sync 驱动程序而不是 beanstalkd 驱动程序的特定路线工作。中间件似乎是这里的答案
# /app/Http/Controllers/MyController.php
public function create(Request $request)
{
$this->dispatch(new \App\Jobs\MyJob());
}
# /app/Http/routes.php
Route::post('/create', ['middleware' => 'no_queue', 'uses' => 'MyController@create']);
# /app/Http/Middleware/NoQueue.php
public function handle($request, Closure $next)
{
$response = $next($request);
config(['queue.default'=>'sync']);
return $response;
}
但是,作业仍在被推送到 beanstalkd 队列。
换句话说,当从控制器调度作业时,如何在运行时更改队列驱动程序?
编辑: 调用 config(['queue.default'=>'sync']) 确实 似乎可以在 Artisan 命令中工作,而不是来自 Http 控制器...... p>
# /app/Conosle/Commands/MyCommand.php
class ScrapeDrawing extends Command
{
use DispatchesJobs;
...
...
public function handle()
{
config(['queue.default'=>'sync'])
$this->dispatch(new \App\Jobs\MyJob());
}
}
【问题讨论】:
-
Queue::driver('sync')->push(new \App\Jobs\MyJob());怎么样 -
Call to undefined method Illuminate\Queue\Queue::driver()
标签: php laravel laravel-5 laravel-5.1