Skip to content

Commit 8dc147e

Browse files
committed
Implement feed updates.
1 parent 9f3951e commit 8dc147e

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

src/Apod.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ bool Apod::InitializeImpl( ini_t* config )
2323
return status;
2424
}
2525

26-
bool Apod::FetchImpl()
26+
bool Apod::FetchImpl( bool first )
2727
{
2828
auto page = FetchPage( "https://apod.nasa.gov/apod/astropix.html" );
2929
if( !PageHashChanged( page ) ) return true;
@@ -64,10 +64,14 @@ bool Apod::FetchImpl()
6464
{
6565
ProcessArticle( article, url.c_str(), timestamp );
6666
}
67+
else if( !first )
68+
{
69+
return true;
70+
}
6771

6872
auto next = article->select_node( "//a[text()='<']" );
6973
if( !next ) return !m_articles.empty();
70-
if( ++num == m_numArticles ) return true;
74+
if( first && ++num == m_numArticles ) return true;
7175

7276
url = m_baseUrl + next.node().attribute( "href" ).as_string();
7377
article = FetchDom( FetchPage( url.c_str() ) );

src/Apod.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Apod : public Handler
1313

1414
private:
1515
bool InitializeImpl( ini_t* config ) final;
16-
bool FetchImpl() final;
16+
bool FetchImpl( bool first ) final;
1717

1818
void ProcessArticle( const std::unique_ptr<pugi::xml_document>& article, const char* url, uint64_t timestamp );
1919

src/Engine.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <atomic>
22
#include <assert.h>
3+
#include <chrono>
34
#include <signal.h>
45
#include <sstream>
56
#include <stdio.h>
@@ -87,9 +88,32 @@ bool Engine::Initialize( ini_t* config )
8788
printf( "Populating feeds\n" );
8889
for( auto& hnd : m_handlers )
8990
{
90-
Enqueue( 0, [&hnd] {
91-
hnd->Fetch();
92-
} );
91+
std::function<void()> FetchFunc, PopulateFunc;
92+
FetchFunc = [&hnd, FetchFunc] {
93+
const auto res = hnd->Fetch( false );
94+
const auto ct = std::chrono::duration_cast<std::chrono::seconds>( std::chrono::steady_clock::now().time_since_epoch() ).count();
95+
if( res )
96+
{
97+
Engine::Instance()->Enqueue( ct + hnd->GetRefresh(), FetchFunc );
98+
}
99+
else
100+
{
101+
Engine::Instance()->Enqueue( ct + hnd->GetFailureRefresh(), FetchFunc );
102+
}
103+
};
104+
PopulateFunc = [&hnd, FetchFunc, PopulateFunc] {
105+
const auto res = hnd->Fetch( true );
106+
const auto ct = std::chrono::duration_cast<std::chrono::seconds>( std::chrono::steady_clock::now().time_since_epoch() ).count();
107+
if( res )
108+
{
109+
Engine::Instance()->Enqueue( ct + hnd->GetRefresh(), FetchFunc );
110+
}
111+
else
112+
{
113+
Engine::Instance()->Enqueue( ct + hnd->GetFailureRefresh(), PopulateFunc );
114+
}
115+
};
116+
Enqueue( 0, PopulateFunc );
93117
}
94118
return true;
95119
}

src/Engine.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef __ENGINE_HPP__
22
#define __ENGINE_HPP__
33

4+
#include <functional>
45
#include <memory>
56
#include <vector>
67

src/Handler.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,19 @@ bool Handler::Initialize( ini_t* config )
4848
return InitializeImpl( config );
4949
}
5050

51-
bool Handler::Fetch()
51+
bool Handler::Fetch( bool first )
5252
{
53-
const auto status = FetchImpl();
53+
const auto status = FetchImpl( first );
5454
if( status )
5555
{
5656
SortArticles();
5757
TrimArticles();
5858
CacheFeed();
59-
PrintStatus( true, "Fetch: %s (%i/%i articles)", GetTitle().c_str(), (int)m_articles.size(), m_numArticles );
60-
m_ready.store( true, std::memory_order_release );
59+
if( first )
60+
{
61+
PrintStatus( true, "Fetch: %s (%i/%i articles)", GetTitle().c_str(), (int)m_articles.size(), m_numArticles );
62+
m_ready.store( true, std::memory_order_release );
63+
}
6164
}
6265
else
6366
{

src/Handler.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Handler
2626
virtual ~Handler();
2727

2828
bool Initialize( ini_t* config );
29-
bool Fetch();
29+
bool Fetch( bool first );
3030

3131
const std::string& GetTitle() const { return m_title; }
3232
const std::string& GetDescription() const { return m_description; }
@@ -44,7 +44,7 @@ class Handler
4444

4545
protected:
4646
virtual bool InitializeImpl( ini_t* config ) = 0;
47-
virtual bool FetchImpl() = 0;
47+
virtual bool FetchImpl( bool first ) = 0;
4848

4949
std::vector<uint8_t> FetchPage( const char* url );
5050
bool PageHashChanged( const std::vector<uint8_t>& page );

0 commit comments

Comments
 (0)