|
@@ -21,9 +21,7 @@ SERVICE_STATUS_HANDLE hServiceStatus;
|
|
|
CScriptExecutor::CScriptExecutor(void)
|
|
|
{
|
|
|
m_hWorkThread = NULL;
|
|
|
- m_hWorkEvent = NULL;
|
|
|
m_hLogThread = NULL;
|
|
|
- m_hLogEvent = NULL;
|
|
|
m_bStatus = FALSE;
|
|
|
m_nRunType = 0;
|
|
|
m_hStdOutRead = NULL;
|
|
@@ -57,8 +55,7 @@ CScriptExecutor::~CScriptExecutor(void)
|
|
|
DWORD CScriptExecutor::_WorkerThread(LPVOID lpParam)
|
|
|
{
|
|
|
CScriptExecutor* that = (CScriptExecutor*)lpParam;
|
|
|
- if ( !that )
|
|
|
- {
|
|
|
+ if ( !that ) {
|
|
|
printf("Error:参数失效\n");
|
|
|
return -1;
|
|
|
}
|
|
@@ -78,11 +75,11 @@ DWORD CScriptExecutor::_WorkerThread(LPVOID lpParam)
|
|
|
#endif
|
|
|
}
|
|
|
|
|
|
- CloseHandle(that->m_hWorkThread);
|
|
|
- that->m_hWorkThread = NULL;
|
|
|
-
|
|
|
+
|
|
|
+ Sleep(2000);
|
|
|
|
|
|
that->m_bStopLogExport = TRUE;
|
|
|
+ GLOBAL::WriteTextLog(_T("脚本执行线程结束,用例名=%s"), that->m_pCaseObj->strCaseName.c_str());
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -90,8 +87,7 @@ DWORD CScriptExecutor::_WorkerThread(LPVOID lpParam)
|
|
|
DWORD CScriptExecutor::_LogExportThread(LPVOID lpParam)
|
|
|
{
|
|
|
CScriptExecutor* that = (CScriptExecutor*)lpParam;
|
|
|
- if (!that)
|
|
|
- {
|
|
|
+ if (!that) {
|
|
|
printf("Error:参数失效\n");
|
|
|
return -1;
|
|
|
}
|
|
@@ -143,6 +139,7 @@ DWORD CScriptExecutor::_LogExportThread(LPVOID lpParam)
|
|
|
|
|
|
|
|
|
that->EndSubprocessStdOut();
|
|
|
+ GLOBAL::WriteTextLog(_T("脚本日志线程结束,用例名=%s"), that->m_pCaseObj->strCaseName.c_str());
|
|
|
|
|
|
return 0;
|
|
|
}
|
|
@@ -395,7 +392,6 @@ int CScriptExecutor::RunEmbeddedScript()
|
|
|
return -4;
|
|
|
}
|
|
|
|
|
|
- m_bRuned = TRUE;
|
|
|
|
|
|
PyObject* pResult = PyObject_CallObject(pFunction, NULL);
|
|
|
if (!pResult)
|
|
@@ -407,6 +403,11 @@ int CScriptExecutor::RunEmbeddedScript()
|
|
|
return -5;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ Sleep(2000);
|
|
|
+
|
|
|
+ m_bStopLogExport = TRUE;
|
|
|
+
|
|
|
Py_DECREF(pResult);
|
|
|
|
|
|
Py_Finalize();
|
|
@@ -454,7 +455,7 @@ int CScriptExecutor::RunScriptProcess()
|
|
|
&m_pi)
|
|
|
)
|
|
|
{
|
|
|
- printf("Error:创建子进程失败 (%d).\n", GetLastError());
|
|
|
+ GLOBAL::WriteTextLog("Error:创建子进程失败 (%d)", GetLastError());
|
|
|
return -3;
|
|
|
}
|
|
|
|
|
@@ -464,6 +465,7 @@ int CScriptExecutor::RunScriptProcess()
|
|
|
WaitForSingleObject(m_pi.hProcess, INFINITE);
|
|
|
|
|
|
Sleep(5000);
|
|
|
+ GLOBAL::WriteTextLog("脚本进程结束(%ld)", m_pi.dwProcessId);
|
|
|
|
|
|
m_bStopLogExport = TRUE;
|
|
|
|
|
@@ -641,27 +643,13 @@ int CScriptExecutor::ServiceRunScriptProcess()
|
|
|
return TRUE;
|
|
|
}
|
|
|
|
|
|
-bool CScriptExecutor::StartWorkThread()
|
|
|
+bool CScriptExecutor::StartThread()
|
|
|
{
|
|
|
|
|
|
m_hWorkThread = CreateThread(NULL, 0, _WorkerThread, this, 0, &m_dwThreadId);
|
|
|
if (!m_hWorkThread)
|
|
|
{
|
|
|
- printf("Error:创建线程失败\n");
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- return true;
|
|
|
-}
|
|
|
-
|
|
|
-bool CScriptExecutor::StartLogThread()
|
|
|
-{
|
|
|
- printf("StartLogThread\n");
|
|
|
-
|
|
|
- m_hLogEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
|
|
- if (!m_hLogEvent)
|
|
|
- {
|
|
|
- printf("Error:无法为线程创建控制事件\n");
|
|
|
+ printf("Error:创建执行线程失败\n");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -669,10 +657,7 @@ bool CScriptExecutor::StartLogThread()
|
|
|
m_hLogThread = CreateThread(NULL, 0, _LogExportThread, this, CREATE_SUSPENDED, NULL);
|
|
|
if (!m_hLogThread)
|
|
|
{
|
|
|
- printf("Error:创建线程失败\n");
|
|
|
- CloseHandle(m_hLogEvent);
|
|
|
- m_hLogEvent = NULL;
|
|
|
-
|
|
|
+ printf("Error:创建日志线程失败\n");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
@@ -681,12 +666,21 @@ bool CScriptExecutor::StartLogThread()
|
|
|
|
|
|
void CScriptExecutor::EndWorkThread()
|
|
|
{
|
|
|
-
|
|
|
- if (m_hWorkThread)
|
|
|
- {
|
|
|
+
|
|
|
+ EndSubprocess();
|
|
|
+
|
|
|
+ if (m_hWorkThread) {
|
|
|
+ if (WaitForSingleObject(m_hWorkThread, 3000) == WAIT_OBJECT_0) {
|
|
|
+ CloseHandle(m_hWorkThread);
|
|
|
+ m_hWorkThread = NULL;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if ( m_hWorkThread ) {
|
|
|
|
|
|
- for (int i = 0; i < 5; i++)
|
|
|
- {
|
|
|
+ for (int i = 0; i < 5; i++) {
|
|
|
if (TerminateThread(m_hWorkThread, 0))
|
|
|
break;
|
|
|
}
|
|
@@ -698,42 +692,33 @@ void CScriptExecutor::EndWorkThread()
|
|
|
|
|
|
void CScriptExecutor::EndLogThread()
|
|
|
{
|
|
|
-
|
|
|
- if (m_hLogEvent)
|
|
|
- SetEvent(m_hLogEvent);
|
|
|
+ m_bStopLogExport = TRUE;
|
|
|
+
|
|
|
+ if ( m_hLogThread ) {
|
|
|
+ if ( WaitForSingleObject(m_hLogThread, 3000) == WAIT_OBJECT_0 ) {
|
|
|
+ CloseHandle(m_hLogThread);
|
|
|
+ m_hLogThread = NULL;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (m_hLogThread) {
|
|
|
+
|
|
|
+ for (int i = 0; i < 5; i++) {
|
|
|
+ if (TerminateThread(m_hLogThread, 0))
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
-
|
|
|
- if (m_hLogThread)
|
|
|
- {
|
|
|
- WaitForSingleObject(m_hLogThread, INFINITE);
|
|
|
CloseHandle(m_hLogThread);
|
|
|
m_hLogThread = NULL;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- if (m_hLogEvent)
|
|
|
- CloseHandle(m_hLogEvent);
|
|
|
- m_hLogEvent = NULL;
|
|
|
}
|
|
|
|
|
|
-void CScriptExecutor::EndThread(HANDLE hThread, HANDLE hEvent)
|
|
|
+void CScriptExecutor::EndThread()
|
|
|
{
|
|
|
-
|
|
|
- if (hEvent)
|
|
|
- SetEvent(hEvent);
|
|
|
-
|
|
|
-
|
|
|
- if (hThread)
|
|
|
- {
|
|
|
- WaitForSingleObject(hThread, INFINITE);
|
|
|
- CloseHandle(hThread);
|
|
|
- hThread = NULL;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (hEvent)
|
|
|
- CloseHandle(hEvent);
|
|
|
- hEvent = NULL;
|
|
|
+ EndWorkThread();
|
|
|
+ EndLogThread();
|
|
|
}
|
|
|
|
|
|
BOOL CScriptExecutor::EndSubprocess()
|
|
@@ -1035,16 +1020,8 @@ bool CScriptExecutor::InitScript(std::string strScript, std::string strLogPath,
|
|
|
|
|
|
bool CScriptExecutor::StartScript()
|
|
|
{
|
|
|
- if (StartWorkThread())
|
|
|
- {
|
|
|
-
|
|
|
- if (!StartLogThread())
|
|
|
- {
|
|
|
- printf("Error:日志导出线程创建出错\n");
|
|
|
- }
|
|
|
-
|
|
|
+ if (StartThread()) {
|
|
|
Sleep(100);
|
|
|
-
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -1066,24 +1043,12 @@ void CScriptExecutor::StopScript()
|
|
|
|
|
|
bool CScriptExecutor::IsScriptOver()
|
|
|
{
|
|
|
- int i = 10;
|
|
|
- while (!m_bRuned)
|
|
|
- {
|
|
|
- Sleep(300);
|
|
|
- if ( --i == 0 )
|
|
|
- break;
|
|
|
- }
|
|
|
-
|
|
|
- if (m_nRunType == EMBEDDED)
|
|
|
+ if ( WaitForSingleObject(m_hLogThread, 0) == WAIT_OBJECT_0 )
|
|
|
{
|
|
|
- if ( m_hWorkThread == NULL )
|
|
|
- return true;
|
|
|
- }
|
|
|
- else if (m_nRunType == SUBPROCESS)
|
|
|
- {
|
|
|
- if ( m_pi.hProcess == NULL && m_hWorkThread == NULL )
|
|
|
- return true;
|
|
|
+ GLOBAL::WriteTextLog(_T("脚本(%s)已完成"), m_pCaseObj->strCaseName.c_str());
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
+ GLOBAL::WriteTextLog(_T("脚本(%s)未完成"), m_pCaseObj->strCaseName.c_str());
|
|
|
return false;
|
|
|
}
|