我想在 Amazon EC2 上运行Phirehose,这是一个 php Twitter 流 API 以连续运行。我使用了以下代码
<?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. \n";
$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();
?>
将此代码上传到 EC2 实例后,我使用 Putty 打开 ssh 并执行以下命令
$ cd /var/www/html/service
$ nohup php TweetStream.php &
大约 5 分钟后,当我运行时top -p $(pgrep -d',' php)
,我得到以下信息
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
我可以推测它已经运行了 6 分钟左右并进入了睡眠状态。我能知道我错过了什么吗?
如何让这个过程在 Amazon EC2 Instance 上持续运行?请朋友们帮帮我。
顺便说一句,mysqli 正在连接到在 Amazon 中运行的 RDS 实例。