Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
#351: reworked timed services
Browse files Browse the repository at this point in the history
Now sleeping much longer, up to next scheduled work, instead of often
wake ups to check if the work needs to be performed. This improves CPU
usage in idle.
  • Loading branch information
hugbug committed Jan 21, 2019
1 parent 137c936 commit 92828ac
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 69 deletions.
29 changes: 19 additions & 10 deletions daemon/main/DiskService.cpp
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2015-2016 Andrey Prygunkov <[email protected]>
* Copyright (C) 2015-2019 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -26,18 26,23 @@
#include "Util.h"
#include "FileSystem.h"

int DiskService::ServiceInterval()
{
return m_waitingRequiredDir ? 1 :
g_Options->GetDiskSpace() <= 0 ? Service::Sleep :
// g_StatMeter->GetStandBy() ? Service::Sleep : // for that to work we need to react on changing of idle-state
1;
}

void DiskService::ServiceWork()
{
m_interval ;
if (m_interval == 5)
debug("Disk service work");

if (!g_Options->GetPauseDownload() &&
g_Options->GetDiskSpace() > 0 && !g_StatMeter->GetStandBy())
{
if (!g_Options->GetPauseDownload() &&
g_Options->GetDiskSpace() > 0 && !g_StatMeter->GetStandBy())
{
// check free disk space every 1 second
CheckDiskSpace();
}
m_interval = 0;
// check free disk space every 1 second
CheckDiskSpace();
}

if (m_waitingRequiredDir)
Expand All @@ -48,6 53,8 @@ void DiskService::ServiceWork()

void DiskService::CheckDiskSpace()
{
debug("Disk service work: check disk space");

int64 freeSpace = FileSystem::FreeDiskSize(g_Options->GetDestDir());
if (freeSpace > -1 && freeSpace / 1024 / 1024 < g_Options->GetDiskSpace())
{
Expand All @@ -68,6 75,8 @@ void DiskService::CheckDiskSpace()

void DiskService::CheckRequiredDir()
{
debug("Disk service work: check required dir");

if (!Util::EmptyStr(g_Options->GetRequiredDir()))
{
bool allExist = true;
Expand Down
5 changes: 2 additions & 3 deletions daemon/main/DiskService.h
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2015-2016 Andrey Prygunkov <[email protected]>
* Copyright (C) 2015-2019 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -26,11 26,10 @@
class DiskService : public Service
{
protected:
virtual int ServiceInterval() { return 200; }
virtual int ServiceInterval();
virtual void ServiceWork();

private:
int m_interval = 0;
bool m_waitingRequiredDir = true;
bool m_waitingReported = false;

Expand Down
23 changes: 22 additions & 1 deletion daemon/main/Scheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2008-2017 Andrey Prygunkov <[email protected]>
* Copyright (C) 2008-2019 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -51,13 51,33 @@ void Scheduler::FirstCheck()
CheckTasks();
}

void Scheduler::ScheduleNextWork()
{
// Ideally we should calculate wait time until next scheduler task or until resume time.
// The first isn't trivial and the second requires watching/reaction on changed scheduled resume time.
// We do it simpler instead: check once per minute, when seconds are changing from 59 to 00.

time_t curTime = Util::CurrentTime();
tm sched;
gmtime_r(&curTime, &sched);
sched.tm_min ;
sched.tm_sec = 0;
time_t nextMinute = Util::Timegm(&sched);

m_serviceInterval = nextMinute - curTime;
}

void Scheduler::ServiceWork()
{
debug("Scheduler service work");

if (!DownloadQueue::IsLoaded())
{
return;
}

debug("Scheduler service work: doing work");

if (!m_firstChecked)
{
FirstCheck();
Expand All @@ -68,6 88,7 @@ void Scheduler::ServiceWork()
m_executeProcess = true;
CheckTasks();
CheckScheduledResume();
ScheduleNextWork();
}

void Scheduler::CheckTasks()
Expand Down
6 changes: 4 additions & 2 deletions daemon/main/Scheduler.h
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2008-2016 Andrey Prygunkov <[email protected]>
* Copyright (C) 2008-2019 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -66,7 66,7 @@ class Scheduler : public Service
void AddTask(std::unique_ptr<Task> task);

protected:
virtual int ServiceInterval() { return 1000; }
virtual int ServiceInterval() { return m_serviceInterval; }
virtual void ServiceWork();

private:
Expand All @@ -84,6 84,7 @@ class Scheduler : public Service
bool m_serverChanged;
ServerStatusList m_serverStatusList;
bool m_firstChecked = false;
int m_serviceInterval = 1;

void ExecuteTask(Task* task);
void CheckTasks();
Expand All @@ -93,6 94,7 @@ class Scheduler : public Service
void FetchFeed(const char* feedList);
void CheckScheduledResume();
void FirstCheck();
void ScheduleNextWork();
};

#endif
4 changes: 2 additions & 2 deletions daemon/queue/HistoryCoordinator.h
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2007-2016 Andrey Prygunkov <[email protected]>
* Copyright (C) 2007-2019 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -34,7 34,7 @@ class HistoryCoordinator : public Service
void Redownload(DownloadQueue* downloadQueue, HistoryInfo* historyInfo);

protected:
virtual int ServiceInterval() { return 600000; }
virtual int ServiceInterval() { return 60 * 60; }
virtual void ServiceWork();

private:
Expand Down
87 changes: 50 additions & 37 deletions daemon/queue/Scanner.cpp
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2007-2018 Andrey Prygunkov <[email protected]>
* Copyright (C) 2007-2019 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -73,60 73,72 @@ void Scanner::QueueData::SetNzbId(int nzbId)

void Scanner::InitOptions()
{
m_nzbDirInterval = g_Options->GetNzbDirInterval() * 1000;
m_nzbDirInterval = 1;
m_scanScript = ScanScriptController::HasScripts();
}

int Scanner::ServiceInterval()
{
return m_requestedNzbDirScan ? Service::Now :
g_Options->GetNzbDirInterval() <= 0 ? Service::Sleep :
// g_Options->GetPauseScan() ? Service::Sleep : // for that to work we need to react on changing of pause-state
m_nzbDirInterval;
}

void Scanner::ServiceWork()
{
debug("Scanner service work");

if (!DownloadQueue::IsLoaded())
{
return;
}

m_nzbDirInterval = g_Options->GetNzbDirInterval();

if (g_Options->GetPauseScan() && !m_requestedNzbDirScan)
{
return;
}

debug("Scanner service work: doing work");

Guard guard(m_scanMutex);

if (m_requestedNzbDirScan ||
(!g_Options->GetPauseScan() && g_Options->GetNzbDirInterval() > 0 &&
m_nzbDirInterval >= g_Options->GetNzbDirInterval() * 1000))
// check nzbdir every g_pOptions->GetNzbDirInterval() seconds or if requested
bool checkStat = !m_requestedNzbDirScan;
m_requestedNzbDirScan = false;
m_scanning = true;
CheckIncomingNzbs(g_Options->GetNzbDir(), "", checkStat);
if (!checkStat && m_scanScript)
{
// check nzbdir every g_pOptions->GetNzbDirInterval() seconds or if requested
bool checkStat = !m_requestedNzbDirScan;
m_requestedNzbDirScan = false;
m_scanning = true;
// if immediate scan requested, we need second scan to process files extracted by scan-scripts
CheckIncomingNzbs(g_Options->GetNzbDir(), "", checkStat);
if (!checkStat && m_scanScript)
}
m_scanning = false;

// if NzbDirFileAge is less than NzbDirInterval (that can happen if NzbDirInterval
// is set for rare scans like once per hour) we make 4 scans:
// - one additional scan is neccessary to check sizes of detected files;
// - another scan is required to check files which were extracted by scan-scripts;
// - third scan is needed to check sizes of extracted files.
if (g_Options->GetNzbDirInterval() > 0 && g_Options->GetNzbDirFileAge() < g_Options->GetNzbDirInterval())
{
int maxPass = m_scanScript ? 3 : 1;
if (m_pass < maxPass)
{
// if immediate scan requested, we need second scan to process files extracted by scan-scripts
CheckIncomingNzbs(g_Options->GetNzbDir(), "", checkStat);
// scheduling another scan of incoming directory in NzbDirFileAge seconds.
m_nzbDirInterval = g_Options->GetNzbDirFileAge();
m_pass ;
}
m_scanning = false;
m_nzbDirInterval = 0;

// if NzbDirFileAge is less than NzbDirInterval (that can happen if NzbDirInterval
// is set for rare scans like once per hour) we make 4 scans:
// - one additional scan is neccessary to check sizes of detected files;
// - another scan is required to check files which were extracted by scan-scripts;
// - third scan is needed to check sizes of extracted files.
if (g_Options->GetNzbDirInterval() > 0 && g_Options->GetNzbDirFileAge() < g_Options->GetNzbDirInterval())
else
{
int maxPass = m_scanScript ? 3 : 1;
if (m_pass < maxPass)
{
// scheduling another scan of incoming directory in NzbDirFileAge seconds.
m_nzbDirInterval = (g_Options->GetNzbDirInterval() - g_Options->GetNzbDirFileAge()) * 1000;
m_pass ;
}
else
{
m_pass = 0;
}
m_pass = 0;
}

DropOldFiles();
m_queueList.clear();
}
m_nzbDirInterval = 200;

DropOldFiles();
m_queueList.clear();
}

/**
Expand Down Expand Up @@ -486,6 498,7 @@ void Scanner::ScanNzbDir(bool syncMode)
Guard guard(m_scanMutex);
m_scanning = true;
m_requestedNzbDirScan = true;
WakeUp();
}

while (syncMode && (m_scanning || m_requestedNzbDirScan))
Expand All @@ -495,7 508,7 @@ void Scanner::ScanNzbDir(bool syncMode)
}

Scanner::EAddStatus Scanner::AddExternalFile(const char* nzbName, const char* category,
int priority, const char* dupeKey, int dupeScore, EDupeMode dupeMode,
int priority, const char* dupeKey, int dupeScore, EDupeMode dupeMode,
NzbParameterList* parameters, bool addTop, bool addPaused, NzbInfo* urlInfo,
const char* fileName, const char* buffer, int bufSize, int* nzbId)
{
Expand Down
4 changes: 2 additions & 2 deletions daemon/queue/Scanner.h
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2007-2016 Andrey Prygunkov <[email protected]>
* Copyright (C) 2007-2019 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -46,7 46,7 @@ class Scanner : public Service
void InitPPParameters(const char* category, NzbParameterList* parameters, bool reset);

protected:
virtual int ServiceInterval() { return 200; }
virtual int ServiceInterval();
virtual void ServiceWork();

private:
Expand Down
Loading

0 comments on commit 92828ac

Please sign in to comment.