use*_*349 1 php queue closures laravel
我需要帮助来使用 Laravel 分派作业,似乎系统在使用队列作业时尝试序列化闭包,因此出现此错误。
我该如何解决这个问题?我也尝试过这个包
https://github.com/jeremeamia/super_closure
但它在我的情况下不起作用。
这是我的代码:
在控制器 FacebookController 中:
public function getPosts(\SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) //get all post of logged user
{
dispatch(new SyncFacebook($fb));
}
Run Code Online (Sandbox Code Playgroud)
以及要调度的作业:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Session, DB, Carbon\Carbon, Auth;
use App\{
User, Reaction, Post, Synchronisation
};
class SyncFacebook implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $fb;
public function __construct($fb)
{
$this->fb = $fb;
}
public function handle()
{
set_time_limit(0);
$user = Auth::user();
try {
$response = $this->fb->get('/me/posts?limit=100', session('fb_user_access_token'));
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
dd($e->getMessage());
}
$postEdge = $response->getGraphEdge();
if(count($postEdge) > 0){
$pageCount = 0;
$maxPages = 9000000000000000000;
DB::beginTransaction();
try{
do{
foreach ($postEdge as $result) {
$result = $result->asArray();
$post_id = $result['id'];
$post = Post::where('post_id', $post_id)->first() ?: new Post;
$post->post_id = $post_id;
$post->user_id = $user->id;
$post->timezone = collect($result['created_time'])->toArray()['timezone'];
$post->post_date = collect($result['created_time'])->toArray()['date'];
$post->content = $result['message'] ?? $result['story'] ?? '';
$post->save();
$req = !empty(Synchronisation::whereUserId($user->id)->orderBy('id','desc')->date) ?
$post_id.'/reactions?limit=100&summary=total_count&since'.time(Synchronisation::whereUserId($user->id)->orderBy('id','desc')->date) :
$post_id.'/reactions?limit=100&summary=total_count';
try {
$response = $this->fb->get($req, session('fb_user_access_token'));
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
dd($e->getMessage());
}
$reactionEdge = $response->getGraphEdge();
$total_reactions = $reactionEdge->getMetaData()['summary']['total_count'] ?? null;
//dd($total_reactions);
$post->total_reactions = $total_reactions;
$post->save();
if(count($reactionEdge)){
$pagesReactionCount = 0;
do{
foreach($reactionEdge as $react){
$react = $react->asArray();
$reaction = Reaction::where('reaction_id', $react['id'])->first() ?: new Reaction;
$reaction->type = $react['type'];
$reaction->reaction_id = $react['id'];
$reaction->post_id = $post->id;
$reaction->author = $react['name'];
$reaction->save();
}
$pagesReactionCount++;
}
while($pagesReactionCount < $maxPages && $reactionEdge = $this->fb->next($reactionEdge));
}
}
$pageCount++;
}
while ($pageCount < $maxPages && $postEdge = $this->fb->next($postEdge));
$synchro = new Synchronisation;
$synchro->user_id = $user->id;
$synchro->date = now();
$synchro->completed = true;
$synchro->save();
}
catch(ValidationException $e){
DB::rollback();
if(env('APP_ENV') != 'production'){
dd($e->getMessage());
}
}
DB::commit();
}
else{
//no post
}
}
}
Run Code Online (Sandbox Code Playgroud)
看来错误出现在$this->fb = $fb;// Serialization of 'Closure' is not allowed 的构造函数上
我的理解是,你不能在作业中包含不可序列化的变量,因为 Laravel 在存储作业详细信息时不知道如何处理它们。
因此,您需要$fb从construct()方法中删除 以及关联的protected $fb;和$this->fb = $fb;行。
相反,你有两个选择
1)简单
在您的方法中创建一个新\SammyK\LaravelFacebookSdk\LaravelFacebookSdk对象handle(),例如
public function handle() {
$fb = new \SammyK\LaravelFacebookSdk\LaravelFacebookSdk;
'.... replace all $this->fb with $fb
}
Run Code Online (Sandbox Code Playgroud)
2) 高级
如果您需要传递之前使用的特定 FB 实例,请注册一个FacebookSDK 服务提供者并将其注入到处理程序中,如文档中所示。
例如
public function handle(FacebookSDK $fb) {
'.... replace all $this->fb with $fb
}
Run Code Online (Sandbox Code Playgroud)
当您为 Facebook SDK 注册绑定服务容器时,方法中的这种类型提示handle()会自动检索 $fb 实例并将其注入到准备使用的方法中。您可以决定是每次创建一个新的 FacebookSDK 对象,还是只创建一个并始终使用该对象(称为单例)。
| 归档时间: |
|
| 查看次数: |
7656 次 |
| 最近记录: |