Search in sources :

Example 6 with Feature

use of com.jeanchampemont.wtfdyum.dto.Feature 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)

Aggregations

Feature (com.jeanchampemont.wtfdyum.dto.Feature)6 HashMap (java.util.HashMap)3 Event (com.jeanchampemont.wtfdyum.dto.Event)2 WTFDYUMException (com.jeanchampemont.wtfdyum.utils.WTFDYUMException)2 Secured (com.jeanchampemont.wtfdyum.security.Secured)1 FeatureStrategy (com.jeanchampemont.wtfdyum.service.feature.FeatureStrategy)1 NotifyUnfollowFeatureStrategy (com.jeanchampemont.wtfdyum.service.feature.impl.NotifyUnfollowFeatureStrategy)1 FeatureServiceImpl (com.jeanchampemont.wtfdyum.service.impl.FeatureServiceImpl)1 HashSet (java.util.HashSet)1 Before (org.junit.Before)1 Test (org.junit.Test)1 Bean (org.springframework.context.annotation.Bean)1 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)1 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)1 Scheduled (org.springframework.scheduling.annotation.Scheduled)1 StopWatch (org.springframework.util.StopWatch)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ModelAndView (org.springframework.web.servlet.ModelAndView)1