可以将类重新加载到PHP中吗?

时间:2018-02-10 16:08:36

标签: php class reload

目标是不断收集当前温度的数据。但是一个单独的过程应该分析该数据的输出,因为我必须经常调整算法,但我想避免停机,因此停止该过程是不行的。

问题是当我分离这些进程时,进程2要么不断地调用数据库,要么从本地文件中读取以对1进程生成的输出执行某些操作但我想立即对其执行操作这在资源方面是昂贵的。

当文件发生变化时,是否有可能以某种方式将类重新加载到内存中,例如编写一个继续计算文件MD5的函数,如果它发生了变化而不是以某种方式重新加载类?所以这个单独的类应该充当插件。有没有办法让这项工作?

1 个答案:

答案 0 :(得分:1)

这是一个可能的解决方案。使用Beanstalk(https://github.com/kr/beanstalkd)。

与Beanstalk(https://github.com/pda/pheanstalk

交谈的PHP类

运行beanstalk。

创建一个进程,该进程进入从Beanstalk队列读取的无限循环。 (Beanstalk队列称为" Tubes")。 PHP进程并不打算运行很长时间。主要原因是记忆。处理此问题的最简单方法是每隔一段时间重新启动一次进程,或者如果内存达到某个阈值。

注意:我所做的是让流程在一段固定时间后退出,或者如果它使用了一定量的内存。然后,我使用Supervisor重新启动它。

您可以将数据作为JSON放入Beanstalk并在接收端解码。发送和接收过程需要就该格式达成一致。您可以将工作负载存储在数据库中,只需将主键发送到队列中。

以下是您可以使用的一些代码:

class BeanstalkClient extends AbstractBaseQueue{
    public $queue;
    public $host;
    public $port;
    public $timeout;

    function __construct($timeout=null) {
        $this->loadClasses();
        $this->host = '127.0.0.1';
        $this->port = BEANSTALK_PORT;
        $this->timeout = 30;
        $this->connect();
    }
    public function connect(){
        $this->queue = new \Pheanstalk\Pheanstalk($this->host, $this->port);
    }
    public function publish($tube, $data, $delay){
        $payload = $this->encodeData($data);
        $this->queue->useTube($tube)->put($payload,
                        \Pheanstalk\PheanstalkInterface::DEFAULT_PRIORITY, $delay);

    }
    public function waitForMessages($tube, $callback=null){
        if ( $this->timeout ) {
            return $this->queue->watchOnly($tube)->reserve($this->timeout);
        }
        return $this->queue->watchOnly($tube)->reserve();
    }

    public function delete($message){
        $this->queue->delete($message);
    }

    public function encodeData($data){
        $payload = json_encode($data);
        return $payload;
    }

    public function decodeData($encodedData) {
        return json_decode($encodedData, true);
    }

    public function getData($message){
        if ( is_string($message) ) {
            throw new Exception('message is a string');
        }
        return json_decode($message->getData(), true);
    }
}



abstract class BaseQueueProcess {
    protected $channelName = ''; // child class should set this
    // The queue object
    public $queue = null;
    public $processId = null; // this is the system process id
    public $name = null;
    public $status = null;

    public function initialize() {
        $this->processId = getmypid();
        $this->name = get_called_class();
        $this->endTime = time() + (2 * 60 * 60); // restart every hour
        // seconds to timeout when waiting for a message
        // if the process isn't doing anything, timeout so they have a chance to do housekeeping.
        $queueTimeout = 900; 
        if ( empty($this->queue) ) {
            $this->queue = new BeanstalkClient($queueTimeout);
        }   
    }

    public function receiveMessage($queueMessage) {
        $taskData = $this->queue->getData($queueMessage);
        // debuglog(' Task Data = ' . print_r($taskData, true));
        if ( $this->validateTaskData($taskData) ) {
            // process the message
            $good = $this->didReceiveMessage($taskData);
            if ( $good !== false ) {
                // debuglog("Completing task {$this->taskId}");
                $this->completeTask($queueMessage);
            }
            else {
                $this->failTask($queueMessage);
            }
        }
        else {
            // Handle bad message
            $this->queue->delete($queueMessage);
        }
    }

    public function run() {
        $this->processName = $this->channelName;
        // debuglog('Start ' . $this->processName);
        // debuglog(print_r($this->params, true));
        while(1) {
            $queueMessage = $this->queue->waitForMessages($this->channelName);

            if ( ! empty($queueMessage) ) {
                $this->receiveMessage($queueMessage);
            } 
            else {
                // empty message
                // a timeout
                // // debuglog("empty message " . get_called_class());
            }
            $memory = memory_get_usage();

            if( $memory > 20000000  ) {
                // debuglog('Exit '.get_called_class().' due to memory. Memory:'. ($memory/1024/1024).' MB');
                // Supervisor will restart process.
                exit;
            }
            elseif ( time() > $this->endTime ) {
                // debuglog('Exit '.get_called_class().' due to time.');
                // Supervisor will restart process.
                exit;
            }
            usleep(10);

        }
    }


    public function completeTask($queueMessage) {
        // 
        $this->queue->delete($queueMessage);
    }

    public function failTask($queueMessage) {
        //
        $this->queue->delete($queueMessage);
    }
}

class MyProcess extends BaseQueueProcess {
    public function initialize() {
        $this->channelName = 'Temperature';
        parent::initialize();
    }

    public function didReceiveMessage($taskData) {
        // debuglog(print_r($taskData, true));

        // process data here
        // return false if something went wrong

        return true;
    }
}


//Sender

class WorkSender {

    const TubeName = 'Temperature';
    const TubeDelay = 0; // Set delay to 0, i.e. don't use a delay.

    function send($data) {
        $c = BeanstalkClient();
        $c->publish(self::TubeName, $data, self::TubeDelay);
    }

}