我正在尝试在我的一个项目中设置事件,侦听器和作业。如果删除了“ implements ShouldQueue”,该代码将能够运行。但是一旦我在Job类中包含了ShouldSoulQueue工具,它就会停止工作
我的EventServiceProvider
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
'App\Events\IncorpPaid' => [
'App\Listeners\ProcessIncorpReceived',
],
];
}
我的活动课
Namespace App\Events;
use App\Company;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class IncorpPaid
{
use Dispatchable, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
}
我的听众
namespace App\Listeners;
use App\Company;
use App\Events\IncorpPaid;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Http\Request;
use Illuminate\Queue\SerializesModels;
class ProcessIncorpReceived
{
//use SerializesModels;
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
}
/**
* Handle the event.
*
* @param IncorpPaid $event
* @return void
*/
public function handle(IncorpPaid $event)
{
\App\Jobs\SendKYC::dispatch($event->company);
}
}
最后是我的问题代码。
以下作品。但是问题在于使用这种方法不会使作业排队。
namespace App\Jobs;
use App\Company;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendKYC
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Company $company)
{
//error_log(print_r($this->company->puc,true));
// error_log(print_r($company,true));
\App\Services\SendDocsForSignature::processKYC($this->company);
}
}
但是,这使作业排队,但不起作用。 $ this-> company返回一个空白的公司模型。
namespace App\Jobs;
use App\Company;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class SendKYC implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}
/**
* Execute the job.
*
* @return void
*/
public function handle(Company $company)
{
\App\Services\SendDocsForSignature::processKYC($this->company);
}
}
答案 0 :(得分:0)
您的最后一个代码段看起来几乎正确。您只需要声明要在其中存储$company
值的实例变量。换句话说,您需要将protected $company;
添加到SendKYC
类中,才能将$company
的值分配给$this->company
。
您SendKYC
的课程应该这样开始:
class SendKYC implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $company;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Company $company)
{
$this->company = $company;
}