我被困在Laravel队列中......根据文档我正在做的事情,但每次排队的工作都会进入失败的工作表。
我正在尝试将一个将csv文件导入数据库的作业排队。
在.env文件中我完成了QUEUE_DRIVER = database。 (P.S我正在为我的队列使用数据库,在我的本地主机上,我在我的mac上使用PHP版本5.5.3)。作业正在数据库中创建,但它正在失败。
我的控制器中的代码 -
if($request->hasFile('import_file')){
$country = MemberProfile::where('username',$username)->first(['country']);
$csvname = $username.".".$request->file('import_file')->getClientOriginalExtension();
$csvpath = base_path() . '/public/assets/memberCollection/';
$request->file('import_file')->move($csvpath, $csvname);
$data = new importDataJob;
$data->path = $csvpath."".$csvname;
$data->listindex = $listindex;
$data->country = $country->country;
$data->username = $username;
$data->save();
$this->dispatch(new ImportLeads($data));
}
ImportLead.php作业文件中的代码是 -
protected $impStuff;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(importDataJob $values)
{
$this->impStuff = $values;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try{
$data = Excel::load($this->impStuff->path, function($reader) {
})->get();
Storage::disk('local')->append('logtest.txt', $this->impStuff->path);
//Inserting record by record into database
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$member = new MemberController();
//dd($this->impStuff["country"]);
$number = $member->E164FormatNumber($value->mobile,$this->impStuff->country);
$leadlistassoc_insert[] = ['listindex' => $this->impStuff->listindex, 'leadindex' => DB::table('leads')->insertGetId(['firstName' => $value->firstname, 'lastName' => $value->lastName, 'mobile' => $number, 'username' => $this->impStuff->username, 'created_at' => Carbon::now(), 'category' => 1])];
}
if(!empty($leadlistassoc_insert)){
DB::table('leadlistassoc')->insert($leadlistassoc_insert);
}
}
}
catch(Exception $e){
Storage::disk('local')->append('logtest.txt', $e);
}
}
public function failed()
{
Storage::disk('local')->append('logtest.txt', "failed");
}
如果我将其放入另一个类中,并且手动调用它的句柄函数,则相同的代码可以正常工作。
使用Laravel 5.2命令侦听数据库:
php artisan queue:listen --tries=3