diff --git a/ParChecker.cpp b/ParChecker.cpp index 0f34a277..7c0b4544 100644 --- a/ParChecker.cpp +++ b/ParChecker.cpp @@ -191,6 +191,7 @@ void ParChecker::Run() if (res != eSuccess || IsStopped()) { error("Could not verify %s: %s", m_szInfoName, IsStopped() ? "due stopping" : "par2-file could not be processed"); + m_szErrMsg = strdup("par2-file could not be processed"); SetStatus(psFailed); delete pRepairer; return; diff --git a/PrePostProcessor.cpp b/PrePostProcessor.cpp index d46338a2..faf5fbb3 100644 --- a/PrePostProcessor.cpp +++ b/PrePostProcessor.cpp @@ -330,6 +330,10 @@ void PrePostProcessor::CheckDiskSpace() void PrePostProcessor::CheckPostQueue() { + int iCleanupGroupID = 0; + char szNZBNiceName[1024]; + + DownloadQueue* pDownloadQueue = g_pQueueCoordinator->LockQueue(); m_mutexQueue.Lock(); if (!m_PostQueue.empty()) @@ -346,11 +350,22 @@ void PrePostProcessor::CheckPostQueue() #endif if (pPostInfo->GetStage() == PostInfo::ptQueued) { - StartScriptJob(pPostInfo); + StartScriptJob(pDownloadQueue, pPostInfo); } else if (pPostInfo->GetStage() == PostInfo::ptFinished) { - JobCompleted(pPostInfo); +#ifndef DISABLE_PARCHECK + if (g_pOptions->GetParCleanupQueue() && + IsNZBFileCompleted(pDownloadQueue, pPostInfo->GetNZBFilename(), true, true, true) && + pPostInfo->GetParStatus() != PARSTATUS_NOT_CHECKED && + pPostInfo->GetParStatus() != PARSTATUS_FAILED && + !HasFailedParJobs(pPostInfo->GetNZBFilename())) + { + iCleanupGroupID = GetParCleanupQueueGroup(pDownloadQueue, pPostInfo->GetNZBFilename()); + NZBInfo::MakeNiceNZBName(pPostInfo->GetNZBFilename(), szNZBNiceName, sizeof(szNZBNiceName)); + } +#endif + JobCompleted(pDownloadQueue, pPostInfo); } else { @@ -360,6 +375,13 @@ void PrePostProcessor::CheckPostQueue() } m_mutexQueue.Unlock(); + g_pQueueCoordinator->UnlockQueue(); + + if (iCleanupGroupID > 0) + { + info("Cleaning up download queue for %s", szNZBNiceName); + g_pQueueCoordinator->GetQueueEditor()->EditEntry(iCleanupGroupID, false, QueueEditor::eaGroupDelete, 0); + } } /** @@ -399,7 +421,7 @@ void PrePostProcessor::SanitisePostQueue() /** * Mutex "m_mutexQueue" must be locked prior to call of this function. */ -void PrePostProcessor::StartScriptJob(PostInfo* pPostInfo) +void PrePostProcessor::StartScriptJob(DownloadQueue* pDownloadQueue, PostInfo* pPostInfo) { const char* szScript = g_pOptions->GetPostProcess(); if (!szScript || strlen(szScript) == 0) @@ -415,7 +437,13 @@ void PrePostProcessor::StartScriptJob(PostInfo* pPostInfo) pPostInfo->SetStageProgress(0); SavePostQueue(); - bool bNZBFileCompleted = IsNZBFileCompleted(NULL, pPostInfo->GetNZBFilename(), true, true, true); + if (!pPostInfo->GetStartTime()) + { + pPostInfo->SetStartTime(time(NULL)); + } + pPostInfo->SetStageTime(time(NULL)); + + bool bNZBFileCompleted = IsNZBFileCompleted(pDownloadQueue, pPostInfo->GetNZBFilename(), true, true, true); #ifndef DISABLE_PARCHECK bool bHasFailedParJobs = HasFailedParJobs(pPostInfo->GetNZBFilename()) || pPostInfo->GetParFailed(); #else @@ -428,7 +456,7 @@ void PrePostProcessor::StartScriptJob(PostInfo* pPostInfo) /** * Mutex "m_mutexQueue" must be locked prior to call of this function. */ -void PrePostProcessor::JobCompleted(PostInfo* pPostInfo) +void PrePostProcessor::JobCompleted(DownloadQueue* pDownloadQueue, PostInfo* pPostInfo) { pPostInfo->SetWorking(false); pPostInfo->SetProgressLabel(""); @@ -440,17 +468,6 @@ void PrePostProcessor::JobCompleted(PostInfo* pPostInfo) pPostInfo->SetScriptThread(NULL); } -#ifndef DISABLE_PARCHECK - if (g_pOptions->GetParCleanupQueue() && - IsNZBFileCompleted(NULL, pPostInfo->GetNZBFilename(), true, true, true) && - !HasFailedParJobs(pPostInfo->GetNZBFilename())) - { - m_mutexQueue.Unlock(); - ParCleanupQueue(pPostInfo->GetNZBFilename()); - m_mutexQueue.Lock(); - } -#endif - for (PostQueue::iterator it = m_PostQueue.begin(); it != m_PostQueue.end(); it++) { if (pPostInfo == *it) @@ -462,7 +479,7 @@ void PrePostProcessor::JobCompleted(PostInfo* pPostInfo) m_CompletedJobs.push_back(pPostInfo); - if (IsNZBFileCompleted(NULL, pPostInfo->GetNZBFilename(), false, false, true)) + if (IsNZBFileCompleted(pDownloadQueue, pPostInfo->GetNZBFilename(), false, false, true)) { ClearCompletedJobs(pPostInfo->GetNZBFilename()); } @@ -501,6 +518,8 @@ bool PrePostProcessor::CheckScript(FileInfo * pFileInfo) pFileInfo->GetNZBInfo()->GetNiceNZBName(szNZBNiceName, 1024); info("Queueing %s for post-process-script", szNZBNiceName); + for (int i = 0; i < 1000; i++) + { PostInfo* pPostInfo = new PostInfo(); pPostInfo->SetNZBFilename(pFileInfo->GetNZBInfo()->GetFilename()); pPostInfo->SetDestDir(pFileInfo->GetNZBInfo()->GetDestDir()); @@ -508,6 +527,7 @@ bool PrePostProcessor::CheckScript(FileInfo * pFileInfo) pPostInfo->SetInfoName(szNZBNiceName); pPostInfo->SetParCheck(false); m_PostQueue.push_back(pPostInfo); + } SavePostQueue(); m_bHasMoreJobs = true; bJobAdded = true; @@ -583,19 +603,14 @@ void PrePostProcessor::PausePars(DownloadQueue* pDownloadQueue, const char* szNZ } /** - * Mutex "m_mutexQueue" must be locked prior to call of this function. + * Mutex "m_mutexQueue" must be locked prior to call of this function, + * if the parameter "bCheckPostQueue" is set to "true". */ bool PrePostProcessor::IsNZBFileCompleted(DownloadQueue* pDownloadQueue, const char* szNZBFilename, bool bIgnoreFirstInPostQueue, bool bIgnorePaused, bool bCheckPostQueue) { bool bNZBFileCompleted = true; - bool bNeedQueueLock = !pDownloadQueue; - if (bNeedQueueLock) - { - pDownloadQueue = g_pQueueCoordinator->LockQueue(); - } - for (DownloadQueue::iterator it = pDownloadQueue->begin(); it != pDownloadQueue->end(); it++) { FileInfo* pFileInfo = *it; @@ -607,11 +622,6 @@ bool PrePostProcessor::IsNZBFileCompleted(DownloadQueue* pDownloadQueue, const c } } - if (bNeedQueueLock) - { - g_pQueueCoordinator->UnlockQueue(); - } - if (bNZBFileCompleted && bCheckPostQueue) { for (PostQueue::iterator it = m_PostQueue.begin() + int(bIgnoreFirstInPostQueue); it != m_PostQueue.end(); it++) @@ -840,17 +850,16 @@ void PrePostProcessor::ParCheckerUpdate(Subject * Caller, void * Aspect) } /** - * Delete unneeded (paused) par-files from download queue after successful par-check. + * Check if the deletion of unneeded (paused) par-files from download queue after successful par-check is possible. * If the collection has paused non-par-files, none files will be deleted (even pars). + * Returns the group-ID, which can be later used in call to GetQueueEditor()->EditEntry() */ -void PrePostProcessor::ParCleanupQueue(const char* szNZBFilename) +int PrePostProcessor::GetParCleanupQueueGroup(DownloadQueue* pDownloadQueue, const char* szNZBFilename) { - debug("Cleaning up download queue from par-files"); + debug("Preparing to cleaning up download queue from par-files"); // check if nzb-file has only pars paused int ID = 0; - bool bOnlyPars = true; - DownloadQueue* pDownloadQueue = g_pQueueCoordinator->LockQueue(); for (DownloadQueue::iterator it = pDownloadQueue->begin(); it != pDownloadQueue->end(); it++) { FileInfo* pFileInfo = *it; @@ -860,20 +869,11 @@ void PrePostProcessor::ParCleanupQueue(const char* szNZBFilename) if (!pFileInfo->GetPaused() && !pFileInfo->GetDeleted() && !ParseParFilename(pFileInfo->GetFilename(), NULL, NULL)) { - bOnlyPars = false; - break; + return 0; } } } - g_pQueueCoordinator->UnlockQueue(); - - if (bOnlyPars && ID > 0) - { - char szNZBNiceName[1024]; - NZBInfo::MakeNiceNZBName(szNZBFilename, szNZBNiceName, sizeof(szNZBNiceName)); - info("Cleaning up download queue for %s", szNZBNiceName); - g_pQueueCoordinator->GetQueueEditor()->EditEntry(ID, false, QueueEditor::eaGroupDelete, 0); - } + return ID; } /** diff --git a/PrePostProcessor.h b/PrePostProcessor.h index 9be7fe3a..5f379fd0 100644 --- a/PrePostProcessor.h +++ b/PrePostProcessor.h @@ -90,8 +90,8 @@ private: bool JobExists(PostQueue* pPostQueue, const char* szNZBFilename); bool ClearCompletedJobs(const char* szNZBFilename); void CheckPostQueue(); - void JobCompleted(PostInfo* pPostInfo); - void StartScriptJob(PostInfo* pPostInfo); + void JobCompleted(DownloadQueue* pDownloadQueue, PostInfo* pPostInfo); + void StartScriptJob(DownloadQueue* pDownloadQueue, PostInfo* pPostInfo); void SavePostQueue(); void SanitisePostQueue(); void CheckDiskSpace(); @@ -109,7 +109,7 @@ private: bool AddPar(FileInfo* pFileInfo, bool bDeleted); bool SameParCollection(const char* szFilename1, const char* szFilename2); bool FindMainPars(const char* szPath, FileList* pFileList); - void ParCleanupQueue(const char* szNZBFilename); + int GetParCleanupQueueGroup(DownloadQueue* pDownloadQueue, const char* szNZBFilename); bool HasFailedParJobs(const char* szNZBFilename); bool ParJobExists(PostQueue* pPostQueue, const char* szParFilename); bool ParseParFilename(const char* szParFilename, int* iBaseNameLen, int* iBlocks); diff --git a/ScriptController.cpp b/ScriptController.cpp index 2242372f..6d08d75b 100644 --- a/ScriptController.cpp +++ b/ScriptController.cpp @@ -53,13 +53,6 @@ extern Options* g_pOptions; void ScriptController::StartScriptJob(PostInfo* pPostInfo, const char* szScript, bool bNZBFileCompleted, bool bHasFailedParJobs) { - if (!pPostInfo->GetStartTime()) - { - pPostInfo->SetStartTime(time(NULL)); - } - pPostInfo->SetStageTime(time(NULL)); - pPostInfo->SetStageProgress(0); - if (!Util::FileExists(szScript)) { error("Could not start post-process-script: could not find file %s", szScript); @@ -80,12 +73,6 @@ void ScriptController::StartScriptJob(PostInfo* pPostInfo, const char* szScript, pPostInfo->SetScriptThread(pScriptController); pScriptController->Start(); - - // wait until process starts or fails to start - while (pPostInfo->GetWorking() && !pScriptController->m_hProcess) - { - usleep(50 * 1000); - } } void ScriptController::Run() @@ -106,7 +93,7 @@ void ScriptController::Run() #ifdef WIN32 char szCmdLine[2048]; - snprintf(szCmdLine, 2048, "%s \"%s\" \"%s\" \"%s\" %s %s %s", m_szScript, m_pPostInfo->GetDestDir(), + snprintf(szCmdLine, 2048, "\"%s\" \"%s\" \"%s\" \"%s\" %s %s %s", m_szScript, m_pPostInfo->GetDestDir(), m_pPostInfo->GetNZBFilename(), m_pPostInfo->GetParFilename(), szParStatus, szCollectionCompleted, szHasFailedParJobs); szCmdLine[2048-1] = '\0'; @@ -139,11 +126,11 @@ void ScriptController::Run() if (FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM || FORMAT_MESSAGE_IGNORE_INSERTS || FORMAT_MESSAGE_ARGUMENT_ARRAY, NULL, dwErrCode, 0, szErrMsg, 255, NULL)) { - error("Could not start post-process: %s", szErrMsg); + error("Could not start post-process-script: %s", szErrMsg); } else { - error("Could not start post-process: error %i", dwErrCode); + error("Could not start post-process-script: error %i", dwErrCode); } m_pPostInfo->SetStage(PostInfo::ptFinished); m_pPostInfo->SetWorking(false); @@ -186,12 +173,13 @@ void ScriptController::Run() /* orient the pipe */ pipein = p[0]; pipeout = p[1]; - + + debug("forking"); pid_t pid = fork(); if (pid == -1) { - error("Could not start post-process: errno %i", errno); + error("Could not start post-process-script: errno %i", errno); m_pPostInfo->SetStage(PostInfo::ptFinished); m_pPostInfo->SetWorking(false); return; @@ -216,10 +204,12 @@ void ScriptController::Run() execlp(m_szScript, m_szScript, szDestDir, szNZBFilename, szParFilename, szParStatus, szCollectionCompleted, szHasFailedParJobs, NULL); - error("Could not start post-process: %s", strerror(errno)); - exit(-1); + error("Could not start post-process-script: %s", strerror(errno)); + _exit(-1); } + debug("forked"); + // continue the first instance m_hProcess = pid; @@ -266,6 +256,8 @@ void ScriptController::Run() void ScriptController::AddMessage(char* szText) { + debug("Adding message received from post-process-script"); + for (char* pend = szText + strlen(szText) - 1; pend >= szText && (*pend == '\n' || *pend == '\r' || *pend == ' '); pend--) *pend = '\0'; if (strlen(szText) == 0) @@ -363,6 +355,8 @@ void ScriptController::AddMessage(char* szText) m_pPostInfo->AppendMessage(eKind, szText); } } + + debug("Adding message received from post-process-script - completed"); } void ScriptController::Stop()