|
12 | 12 | import org.slf4j.Logger; |
13 | 13 | import org.slf4j.LoggerFactory; |
14 | 14 | import org.springframework.beans.factory.annotation.Autowired; |
| 15 | +import org.springframework.beans.factory.annotation.Qualifier; |
15 | 16 | import org.springframework.scheduling.annotation.Scheduled; |
16 | 17 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; |
17 | 18 | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
18 | 19 | import org.springframework.security.core.context.SecurityContextHolder; |
19 | 20 | import org.springframework.security.oauth2.client.OAuth2RestTemplate; |
20 | 21 | import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken; |
21 | 22 | import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken; |
| 23 | +import org.springframework.stereotype.Component; |
22 | 24 | import org.springframework.util.LinkedMultiValueMap; |
23 | 25 | import org.springframework.util.MultiValueMap; |
24 | 26 |
|
25 | 27 | import com.fasterxml.jackson.databind.JsonNode; |
26 | 28 |
|
27 | | -public class ScheduledTasks { |
| 29 | +@Component |
| 30 | +public class RedditScheduler { |
28 | 31 |
|
| 32 | + @Autowired |
| 33 | + @Qualifier("schedulerRedditTemplate") |
29 | 34 | private OAuth2RestTemplate redditRestTemplate; |
30 | | - private final Logger logger = LoggerFactory.getLogger(getClass()); |
31 | 35 |
|
32 | 36 | @Autowired |
33 | 37 | private PostRepository postReopsitory; |
34 | 38 |
|
| 39 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 40 | + |
35 | 41 | @Scheduled(fixedRate = 1 * 60 * 1000) |
36 | | - public void reportCurrentTime() { |
| 42 | + public void schedulePosts() { |
37 | 43 | final List<Post> posts = postReopsitory.findBySubmissionDateBeforeAndIsSent(new Date(), false); |
38 | 44 | logger.info(posts.size() + " Posts in the queue."); |
39 | 45 | for (final Post post : posts) { |
40 | 46 | submitPost(post); |
41 | 47 | } |
| 48 | + } |
42 | 49 |
|
| 50 | + @Scheduled(fixedRate = 3 * 60 * 1000) |
| 51 | + public void checkAndReSubmitPosts() { |
43 | 52 | final List<Post> submitted = postReopsitory.findByRedditIDNotNullAndNoOfAttemptsGreaterThan(0); |
44 | 53 | logger.info(submitted.size() + " Posts to check their score"); |
45 | 54 | for (final Post post : submitted) { |
46 | | - checkIfNeedResubmit(post); |
| 55 | + checkAndReSubmit(post); |
47 | 56 | } |
48 | 57 | } |
49 | 58 |
|
@@ -113,7 +122,15 @@ private void deletePost(String redditId) { |
113 | 122 | logger.info(node.toString()); |
114 | 123 | } |
115 | 124 |
|
116 | | - private void checkIfNeedResubmit(Post post) { |
| 125 | + private void checkAndReSubmit(Post post) { |
| 126 | + try { |
| 127 | + checkAndReSubmitInternal(post); |
| 128 | + } catch (final Exception e) { |
| 129 | + logger.error("Error occurred while check post " + post.toString(), e); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private void checkAndReSubmitInternal(Post post) { |
117 | 134 | final long currentTime = new Date().getTime(); |
118 | 135 | final long interval = currentTime - post.getSubmissionDate().getTime(); |
119 | 136 | final long intervalInMinutes = TimeUnit.MINUTES.convert(interval, TimeUnit.MILLISECONDS); |
|
0 commit comments