Authored by xiaofeng.yao@yoho.cn

房间结束超时检查

... ... @@ -5,7 +5,7 @@ namespace app\models;
use Yii;
use common\lib\QcloudApi\Client as QcloudApiClient;
use common\models\RoomNums;
use common\models\Share;
use app\models\Share;
use common\config\Params;
/**
... ... @@ -300,22 +300,23 @@ class Room extends \yii\db\ActiveRecord
if (!$model->delete()){
throw new \Exception(current($model->getFirstErrors()));
}
//删除分享
$delShare = (new Share())->delLivingShare($model->room_id);
if (!$delShare) {
throw new \Exception("删除分享失败");
}
if ($model->roomQchannel){
if (!$model->roomQchannel->delete()){
throw new \Exception(current($model->roomQchannel->getFirstErrors()));
}
//fixme 很有可能因为腾讯云后台被删,导致找不到频道,本地服务器记录一直删不掉
if (!QcloudApiClient::self()->DeleteLVBChannel([$model->roomQchannel->channel_id])){
throw new \Exception(QcloudApiClient::self()->error());
}
}
//删除分享
$delShare = (new Share())->delLivingShare($model->room_id);
if (!$delShare) {
throw new \Exception("删除分享失败");
}
$transaction->commit();
return true;
}catch (\Exception $e) {
... ...
... ... @@ -68,9 +68,21 @@ class Share extends \yii\db\ActiveRecord
];
}
/**
* 删除分享
* @param $obj_id
* @param $type
* @return bool
* @throws \Exception
*/
private function del($obj_id,$type)
{
return self::findOne(['obj_id'=>$obj_id,'type'=>$type])->delete();
try{
$rowCount = Yii::$app->db->createCommand()->delete(self::tableName(), ['obj_id'=>$obj_id,'type'=>$type])->execute();
return ($rowCount>=0) ? true:false;
}catch (\Exception $e){
throw new \Exception($e->getMessage());
}
}
public function delLivingShare($room_id)
... ...
... ... @@ -4,12 +4,16 @@ namespace console\controllers;
use Yii;
use yii\console\Controller;
use common\models\Room;
use common\config\Params;
use common\lib\QcloudApi\Client as QcloudApiClient;
use common\models\Video;
/**
* Site controller
*/
class RoomController extends Controller
{
const QCLOUD_LVING_STAT = 1;
/**
* 重置房间状态
... ... @@ -57,4 +61,62 @@ class RoomController extends Controller
}
}
/**
* 检查直播中的房间,如果腾讯云返回不是直播中的置为直播结束,30min检查一次
* https://www.qcloud.com/doc/api/258/4717
* 0 无输入流 ,1 直播中, 2 异常, 3 关闭
* @author yaoxiaofeng
* @use php yii room/check
*/
public function actionCheck()
{
//获取本地服务器直播中的房间
$result = Room::find()
->where(['living' => Params::LIVING])
->orderBy(['starting_time' => SORT_ASC])
->all();
if ($result) {
foreach ($result as $room) {
if ($room->roomQchannel) {
$channel_id = (string)$room->roomQchannel->channel_id;
$ret = QcloudApiClient::self()->DescribeLVBChannel($channel_id);
if (!$ret) {
$this->_log(QcloudApiClient::self()->error());
return;
}
//腾讯云返回不是直播中的置为直播结束
if ($ret['channelInfo'][0]['channel_status'] != self::QCLOUD_LVING_STAT) {
/*$room->setAttributes([
'living' => Params::LIVE_END,
'unstart_sort' => 0,
'living_sort' => 0
]);
if ($room->save(false)) {
$this->_log("room[{$room->room_id}] closed success");
} else {
$this->_log("room[{$room->room_id}] closed faild");
}*/
//是否要调用统一的结束接口,如果掉用的话存在资讯抛异常问题,那么房间就一直结束不了
try {
$video_res = (new Video())->setVideoStop($room->room_id);
if($video_res){
$this->_log("room[{$room->room_id}] closed success");
}else{
$this->_log("room[{$room->room_id}] closed faild");
}
} catch (\Exception $e) {
$this->_log("room[{$room->room_id}] exception:".$e->getMessage());
}
}
}
}
}
}
private function _log($string){
$string = sprintf("[%s]%s \n",date('Y-m-d H:i:s'),$string);
$this->stdout($string);
}
}
... ...