feed controller: move feed result fetch to spring scheduler

memoizeWithExpiration will block threads if one is waiting on a result, or crashes when getting the result
This commit is contained in:
Adam
2018-10-04 09:31:57 -04:00
parent 8357a67a5a
commit d4cbe0e730

View File

@@ -24,66 +24,30 @@
*/
package net.runelite.http.service.feed;
import com.google.common.base.Suppliers;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import net.runelite.http.api.feed.FeedResult;
import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.feed.FeedItem;
import net.runelite.http.api.feed.FeedResult;
import net.runelite.http.service.feed.blog.BlogService;
import net.runelite.http.service.feed.osrsnews.OSRSNewsService;
import net.runelite.http.service.feed.twitter.TwitterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/feed")
@Slf4j
public class FeedController
{
private static final Logger logger = LoggerFactory.getLogger(FeedController.class);
private final BlogService blogService;
private final TwitterService twitterService;
private final OSRSNewsService osrsNewsService;
private BlogService blogService;
private TwitterService twitterService;
private OSRSNewsService osrsNewsService;
private final Supplier<FeedResult> feed = Suppliers.memoizeWithExpiration(() ->
{
List<FeedItem> items = new ArrayList<>();
try
{
items.addAll(blogService.getBlogPosts());
}
catch (IOException e)
{
logger.warn(null, e);
}
try
{
items.addAll(twitterService.getTweets());
}
catch (IOException e)
{
logger.warn(null, e);
}
try
{
items.addAll(osrsNewsService.getNews());
}
catch (IOException e)
{
logger.warn(null, e);
}
return new FeedResult(items);
}, 10, TimeUnit.MINUTES);
private FeedResult feedResult;
@Autowired
public FeedController(BlogService blogService, TwitterService twitterService, OSRSNewsService osrsNewsService)
@@ -93,9 +57,44 @@ public class FeedController
this.osrsNewsService = osrsNewsService;
}
@RequestMapping
public FeedResult getFeed() throws IOException
@Scheduled(fixedDelay = 10 * 60 * 1000)
public void updateFeed()
{
return feed.get();
List<FeedItem> items = new ArrayList<>();
try
{
items.addAll(blogService.getBlogPosts());
}
catch (IOException e)
{
log.warn(null, e);
}
try
{
items.addAll(twitterService.getTweets());
}
catch (IOException e)
{
log.warn(null, e);
}
try
{
items.addAll(osrsNewsService.getNews());
}
catch (IOException e)
{
log.warn(null, e);
}
feedResult = new FeedResult(items);
}
@RequestMapping
public FeedResult getFeed()
{
return feedResult;
}
}