From a2c87535c08c7c81accf0e3e30e2dd31fc6ca82f Mon Sep 17 00:00:00 2001 From: Miguel Freitas Date: Sun, 10 Nov 2013 00:03:24 -0200 Subject: [PATCH] periodically save resume data. if daemon crashes we don't lose everything. --- src/twister.cpp | 74 ++++++++++++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 29 deletions(-) diff --git a/src/twister.cpp b/src/twister.cpp index 6c25ea73..38f651b8 100644 --- a/src/twister.cpp +++ b/src/twister.cpp @@ -280,6 +280,42 @@ bool isBlockChainUptodate() { return (pindexBest->GetBlockTime() > GetTime() - 1 * 60 * 60); } +bool yes(libtorrent::torrent_status const&) +{ return true; } + +void saveTorrentResumeData() +{ + if( ses ){ + printf("saving resume data\n"); + std::vector temp; + ses->get_torrent_status(&temp, &yes, 0); + for (std::vector::iterator i = temp.begin(); + i != temp.end(); ++i) + { + torrent_status& st = *i; + if (!st.handle.is_valid()) + { + printf(" skipping, invalid handle\n"); + continue; + } + if (!st.has_metadata) + { + printf(" skipping %s, no metadata\n", st.name.c_str()); + continue; + } + if (!st.need_save_resume) + { + printf(" skipping %s, resume file up-to-date\n", st.name.c_str()); + continue; + } + + // save_resume_data will generate an alert when it's done + st.handle.save_resume_data(); + ++num_outstanding_resume_data; + } + } +} + void ThreadMaintainDHTNodes() { RenameThread("maintain-dht-nodes"); @@ -288,6 +324,8 @@ void ThreadMaintainDHTNodes() MilliSleep(200); } + int64 lastSaveResumeTime = GetTime(); + while(1) { session_status ss = ses->status(); int dht_nodes = ss.dht_nodes; @@ -368,6 +406,12 @@ void ThreadMaintainDHTNodes() } } + // periodically save resume data. if daemon crashes we don't lose everything. + if( GetTime() > lastSaveResumeTime + 15 * 60 ) { + lastSaveResumeTime = GetTime(); + saveTorrentResumeData(); + } + MilliSleep(5000); } } @@ -535,41 +579,13 @@ void startSessionTorrent(boost::thread_group& threadGroup) threadGroup.create_thread(boost::bind(&ThreadSessionAlerts)); } -bool yes(libtorrent::torrent_status const&) -{ return true; } - void stopSessionTorrent() { if( ses ){ ses->pause(); - printf("saving resume data\n"); - std::vector temp; - ses->get_torrent_status(&temp, &yes, 0); - for (std::vector::iterator i = temp.begin(); - i != temp.end(); ++i) - { - torrent_status& st = *i; - if (!st.handle.is_valid()) - { - printf(" skipping, invalid handle\n"); - continue; - } - if (!st.has_metadata) - { - printf(" skipping %s, no metadata\n", st.name.c_str()); - continue; - } - if (!st.need_save_resume) - { - printf(" skipping %s, resume file up-to-date\n", st.name.c_str()); - continue; - } + saveTorrentResumeData(); - // save_resume_data will generate an alert when it's done - st.handle.save_resume_data(); - ++num_outstanding_resume_data; - } printf("\nwaiting for resume data [%d]\n", num_outstanding_resume_data); while (num_outstanding_resume_data > 0) {