Commit 69839807 by guanghui.cui

程序单进程运行

parent 068e2172
...@@ -10,3 +10,4 @@ ...@@ -10,3 +10,4 @@
*.idb *.idb
*.suo *.suo
*.opendb *.opendb
*.user
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
port=24446 port=24446
[ODS] [ODS]
ip=172.16.13.71 ip=127.0.0.1
pushPort=30001 pushPort=30001
recvPort=30002 recvPort=30002
socketTimeout=60 socketTimeout=60
......
...@@ -91,7 +91,7 @@ ...@@ -91,7 +91,7 @@
<PrecompiledHeader>NotUsing</PrecompiledHeader> <PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;_DEBUG;_CONSOLE</PreprocessorDefinitions>
<SDLCheck>false</SDLCheck> <SDLCheck>false</SDLCheck>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile> </ClCompile>
...@@ -121,7 +121,7 @@ ...@@ -121,7 +121,7 @@
<Optimization>MaxSpeed</Optimization> <Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking> <FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions> <IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>WIN32;_CRT_SECURE_NO_WARNINGS;_WINSOCK_DEPRECATED_NO_WARNINGS;NDEBUG;_CONSOLE</PreprocessorDefinitions>
<SDLCheck>false</SDLCheck> <SDLCheck>false</SDLCheck>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary> <RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile> </ClCompile>
......
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
INITIALIZE_EASYLOGGINGPP INITIALIZE_EASYLOGGINGPP
#define VERSION "1.0.3" //版本号 #define VERSION "1.0.3 Beta 2" //版本号
std::string g_init_data; std::string g_init_data;
std::string g_init_data_ods_back; std::string g_init_data_ods_back;
...@@ -31,16 +32,15 @@ int ods_push_port; ...@@ -31,16 +32,15 @@ int ods_push_port;
int ods_recv_port; int ods_recv_port;
int client_listen_port; int client_listen_port;
int pos_listen_port; int pos_listen_port;
//std::vector<orderSendFailedObj> vecFailedOrders;
//bool bRetryThreadRunning=false; //retry线程是否正在运行
bool bInitDone=false; //初始化完成 bool bInitDone=false; //初始化完成
bool bPriorityDone=true; //设置POS优先级 bool bPriorityDone=true; //设置POS优先级
//SQLite sqlite;
//函数,订单信息发送给pos //函数,订单信息发送给pos
bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT std::string &back_json); bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT std::string &back_json);
//失败订单写入vector及sqlite
//void write_failed_order(IN std::string &order_json); //但进程运行,杀掉已有进程
void kill_origin_process();
void logRolloutHandler(const char* filename, std::size_t size) void logRolloutHandler(const char* filename, std::size_t size)
{ {
...@@ -67,7 +67,7 @@ void* listen_pos_func(void* arg) ...@@ -67,7 +67,7 @@ void* listen_pos_func(void* arg)
}else }else
{ {
LOG(INFO) << "local listen port: [" << client_listen_port << "] failed, maybe instance is exist,exit!!!"; LOG(INFO) << "local listen port: [" << client_listen_port << "] failed, maybe instance is exist,exit!!!";
exit(1); exit(0);
} }
while(true) while(true)
...@@ -143,7 +143,7 @@ void* listen_pos_func(void* arg) ...@@ -143,7 +143,7 @@ void* listen_pos_func(void* arg)
LOG(INFO)<<"POS send goods change data:"<<posReq.data(); LOG(INFO)<<"POS send goods change data:"<<posReq.data();
jsonTool.getPosResponseData(100, "success", responseData); jsonTool.getPosResponseData(100, "success", responseData);
} }
else else if(OPERATION_POS_CONFIRM == reqType|| OPERATION_POS_CANCEL == reqType || OPERATION_POS_REFUND_AGREE == reqType || OPERATION_POS_REFUND_DISAGREE == reqType)
{ {
LOG(INFO)<<"REQ POS ===>> PLUGIN:"<<posRequestData.data(); LOG(INFO)<<"REQ POS ===>> PLUGIN:"<<posRequestData.data();
// 将POS请求数据转换为中台可接受数据格式 // 将POS请求数据转换为中台可接受数据格式
...@@ -180,6 +180,16 @@ void* listen_pos_func(void* arg) ...@@ -180,6 +180,16 @@ void* listen_pos_func(void* arg)
jsonTool.getPosResponseData(101, "convert data to [ODS] format failed!", responseData); jsonTool.getPosResponseData(101, "convert data to [ODS] format failed!", responseData);
} }
} }
else if (-1 == reqType) {
//接收到-1,程序需要重启,关闭本进程
LOG(INFO) << "recive exit msessage,exit!!!";
pos.write("100");
pos.close();
server.close();
exit(0);
}
// TODO待加入重试机制 // TODO待加入重试机制
LOG(INFO) << "PLUGIN ===>>POS:"<<responseData.data(); LOG(INFO) << "PLUGIN ===>>POS:"<<responseData.data();
...@@ -245,10 +255,11 @@ int main(int argc,char *argv[]) ...@@ -245,10 +255,11 @@ int main(int argc,char *argv[])
printf("version:%s\n", VERSION); printf("version:%s\n", VERSION);
return 0; return 0;
} }
/* Mark /* Mark
* 与ODS通信全部加数据头(长度) * 与ODS通信全部加数据头(长度)
* 与POS通信全部不加数据头 * 与POS通信全部不加数据头
* end*/ * end*/
#ifdef WIN32 #ifdef WIN32
WSADATA transData; WSADATA transData;
WSAStartup(MAKEWORD(2, 2), &transData); WSAStartup(MAKEWORD(2, 2), &transData);
...@@ -256,7 +267,7 @@ int main(int argc,char *argv[]) ...@@ -256,7 +267,7 @@ int main(int argc,char *argv[])
#else #else
signal(SIGPIPE, SIG_IGN); signal(SIGPIPE, SIG_IGN);
#endif #endif
// 初始化日志 // 初始化日志
el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck); el::Loggers::addFlag(el::LoggingFlag::StrictLogFileSizeCheck);
std::string strBinPath = GetProcDir(); std::string strBinPath = GetProcDir();
...@@ -268,14 +279,8 @@ int main(int argc,char *argv[]) ...@@ -268,14 +279,8 @@ int main(int argc,char *argv[])
/// 注册回调函数 /// 注册回调函数
el::Helpers::installPreRollOutCallback(logRolloutHandler); el::Helpers::installPreRollOutCallback(logRolloutHandler);
LOG(INFO)<<"---------software start---------"; LOG(INFO) << "---------software start---------";
//sqlite初始化
// if(!sqlite.initSQLite()){
// LOG(INFO)<<"initSQLite failed";
// return 0;
// }
// 读取配置文件信息 // 读取配置文件信息
std::string strIniPath(strBinPath.data()); std::string strIniPath(strBinPath.data());
strIniPath.append("config.ini"); strIniPath.append("config.ini");
...@@ -298,6 +303,8 @@ int main(int argc,char *argv[]) ...@@ -298,6 +303,8 @@ int main(int argc,char *argv[])
LOG(INFO) << "[POS]ip: " << pos_ip LOG(INFO) << "[POS]ip: " << pos_ip
<< "-listen port: " << pos_listen_port; << "-listen port: " << pos_listen_port;
LOG(INFO) << "[Local]listen port: " << client_listen_port; LOG(INFO) << "[Local]listen port: " << client_listen_port;
kill_origin_process(); //如果已有进程,杀掉
//end //end
#ifdef WIN32 #ifdef WIN32
...@@ -508,3 +515,45 @@ bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT s ...@@ -508,3 +515,45 @@ bool order_send_to_pos(IN std::string &order_json,IN std::string &ods_json,OUT s
// delete[] lpSql; // delete[] lpSql;
// } // }
//实现逻辑:向本地socket发送一个命令,程序接到命令后自杀,如果没有自杀成功,根据名称杀掉
void kill_origin_process()
{
std::string execName; //可执行文件名
#ifdef WIN32
#ifdef _DEBUG
execName = "takeaway_d.exe";
#else
execName = "takeaway.exe";
#endif // _DEBUG
#else
execName = "takeaway";
#endif
bool rlt = true;
TCPClient pos;
JsonModule jsonTool;
std::string tmp = "{\"fm_cmd\": -1}";
if (pos.doConnect(client_listen_port, pos_ip.c_str())){
if (pos.write(tmp.c_str())){
char tmpBuf[BUF_SIZE] = { 0 };
if (pos.read(tmpBuf, sizeof(tmpBuf))){
LOG(INFO) << "kill back:" << tmpBuf;
if (strcmp(tmpBuf, "100") == 0) {
os_sleep(2); //成功杀掉进程,延时2s等待释放端口资源
}
else
rlt = false;
}
else
rlt = false;
}
else
rlt = false;
pos.close();
}
if (!rlt) {
//如果关闭失败,根据进程名称杀掉进程
kill_process_by_name(execName.c_str());
}
}
\ No newline at end of file
...@@ -5,14 +5,18 @@ ...@@ -5,14 +5,18 @@
#include <iostream> #include <iostream>
#include <string.h> #include <string.h>
#include <chrono> #include <chrono>
#include "../3rdParty/easylogging/easylogging++.h"
#ifdef WIN32 #ifdef WIN32
#include <Windows.h> #include <Windows.h>
#include <ctime> #include <ctime>
#include <tlhelp32.h>
#else #else
#include <iconv.h> #include <iconv.h>
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <dirent.h>
#include <signal.h>
#endif #endif
//函数名: GetProcDir() //函数名: GetProcDir()
...@@ -202,3 +206,99 @@ void os_sleep(int seconds) ...@@ -202,3 +206,99 @@ void os_sleep(int seconds)
sleep(seconds); sleep(seconds);
#endif #endif
} }
#ifdef WIN32
DWORD getPidByName(const char *name)
{
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE == hSnapshot) {
return 0;
}
HANDLE currentProcess = GetCurrentProcess();
DWORD currentPid = GetProcessId(currentProcess);
LOG(INFO) << "this process pid is:" << currentPid;
PROCESSENTRY32 pe = { sizeof(pe) };
int i = 0;
for (BOOL ret = Process32First(hSnapshot, &pe); ret; ret = Process32Next(hSnapshot, &pe)) {
i++;
if (strcmp(pe.szExeFile, name) == 0 && currentPid != pe.th32ProcessID) {
CloseHandle(hSnapshot);
return pe.th32ProcessID;
}
}
CloseHandle(hSnapshot);
return 0;
}
#else
pid_t getPidByName(const char *name)
{
DIR *pdir = NULL;
struct dirent *pde = NULL;
FILE *pf = NULL;
char buff[128];
pid_t pid;
char szName[128];
pid_t nowPid = getpid();
LOG(INFO) << "this process pid is:" << nowPid;
// 遍历/proc目录下所有pid目录
pdir = opendir("/proc");
if (!pdir) {
perror("open /proc fail.\n");
return -1;
}
while ((pde = readdir(pdir))) {
if ((pde->d_name[0] < '0') || (pde->d_name[0] > '9')) {
continue;
}
sprintf(buff, "/proc/%s/status", pde->d_name);
pf = fopen(buff, "r");
if (pf) {
fgets(buff, sizeof(buff), pf);
fclose(pf);
sscanf(buff, "%*s %s", szName);
pid = atoi(pde->d_name);
if (strcmp(szName, name) == 0 && pid!= nowPid) {
closedir(pdir);
return pid;
}
}
}
closedir(pdir);
return 0;
}
#endif
void kill_process_by_name(const char *name)
{
#ifdef WIN32
DWORD pid = getPidByName(name);
LOG(INFO) << "origin pid is:" << pid;
if (pid > 0) {
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
BOOL bOkey = TerminateProcess(hProcess, 0);
if (bOkey) {
WaitForSingleObject(hProcess, 3000);
CloseHandle(hProcess);
hProcess = NULL;
LOG(INFO) << pid << " is killed";
}
else {
LOG(INFO) << "kill failed";
}
os_sleep(3); //延时,防止杀掉进程后,端口还没有释放
}
#else
int pid = getPidByName(name);
LOG(INFO) << "origin pid is:" << pid;
if (pid>0) {
int rlt = kill(pid, SIGKILL);
if (0 == rlt) {
LOG(INFO) <<pid<< " is killed";
}
else {
LOG(INFO) << "kill failed";
}
os_sleep(3); //延时,防止杀掉进程后,端口还没有释放
}
#endif
}
\ No newline at end of file
...@@ -25,4 +25,7 @@ std::string getdatetime(int64_t timestamp); ...@@ -25,4 +25,7 @@ std::string getdatetime(int64_t timestamp);
//延时秒数 //延时秒数
void os_sleep(int seconds); void os_sleep(int seconds);
//根据进程名称杀死进程
void kill_process_by_name(const char *name);
#endif #endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment