用PHP实现一个简单的环形队列

缺少一个好的计时器. 但是功能能正常运行. 替换成一个好的计时器。状态可以保存在DB中.

/**
 * 环型队列.
 * 创建一个环形为3000个元素.
 * 作cron,其中每秒走一个位置.
 */

/**
 * Class Task
 */
class Task
{

    private $TaskId;
    private $Time;
    private $CallBack;
    private $Run;

    public function __construct($TaskId = null, $Time = null, $CallBack = null)
    {
        $this->Run = FALSE;
        $this->SetId($TaskId);
        $this->SetTime($Time);
        $this->SetCallback($CallBack);
    }

    public function SetId($TaskId)
    {
        $this->TaskId = $TaskId;
    }

    public function GetId()
    {
        return $this->TaskId;
    }

    public function SetTime($Time)
    {
        $this->Time = $Time;
    }

    public function GetTime()
    {
        return $this->Time;
    }

    public function SetCallBack($CallBack)
    {
        $this->CallBack = $CallBack;
    }

    public function GetCallBack()
    {
        return $this->CallBack;
    }

    public function Run()
    {
        if (!$this->Run && is_callable($this->GetCallBack())) {
            $this->Run = TRUE;
            $this->GetCallBack();
        }
    }
}

/**
 * Class Mqueue
 *
 * 3000的环形队列.
 *
 */
class Mqueue
{
    private $Queue;
    private $Tasks;
    private $CurrentTask;
    private $QueueLenth = 3000;
    private $CurrentIndex; // 当前为在哪个格子里面.
    private $CurrentNum;  // 当前在第几圈.

    /**
     * Add task.
     *
     */
    public function AddTask(Task $task)
    {
        $this->Tasks[$task->GetId()] = $task;
        $this->CurrentTask = $task->GetId();
        $this->SetQueue($task);
    }

    /**
     * 将任务添加到环中.
     *
     * @param $task
     */
    private function SetQueue($task)
    {
        $postion = $this->GetPosition($task);
        $this->Queue[$postion[1]][$postion[0]][$task->GetId()] = $task;
    }

    /**
     * 计算任务在当前哪个环中.
     *
     * @param $task
     *
     * @return array
     */
    private function GetPosition($task)
    {
        $time = strtotime($task->GetTime()) - time();

        return [
          $time % $this->QueueLenth,
          $this->CurrentNum + floor($time / $this->QueueLenth),
        ];
    }

    private function Timer()
    {
        $this->CurrentIndex = 0;
        $this->CurrentNum = 0;
        $this->Ring();
    }

    public function GetQueue() {
        return $this->Queue;
    }

    /**
     * @TODO Good timer.
     */
    private function Ring() {
        // 每秒跑一格.
        // 到尾了跑第二环.
        for($i = 0; $i <= $this->QueueLenth; $i++) {
            $this->CurrentIndex++;
            if(isset($this->Queue[$this->CurrentNum][$this->CurrentIndex])) {
                $this->Queue[$this->CurrentNum][$this->CurrentIndex]->Run();
            }
            sleep(1000);

            if($this->CurrentIndex <= $this->QueueLenth) {
                $this->CurrentNum++;
                $this->Ring();
            }

        }
    }
}

$task = new Task('thisismyfirsttask', '2017-3-21', function () {
        echo 'hello world';
    }
);

$mq = New Mqueue();
$mq->AddTask($task);
print_r($mq->GetQueue());

大巧若拙,大辩若讷

最有智慧的人,真正有本事的人,虽然有才华学识,但平时像个呆子,不自作聪明;虽然能言善辩,但好像不会讲话一样。无论是初涉世事还是位居高官,无论是做大事还是一般人际关系,锋芒不可毕露。有了才华固然很好,但在合适的时机运用才华而不被或少被人忌,避免功高盖主,才算是更大的才华,这种才华对社会、对人对己才有真正的用处。

Ubuntu 配置SSL+Apache

启用ssl 模块, 并且重启Apache

sudo a2enmod ssl
sudo service apache2 restart

创建SSL密钥和证书保存的目录

sudo mkdir /etc/apache2/ssl

生成一个证书

sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt

获取一个免费的有效证书https://www.startssl.com/
参考文章:http://blog.nicky1605.com/the-free-ssl-configuration-startssl-on-nginx.html

配置Apache:

sudo vim /etc/apache2/sites-available/default-ssl.conf

以下分别指向你的证书文件

SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key

Fedora 安装Drupal 7出现files目录无权限

在安装Drupal 7时,竟然出现了Files目录无权限。但是我已经将我的files改成了 777(环境LAMP)
我的www目录是 /home/zhouitpro/d7
接着我在index.php 直接运行 mkdir(“./hello”); 也报了一个错误. 说没有权限

Warning: mkdir(): Permission denied

找到这里有一个解决办法: http://stackoverflow.com/questions/13908722/php-unable-to-create-a-directory-with-mkdir
应该是SELinux内容要重新设置一下类型

sudo chcon -R -t httpd_sys_content_rw_t /home/zhouitpro/www/

设置以后正学工作了

CentOS 安装LAMP

1. 基本安装

#yum install mysql-server httpd php php-mysql php-pdo php-gd php-pear php-dom php-mbstring

2. 开机启动mysql和Apache

#chkconfig httpd on
#chkconfig mysqld on

3. 配置mysql

#service mysqld start
#mysql_secure_installation
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y                                            
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

4. 测试PHP, 创建/var/www/html/info.php

phpinfo()

5. 配置虚拟主机
\
ServerAdmin zhouitpro@gmail.com
DocumentRoot /var/www/html/baifi.com
ServerName www.baifi.com
ServerAlias baifi.com
\