protected function execute($arguments = array(), $options = array())
 {
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
     $applicationConfig = sfProjectConfiguration::getApplicationConfiguration('frontend', 'prod', true);
     $context = sfContext::createInstance($applicationConfig);
     ProjectConfiguration::registerCron();
     $quiet = (bool) $options['quiet'];
     $passed = array();
     $assigned = array();
     if ($arguments['subreddit'] == '%') {
         if (!$quiet) {
             echo "Advancing EpisodeAssignments for all Subreddits...";
         }
         SubredditTable::getInstance()->advanceEpisodeAssignments();
     } else {
         $subreddit = SubredditTable::getInstance()->findOneByName($arguments['subreddit']);
         if ($subreddit) {
             if (!$quiet) {
                 echo "Advancing EpisodeAssignments for {$subreddit} Subreddit...";
             }
             $subreddit->advanceEpisodeAssignments();
         } else {
             throw new sfException('Cannot find Subreddit: ' . $arguments['subreddit']);
         }
     }
     if (!$quiet) {
         echo "\n";
     }
 }
 protected function execute($arguments = array(), $options = array())
 {
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
     ProjectConfiguration::registerCron();
     $quiet = (bool) $options['quiet'];
     if (!$quiet) {
         echo "Checking to see which subreddits need new episodes...";
     }
     $subreddits = Doctrine::getTable('Subreddit')->getSubredditsNeedingEpisodeGeneration($arguments['subreddit']);
     if (!$quiet) {
         echo "\n" . count($subreddits) . ' subreddits found that need episodes generated...';
     }
     $iteration = 0;
     foreach ($subreddits as $subreddit) {
         if (!$quiet) {
             echo (++$iteration != 1 ? ', ' : "\n") . $iteration;
         }
         $episodes = $subreddit->collectGeneratedEpisodes();
         if (!$quiet) {
             echo ' (' . count($episodes) . ')';
         }
         try {
             $episodes->save();
         } catch (Exception $e) {
             if (!$quiet) {
                 echo 'Error';
             }
             unset($e);
         }
     }
     if (!$quiet) {
         echo "\nFinished.\n";
     }
 }
 /**
  * Creates a collection of Episodes with released dates assembled using the
  * Subreddit's Episode schedule between the Subreddit's creation interval.
  *
  * @return array  An array of unsaved Episode objects
  */
 public function collectGeneratedEpisodes()
 {
     ProjectConfiguration::registerCron();
     $this->calculateCreationInterval();
     $episode_schedule = $this->getEpisodeScheduleAsCronExpression();
     $creation_schedule = $this->getCreationScheduleAsCronExpression();
     $seconds = $this->getCreationInterval();
     $last_episode = new DateTime(date('R', $this->getDateOfLastEpisode()));
     // Jan 31 2011
     if ($last_episode->getTimestamp() <= time()) {
         $last_episode = new DateTime();
     }
     $stop_creating = $creation_schedule->getNextRunDate($last_episode);
     // 01 Feb 2011
     while (time() + $seconds > $stop_creating->format('U')) {
         $stop_creating = $creation_schedule->getNextRunDate($stop_creating);
         // Push it out one segment further
     }
     $episode_date = $last_episode;
     $new_episodes = new Doctrine_Collection('Episode');
     $i = 0;
     while ($episode_schedule->getNextRunDate($episode_date)->getTimestamp() <= $stop_creating->getTimestamp()) {
         $episode_date = $episode_schedule->getNextRunDate($episode_date);
         $episode = new Episode();
         $episode->setSubreddit($this);
         $episode->setReleaseDate($episode_date->format('Y-m-d H:i:s'));
         $new_episodes[$i++] = $episode;
     }
     return $new_episodes;
 }