用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());

Drupal 7 rewrite settings.php

/**
 * Form callback.
 *
 * @param $form
 * @param $form_state
 */
function myform_submit(&$form, &$form_state) {

  global $databases;

  // Update settings.php.
  $inputs = $form_state['input'];

  require_once DRUPAL_ROOT . '/includes/install.inc';

  $settings['databases'] = array(
    'value'    => array('default' => array(
      'default' => $databases['default']['default'],
      'odbc' => array (
        'database' => $inputs['olm_mart_db_name'],
        'username' => $inputs['olm_mart_db_user'],
        'password' => $inputs['olm_mart_db_pass']['pass1'],
        'host' => $inputs['olm_mart_db_host'],
        'port' => $inputs['olm_mart_db_port'],
        'driver' => 'odbc',
        'prefix' => '',
      ),
    )),
    'required' => TRUE,
  );

  //  $settings['drupal_hash_salt'] = array(
  //    'value'    => drupal_random_key(),
  //    'required' => TRUE,
  //  );
  drupal_rewrite_settings($settings);
}

Docker configuration nginx,php7,mysql

Download php7, mysql, nginx

$ docker pull php:7-fpm-alpine
$ docker pull nginx
$ docker pull mysql

Run php

在这里必须挂载目录到php-fpm, nginx不会直接执行,它检测到php文件,会把php文件的路径发到php-fpm去,php-fpm再依据nginx发过来的路径找到代码并执行啊。

$ docker run -it --rm --name xphp --link xmysql:xmysql -v /Users/zhouitpro/www/docker_web:/usr/share/nginx/html php:7-fpm-alpine
# --link xmysql:xmysql 映射mysql

Install php ext

# 安装pdo_mysql
$ docker exec -it run xphp bash
Docker# docker-php-ext-install pdo_mysql
Docker# docker-php-ext-enable pdo_mysql

Run Nginx

nginx config file

server {
    listen       80;
    server_name  localhost;
    root /usr/share/nginx/html/;
    location ~ \.php$ {
        fastcgi_pass xphp:9000; # 这里对应上面的php-fpm别名, php-fpm端口默认是9000
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
    location ~ /\.ht {
        deny  all;
    }
}
$ docker run -it --rm  --link xphp:xphp -v /Users/zhouitpro/www/docker_web:/usr/share/nginx/html  -v /Users/zhouitpro/www/docker/site.conf:/etc/nginx/conf.d/default.conf -p 9090:80  nginx

# -p 是转发端口 外部的 9090对应nginx提供的80端口
# --link 是映射上面的 php-fpm
# -v 是映射本地的web目录到nginx的www目录,和配置文件

Mysql

$ docker run -it --rm --name xmysql -e MYSQL_ROOT_PASSWORD=root mysql