/**
  * Selects the next due processing job and runs it through the core
  *
  * @return string $json
  */
 public function RunWorkflow($key)
 {
     //Setup the logger
     $logger = \Swiftriver\Core\Setup::GetLogger();
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [Method invoked]", \PEAR_LOG_INFO);
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [START: Setting time out]", \PEAR_LOG_DEBUG);
     set_time_limit(300);
     $timeout = ini_get('max_execution_time');
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [END: Setting time out to {$timeout}]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [START: Constructing Repository]", \PEAR_LOG_DEBUG);
     try {
         //Construct a new repository
         $channelRepository = new \Swiftriver\Core\DAL\Repositories\ChannelRepository();
     } catch (\Exception $e) {
         //get the exception message
         $message = $e->getMessage();
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [An exception was thrown]", \PEAR_LOG_DEBUG);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [{$message}]", \PEAR_LOG_ERR);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [Method finished]", \PEAR_LOG_INFO);
         return parent::FormatErrorMessage("An exception was thrown: {$message}");
     }
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [END: Constructing Repository]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [START: Fetching next Channel]", \PEAR_LOG_DEBUG);
     try {
         //Get the next due channel processign job
         $channel = $channelRepository->SelectNextDueChannel(time());
     } catch (\Exception $e) {
         //get the exception message
         $message = $e->getMessage();
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [An exception was thrown]", \PEAR_LOG_DEBUG);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [{$message}]", \PEAR_LOG_ERR);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [Method finished]", \PEAR_LOG_INFO);
         return parent::FormatErrorMessage("An exception was thrown: {$message}");
     }
     if ($channel == null) {
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [INFO: No Channel due]", \PEAR_LOG_DEBUG);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [END: Fetching next Channel]", \PEAR_LOG_DEBUG);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [Method finished]", \PEAR_LOG_INFO);
         return parent::FormatMessage("OK");
     }
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [END: Fetching next Channel]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [START: Get and parse content]", \PEAR_LOG_DEBUG);
     try {
         $SiSPS = new \Swiftriver\Core\Modules\SiSPS\SwiftriverSourceParsingService();
         $rawContent = $SiSPS->FetchContentFromChannel($channel);
     } catch (\Exception $e) {
         $message = $e->getMessage();
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [An exception was thrown]", \PEAR_LOG_DEBUG);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [{$message}]", \PEAR_LOG_ERR);
         try {
             $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [START: Mark Channel as in error]", \PEAR_LOG_DEBUG);
             $channel->inprocess = false;
             $channelRepository->SaveChannels(array($channel));
             $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [END: Mark Channel as in error]", \PEAR_LOG_DEBUG);
         } catch (\Exception $innerE) {
             $message = $innerE->getMessage();
             $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [An exception was thrown]", \PEAR_LOG_DEBUG);
             $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [{$message}]", \PEAR_LOG_ERR);
             $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [This Channel will remain in state - in progress - and will not be run again, manual action must be taken.]", \PEAR_LOG_ERR);
         }
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [Method finished]", \PEAR_LOG_INFO);
         return parent::FormatErrorMessage("An exception was thrown: {$message}");
     }
     if (isset($rawContent) && is_array($rawContent) && count($rawContent) > 0) {
         $workflow = new \Swiftriver\Core\Workflows\ContentServices\ProcessContent();
         $workflow->RunWorkflow($rawContent);
     } else {
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [END: Get and parse content]", \PEAR_LOG_DEBUG);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [No content found.]", \PEAR_LOG_DEBUG);
     }
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [START: Mark channel processing job as complete]", \PEAR_LOG_DEBUG);
     try {
         $channel->inprocess = false;
         $channel->lastSuccess = time();
         $channel->nextrun = \strtotime("+1 minute");
         $channelRepository->SaveChannels(array($channel));
     } catch (\Exception $e) {
         //get the exception message
         $message = $e->getMessage();
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [An exception was thrown]", \PEAR_LOG_DEBUG);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [{$message}]", \PEAR_LOG_ERR);
         $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [Method finished]", \PEAR_LOG_INFO);
         return parent::FormatErrorMessage("An exception was thrown: {$message}");
     }
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [END: Mark channel processing job as complete]", \PEAR_LOG_DEBUG);
     $logger->log("Core::Workflows::ChannelServices::RunNextChannel::RunWorkflow [Method finished]", \PEAR_LOG_INFO);
     return parent::FormatMessage("OK");
 }
 /**
  * Processes a queue file and does something with it (example only)
  * @param string $queueFile The queue file
  */
 protected function processQueueFile($queueFile)
 {
     $logger = \Swiftriver\Core\Setup::GetLogger();
     $logger->log("Core::Modules::TwitterStreamingSearchClient start processing " . $queueFile, \PEAR_LOG_DEBUG);
     // Open file
     $fp = fopen($queueFile, 'r');
     // Check if something has gone wrong, or perhaps the file is just locked by another process
     if (!is_resource($fp)) {
         $logger->log("Core::Modules::TwitterStreamingSearchClient file: " . $queueFile . " already open, skipping", \PEAR_LOG_DEBUG);
         return FALSE;
     }
     // Lock file
     flock($fp, LOCK_EX);
     $content = array();
     $logger->log("Core::Modules::TwitterStreamingSearchClient: START Looping through content", \PEAR_LOG_DEBUG);
     while ($rawStatus = fgets($fp, 4096)) {
         try {
             $status = \json_decode($rawStatus);
             $source_name = $status->user->screen_name;
             if ($source_name == null || $source_name == "") {
                 $source_name = "UNKNOWN";
             }
             $source = \Swiftriver\Core\ObjectModel\ObjectFactories\SourceFactory::CreateSourceFromIdentifier($source_name, false);
             $source->name = $source_name;
             $source->link = "http://twitter.com/" . $source_name;
             $source->parent = "TWITTERSTREAM";
             $source->type = "Twitter Stream";
             $source->subType = "Filter";
             $source->applicationIds["twitter"] = $status->user->id;
             $source->applicationProfileImages["twitter"] = $status->user->profile_image_url;
             //Create a new Content item
             $item = \Swiftriver\Core\ObjectModel\ObjectFactories\ContentFactory::CreateContent($source);
             //Fill the Content Item
             $item->text[] = new \Swiftriver\Core\ObjectModel\LanguageSpecificText(null, $status->text, array($status->text));
             $item->link = "http://twitter.com/" . $source_name . "/statuses/" . $status->id_str;
             $item->date = strtotime($status->created_at);
             /* GEO is not yet supported on this streamin feed
                if($tweet->geo != null && $tweet->geo->type == "Point" && \is_array($tweet->geo->coordinates))
                    $item->gisData[] = new \Swiftriver\Core\ObjectModel\GisData (
                            $tweet->geo->coordinates[1],
                            $tweet->geo->coordinates[0],
                            "");
                */
             //Sanitize the tweet text into a DIF collection
             $sanitizedTweetDiffCollection = $this->ParseTweetToSanitizedTweetDiffCollection($item);
             //Add the dif collection to the item
             $item->difs = array($sanitizedTweetDiffCollection);
             $content[] = $item;
         } catch (\Exception $e) {
             $logger->log("Core::Modules::TwitterStreamingSearchClient: {$e}", \PEAR_LOG_ERR);
         }
     }
     $logger->log("Core::Modules::TwitterStreamingSearchClient: START Looping through content", \PEAR_LOG_DEBUG);
     $workflow = new \Swiftriver\Core\Workflows\ContentServices\ProcessContent();
     //Here we are running the workflow without pre processing.
     $workflow->RunWorkflow($content, false);
     // Release lock and close
     flock($fp, LOCK_UN);
     fclose($fp);
     // All done with this file
     $logger->log("Core::Modules::TwitterStreamingSearchClient finshed processing " . $queueFile, \PEAR_LOG_DEBUG);
     unlink($queueFile);
 }