1. 程式人生 > >supervisor 常駐記憶體程序

supervisor 常駐記憶體程序

 

因專案中需要使用跑批執行佇列任務。

第一種方法:定時跑批任務

第二種方法:使用supervisor常駐記憶體

2.1安裝supervisor

$ sudo apt-get install supervisor

2.2.生成配置檔案放在/etc/supervisor/conf.d/資料夾下面取名yesdk_queue.conf

directory=/Project/YeSDK/YeSDK_Server_V2_1

command=/usr/local/php/bin/php /Project/YeSDK/YeSDK_Server_V2_1/queue.php

autostart=true

autorestart=true

startretries=3

redirect_stderr=true

stdout_logfile=/var/log/yesdk_stdout.log

2.3執行supervisorctl update

2.4執行supervisor stop all 

2.5執行supervisor start all

核心程式碼如下;

入佇列:

public function addQueue($orderInfo){

      $orderEventData = $this->getOrderEventData($orderInfo);

      $queueManager = new QueueManager();

       return $queueManager::instance('','yesdkOrderEventLog')->push(new OrderStateChangeJob($orderEventData));

}

出佇列:

queue

 

 

 

 

佇列類--------------------------------------------------------------------------------------------------------------

<?php

/**

* 佇列類

*

* @author Flc <2017-02-24 16:48:26> *

*/

class Queue

{

/**

* redis服務

* @var [type]

*/

protected $redis;

 

/**

* 佇列名稱

* @var [type]

*/

protected $queue_name;

 

/**

* 初始化

* @param Redis $redis [description]

*/

function __construct(Redis $redis, $queue_name = '')

{

$this->redis = $redis;

$this->queue_name = $queue_name;

}

 

/**

* 推送佇列任務

* @param \App\Queue\JobInterface $jobs [description]

* @return [type] [description]

*/

public function push($job)

{

return $this->redis->lpush($this->getRedisKey(), serialize($job));

}

 

/**

* 出佇列

*

* @return OrderStateChangeJob

*/

public function pull()

{

$job_seria = $this->redis->rpop($this->getRedisKey());

$job = unserialize($job_seria);

 

if (false === $job ||

//! $job instanceof JobInterface

false

) {

return false;

}

 

return $job;

}

 

/**

* 獲取佇列任務總數

* @return [type] [description]

*/

public function count()

{

return $this->redis->llen($this->getRedisKey());

}

 

/**

* 返回redis儲存的key名

* @return [type] [description]

*/

protected function getRedisKey()

{

$name = ! empty($this->queue_name) ? $this->queue_name : $this->getRedisDefaultKeyName();

 

return 'queues:' . $name;

}

 

/**

* 獲取預設的key名

* @return [type] [description]

*/

protected function getRedisDefaultKeyName()

{

return 'default';

}

}

 

 

佇列管理類:---------------------------------------------------------------------------

<?php


 

/**

* 佇列管理類

*

* @author Flc <2017-02-24 16:48:26> *

*/

class QueueManager

{

/**

* 單例模式

* @var null

*/

protected static $_static = null;

 

/**

* 工廠模式

* @param Redis $redis redis

* @param string $queue_name 佇列名稱

* @return [type] [description]

*/

public static function factory($redis = null, $queue_name = null)

{

if ($redis == null) {

$redis = new Redis;

//$redis->connect('192.168.5.105', 6379);

//測試環境

$redis->connect('120.26.162.86', 6379);

$redis->auth('JsJOE5b9U6xwyWXa5oVl');

}

 

$queue = new Queue($redis, $queue_name);

return clone $queue;

}

 

/**

* 單例模式

* @param mixed $redis

* @param string $queue_name

*

* @return Queue

*/

public static function instance($redis = null, $queue_name = null)

{

if (self::$_static == null) {

 

self::$_static = self::factory($redis, $queue_name);

 

}

 

return self::$_static;

}

}

 

佇列消費類---------------------------------------------------------------

<?php

 

/**

* 佇列任務介面類

*

* @author Flc <2017-02-24 16:46:51>

*/

class OrderStateChangeJob extends JobInterface

{

/**

* 訂單資料

*

* @var array

*/

protected $order = null;

 

/**

* 執行次數

*

* @var int

*/

public $num = 0;

 

/**

* 構造方法

*

* @param array $order 訂單資料

*/

public function __construct($order) {

$this->order = $order;

}

 

/**

* 任務處理方法

*

* @return void

*/

public function handle() {

if(!empty($this->order)){

$order = $this->order;

$data =array(

'order_id' => $order['order_id'],

'status' => $order['status'],

'status_info' => $order['status_info'],

'event_time' => $order['event_time'],

'ip' => $order['ip'],

'log_time' => $order['log_time'],

 

);

$bool = DI()->notorm->order_status_event_log->insert($data);

}

}

}

 

class JobInterface

{

/**

* 任務處理方法

* @return [type] [description]

*/

public function handle()

{

 

}

}