/**
  * Restores everything back to normal
  *
  * @return void
  **/
 public function tearDown()
 {
     parent::tearDown();
     Nodes\Environment::setRoot($this->_root);
     GearmanQueue::config(array('servers' => array('127.0.0.1')));
     GearmanQueue::client(false);
     CakeLog::enable('stderr');
     CakeLog::drop('queuetest');
     unset($this->logger);
 }
 /**
  * Load cronjobs.json and lint the file
  *
  * Then convert the complex array to the same
  * format as `_parseExistingCronjobs` does
  *
  * @return array
  */
 protected function _loadApplicationCronjobs()
 {
     $cronjobs = $this->lint(true);
     $list = array();
     foreach ($cronjobs as $cronjob) {
         $name = sprintf('# %s - %s', Nodes\Environment::getProjectName(), $cronjob['name']);
         $command = sprintf('cd /var/www/%s/htdocs/ && %s', Nodes\Environment::getProjectName(), $cronjob['command']);
         $command = sprintf("%s\t%s\t%s\t%s\t%s\t%s", $cronjob['minute'], $cronjob['hour'], $cronjob['day_of_month'], $cronjob['month'], $cronjob['day_of_week'], $command);
         $list[$name] = $command;
     }
     return $list;
 }
 /**
  * Starts a new background task by passing some data to it with a priority
  *
  * @param string $taskName name of the task to be executed
  * @param mixed $data info to be passed to background task
  * @param sting $priority null for normal or either "low" or "high"
  * @return boolean success
  **/
 public static function execute($taskName, $data = null, $priority = null)
 {
     if (!empty($priority)) {
         $priority = strtolower($priority);
         if (!in_array($priority, array('low', 'high'))) {
             throw new InvalidArgumentException(sprintf('%s is not a valid priority, only accepting low and high', $priority));
         }
     }
     $data = json_encode($data);
     $taskName = Nodes\Environment::getProjectName() . '_' . $taskName;
     CakeLog::debug(sprintf('Creating background job: %s (%s)', $taskName, $data));
     $job = static::client()->{'do' . ucFirst($priority) . 'Background'}($taskName, $data);
     if (static::client()->returnCode() !== GEARMAN_SUCCESS) {
         CakeLog::error(sprintf('Could not create background job for task %s and data %s. Got %s (%s)', $taskName, $data, $job, static::client()->error()));
         return false;
     }
     return true;
 }
 /**
  * Setup workers and their callbacks for Gearman
  *
  * @return void
  */
 protected function _setupWorkers()
 {
     $worker = $this->_worker();
     foreach (Hash::normalize($this->tasks) as $t => $conf) {
         list($plugin, $class) = pluginSplit($t);
         if (!method_exists($this->{$class}, 'workerMethods')) {
             continue;
         }
         if (method_exists($this->{$class}, 'startup')) {
             $this->{$class}->startup();
         }
         $methods = $this->{$class}->workerMethods();
         foreach ($methods as $name => $method) {
             $name = Nodes\Environment::getProjectName() . '_' . $name;
             $callback = $this->_addWorkerFunction($class, $method);
             $worker->addFunction($name, $callback);
             $this->log("Registered function for {$name}", 'info');
         }
     }
 }