Files
Athou_commafeed/src/main/java/com/commafeed/backend/feed/FeedQueues.java

161 lines
5.0 KiB
Java
Raw Normal View History

package com.commafeed.backend.feed;
2014-12-12 10:19:32 +01:00
import java.util.ArrayList;
import java.util.Date;
2014-12-12 10:19:32 +01:00
import java.util.LinkedHashMap;
import java.util.List;
2013-05-28 12:55:47 +02:00
import java.util.Map;
import java.util.Queue;
2014-12-12 10:19:32 +01:00
import java.util.concurrent.ConcurrentLinkedQueue;
2014-12-18 14:09:15 +01:00
import java.util.stream.Collectors;
import javax.inject.Inject;
2014-08-17 14:16:30 +02:00
import javax.inject.Singleton;
2013-07-02 14:33:53 +02:00
import org.apache.commons.codec.digest.DigestUtils;
2014-10-28 16:36:09 +01:00
import org.apache.commons.lang3.time.DateUtils;
2015-02-23 15:33:52 +01:00
import org.hibernate.SessionFactory;
2013-12-10 14:02:06 +01:00
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.commafeed.CommaFeedConfiguration;
import com.commafeed.backend.dao.FeedDAO;
2015-02-23 15:33:52 +01:00
import com.commafeed.backend.dao.UnitOfWork;
import com.commafeed.backend.model.Feed;
2014-08-17 14:16:30 +02:00
@Singleton
public class FeedQueues {
2015-02-23 15:33:52 +01:00
private SessionFactory sessionFactory;
private final FeedDAO feedDAO;
private final CommaFeedConfiguration config;
2013-05-21 16:40:37 +02:00
2014-12-12 10:19:32 +01:00
private Queue<FeedRefreshContext> addQueue = new ConcurrentLinkedQueue<>();
private Queue<FeedRefreshContext> takeQueue = new ConcurrentLinkedQueue<>();
private Queue<Feed> giveBackQueue = new ConcurrentLinkedQueue<>();
2013-04-16 12:36:36 +02:00
2013-08-18 17:19:01 +02:00
private Meter refill;
@Inject
2015-02-23 15:33:52 +01:00
public FeedQueues(SessionFactory sessionFactory, FeedDAO feedDAO, CommaFeedConfiguration config, MetricRegistry metrics) {
this.sessionFactory = sessionFactory;
this.config = config;
this.feedDAO = feedDAO;
2013-08-18 17:19:01 +02:00
refill = metrics.meter(MetricRegistry.name(getClass(), "refill"));
2013-12-10 14:02:06 +01:00
metrics.register(MetricRegistry.name(getClass(), "addQueue"), new Gauge<Integer>() {
@Override
public Integer getValue() {
return addQueue.size();
}
});
metrics.register(MetricRegistry.name(getClass(), "takeQueue"), new Gauge<Integer>() {
@Override
public Integer getValue() {
return takeQueue.size();
}
});
metrics.register(MetricRegistry.name(getClass(), "giveBackQueue"), new Gauge<Integer>() {
@Override
public Integer getValue() {
return giveBackQueue.size();
}
});
}
2013-07-29 09:45:31 +02:00
/**
* take a feed from the refresh queue
*/
public synchronized FeedRefreshContext take() {
FeedRefreshContext context = takeQueue.poll();
2013-05-28 12:55:47 +02:00
if (context == null) {
2015-02-23 15:33:52 +01:00
UnitOfWork.run(sessionFactory, () -> refill());
context = takeQueue.poll();
2013-05-30 13:18:26 +02:00
}
return context;
}
2013-07-29 09:45:31 +02:00
/**
* add a feed to the refresh queue
*/
public void add(Feed feed, boolean urgent) {
int refreshInterval = config.getApplicationSettings().getRefreshIntervalMinutes();
2013-07-29 10:33:18 +02:00
if (feed.getLastUpdated() == null || feed.getLastUpdated().before(DateUtils.addMinutes(new Date(), -1 * refreshInterval))) {
2014-12-18 10:13:44 +01:00
boolean alreadyQueued = addQueue.stream().anyMatch(c -> c.getFeed().getId().equals(feed.getId()));
2014-08-19 00:43:23 +02:00
if (!alreadyQueued) {
addQueue.add(new FeedRefreshContext(feed, urgent));
}
2013-05-30 13:18:26 +02:00
}
}
2013-05-21 16:40:37 +02:00
2013-07-29 09:45:31 +02:00
/**
* refills the refresh queue and empties the giveBack queue while at it
*/
2013-05-30 13:18:26 +02:00
private void refill() {
2013-08-18 17:19:01 +02:00
refill.mark();
2013-07-29 09:45:31 +02:00
2014-12-12 10:19:32 +01:00
List<FeedRefreshContext> contexts = new ArrayList<>();
int batchSize = Math.min(100, 3 * config.getApplicationSettings().getBackgroundThreads());
// add feeds we got from the add() method
int addQueueSize = addQueue.size();
for (int i = 0; i < Math.min(batchSize, addQueueSize); i++) {
contexts.add(addQueue.poll());
2013-07-05 08:23:18 +02:00
}
2013-05-22 00:07:13 +02:00
// add feeds that are up to refresh from the database
2014-08-09 19:35:05 +02:00
int count = batchSize - contexts.size();
if (count > 0) {
List<Feed> feeds = feedDAO.findNextUpdatable(count, getLastLoginThreshold());
for (Feed feed : feeds) {
contexts.add(new FeedRefreshContext(feed, false));
}
2013-05-30 13:18:26 +02:00
}
2013-05-22 10:39:03 +02:00
// set the disabledDate as we use it in feedDAO to decide what to refresh next. We also use a map to remove
2013-07-29 09:45:31 +02:00
// duplicates.
2014-12-12 10:19:32 +01:00
Map<Long, FeedRefreshContext> map = new LinkedHashMap<>();
for (FeedRefreshContext context : contexts) {
Feed feed = context.getFeed();
feed.setDisabledUntil(DateUtils.addMinutes(new Date(), config.getApplicationSettings().getRefreshIntervalMinutes()));
map.put(feed.getId(), context);
}
2013-07-29 09:45:31 +02:00
// refill the queue
2013-05-30 13:18:26 +02:00
takeQueue.addAll(map.values());
2013-07-29 09:45:31 +02:00
// add feeds from the giveBack queue to the map, overriding duplicates
int giveBackQueueSize = giveBackQueue.size();
for (int i = 0; i < giveBackQueueSize; i++) {
Feed feed = giveBackQueue.poll();
map.put(feed.getId(), new FeedRefreshContext(feed, false));
}
2013-05-30 13:18:26 +02:00
2013-07-29 09:45:31 +02:00
// update all feeds in the database
2014-12-18 14:09:15 +01:00
List<Feed> feeds = map.values().stream().map(c -> c.getFeed()).collect(Collectors.toList());
feedDAO.merge(feeds);
}
2013-04-16 09:29:33 +02:00
2013-07-29 09:45:31 +02:00
/**
* give a feed back, updating it to the database during the next refill()
*/
2013-05-22 00:07:13 +02:00
public void giveBack(Feed feed) {
2013-07-02 14:33:53 +02:00
String normalized = FeedUtils.normalizeURL(feed.getUrl());
feed.setNormalizedUrl(normalized);
feed.setNormalizedUrlHash(DigestUtils.sha1Hex(normalized));
feed.setLastUpdated(new Date());
2013-05-22 00:07:13 +02:00
giveBackQueue.add(feed);
}
private Date getLastLoginThreshold() {
2014-12-04 10:52:41 +01:00
if (config.getApplicationSettings().getHeavyLoad()) {
return DateUtils.addDays(new Date(), -30);
} else {
return null;
}
}
}