supervisor 常驻内存进程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_41198398/article/details/81085863

因项目中需要使用跑批执行队列任务。

第一种方法:定时跑批任务

第二种方法:使用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()

{

}

}

猜你喜欢

转载自blog.csdn.net/qq_41198398/article/details/81085863