Search in sources :

Example 1 with Periodical

use of org.graylog2.plugin.periodical.Periodical in project graylog2-server by Graylog2.

the class PeriodicalsTest method testExceptionIsNotUncaught.

@Test
public void testExceptionIsNotUncaught() {
    final Logger logger = mock(Logger.class);
    final Periodical periodical1 = new Periodical() {

        @Override
        public boolean runsForever() {
            return false;
        }

        @Override
        public boolean stopOnGracefulShutdown() {
            return false;
        }

        @Override
        public boolean masterOnly() {
            return false;
        }

        @Override
        public boolean startOnThisNode() {
            return true;
        }

        @Override
        public boolean isDaemon() {
            return false;
        }

        @Override
        public int getInitialDelaySeconds() {
            return 0;
        }

        @Override
        public int getPeriodSeconds() {
            return 1;
        }

        @Override
        protected Logger getLogger() {
            return logger;
        }

        @Override
        public void doRun() {
            throw new NullPointerException();
        }
    };
    periodical1.run();
    // the uncaught exception from doRun should have been logged
    verify(logger, atLeastOnce()).error(anyString(), any(Throwable.class));
}
Also used : Periodical(org.graylog2.plugin.periodical.Periodical) Logger(org.slf4j.Logger) Test(org.junit.Test)

Example 2 with Periodical

use of org.graylog2.plugin.periodical.Periodical in project graylog2-server by Graylog2.

the class PeriodicalsService method startUp.

@Override
protected void startUp() throws Exception {
    LOG.info("Starting {} periodicals ...", periodicalSet.size());
    for (Periodical periodical : periodicalSet) {
        try {
            periodical.initialize();
            if (periodical.masterOnly() && !serverStatus.hasCapability(ServerStatus.Capability.MASTER)) {
                LOG.info("Not starting [{}] periodical. Only started on Graylog master nodes.", periodical.getClass().getCanonicalName());
                continue;
            }
            if (!periodical.startOnThisNode()) {
                LOG.info("Not starting [{}] periodical. Not configured to run on this node.", periodical.getClass().getCanonicalName());
                continue;
            }
            // Register and start.
            periodicals.registerAndStart(periodical);
        } catch (Exception e) {
            LOG.error("Could not initialize periodical.", e);
        }
    }
}
Also used : Periodical(org.graylog2.plugin.periodical.Periodical)

Example 3 with Periodical

use of org.graylog2.plugin.periodical.Periodical in project graylog2-server by Graylog2.

the class Periodicals method registerAndStart.

public synchronized void registerAndStart(Periodical periodical) {
    if (periodical.runsForever()) {
        LOG.info("Starting [{}] periodical, running forever.", periodical.getClass().getCanonicalName());
        for (int i = 0; i < periodical.getParallelism(); i++) {
            Thread t = new Thread(periodical);
            t.setDaemon(periodical.isDaemon());
            t.setName("periodical-" + periodical.getClass().getCanonicalName() + "-" + i);
            t.setUncaughtExceptionHandler(new Tools.LogUncaughtExceptionHandler(LOG));
            t.start();
        }
    } else {
        LOG.info("Starting [{}] periodical in [{}s], polling every [{}s].", periodical.getClass().getCanonicalName(), periodical.getInitialDelaySeconds(), periodical.getPeriodSeconds());
        ScheduledExecutorService scheduler = periodical.isDaemon() ? this.daemonScheduler : this.scheduler;
        ScheduledFuture<?> future = scheduler.scheduleAtFixedRate(periodical, periodical.getInitialDelaySeconds(), periodical.getPeriodSeconds(), TimeUnit.SECONDS);
        futures.put(periodical, future);
    }
    periodicals.add(periodical);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Tools(org.graylog2.plugin.Tools)

Example 4 with Periodical

use of org.graylog2.plugin.periodical.Periodical in project graylog2-server by Graylog2.

the class PeriodicalsService method shutDown.

@Override
protected void shutDown() throws Exception {
    for (Periodical periodical : periodicals.getAllStoppedOnGracefulShutdown()) {
        LOG.info("Shutting down periodical [{}].", periodical.getClass().getCanonicalName());
        Stopwatch s = Stopwatch.createStarted();
        // Cancel future executions.
        Map<Periodical, ScheduledFuture> futures = periodicals.getFutures();
        if (futures.containsKey(periodical)) {
            futures.get(periodical).cancel(false);
            s.stop();
            LOG.info("Shutdown of periodical [{}] complete, took <{}ms>.", periodical.getClass().getCanonicalName(), s.elapsed(TimeUnit.MILLISECONDS));
        } else {
            LOG.error("Could not find periodical [{}] in futures list. Not stopping execution.", periodical.getClass().getCanonicalName());
        }
    }
}
Also used : Periodical(org.graylog2.plugin.periodical.Periodical) Stopwatch(com.google.common.base.Stopwatch) ScheduledFuture(java.util.concurrent.ScheduledFuture)

Example 5 with Periodical

use of org.graylog2.plugin.periodical.Periodical in project graylog2-server by Graylog2.

the class PeriodicalsTest method testGetAllStoppedOnGracefulShutdown.

@Test
public void testGetAllStoppedOnGracefulShutdown() throws Exception {
    final Periodical periodical2 = mock(Periodical.class);
    when(periodical2.stopOnGracefulShutdown()).thenReturn(true);
    periodicals.registerAndStart(periodical);
    periodicals.registerAndStart(periodical2);
    List<Periodical> allStoppedOnGracefulShutdown = periodicals.getAllStoppedOnGracefulShutdown();
    assertFalse("periodical without graceful shutdown is in the list", allStoppedOnGracefulShutdown.contains(periodical));
    assertTrue("graceful shutdown periodical is not in the list", allStoppedOnGracefulShutdown.contains(periodical2));
    assertEquals("more graceful shutdown periodicals in the list", allStoppedOnGracefulShutdown.size(), 1);
}
Also used : Periodical(org.graylog2.plugin.periodical.Periodical) Test(org.junit.Test)

Aggregations

Periodical (org.graylog2.plugin.periodical.Periodical)5 Test (org.junit.Test)3 Stopwatch (com.google.common.base.Stopwatch)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 ScheduledFuture (java.util.concurrent.ScheduledFuture)1 Tools (org.graylog2.plugin.Tools)1 Logger (org.slf4j.Logger)1