Exemplo n.º 1
0
 /**
  * @return
  */
 public function launchStreams()
 {
     $logger = Logger::getInstance('stream_log_location');
     if (!$this->php_path) {
         $logger->logError("php path is not set: check Twitter Realtime plugin configuration", __METHOD__ . ',' . __LINE__);
         return;
     }
     // get information from database about all streams.  This data is indexed by email + instance id.
     $stream_hash = $this->stream_proc_dao->getAllStreamProcesses();
     // get all owners
     $owners = $this->owner_dao->getAllOwners();
     $count = 0;
     // exec the stream processing script for each owner. This will fire up the
     // stream consumption if the owner has a twitter instance.
     foreach ($owners as $owner) {
         if ($count == self::MAX_INSTANCES) {
             break;
             // only open user stream process for up to MAX_INSTANCES instances
         }
         // the last argument in the following causes only active instances to be retrieved.
         $instances = $this->instance_dao->getByOwnerAndNetwork($owner, 'twitter', true, true);
         foreach ($instances as $instance) {
             $owner_email = $owner->email;
             if (isset($owner_email)) {
                 $idx = $owner_email . "_" . $instance->id;
                 $start_new_proc = false;
                 // if a 'live' process for that user is already running, take no action
                 if (isset($stream_hash[$idx]) && $stream_hash[$idx]['email'] == $owner_email && $stream_hash[$idx]['instance_id'] == $instance->id) {
                     if (strtotime($stream_hash[$idx]['last_report']) < time() - self::GAP_TIME) {
                         $logger->logInfo("killing process " . $stream_hash[$idx]['process_id'] . " -- it has not updated recently", __METHOD__ . ',' . __LINE__);
                         $this->psKill($stream_hash[$idx]['process_id']);
                         $this->stream_proc_dao->deleteProcess($stream_hash[$idx]['process_id']);
                         $start_new_proc = true;
                     } else {
                         $logger->logInfo("process " . $stream_hash[$idx]['process_id'] . " listed with recent update time for instance with {$owner_email} and " . $stream_hash[$idx]['instance_id'] . "-- not starting another one", __METHOD__ . ',' . __LINE__);
                         $count++;
                         // include this proc in the count of running processes
                     }
                 } else {
                     // start up a process for that instance
                     $start_new_proc = true;
                 }
                 if ($start_new_proc) {
                     $logger->logInfo("starting new process for " . "{$owner_email} and " . $instance->id, __METHOD__ . ',' . __LINE__);
                     $pass = $this->owner_dao->getPass($owner_email);
                     if ($pass && isset($this->php_path)) {
                         // @TODO - check that the dir paths are set properly
                         // then exec using that owner email and the encrypted pwd as args
                         $logfile = $this->log_dir . '/' . $owner_email . '_' . $instance->id . '.log';
                         $pid = shell_exec('cd ' . $this->streaming_dir . '; ' . $this->php_path . ' stream2.php ' . ' ' . $instance->id . ' ' . $owner_email . ' ' . $pass . ' > ' . $logfile . ' 2>&1 & echo $!');
                         if (!isset($pid)) {
                             throw new StreamingException("error: could not obtain PID when starting stream2 process.");
                         }
                         // insert PID and email/instance id information into the database.
                         $res = $this->stream_proc_dao->insertProcessInfo(trim($pid), $owner_email, $instance->id);
                         if (!$res) {
                             throw new StreamingException("error: issue inserting process information into database.");
                         }
                         $logger->logInfo("started pid " . trim($pid) . " for {$owner_email} and instance id " . $instance->id, __METHOD__ . ',' . __LINE__);
                         $count++;
                     } else {
                         $logger->logError("error: not launching stream for {$owner_email}-- error " . "with specified password or php path", __METHOD__ . ',' . __LINE__);
                     }
                 }
                 if ($count == self::MAX_INSTANCES) {
                     break;
                     // only open user stream process for up to MAX_OWNERS instances
                 }
             } else {
                 $logger->logError("error: email info not available. not launching stream for instance " . $instance->id, __METHOD__ . ',' . __LINE__);
             }
         }
         // end foreach instance
     }
     // end foreach owner
 }