早上好!Laravel 5.2:使用Laravel隊列服務處理數據庫插入
我有下面的代碼在我的控制器:
$num_rows = $this->drupalRequestRowCount();
$uris = $this->drupalRequestPrepareUri($num_rows, config('constants.IMPORT_DATA_URI'));
$requestHandler = new RequestHandler($uris[0]);
$content = $requestHandler->httpRequest();
// Dispatch each URI for the job that will handle
// the insertion of our Drupal data.
foreach($uris as $uri) {
$job = (new DataJob($uri))->onQueue('data_export_insert');
$this->dispatch($job);
}
return 'Jobs dispatched';
我有,我想每天晚上我Laravel數據庫進行同步數據的Drupal數據庫。我利用我自己在Drupal中開發的一個API,它返回我需要的大量數據以用於我的Laravel數據庫。
該API最多返回10個項目(其中包含大量數據)。 Drupal數據庫每天晚上需要導入+ - 17000個項目。我認爲Queue API可能是一個很好的解決方案,可以批量導入數據,而不是一次導入所有數據。我循環遍歷foreach循環中的$uris
數組,根據我得到的$num_rows
向API附加偏移量。
所以,如果$num_rows
返回17000項,則$uris
數組將包含1700項,像這樣:
[0] => 'http://mywebsite.com/api/request_data.json?offset=0'
[1] => 'http://mywebsite.com/api/request_data.json?offset=10'
[2] => 'http://mywebsite.com/api/request_data.json?offset=20'
等
這意味着,我想有1700一旦我運行php artisan queue:listen database
命令,就會派遣Laravel執行我的任務。
以下代碼是我要執行1700次的作業。
public function __construct($uri)
{
$this->uri = $uri;
}
public function handle()
{
try {
Log::info('Inserting data into database for uri: ' . $this->uri);
$requestHandler = new RequestHandler($this->uri);
$content = $requestHandler->httpRequest();
foreach($content as $key => $value) {
$id = $value->id;
$name = $value->name;
DB::insert('INSERT INTO table_1 (id, name) VALUES (?, ?)', [$id, $name]);
}
} catch(\Exception $e) {
Log::error('Error in job: '. $e->getMessage());
}
}
我的問題是,喬布斯根本沒有得到執行。作業正在正確分派到數據庫,因爲當我檢查數據庫中的作業表時,可以在作業表中看到1700行。我試着記錄手柄功能,但當我運行php artisan queue:listen
或php artisan queue:work
什麼也沒有發生。 failed_jobs表也沒有任何記錄。
句柄函數沒有被執行,所以我不能記錄裏面發生的任何事情。我嘗試用例子在互聯網上找到信息,但我能找到的是一些我不需要的郵件例子。
任何幫助將不勝感激!