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; package net.runelite.http.service.feed;
import com.google.common.base.Suppliers;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import lombok.extern.slf4j.Slf4j;
import java.util.function.Supplier;
import net.runelite.http.api.feed.FeedResult;
import net.runelite.http.api.feed.FeedItem; 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.blog.BlogService;
import net.runelite.http.service.feed.osrsnews.OSRSNewsService; import net.runelite.http.service.feed.osrsnews.OSRSNewsService;
import net.runelite.http.service.feed.twitter.TwitterService; 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.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@RestController @RestController
@RequestMapping("/feed") @RequestMapping("/feed")
@Slf4j
public class FeedController 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 FeedResult feedResult;
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);
@Autowired @Autowired
public FeedController(BlogService blogService, TwitterService twitterService, OSRSNewsService osrsNewsService) public FeedController(BlogService blogService, TwitterService twitterService, OSRSNewsService osrsNewsService)
@@ -93,9 +57,44 @@ public class FeedController
this.osrsNewsService = osrsNewsService; this.osrsNewsService = osrsNewsService;
} }
@RequestMapping @Scheduled(fixedDelay = 10 * 60 * 1000)
public FeedResult getFeed() throws IOException 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;
} }
} }