普通网友 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

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

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大