Skip to content

Commit

Permalink
add tars basic service framework files
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Sep 5, 2018
0 parents commit c255e35
Show file tree
Hide file tree
Showing 225 changed files with 35,552 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "tarscpp"]
path = tarscpp
url = https://github.com/TarsCloud/TarsCpp.git
[submodule "thirdparty/rapidjson"]
path = thirdparty/rapidjson
url = https://github.com/Tencent/rapidjson.git
112 changes: 112 additions & 0 deletions AdminRegistryServer/AdminReapThread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

#include "AdminReapThread.h"

extern TC_Config * g_pconf;

AdminReapThread::AdminReapThread()
: _terminate(false)
, _timeout(150)
{
}

AdminReapThread::~AdminReapThread()
{
if (isAlive())
{
terminate();
notifyAll();
getThreadControl().join();
}
}


int AdminReapThread::init()
{
TLOGDEBUG("begin AdminReapThread init"<<endl);

//初始化配置db连接
_db.init(g_pconf);

//服务心跳更新时间间隔
_updateInterval = TC_Common::strto<int>((*g_pconf).get("/tars/reap<updateHeartInterval>", "10"));
//最小值保护
_updateInterval = _updateInterval < 5 ? 5 : _updateInterval;

//管理主控心跳超时时间
_timeout = TC_Common::strto<int>((*g_pconf)["/tars/reap<reigistryTimeout>"]);
_timeout = _timeout < 5 ? 5 : _timeout;

//是否关闭更新管理主控心跳时间,一般需要迁移时,设置此项为Y
_heartBeatOff = (*g_pconf).get("/tars/reap<heartbeatoff>", "N") == "Y"?true:false;

_db.updateRegistryInfo2Db(_heartBeatOff);
_db.loadIPPhysicalGroupInfo();

TLOGDEBUG("AdminReapThread init ok."<<endl);

return 0;
}

void AdminReapThread::terminate()
{
TLOGDEBUG("[ReapThread terminate.]" << endl);
_terminate = true;
}

void AdminReapThread::run()
{
//更新服务心跳时间
time_t tLastUpdateTime = TC_TimeProvider::getInstance()->getNow();
time_t tLastQueryServer = 0;
time_t tNow;
while(!_terminate)
{
try
{
tNow = TNOW;
//更新心跳
if(tNow - tLastUpdateTime >= _updateInterval)
{
tLastUpdateTime = tNow;
_db.updateRegistryInfo2Db(_heartBeatOff);
_db.loadIPPhysicalGroupInfo();
}

//轮询心跳超时的主控
if(tNow - tLastQueryServer >= _timeout)
{
tLastQueryServer = tNow;
_db.checkRegistryTimeout(_timeout);
}

TC_ThreadLock::Lock lock(*this);
timedWait(100); //ms
}
catch(exception & ex)
{
TLOGERROR("AdminReapThread exception:" << ex.what() << endl);
}
catch(...)
{
TLOGERROR("AdminReapThread unknown exception:" << endl);
}
}
}



74 changes: 74 additions & 0 deletions AdminRegistryServer/AdminReapThread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Tencent is pleased to support the open source community by making Tars available.
*
* Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

#ifndef __AMINREAP_THREAD_H__
#define __AMINREAP_THREAD_H__

#include <iostream>
#include "util/tc_thread.h"
#include "DbProxy.h"

using namespace tars;

/**
* 用于执行定时操作的线程类
*/
class AdminReapThread : public TC_Thread, public TC_ThreadLock
{
public:
/*
* 初始化函数
*/
AdminReapThread();

/*
* 析构函数
*/
~AdminReapThread();

/*
* 结束线程
*/
void terminate();

/**
* 初始化
*/
int init();

/**
* 轮询函数
*/
virtual void run();

protected:
//线程结束标志
bool _terminate;

//数据库操作
DbProxy _db;

//心跳更新时间间隔
int _updateInterval;

//服务心跳超时时间
int _timeout;

//服务心跳包开关
bool _heartBeatOff;
};

#endif
Loading

0 comments on commit c255e35

Please sign in to comment.