Adam Green
Twitter API Consultant
adam@140dev.com
781-879-2960
@140dev

Free Source Code – Twitter Database Server: monitor_tweets.php

The Twitter database server code already tracks MySQL database errors in db_lib.php. Any error that MySQL returns is written to error_log.txt in the db directory. You should check the contents of this file after making any code changes that could affect database operations.

The other type of error is the flow of new tweets stopping. This can be caused by a problem with the Twitter API, or a failure of either get_tweets.php or parse_tweets.php. Monitor_tweets.php checks the tweets table for new tweets and sends an error email if none are found. The interval for checking and the email address for sending the error message are stored in 140dev_config.php. You should set up a cron job to run monitor_tweets.php regularly, as described in the install instructions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
/**
* monitor_tweets.php
* Check for new tweets in the tweets table and report by email if they aren't found
* Latest copy of this code: http://140dev.com/free-twitter-api-source-code-library/
* @author Adam Green <140dev@gmail.com>
* @license GNU Public License
* @version BETA 0.30
*/
require_once('140dev_config.php');
require_once('db_lib.php');
$oDB = new db;

if ($oDB->error) {
  // Report a DB connection error 
  error_email('Twitter Database Error', 'Unable to connect to Twitter database');
  exit;
}

// Check for new tweets arriving in the tweets table
// TWEET_ERROR_INTERVAL is defined in 140dev_config.php
$query = 'SELECT COUNT(*) AS cnt FROM tweets ' .
  'WHERE created_at > DATE_SUB(NOW( ), ' .
  'INTERVAL ' . TWEET_ERROR_INTERVAL . ' MINUTE)';
$result = $oDB->select($query);
$row = mysqli_fetch_assoc($result);

// If there are no new tweets
if ($row['cnt']==0) {
  // Get the date and time of the last tweet added
  $query = 'SELECT created_at FROM tweets ' .
    'ORDER BY created_at DESC LIMIT 1';
  $result = $oDB->select($query);
  $row = mysqli_fetch_assoc($result);
  $time_since = (time() - strtotime($row['created_at']))/60;
  $time_since = (int) $time_since;
  $error_message = 'No tweets added for ' . $time_since . " minutes."; 
  $subject = 'Twitter Database Server Error';
  error_email($subject,$error_message);
}

// Email the error message
function error_email($subject,$message) {
  $to = TWEET_ERROR_ADDRESS;
  $from = "From: Twitter Database Server Monitor <" . 
    TWEET_ERROR_ADDRESS . ">\r\n";
  mail($to,$subject,$message,$from);
}
?>

streaming_framework