Search in sources :

Example 11 with WTFDYUMException

use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.

the class CronServiceImpl method cron.

@Override
@Scheduled(fixedDelayString = "${wtfdyum.unfollow-check-delay}", initialDelay = 120000L)
public void cron() {
    log.debug("Starting cron method...");
    final StopWatch watch = new StopWatch();
    watch.start();
    final Set<Long> members = principalService.getMembers();
    for (final Long userId : members) {
        try {
            final Set<Feature> enabledFeatures = userService.getEnabledFeatures(userId);
            final Set<Event> events = new HashSet<>();
            for (final Feature enabledFeature : enabledFeatures) {
                final Set<Event> es = featureService.cron(userId, enabledFeature);
                events.addAll(es);
            }
            for (final Event e : events) {
                userService.addEvent(userId, e);
            }
            for (final Feature enabledFeature : enabledFeatures) {
                featureService.completeCron(userId, enabledFeature);
            }
        } catch (final WTFDYUMException e) {
            if (WTFDYUMExceptionType.GET_FOLLOWERS_RATE_LIMIT_EXCEEDED.equals(e.getType())) {
                userService.addEvent(userId, new Event(EventType.RATE_LIMIT_EXCEEDED, null));
                log.warn("GET_FOLLOWERS_RATE_LIMIT_EXCEEDED for user id {}", userId);
            } else {
                userService.addEvent(userId, new Event(EventType.TWITTER_ERROR, null));
                log.error("Twitter error for userId " + userId, e.getCause());
            }
        } catch (final Throwable t) {
            userService.addEvent(userId, new Event(EventType.UNKNOWN_ERROR, null));
            log.error("Unknown error for user id " + userId, t);
        }
    }
    watch.stop();
    log.debug("Finished cron in {} ms", watch.getTotalTimeMillis());
}
Also used : WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) Event(com.jeanchampemont.wtfdyum.dto.Event) Feature(com.jeanchampemont.wtfdyum.dto.Feature) StopWatch(org.springframework.util.StopWatch) HashSet(java.util.HashSet) Scheduled(org.springframework.scheduling.annotation.Scheduled)

Example 12 with WTFDYUMException

use of com.jeanchampemont.wtfdyum.utils.WTFDYUMException in project WTFDYUM by jchampemont.

the class TwitterServiceImpl method getUser.

@Override
public User getUser(final Principal principal, final Long id) throws WTFDYUMException {
    User result = null;
    try {
        final twitter4j.User user = twitter(principal).users().showUser(id);
        result = mapper.map(user, User.class);
    } catch (final TwitterException e) {
        log.debug("Error while getUser", e);
        throw new WTFDYUMException(e, WTFDYUMExceptionType.TWITTER_ERROR);
    }
    return result;
}
Also used : User(com.jeanchampemont.wtfdyum.dto.User) WTFDYUMException(com.jeanchampemont.wtfdyum.utils.WTFDYUMException) twitter4j(twitter4j)

Aggregations

WTFDYUMException (com.jeanchampemont.wtfdyum.utils.WTFDYUMException)12 Test (org.junit.Test)5 Event (com.jeanchampemont.wtfdyum.dto.Event)3 Feature (com.jeanchampemont.wtfdyum.dto.Feature)3 Principal (com.jeanchampemont.wtfdyum.dto.Principal)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 User (com.jeanchampemont.wtfdyum.dto.User)2 Secured (com.jeanchampemont.wtfdyum.security.Secured)2 ResponseListMockForTest (com.jeanchampemont.wtfdyum.utils.ResponseListMockForTest)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 twitter4j (twitter4j)2 AdminService (com.jeanchampemont.wtfdyum.service.AdminService)1 AuthenticationService (com.jeanchampemont.wtfdyum.service.AuthenticationService)1 PrincipalService (com.jeanchampemont.wtfdyum.service.PrincipalService)1 TwitterService (com.jeanchampemont.wtfdyum.service.TwitterService)1 SessionManager (com.jeanchampemont.wtfdyum.utils.SessionManager)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Random (java.util.Random)1