/**
     * Execute the action
     *
     * @return	void
     */
    public function execute()
    {
        // no timelimit
        set_time_limit(0);
        // get database
        $db = BackendModel::getDB(true);
        // create log
        $log = new SpoonLog('custom', BACKEND_CACHE_PATH . '/logs/events');
        // get process-id
        $pid = getmypid();
        // store PID
        SpoonFile::setContent(BACKEND_CACHE_PATH . '/hooks/pid', $pid);
        // loop forever
        while (true) {
            // get 1 item
            $item = $db->getRecord('SELECT *
									FROM hooks_queue
									WHERE status = ?
									LIMIT 1', array('queued'));
            // any item?
            if (!empty($item)) {
                // init var
                $processedSuccesfully = true;
                // set item as busy
                $db->update('hooks_queue', array('status' => 'busy'), 'id = ?', array($item['id']));
                // unserialize data
                $item['callback'] = unserialize($item['callback']);
                $item['data'] = unserialize($item['data']);
                // check if the item is callable
                if (!is_callable($item['callback'])) {
                    // in debug mode we want to know if there are errors
                    if (SPOON_DEBUG) {
                        throw new BackendException('Invalid callback.');
                    }
                    // set to error state
                    $db->update('hooks_queue', array('status' => 'error'), 'id = ?', $item['id']);
                    // reset state
                    $processedSuccesfully = false;
                    // logging when we are in debugmode
                    if (SPOON_DEBUG) {
                        $log->write('Callback (' . serialize($item['callback']) . ') failed.');
                    }
                }
                try {
                    // logging when we are in debugmode
                    if (SPOON_DEBUG) {
                        $log->write('Callback (' . serialize($item['callback']) . ') called.');
                    }
                    // call the callback
                    $return = call_user_func($item['callback'], $item['data']);
                    // failed?
                    if ($return === false) {
                        // set to error state
                        $db->update('hooks_queue', array('status' => 'error'), 'id = ?', $item['id']);
                        // reset state
                        $processedSuccesfully = false;
                        // logging when we are in debugmode
                        if (SPOON_DEBUG) {
                            $log->write('Callback (' . serialize($item['callback']) . ') failed.');
                        }
                    }
                } catch (Exception $e) {
                    // set to error state
                    $db->update('hooks_queue', array('status' => 'error'), 'id = ?', $item['id']);
                    // reset state
                    $processedSuccesfully = false;
                    // logging when we are in debugmode
                    if (SPOON_DEBUG) {
                        $log->write('Callback (' . serialize($item['callback']) . ') failed.');
                    }
                }
                // everything went fine so delete the item
                if ($processedSuccesfully) {
                    $db->delete('hooks_queue', 'id = ?', $item['id']);
                }
                // logging when we are in debugmode
                if (SPOON_DEBUG) {
                    $log->write('Callback (' . serialize($item['callback']) . ') finished.');
                }
            } else {
                // remove the file
                SpoonFile::delete(BACKEND_CACHE_PATH . '/hooks/pid');
                // stop the script
                exit;
            }
        }
    }
示例#2
0
 /**
  * Set the logpath.
  *
  * @return	void
  * @param	string $path	The path where the logfiles should be stored.
  */
 public static function setPath($path)
 {
     self::$path = (string) $path;
 }
示例#3
0
    /**
     * Trigger an event
     *
     * @param string $module The module that triggers the event.
     * @param string $eventName The name of the event.
     * @param mixed[optional] $data The data that should be send to subscribers.
     */
    public static function triggerEvent($module, $eventName, $data = null)
    {
        $module = (string) $module;
        $eventName = (string) $eventName;
        // create log instance
        $log = new SpoonLog('custom', PATH_WWW . '/frontend/cache/logs/events');
        // logging when we are in debugmode
        if (SPOON_DEBUG) {
            $log->write('Event (' . $module . '/' . $eventName . ') triggered.');
        }
        // get all items that subscribe to this event
        $subscriptions = (array) self::getDB()->getRecords('SELECT i.module, i.callback
			 FROM hooks_subscriptions AS i
			 WHERE i.event_module = ? AND i.event_name = ?', array($module, $eventName));
        // any subscriptions?
        if (!empty($subscriptions)) {
            // init var
            $queuedItems = array();
            // loop items
            foreach ($subscriptions as $subscription) {
                // build record
                $item['module'] = $subscription['module'];
                $item['callback'] = $subscription['callback'];
                $item['data'] = serialize($data);
                $item['status'] = 'queued';
                $item['created_on'] = FrontendModel::getUTCDate();
                // add
                $queuedItems[] = self::getDB(true)->insert('hooks_queue', $item);
                // logging when we are in debugmode
                if (SPOON_DEBUG) {
                    $log->write('Callback (' . $subscription['callback'] . ') is subcribed to event (' . $module . '/' . $eventName . ').');
                }
            }
            // start processing
            self::startProcessingHooks();
        }
    }
示例#4
0
 public function testRotate()
 {
     $this->log->write('Message for the log');
     $this->log->rotate();
     $this->assertFalse(SpoonFile::exists($this->log->getPath() . '/custom.log'));
 }