Search in sources :

Example 1 with PluginRunnable

use of net.playeranalytics.plugin.scheduling.PluginRunnable in project Plan by plan-player-analytics.

the class DBCleanTask method register.

@Override
public void register(RunnableFactory runnableFactory) {
    PluginRunnable taskToRegister = this;
    // Secondary task for registration due to database queries.
    runnableFactory.create(() -> {
        // Distribute clean task evenly between multiple servers.
        // see https://github.com/plan-player-analytics/Plan/issues/1641 for why
        Integer biggestId = dbSystem.getDatabase().query(ServerQueries.fetchBiggestServerID());
        Integer id = serverInfo.getServer().getId().orElse(1);
        // 0 < distributor <= 1
        double distributor = id * 1.0 / biggestId;
        long distributingOverTimeMs = config.get(TimeSettings.CLEAN_DATABASE_PERIOD);
        // -40 seconds to start first at 20 seconds if only one server is present and period is 1 minute.
        long startAfterMs = (long) (distributor * distributingOverTimeMs) - TimeUnit.SECONDS.toMillis(40L);
        long delayTicks = TimeAmount.toTicks(startAfterMs, TimeUnit.MILLISECONDS);
        long periodTicks = TimeAmount.toTicks(config.get(TimeSettings.CLEAN_DATABASE_PERIOD), TimeUnit.MILLISECONDS);
        runnableFactory.create(taskToRegister).runTaskTimerAsynchronously(delayTicks, periodTicks);
    }).runTaskAsynchronously();
}
Also used : PluginRunnable(net.playeranalytics.plugin.scheduling.PluginRunnable)

Aggregations

PluginRunnable (net.playeranalytics.plugin.scheduling.PluginRunnable)1