Laravel 队列不允许“闭包"的序列化

2022-01-21 00:00:00 queue closures php laravel

我需要帮助以使用 Laravel 调度作业,似乎系统在使用队列作业时尝试序列化闭包,因此出现此错误.

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.

我该如何解决这个问题?我也试过这个包
https://github.com/jeremeamia/super_closure
但在我的情况下它不起作用.

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.

这是我的代码:

在控制器 FacebookController 中:

In the controller FacebookController:

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

还有要派遣的工作:

<?php

namespace AppJobs;

use IlluminateBusQueueable;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;

use Session, DB, CarbonCarbon, 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(FacebookExceptionsFacebookSDKException $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(FacebookExceptionsFacebookSDKException $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' 的序列化的构造函数上

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

推荐答案

我的理解是不能在 Job 中包含不可序列化的变量,因为 Laravel 在存储 Job 详细信息时不知道如何处理它们.

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.

所以你需要从 construct() 方法和关联的 protected $fb;$ 中删除 $fbthis->fb = $fb; 行.

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

相反,您有两个选择

1) 简单

在您的 handle() 方法中创建一个新的 SammyKLaravelFacebookSdkLaravelFacebookSdk 对象,例如

Create a new SammyKLaravelFacebookSdkLaravelFacebookSdk object in your handle() method, e.g.

public function handle() {
    $fb = new SammyKLaravelFacebookSdkLaravelFacebookSdk;

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

2) 高级

如果需要传递之前使用的具体FB实例,注册一个FacebookSDK Service Provider 和 将其注入处理程序,如文档.

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.

例如

public function handle(FacebookSDK $fb) {

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

}

当您为 Facebook SDK 注册绑定服务容器时,handle() 方法中的这种类型提示会自动检索 $fb 实例并将其注入到可供使用的方法中.您可以确定是每次都创建一个新的 FacebookSDK 对象,还是只创建一个并始终使用该对象(称为 Singleton).

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).

相关文章