dqy27359
2015-03-07 06:45
浏览 37
已采纳

在EC2微实例上连续运行Phirehose

I want to run Phirehose, a php Twitter streaming API on Amazon EC2 to run continuously. I used the following code

<?php
// TweetStream.php
require_once "OauthPhirehose.php";
require_once "../ajax/common.php";

const table_name = "tweets";

set_time_limit(0);

class Consumer extends OauthPhirehose {
    private $cobj; // mysqli object

    protected $count = 0;

    public function __construct($a, $b, $c) {
        parent::__construct($a, $b, $c);
        $this->cobj = new mysqli(db_host, db_user, db_pass, db_name);
    }

    public function enqueueStatus($status) {
        $data = json_decode($status);

        $cond = !isset($data->delete) && !isset($data->warning);
        $cond = $cond && !($data->id == 0 || trim($data->user->name) == "" || trim($data->text) == "" || $data->geo == NULL);
        if($cond) {
            ++$this->count;

            echo str_pad($this->count, 12, " ", STR_PAD_LEFT).": Getting Tweet: ".$data->id_str." Inserting... ";

            $id = $data->id_str;
            $idi = $data->id;
            $user = $data->user->name;
            $coord = $data->coordinates->coordinates;
            $lat = $coord[1];
            $lng = $coord[0];
            $tweet = $data->text;

            $stmt = $this->cobj->prepare("insert into ".table_name." values(?, ?, ?, ?, ?)");
            $stmt->bind_param("ssdds", $id, $user, $lat, $lng, $tweet);
            $stmt->execute();

            if($stmt->errno) echo "skipped: ".$stmt->error;
            else echo "done. 
";

            $stmt->close();
        }
    }
}

define("TWITTER_CONSUMER_KEY", "******");
define("TWITTER_CONSUMER_SECRET", "******");

define("OAUTH_TOKEN", "******");
define("OAUTH_SECRET", "********");

$sc = new Consumer(OAUTH_TOKEN, OAUTH_SECRET, Phirehose::METHOD_SAMPLE);
$sc->setLang("en");
$sc->consume();
?>

Once I upload this code to the EC2 instance I open ssh using Putty and execute following commands

$ cd /var/www/html/service
$ nohup php TweetStream.php &

After 5 minutes or so, when I run top -p $(pgrep -d',' php), I get the following

top - 06:38:52 up 1 day,  4:04,  1 user,  load average: 64.61, 66.82, 65.65
Tasks:   1 total,   0 running,   1 sleeping,   0 stopped,   0 zombie
Cpu(s):100.0%us,  0.0%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:    606528k total,   599552k used,     6976k free,     1172k buffers
Swap:        0k total,        0k used,        0k free,    29660k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
11902 ec2-user  20   0  438m 9096 1792 S  0.0  1.5   0:06.18 php

I can gather that it has run for 6 minutes or so and got into sleep state. Can I please know what am I missing?

How to make this process run continuously on Amazon EC2 Instance? Please help me friends.

By the way, The mysqli is connecting to RDS instance running in Amazon.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题