Skip to content

Commit

Permalink
Merged error handling changes from 5.5.1.
Browse files Browse the repository at this point in the history
  • Loading branch information
martinknafve committed Sep 27, 2014
2 parents 037f5c5 + 69e54ef commit a784ec5
Show file tree
Hide file tree
Showing 59 changed files with 3,381 additions and 2,098 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ hmailserver/source/Server/hMailServer/Release/*
*.sdf
*.opensdf
*.ipch
*.orig
2,674 changes: 1,890 additions & 784 deletions hmailserver/installation/License.rtf

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions hmailserver/installation/hMailServer.iss
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Source: "Microsoft.VC120.CRT\*"; DestDir: "{app}\Bin"; Flags: ignoreversion; Com
Source: "..\source\server\hMailServer\Release\hMailServer.exe"; DestDir: "{app}\Bin"; Flags: ignoreversion; Components: server admintools;
Source: "..\source\server\hMailServer\Release\hMailServer.tlb"; DestDir: "{app}\Bin"; Flags: ignoreversion; Components: server admintools;


; Database scripts
Source: "..\source\DBScripts\*.sql"; DestDir: "{app}\DBScripts";Flags: ignoreversion recursesubdirs; Components: server;

Expand Down
2 changes: 2 additions & 0 deletions hmailserver/source/Server/Common/Application/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,8 @@ namespace HM
if (notification_server_) notification_server_.reset();
if (folder_manager_) folder_manager_.reset();

MessageIndexer::Instance()->Stop();

ServerStatus::Instance()->SetState(ServerStatus::StateStopped);

LOG_APPLICATION("Servers stopped.")
Expand Down
12 changes: 5 additions & 7 deletions hmailserver/source/Server/Common/Application/ErrorManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "stdafx.h"
#include "ErrorManager.h"


#include <oledb.h>


Expand Down Expand Up @@ -103,25 +104,22 @@ namespace HM
void
ErrorManager::ReportError(eSeverity iSeverity, int iErrorID, const String &sSource, const String &sDescription, const boost::system::system_error &error)
{
String formatted_message;
formatted_message.Format(_T("%s, Error code: %d, Message: %s"), sDescription.c_str(), error.code().value(), String(error.what()).c_str());
String formatted_message
= Formatter::Format(_T("{0}, Error code: {1}, Message: {2}"), sDescription, error.code().value(), error.what());

ReportError(iSeverity, iErrorID, sSource, formatted_message);
}

void
ErrorManager::ReportError(eSeverity iSeverity, int iErrorID, const String &sSource, const String &sDescription, const std::exception &error)
{
String formatted_message;
formatted_message.Format(_T("%s, Message: %s"), sDescription.c_str(), error.what());
String formatted_message
= Formatter::Format(_T("{0}, Message: {1}"), sDescription, error.what());

ReportError(iSeverity, iErrorID, sSource, formatted_message);
}





String
ErrorManager::GetWindowsErrorText(int windows_error_code)
{
Expand Down
7 changes: 3 additions & 4 deletions hmailserver/source/Server/Common/Application/Errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,13 @@
5323, TCPConnection::_StartAsyncConnect, An unknown error occurred while starting asynchronous connect.
5324, TCPConnection::AsyncConnectCompleted, An unknown error occurred while handling asynchronous connect.
5325, TCPConnection::_StartAsyncConnect, An unknown error occurred while starting connection.
5326, TCPConnection::_StartAsyncConnect, An unknown error occurred while handling handshake.
5327, TCPConnection::EnqueueWrite, An unknown error occurred while posting write buffer.
5328, TCPConnection::EnqueueWrite, An unknown error occurred while posting write buffer.
5329, TCPConnection::Write, An unknown error occurred while writing buffer.
5330, TCPConnection::Shutdown, An unknown error occurred while shutting down socket.
5331, TCPConnection::EnqueueRead, An unknown error occurred while posting read buffer.
5332, TCPConnection::EnqueueRead, An unknown error occurred while starting async reading
5333, TCPConnection::UpdateLogoutTimer, An unknown error occurred while updating logout timer.
5334, TCPConnection::CancelLogoutTimer, An unknown error occurred while canceling logout timer.
5335, TCPConnection::EnqueueDisconnect, An unknown error occurred while posting disconnect.
5336, TCPConnection::Disconnect, An unknown error occurred while disconnecting.
5337, TCPConnection::OnTimeout, An unknown error occurred while disconnecting.
5338, TCPConnection::EnqueueShutdown, An unknown error occurred while posting shutdown.
5339, TCPConnection::AsyncWriteCompleted, An unknown error occurred while handling buffer write.
Expand Down Expand Up @@ -113,4 +109,7 @@
5514, Unable to find appropriate MySQL character set.
5515, Failed to enable lingering for session {0}
5516, Attempted DNS lookup for empty host name.
5518, An error occured when accepting a connection
5519, An error has been detected and an error stack will be generated and saved in {0}.
5520, Failed to open socket on IP address {0}.
5601, _AtlModule.WinMain returned {0}.
109 changes: 109 additions & 0 deletions hmailserver/source/Server/Common/Application/ExceptionHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Copyright (c) 2010 Martin Knafve / hMailServer.com.
// http://www.hmailserver.com

#include "StdAfx.h"

#include "ExceptionHandler.h"
#include "../Util/ExceptionLogger.h"

#include <boost/thread/thread.hpp>
#include "../TCPIP/DisconnectedException.h"

#ifdef _DEBUG
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
#define new DEBUG_NEW
#endif

namespace HM
{
ExceptionHandler::ExceptionHandler(void)
{

}

LONG WINAPI ExceptionFilterWithLogging(EXCEPTION_POINTERS* pExp, DWORD dwExpCode)
{
// if an error occurs when shutting down, we want to log it completely before
// the shut down completes.
boost::this_thread::disable_interruption shutdown_temporarily_disabled;

LOG_DEBUG("Logging exception..");

ExceptionLogger::Log(dwExpCode, pExp);

LOG_DEBUG("Completed logging of exception...");

return EXCEPTION_EXECUTE_HANDLER;
}


bool
ExceptionHandler::Run(const String &descriptive_name, boost::function<void()>& func)
{
__try
{
RunWithStandardExceptions(descriptive_name, func);
return true;
}
__except (ExceptionFilterWithLogging(GetExceptionInformation(), GetExceptionCode()))
{
// this has been logged in the exception filter.
return false;
}
}

void
ExceptionHandler::RunWithStandardExceptions(const String &descriptive_name, boost::function<void()>& func)
{
try
{
func();
}
catch (boost::thread_interrupted&)
{
// shutting down
}
catch (DisconnectedException&)
{
LOG_DEBUG("Connection was terminated - Client is disconnected.");
}
catch (boost::system::system_error& error)
{
ErrorManager::Instance()->ReportError(ErrorManager::High, 4208, "ExceptionHandler::Run", GetExceptionText(descriptive_name), error);

throw;
}
catch (std::exception& error)
{
String sErrorMessage =
Formatter::Format("An error occured while executing '{0}'", descriptive_name);

ErrorManager::Instance()->ReportError(ErrorManager::High, 4208, "ExceptionHandler::Run", GetExceptionText(descriptive_name), error);

throw;
}
catch (...)
{
String sErrorMessage =
Formatter::Format("An error occured while executing '{0}'", descriptive_name);

ErrorManager::Instance()->ReportError(ErrorManager::High, 4208, "ExceptionHandler::Run", GetExceptionText(descriptive_name));

throw;
}
}



String
ExceptionHandler::GetExceptionText(const String &descriptive_name)
{
String sErrorMessage =
Formatter::Format("An error occured while executing '{0}'", descriptive_name);

return sErrorMessage;

}


}
22 changes: 22 additions & 0 deletions hmailserver/source/Server/Common/Application/ExceptionHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2010 Martin Knafve / hMailServer.com.
// http://www.hmailserver.com

#pragma once

namespace HM
{
class ExceptionHandler
{
public:
ExceptionHandler();

static bool Run(const String &descriptive_name, boost::function<void()>& functionToRun);

private:

static void RunWithStandardExceptions(const String &descriptive_name, boost::function<void()>& functionToRun);

static String GetExceptionText(const String &descriptive_name);

};
}
47 changes: 26 additions & 21 deletions hmailserver/source/Server/Common/Application/MessageIndexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "StdAfx.h"

#include "MessageIndexer.h"

#include "../Application/ExceptionHandler.h"
#include "../BO/Message.h"
#include "../BO/MessageMetaData.h"
#include "../MIME/MIME.h"
Expand Down Expand Up @@ -42,7 +44,7 @@ namespace HM
}
}
// Start the indexer now.
LOG_DEBUG("Starting message indexing thread...");
LOG_DEBUG("Starting message indexer...");

std::function<void ()> func = std::bind( &MessageIndexer::WorkerFunc, this );
workerThread_ = boost::thread(func);
Expand All @@ -52,30 +54,26 @@ namespace HM
void
MessageIndexer::WorkerFunc()
{
LOG_DEBUG("Indexing messages...");
boost::function<void()> func = boost::bind( &MessageIndexer::WorkerFuncInternal, this );
ExceptionHandler::Run("MessageIndexer", func);
LOG_DEBUG("Message indexer stopped.");
}

try
{
PersistentMessageMetaData persistentMetaData;
persistentMetaData.DeleteOrphanedItems();
void
MessageIndexer::WorkerFuncInternal()
{
LOG_DEBUG("Message indexer started...");

while (true)
{
IndexMessages_();
PersistentMessageMetaData persistentMetaData;
persistentMetaData.DeleteOrphanedItems();

index_now_.WaitFor(boost::chrono::minutes(1));
}
}
catch (const boost::thread_interrupted&)
while (true)
{
LOG_DEBUG("Indexing stopped.");
IndexMessages_();

// shutting down.
}
catch (...)
{
ErrorManager::Instance()->ReportError(ErrorManager::Medium, 5158, "MessageIndexer::DoWork", "An error occured while indexing messages. The indexing was aborted.");
index_now_.WaitFor(boost::chrono::minutes(1));
}

}

void
Expand All @@ -87,10 +85,17 @@ namespace HM
void
MessageIndexer::Stop()
{
workerThread_.interrupt();
if (workerThread_.joinable())
{
if (!workerThread_.timed_join(boost::posix_time::milliseconds(1)))
{
// thread is running. interrupt it.
LOG_DEBUG("Stopping message indexer.");
workerThread_.interrupt();
}
}
}


void
MessageIndexer::IndexMessages_()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace HM


void WorkerFunc();
void WorkerFuncInternal();

void IndexMessages_();

Expand Down
2 changes: 1 addition & 1 deletion hmailserver/source/Server/Common/Application/Scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace HM
if (dtRunTime <= dtNow)
{
// Yup, we should run this task now.
pTask->DoWork();
pTask->Run();

// Update run time.
pTask->SetNextRunTime();
Expand Down
Loading

0 comments on commit a784ec5

Please sign in to comment.