【发布时间】:2018-05-08 14:57:37
【问题描述】:
我需要帮助以使用 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));
}
还有要派遣的工作:
<?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
}
}
}
似乎错误出在$this->fb = $fb; 的构造函数上 // 'Closure' 的序列化是不允许的
【问题讨论】:
-
您是否尝试删除
use的SerializesModels特征? -
同样的错误不允许序列化'Closure' :(
标签: php laravel queue closures