1
0

Upload manager changes

This commit is contained in:
Scott E. Graves
2017-02-26 11:02:27 -06:00
parent 98670b3395
commit 3a2fbcbae0
4 changed files with 108 additions and 21 deletions

View File

@@ -53,6 +53,4 @@ void CAutoThread::StopAutoThread()
_thread->join();
_thread.reset(nullptr);
}
}
}

View File

@@ -27,6 +27,7 @@ protected:
virtual void AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig);
public:
SiaHostConfig GetHostConfig() const { return _siaCurl.GetHostConfig(); }
void StartAutoThread();
void StopAutoThread();
};

View File

@@ -14,7 +14,8 @@ using namespace Sia::Api;
#define QUERY_UPLOADS_BY_SIA_PATH "select id, sia_path, status from upload_table where sia_path=@sia_path order by id desc limit 1;"
#define QUERY_UPLOADS_BY_SIA_PATH_AND_2_STATUS "select id, sia_path, file_path, sd_file_path, status from upload_table where sia_path=@sia_path and (status=@status1 or status=@status2) order by id desc limit 1;"
#define UPDATE_STATUS "update upload_table set status=@status where sia_path=@sia_path;"
#define INSERT_UPLOAD "insert into upload_table (sia_path, status, file_path, sd_file_path) values (@sia_path, @status, @file_path, @sd_file_path)"
#define INSERT_UPLOAD "insert into upload_table (sia_path, status, file_path, sd_file_path) values (@sia_path, @status, @file_path, @sd_file_path);"
#define DELETE_UPLOAD "delete from upload_table where sia_path=@sia_path;"
#define SET_STATUS(status, success_event, fail_event)\
bool statusUpdated = false;\
@@ -35,7 +36,7 @@ try\
}\
catch (SQLite::Exception e)\
{\
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(fail_event(siaPath, filePath, status, CA2W(e.getErrorStr()).m_psz)));\
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseExceptionOccurred(e)));\
}
template <typename... Ts>
@@ -128,7 +129,7 @@ void CUploadManager::FileThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
}
}
void CUploadManager::FileAction(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath, const bool& remove)
void CUploadManager::FileAction(const CSiaCurl& siaCurl, const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath, const bool& remove)
{
{
std::lock_guard<std::mutex> l(_fileActionMutex);
@@ -144,30 +145,51 @@ void CUploadManager::FileAction(const String& siaPath, const String& filePath, c
query.bind("@sia_path", CW2A(siaPath.c_str()).m_psz);
if (query.executeStep())
{
String filePath = CA2W(query.getColumn(3)).m_psz;
String removeFilePath = CA2W(query.getColumn(3)).m_psz;
UploadStatus uploadStatus = static_cast<UploadStatus>(static_cast<unsigned>(query.getColumn(5)));
if (uploadStatus == UploadStatus::Remove)
{
if (::PathFileExists(filePath.c_str()))
bool deleteFromDb = false;
if (::PathFileExists(removeFilePath.c_str()))
{
if (RetryDeleteFileIfExists(filePath.c_str()))
if (RetryDeleteFileIfExists(removeFilePath.c_str()))
{
if (!RetryDeleteFileIfExists(siaDriveFilePath))
{
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, filePath, siaDriveFilePath)));
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DeleteSiaDriveFileFailed(siaPath, removeFilePath, siaDriveFilePath)));
}
// TODO Delete from database
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoved(siaPath, filePath)));
deleteFromDb = true;
}
else
{
// TODO Error condition
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(RemoveFileFailed(siaPath, removeFilePath)));
}
}
else
if (deleteFromDb)
{
// TODO Delete from database
json response;
SiaCurlError cerror = siaCurl.Post(String(L"/renter/delete") + siaPath, {}, response);
if (ApiSuccess(cerror))
{
// TODO validate response
SQLite::Statement del(_uploadDatabase, DELETE_UPLOAD);
del.bind("@sia_path", CW2A(siaPath.c_str()));
if (del.exec() == 1)
{
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoved(siaPath, removeFilePath)));
}
else
{
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(DatabaseDeleteFailed(siaPath, removeFilePath, CA2W(del.getErrorMsg()).m_psz)));
}
}
else
{
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FailedToDeleteFromSia(siaPath, removeFilePath, cerror)));
}
}
}
@@ -318,7 +340,7 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
else if (uploadStatus == UploadStatus::Modified)
{
json response;
SiaCurlError cerror = siaCurl.Post(String(L"/renter/delete/") + siaPath, {}, response);
SiaCurlError cerror = siaCurl.Post(String(L"/renter/delete") + siaPath, {}, response);
if (ApiSuccess(cerror))
{
// TODO validate response
@@ -374,7 +396,7 @@ void CUploadManager::AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig
// TODO Validate response
json response;
SiaCurlError cerror = siaCurl.Post(String(L"/renter/upload/") + siaPath, { {L"source", filePath} }, response);
SiaCurlError cerror = siaCurl.Post(String(L"/renter/upload") + siaPath, { {L"source", filePath} }, response);
if (ApiSuccess(cerror))
{
SET_STATUS(UploadStatus::Uploading, UploadToSiaStarted, ModifyUploadStatusFailed)
@@ -490,7 +512,7 @@ UploadError CUploadManager::AddOrUpdate(const String& siaPath, String filePath)
{
// Queue file upload operation
std::lock_guard<std::mutex> l2(_fileQueueMutex);
_fileQueue.push_back([=]() { this->FileAction(siaPath, filePath, tempSourcePath, siaDriveFilePath, false); });
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, tempSourcePath, siaDriveFilePath, false); });
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(NewFileAdded(siaPath, filePath, siaDriveFilePath)));
}
}
@@ -567,7 +589,7 @@ UploadError CUploadManager::Remove(const String& siaPath)
if (remove)
{
std::lock_guard<std::mutex> l2(_fileQueueMutex);
_fileQueue.push_back([=]() { this->FileAction(siaPath, filePath, nullptr, siaDriveFilePath, true); });
_fileQueue.push_back([=]() { this->FileAction(CSiaCurl(GetHostConfig()), siaPath, filePath, nullptr, siaDriveFilePath, true); });
CEventSystem::EventSystem.NotifyEvent(CreateSystemEvent(FileRemoveAdded(siaPath)));
}

View File

@@ -56,7 +56,7 @@ private:
std::deque<std::function<void()>> _fileQueue;
private:
void FileAction(const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath, const bool& remove);
void FileAction(const CSiaCurl& siaCurl, const String& siaPath, const String& filePath, const String& tempSourcePath, const String& siaDriveFilePath, const bool& remove);
protected:
virtual void AutoThreadCallback(const CSiaCurl& siaCurl, CSiaDriveConfig* siaDriveConfig) override;
@@ -613,6 +613,72 @@ public:
}
};
class DatabaseDeleteFailed :
public CEvent
{
public:
DatabaseDeleteFailed(const String& siaPath, const String& filePath, const String& errorMessage) :
_siaPath(siaPath),
_filePath(filePath),
_errorMessage(errorMessage)
{
}
public:
virtual ~DatabaseDeleteFailed()
{
}
private:
const String _siaPath;
const String _filePath;
const String _errorMessage;
public:
virtual String GetSingleLineMessage() const override
{
return L"DatabaseDeleteFailed|SP|" + _siaPath + L"|FP|" + _filePath + L"|MSG|" + _errorMessage;
}
virtual std::shared_ptr<CEvent> Clone() const override
{
return std::shared_ptr<CEvent>(new DatabaseDeleteFailed(_siaPath, _filePath, _errorMessage));
}
};
class RemoveFileFailed :
public CEvent
{
public:
RemoveFileFailed(const String& siaPath, const String& filePath) :
_siaPath(siaPath),
_filePath(filePath)
{
}
public:
virtual ~RemoveFileFailed()
{
}
private:
const String _siaPath;
const String _filePath;
public:
virtual String GetSingleLineMessage() const override
{
return L"RemoveFileFailed|SP|" + _siaPath + L"|FP|" + _filePath;
}
virtual std::shared_ptr<CEvent> Clone() const override
{
return std::shared_ptr<CEvent>(new RemoveFileFailed(_siaPath, _filePath));
}
};
class ExistingUploadFound :
public CEvent
{