Patch level : 10.0
Files correlati : lurch.exe Ricompilazione Demo : [ ] Commento : Ora il maggiordomo e' in grado di uccidere anche i servi precedenti la sua nomina, potrebbero fargli le scarpe... git-svn-id: svn://10.65.10.50/trunk@20011 c028cbd2-c16b-5b4b-a496-9718f37d4682
This commit is contained in:
parent
2b4f86ae5c
commit
62742e9cdd
125
server/lurch.cpp
125
server/lurch.cpp
@ -17,7 +17,7 @@ class TLurchServer : public TBaseServerApp
|
|||||||
TProcessHashMap m_hmProcMap;
|
TProcessHashMap m_hmProcMap;
|
||||||
wxSemaphore m_Semaphore;
|
wxSemaphore m_Semaphore;
|
||||||
wxTimer m_PingTimer;
|
wxTimer m_PingTimer;
|
||||||
wxArrayString m_aServers;
|
wxArrayString m_aServers, m_aAutoRun;
|
||||||
|
|
||||||
DECLARE_EVENT_TABLE();
|
DECLARE_EVENT_TABLE();
|
||||||
|
|
||||||
@ -31,6 +31,7 @@ protected:
|
|||||||
void EnumerateVariables(const wxString& strApp, wxArrayString& arr) const;
|
void EnumerateVariables(const wxString& strApp, wxArrayString& arr) const;
|
||||||
|
|
||||||
const wxArrayString& GetServersList();
|
const wxArrayString& GetServersList();
|
||||||
|
const wxArrayString& GetAutoRunList();
|
||||||
const TProcessHashMap& GetRunningServers() { return m_hmProcMap; }
|
const TProcessHashMap& GetRunningServers() { return m_hmProcMap; }
|
||||||
|
|
||||||
bool PingProcess(const wxString& strApp);
|
bool PingProcess(const wxString& strApp);
|
||||||
@ -67,10 +68,6 @@ BEGIN_EVENT_TABLE(TLurchServer, TBaseServerApp)
|
|||||||
EVT_END_PROCESS(wxID_ANY, TLurchServer::OnEndProcess)
|
EVT_END_PROCESS(wxID_ANY, TLurchServer::OnEndProcess)
|
||||||
END_EVENT_TABLE()
|
END_EVENT_TABLE()
|
||||||
|
|
||||||
#ifndef max
|
|
||||||
#define max(a,b) ((a)>(b) ? (a) : (b))
|
|
||||||
#endif
|
|
||||||
|
|
||||||
bool TLurchServer::PingProcess(const wxString& strApp)
|
bool TLurchServer::PingProcess(const wxString& strApp)
|
||||||
{
|
{
|
||||||
bool bPinged = false;
|
bool bPinged = false;
|
||||||
@ -78,8 +75,10 @@ bool TLurchServer::PingProcess(const wxString& strApp)
|
|||||||
const wxString strPort = GetConfigString("Port", "", strApp);
|
const wxString strPort = GetConfigString("Port", "", strApp);
|
||||||
if (!strPort.IsEmpty())
|
if (!strPort.IsEmpty())
|
||||||
{
|
{
|
||||||
const int nInterval = m_PingTimer.GetInterval(); // msec
|
int nTimeOut = m_PingTimer.GetInterval()/4; // msec
|
||||||
const int nTimeOut = max(nInterval/4, 250); // msec
|
if (nTimeOut < 250) nTimeOut = 250; else
|
||||||
|
if (nTimeOut > 2000) nTimeOut = 2000;
|
||||||
|
|
||||||
wxIPV4address ipAddress;
|
wxIPV4address ipAddress;
|
||||||
ipAddress.LocalHost();
|
ipAddress.LocalHost();
|
||||||
ipAddress.Service(strPort);
|
ipAddress.Service(strPort);
|
||||||
@ -107,40 +106,43 @@ bool TLurchServer::PingProcess(const wxString& strApp)
|
|||||||
|
|
||||||
void TLurchServer::OnTimer(wxTimerEvent& evt)
|
void TLurchServer::OnTimer(wxTimerEvent& evt)
|
||||||
{
|
{
|
||||||
if (m_Semaphore.TryWait() != wxSEMA_NO_ERROR)
|
const wxSemaError se = m_Semaphore.TryWait();
|
||||||
|
if (se == wxSEMA_NO_ERROR)
|
||||||
{
|
{
|
||||||
evt.Skip();
|
const wxArrayString& aServers = GetAutoRunList();
|
||||||
return;
|
const size_t nServers = aServers.GetCount();
|
||||||
}
|
if (nServers > 0) // Is anybody out there?
|
||||||
|
|
||||||
const TProcessHashMap& hmServers = GetRunningServers();
|
|
||||||
const int nServers = hmServers.size();
|
|
||||||
if (nServers > 0) // Is anybody out there?
|
|
||||||
{
|
|
||||||
const int nRandomIndex = rand() % nServers;
|
|
||||||
wxString strApp; // Usually "Authorization"
|
|
||||||
TProcessHashMap::const_iterator it;
|
|
||||||
for (it = hmServers.begin(); it != hmServers.end(); ++it)
|
|
||||||
strApp = it->first;
|
|
||||||
|
|
||||||
if (!PingProcess(strApp))
|
|
||||||
{
|
{
|
||||||
wxProcess* pProcess = m_hmProcMap[strApp];
|
const int nRandomIndex = ::rand() % nServers;
|
||||||
wxKillError ke = pProcess->Kill(pProcess->GetPid());
|
const wxString& strApp = aServers[nRandomIndex]; // Usually "Authorization"
|
||||||
if (ke == wxKILL_OK)
|
if (!PingProcess(strApp))
|
||||||
WriteLog(strApp + _(" killed!"));
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
wxString strMsg;
|
if (!strApp.IsEmpty()) // Dummy test
|
||||||
strMsg << _("Error ") << ke << _(" killing ") << strApp;
|
{
|
||||||
WriteLog(strMsg);
|
const wxSingleInstanceChecker sic(strApp);
|
||||||
|
if (sic.IsAnotherRunning())
|
||||||
|
{
|
||||||
|
wxProcess* pProcess = m_hmProcMap[strApp];
|
||||||
|
wxKillError ke = pProcess->Kill(pProcess->GetPid());
|
||||||
|
if (ke == wxKILL_OK)
|
||||||
|
WriteLog(strApp + _(" killed!"));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
wxString strMsg;
|
||||||
|
strMsg << _("Error ") << ke << _(" killing ") << strApp;
|
||||||
|
WriteLog(strMsg);
|
||||||
|
}
|
||||||
|
wxSleep(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
StartProcess(strApp);
|
||||||
}
|
}
|
||||||
wxSleep(2);
|
|
||||||
StartProcess(strApp);
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
m_Semaphore.Post();
|
m_Semaphore.Post();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
evt.Skip();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TLurchServer::OnEndProcess(wxProcessEvent& evt)
|
void TLurchServer::OnEndProcess(wxProcessEvent& evt)
|
||||||
@ -187,6 +189,22 @@ const wxArrayString& TLurchServer::GetServersList()
|
|||||||
return m_aServers;
|
return m_aServers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const wxArrayString& TLurchServer::GetAutoRunList()
|
||||||
|
{
|
||||||
|
if (m_aAutoRun.IsEmpty())
|
||||||
|
{
|
||||||
|
const wxArrayString& as = GetServersList();
|
||||||
|
for (size_t i = 0; i < as.GetCount(); i++)
|
||||||
|
{
|
||||||
|
const bool bAutorun = GetConfigBool("Autorun", false, as[i]);
|
||||||
|
if (bAutorun)
|
||||||
|
m_aAutoRun.Add(as[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m_aAutoRun;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
TXmlItem& TLurchServer::AddMiniForm(TXmlItem& tr, const wxChar* action, const wxChar* app, const wxChar* prompt) const
|
TXmlItem& TLurchServer::AddMiniForm(TXmlItem& tr, const wxChar* action, const wxChar* app, const wxChar* prompt) const
|
||||||
{
|
{
|
||||||
TXmlItem& td = tr.AddChild("td").SetAttr("width", "10%");
|
TXmlItem& td = tr.AddChild("td").SetAttr("width", "10%");
|
||||||
@ -242,12 +260,13 @@ void TLurchServer::GenerateFile(wxString& strFilename)
|
|||||||
TXmlItem& img = a.AddChild("img");
|
TXmlItem& img = a.AddChild("img");
|
||||||
img.SetAttr("src", strIcon); img.SetAttr("border", 0L); img.SetAttr("alt", arr[i]);
|
img.SetAttr("src", strIcon); img.SetAttr("border", 0L); img.SetAttr("alt", arr[i]);
|
||||||
|
|
||||||
|
const bool bAutoRun = bLurch || GetAutoRunList().Index(arr[i]) != wxNOT_FOUND;
|
||||||
TXmlItem& buttStart = AddMiniForm(tr, "start.cgi", arr[i], _("Start"));
|
TXmlItem& buttStart = AddMiniForm(tr, "start.cgi", arr[i], _("Start"));
|
||||||
if (bRunning || bLurch)
|
if (bRunning || bAutoRun)
|
||||||
buttStart.SetAttr(wxT("type"), wxT("hidden"));
|
buttStart.SetAttr(wxT("type"), wxT("hidden"));
|
||||||
|
|
||||||
TXmlItem& buttStop = AddMiniForm(tr, "kill.cgi", arr[i], _("Stop"));
|
TXmlItem& buttStop = AddMiniForm(tr, "kill.cgi", arr[i], _("Stop"));
|
||||||
if (!(bRunning || bLurch))
|
if (!bLurch && (!bRunning || bAutoRun))
|
||||||
buttStop.SetAttr(wxT("type"), wxT("hidden"));
|
buttStop.SetAttr(wxT("type"), wxT("hidden"));
|
||||||
|
|
||||||
TXmlItem& buttRestart = AddMiniForm(tr, "restart.cgi", arr[i], _("Restart"));
|
TXmlItem& buttRestart = AddMiniForm(tr, "restart.cgi", arr[i], _("Restart"));
|
||||||
@ -440,7 +459,7 @@ void TLurchServer::ProcessFormStop(const THashTable& args, wxSocketBase& sock)
|
|||||||
StopProcess(strApp);
|
StopProcess(strApp);
|
||||||
MessageBox("Server stopped", strApp, sock);
|
MessageBox("Server stopped", strApp, sock);
|
||||||
|
|
||||||
m_Semaphore.Post();
|
m_Semaphore.Post(); // Inutile, ma cosi' non rompo la simmetria
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -626,25 +645,19 @@ void TLurchServer::ProcessHttpPost(wxString cmd, wxSocketBase& outs)
|
|||||||
|
|
||||||
bool TLurchServer::Initialization()
|
bool TLurchServer::Initialization()
|
||||||
{
|
{
|
||||||
const wxArrayString& arr = GetServersList();
|
const wxArrayString& arr = GetAutoRunList();
|
||||||
for (size_t i = 0; i < arr.GetCount(); i++)
|
const size_t nAuto = arr.GetCount();
|
||||||
{
|
if (nAuto > 0)
|
||||||
const wxString& strApp = arr[i];
|
|
||||||
if (strApp != GetAppName())
|
|
||||||
{
|
|
||||||
const bool bAutorun = GetConfigBool("Autorun", false, strApp);
|
|
||||||
if (bAutorun)
|
|
||||||
StartProcess(strApp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const int nFreq = GetConfigInt("PingFreq");
|
|
||||||
if (nFreq > 0)
|
|
||||||
{
|
{
|
||||||
m_PingTimer.Start(nFreq * 1000); // sec to msec
|
for (size_t i = 0; i < nAuto; i++)
|
||||||
m_Semaphore.Post(); // GREEN!
|
StartProcess(arr[i]);
|
||||||
}
|
const int nFreq = GetConfigInt("PingFreq");
|
||||||
|
if (nFreq > 0)
|
||||||
|
{
|
||||||
|
m_PingTimer.Start(nFreq * 1000); // sec to msec
|
||||||
|
m_Semaphore.Post(); // GREEN!
|
||||||
|
}
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user