服务器在负载很重的情况下崩溃(facebook SDK + Laravel 5.2 + AWS)

时间:2016-03-18 08:08:59

标签: php facebook-graph-api amazon-web-services facebook-php-sdk laravel-5.2

我在AWS的免费层,使用laravel框架和facebook v.2.5 SDK(Web)。我正试图从Facebook获得大约600名用户的最新帖子。最多6000个帖子。每次运行查询时,它都会运行大约10个循环,然后应用程序完全崩溃并脱机。然后几分钟后返回。 Laravel没有向我显示任何错误。

我的代码是:

/**
* Get facebook users posts
* @return \SammyK\LaravelFacebookSdk\LaravelFacebookSdk;
*/
public function posts(\SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb)
{
    // get posts
    $profiles_to_get = DB::table('facebook_profiles')->distinct('username')->get();
    $fb_admin_profile = DB::table('profiles')->where('social_media_type', "facebook")->first();
    $admin_fb_access_token = $fb_admin_profile->oauth_token;
    foreach ($profiles_to_get as $profile_to_get) {
        try {
          $response = $fb->get('/'.$profile_to_get->username.'?fields=posts.limit(10)', $admin_fb_access_token);
          $userNode = $response->getGraphUser();
          $posts = json_decode($userNode['posts']);
            foreach ($posts as $post) 
            {
                isset($post->message) ? $fb_posts[] = array('account_id'   => $profile_to_get->id,
                                                            'facebook_id'  => $userNode->getID(),
                                                            'message_id'   => $post->id, 
                                                            'message'      => $post->message,  
                                                            'created_time' => $post->created_time->date,
                                                            'created_at'   => Carbon::now(),
                                                            'updated_at'   => Carbon::now(),
                                                            ) : null;
                foreach ($fb_posts as $fb_post) 
                {
                    $postDuplicateChecker = DB::table('facebook_posts')->where('message_id', $fb_post['message_id'])->get();
                    if($postDuplicateChecker == !null)
                    {
                        DB::table('facebook_posts')->where('message_id', $fb_post['message_id'])->update($fb_post);
                        $notification = "First notification";
                    }  
                    else
                    {
                        DB::table('facebook_posts')->insert( $fb_post );
                        $notification = "Second notification";
                    }
                }
                if ($post > 0 && $post % 10 == 0) 
                {
                sleep(5);
                }
            }
        } catch(\Facebook\Exceptions\FacebookSDKException $e) {
          dd($e->getMessage());
        }
    }
  return Redirect::route('someroute',[ 'notification' => $notification]);
}

我已经尝试将查询超时设置为300,因此它不会超时,并且在每10个请求之后使循环休眠,以便减少负载。此外,我还有其他应用程序在同一台服务器上运行,但是当这个应用程序崩溃时,它们永远不会脱机。

我的问题是有没有办法优化代码,这样我就不必升级服务器,或者是我升级服务器的唯一选择?

1 个答案:

答案 0 :(得分:0)

答案是使用array_chunk批处理查询,并将进程分成较小的部分,这样可以更容易处理。

array array_chunk ( array $array , int $size [, bool $preserve_keys = false ] )

参考:http://php.net/manual/en/function.array-chunk.php