Laravel queue Serialization of 'Closure' is not allowed

*爱你&永不变心* 提交于 2019-12-11 06:19:02

问题


I need help to dispatch a job with Laravel, seems the system try to serialize a closure when using queue job so there's this error.

How can I fix this problem? I also tried this package
https://github.com/jeremeamia/super_closure
but it doesn't work in my case.

Here is my code:

In the controller FacebookController:

public function getPosts(\SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb) //get all post of logged user
{
    dispatch(new SyncFacebook($fb));
}

And Job to dispatch:

<?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
        }
    }
}

It seems the error is on the constructor at $this->fb = $fb; // Serialization of 'Closure' is not allowed


回答1:


My understanding is that you can't include non-serializable variables in the Job, as Laravel won't know how to deal with them when storing the job details.

So you need to remove the $fb from the construct() method and the associated protected $fb; and $this->fb = $fb; lines.

Instead you have two choices

1) Simple

Create a new \SammyK\LaravelFacebookSdk\LaravelFacebookSdk object in your handle() method, e.g.

public function handle() {
    $fb = new \SammyK\LaravelFacebookSdk\LaravelFacebookSdk;

    '.... replace all $this->fb with $fb
}

2) Advanced

If you need to pass the specific FB instance you used before, register a FacebookSDK Service Provider and inject it into the handler as seen in the docs.

E.g.

public function handle(FacebookSDK $fb) {

    '.... replace all $this->fb with $fb

}

When you register bind the service container for your Facebook SDK, this type-hinting in the handle() method automatically retrieves the $fb instance and injects it into the method ready for use. You can determine whether to create a new FacebookSDK object each time, or just create one and always use that one (known as a Singleton).




回答2:


Sometimes by changing the queue from sync to database can also resolve this issue.



来源:https://stackoverflow.com/questions/47475892/laravel-queue-serialization-of-closure-is-not-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!