普通网友 2016-03-18 08:08
浏览 76
已采纳

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

I'm on the free tier of AWS and using the laravel framework and the facebook v.2.5 SDK (Web). I'm trying to get the latests 10 posts from facebook for approximately 600 users. Which would be 6000 posts max. Every time I run the query it runs through about 10 loops and then the app completely crashes and goes offline. Then returns after a few minutes. Laravel isn't showing me any errors.

My code is:

/**
* 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]);
}

I've tried setting the query timeout at 300 so it doesn't time out and also making the loop sleep after every 10 requests so that it reduces the load. Also I have other apps running on the same server but they never go offline when this app crashes.

My question is is there any way to optimize the code so that I don't have to upgrade the server or is my only choice to upgrade the server?

  • 写回答

1条回答 默认 最新

  • dongshi8425 2016-03-21 11:32
    关注

    The answer was to batch the query using array_chunk and chunk the process into smaller pieces which could be handled easier.

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?