Example #1
0
 /**
  * Executes the command cron:run.
  *
  * Tries to acquire the cron lock, then if no argument has been given runs all ready cron tasks.
  * If the cron lock can not be obtained, an error message is printed
  *		and the exit status is set to 1.
  * If the verbose option is specified, each start of a task is printed.
  *		Otherwise there is no output.
  * If an argument is given to the command, only the task whose name matches the
  *		argument will be started. If verbose option is specified,
  *		an info message containing the name of the task is printed.
  * If no task matches the argument given, an error message is printed
  *		and the exit status is set to 2.
  *
  * @param InputInterface $input The input stream used to get the argument and verboe option.
  * @param OutputInterface $output The output stream, used for printing verbose-mode and error information.
  *
  * @return int 0 if all is ok, 1 if a lock error occured and 2 if no task matching the argument was found.
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     if ($this->lock_db->acquire()) {
         $task_name = $input->getArgument('name');
         if ($task_name) {
             $exit_status = $this->run_one($input, $output, $task_name);
         } else {
             $exit_status = $this->run_all($input, $output);
         }
         $this->lock_db->release();
         return $exit_status;
     } else {
         $output->writeln('<error>' . $this->user->lang('CRON_LOCK_ERROR') . '</error>');
         return 1;
     }
 }
Example #2
0
    /**
     * Regenerate left/right ids from parent/child relationship
     *
     * This method regenerates the left/right ids for the tree based on
     * the parent/child relations. This function executes three queries per
     * item, so it should only be called, when the set has one of the following
     * problems:
     *	- The set has a duplicated value inside the left/right id chain
     *	- The set has a missing value inside the left/right id chain
     *	- The set has items that do not have a left/right id set
     *
     * When regenerating the items, the items are sorted by parent id and their
     * current left id, so the current child/parent relationships are kept
     * and running the function on a working set will not change the order.
     *
     * @param int	$new_id		First left_id to be used (should start with 1)
     * @param int	$parent_id	parent_id of the current set (default = 0)
     * @param bool	$reset_ids	Should we reset all left_id/right_id on the first call?
     * @return	int		$new_id		The next left_id/right_id that should be used
     */
    public function regenerate_left_right_ids($new_id, $parent_id = 0, $reset_ids = false)
    {
        if ($acquired_new_lock = $this->acquire_lock()) {
            $this->db->sql_transaction('begin');
            if (!$reset_ids) {
                $sql = 'UPDATE ' . $this->table_name . '
					SET ' . $this->column_item_parents . " = ''\n\t\t\t\t\t" . $this->get_sql_where('WHERE');
                $this->db->sql_query($sql);
            }
        }
        if ($reset_ids) {
            $sql = 'UPDATE ' . $this->table_name . '
				SET ' . $this->db->sql_build_array('UPDATE', array($this->column_left_id => 0, $this->column_right_id => 0, $this->column_item_parents => '')) . '
				' . $this->get_sql_where('WHERE');
            $this->db->sql_query($sql);
        }
        $sql = 'SELECT *
			FROM ' . $this->table_name . '
			WHERE ' . $this->column_parent_id . ' = ' . (int) $parent_id . '
				' . $this->get_sql_where('AND') . '
			ORDER BY ' . $this->column_left_id . ', ' . $this->column_item_id . ' ASC';
        $result = $this->db->sql_query($sql);
        while ($row = $this->db->sql_fetchrow($result)) {
            // First we update the left_id for this module
            if ($row[$this->column_left_id] != $new_id) {
                $sql = 'UPDATE ' . $this->table_name . '
					SET ' . $this->db->sql_build_array('UPDATE', array($this->column_left_id => $new_id)) . '
					WHERE ' . $this->column_item_id . ' = ' . (int) $row[$this->column_item_id];
                $this->db->sql_query($sql);
            }
            $new_id++;
            // Then we go through any children and update their left/right id's
            $new_id = $this->regenerate_left_right_ids($new_id, $row[$this->column_item_id]);
            // Then we come back and update the right_id for this module
            if ($row[$this->column_right_id] != $new_id) {
                $sql = 'UPDATE ' . $this->table_name . '
					SET ' . $this->db->sql_build_array('UPDATE', array($this->column_right_id => $new_id)) . '
					WHERE ' . $this->column_item_id . ' = ' . (int) $row[$this->column_item_id];
                $this->db->sql_query($sql);
            }
            $new_id++;
        }
        $this->db->sql_freeresult($result);
        if ($acquired_new_lock) {
            $this->db->sql_transaction('commit');
            $this->lock->release();
        }
        return $new_id;
    }