build disabledUntil date for feeds with support for http 304

This commit is contained in:
Athou
2013-05-24 12:01:00 +02:00
parent f5cae4743e
commit d148fa9df7
2 changed files with 18 additions and 10 deletions

View File

@@ -1,8 +1,8 @@
package com.commafeed.backend.feeds;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.inject.Inject;
@@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
import com.commafeed.backend.HttpGetter.NotModifiedException;
import com.commafeed.backend.MetricsBean;
import com.commafeed.backend.dao.FeedEntryDAO;
import com.commafeed.backend.model.Feed;
import com.commafeed.backend.model.FeedEntry;
import com.commafeed.backend.model.FeedEntryContent;
@@ -43,6 +44,9 @@ public class FeedRefreshWorker {
@Inject
MetricsBean metricsBean;
@Inject
FeedEntryDAO feedEntryDAO;
public void start(MutableBoolean running, String threadName) {
log.info("{} starting", threadName);
@@ -83,7 +87,7 @@ public class FeedRefreshWorker {
private void update(Feed feed) {
FetchedFeed fetchedFeed = null;
Collection<FeedEntry> entries = null;
List<FeedEntry> entries = null;
String message = null;
int errorCount = 0;
@@ -96,7 +100,8 @@ public class FeedRefreshWorker {
// thrown
entries = fetchedFeed.getEntries();
if (applicationSettingsService.get().isHeavyLoad()) {
disabledUntil = FeedUtils.buildDisabledUntil(fetchedFeed);
disabledUntil = FeedUtils.buildDisabledUntil(
fetchedFeed.getPublishedDate(), entries);
}
feed.setLastUpdateSuccess(Calendar.getInstance().getTime());
@@ -112,10 +117,13 @@ public class FeedRefreshWorker {
} catch (NotModifiedException e) {
log.debug("Feed not modified (304) : " + feed.getUrl());
if (feed.getErrorCount() == 0) {
// not modified and had no error before, do nothing
return;
List<FeedEntry> feedEntries = feedEntryDAO.findByFeed(feed, 0, 10);
Date publishedDate = null;
if (feedEntries.size() > 0) {
publishedDate = feedEntries.get(0).getInserted();
}
feed.setDisabledUntil(FeedUtils.buildDisabledUntil(publishedDate,
feedEntries));
} catch (Exception e) {
message = "Unable to refresh feed " + feed.getUrl() + " : "
+ e.getMessage();

View File

@@ -134,9 +134,9 @@ public class FeedUtils {
/**
* When the feed was refreshed successfully
*/
public static Date buildDisabledUntil(FetchedFeed feed) {
public static Date buildDisabledUntil(Date publishedDate,
List<FeedEntry> entries) {
Date now = Calendar.getInstance().getTime();
Date publishedDate = feed.getPublishedDate();
if (publishedDate == null) {
// feed with no entries, recheck in 24 hours
@@ -150,9 +150,9 @@ public class FeedUtils {
} else if (publishedDate.before(DateUtils.addDays(now, -7))) {
// older than a week, recheck in 6 hours
return DateUtils.addHours(now, 6);
} else if (CollectionUtils.isNotEmpty(feed.getEntries())) {
} else if (CollectionUtils.isNotEmpty(entries)) {
// use average time between entries to decide when to refresh next
long average = averageTimeBetweenEntries(feed.getEntries());
long average = averageTimeBetweenEntries(entries);
return new Date(Math.min(DateUtils.addHours(now, 6).getTime(),
now.getTime() + average / 3));
} else {