Automatically push updates to other servers via the messaging service when commands are ran

This commit is contained in:
Luck 2017-03-21 21:54:34 +00:00
parent f8ad562b95
commit c36b0d2975
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
15 changed files with 128 additions and 4 deletions

View File

@ -60,6 +60,7 @@ import me.lucko.luckperms.common.managers.impl.GenericGroupManager;
import me.lucko.luckperms.common.managers.impl.GenericTrackManager; import me.lucko.luckperms.common.managers.impl.GenericTrackManager;
import me.lucko.luckperms.common.managers.impl.GenericUserManager; import me.lucko.luckperms.common.managers.impl.GenericUserManager;
import me.lucko.luckperms.common.messaging.InternalMessagingService; import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.common.messaging.NoopMessagingService;
import me.lucko.luckperms.common.messaging.RedisMessaging; import me.lucko.luckperms.common.messaging.RedisMessaging;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.Storage; import me.lucko.luckperms.common.storage.Storage;
@ -217,6 +218,10 @@ public class LPBukkitPlugin extends JavaPlugin implements LuckPermsPlugin {
getLog().warn("Messaging service '" + messagingType + "' not recognised."); getLog().warn("Messaging service '" + messagingType + "' not recognised.");
} }
if (messagingService == null) {
messagingService = new NoopMessagingService();
}
// setup the update task buffer // setup the update task buffer
updateTaskBuffer = new BufferedRequest<Void>(1000L, this::doAsync) { updateTaskBuffer = new BufferedRequest<Void>(1000L, this::doAsync) {
@Override @Override

View File

@ -258,6 +258,9 @@ data:
# none ==> nothing # none ==> nothing
messaging-service: none messaging-service: none
# If LuckPerms should automatically push updates after a change has been made with a command.
auto-push-updates: true
# Settings for Redis. # Settings for Redis.
# Port 6379 is used by default; set address to "host:port" if differs # Port 6379 is used by default; set address to "host:port" if differs
redis: redis:

View File

@ -52,6 +52,7 @@ import me.lucko.luckperms.common.managers.impl.GenericGroupManager;
import me.lucko.luckperms.common.managers.impl.GenericTrackManager; import me.lucko.luckperms.common.managers.impl.GenericTrackManager;
import me.lucko.luckperms.common.managers.impl.GenericUserManager; import me.lucko.luckperms.common.managers.impl.GenericUserManager;
import me.lucko.luckperms.common.messaging.InternalMessagingService; import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.common.messaging.NoopMessagingService;
import me.lucko.luckperms.common.messaging.RedisMessaging; import me.lucko.luckperms.common.messaging.RedisMessaging;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.plugin.LuckPermsScheduler; import me.lucko.luckperms.common.plugin.LuckPermsScheduler;
@ -162,6 +163,10 @@ public class LPBungeePlugin extends Plugin implements LuckPermsPlugin {
getLog().warn("Messaging service '" + messagingType + "' not recognised."); getLog().warn("Messaging service '" + messagingType + "' not recognised.");
} }
if (messagingService == null) {
messagingService = new NoopMessagingService();
}
// setup the update task buffer // setup the update task buffer
updateTaskBuffer = new BufferedRequest<Void>(1000L, this::doAsync) { updateTaskBuffer = new BufferedRequest<Void>(1000L, this::doAsync) {
@Override @Override

View File

@ -199,6 +199,9 @@ data:
# none ==> nothing # none ==> nothing
messaging-service: none messaging-service: none
# If LuckPerms should automatically push updates after a change has been made with a command.
auto-push-updates: true
# Settings for Redis. # Settings for Redis.
# Port 6379 is used by default; set address to "host:port" if differs # Port 6379 is used by default; set address to "host:port" if differs
redis: redis:

View File

@ -46,6 +46,8 @@ import me.lucko.luckperms.common.core.NodeBuilder;
import me.lucko.luckperms.common.core.UserIdentifier; import me.lucko.luckperms.common.core.UserIdentifier;
import me.lucko.luckperms.common.event.EventFactory; import me.lucko.luckperms.common.event.EventFactory;
import me.lucko.luckperms.common.event.LuckPermsEventBus; import me.lucko.luckperms.common.event.LuckPermsEventBus;
import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.common.messaging.NoopMessagingService;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import java.util.Optional; import java.util.Optional;
@ -103,7 +105,8 @@ public class ApiProvider implements LuckPermsApi {
@Override @Override
public Optional<MessagingService> getMessagingService() { public Optional<MessagingService> getMessagingService() {
return Optional.ofNullable(plugin.getMessagingService()); InternalMessagingService service = plugin.getMessagingService();
return service instanceof NoopMessagingService ? Optional.empty() : Optional.of(service);
} }
@Override @Override

View File

@ -29,11 +29,14 @@ import com.google.common.base.Splitter;
import me.lucko.luckperms.common.commands.Arg; import me.lucko.luckperms.common.commands.Arg;
import me.lucko.luckperms.common.commands.sender.Sender; import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.commands.utils.Util; import me.lucko.luckperms.common.commands.utils.Util;
import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.core.model.Group; import me.lucko.luckperms.common.core.model.Group;
import me.lucko.luckperms.common.core.model.Track; import me.lucko.luckperms.common.core.model.Track;
import me.lucko.luckperms.common.core.model.User; import me.lucko.luckperms.common.core.model.User;
import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.common.messaging.NoopMessagingService;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.treeview.PermissionVault; import me.lucko.luckperms.common.treeview.PermissionVault;
import me.lucko.luckperms.common.treeview.TreeNode; import me.lucko.luckperms.common.treeview.TreeNode;
@ -174,6 +177,11 @@ public abstract class SubCommand<T> extends Command<T, Void> {
user.getRefreshBuffer().requestDirectly(); user.getRefreshBuffer().requestDirectly();
} }
InternalMessagingService messagingService = plugin.getMessagingService();
if (!sender.isImport() && !(messagingService instanceof NoopMessagingService) && plugin.getConfiguration().get(ConfigKeys.AUTO_PUSH_UPDATES)) {
messagingService.getUpdateBuffer().request();
}
if (success) { if (success) {
Message.USER_SAVE_SUCCESS.send(sender); Message.USER_SAVE_SUCCESS.send(sender);
} else { } else {
@ -190,6 +198,11 @@ public abstract class SubCommand<T> extends Command<T, Void> {
plugin.getUpdateTaskBuffer().requestDirectly(); plugin.getUpdateTaskBuffer().requestDirectly();
} }
InternalMessagingService messagingService = plugin.getMessagingService();
if (!sender.isImport() && !(messagingService instanceof NoopMessagingService) && plugin.getConfiguration().get(ConfigKeys.AUTO_PUSH_UPDATES)) {
messagingService.getUpdateBuffer().request();
}
if (success) { if (success) {
Message.GROUP_SAVE_SUCCESS.send(sender); Message.GROUP_SAVE_SUCCESS.send(sender);
} else { } else {
@ -206,6 +219,11 @@ public abstract class SubCommand<T> extends Command<T, Void> {
plugin.getUpdateTaskBuffer().requestDirectly(); plugin.getUpdateTaskBuffer().requestDirectly();
} }
InternalMessagingService messagingService = plugin.getMessagingService();
if (!sender.isImport() && !(messagingService instanceof NoopMessagingService) && plugin.getConfiguration().get(ConfigKeys.AUTO_PUSH_UPDATES)) {
messagingService.getUpdateBuffer().request();
}
if (success) { if (success) {
Message.TRACK_SAVE_SUCCESS.send(sender); Message.TRACK_SAVE_SUCCESS.send(sender);
} else { } else {

View File

@ -30,6 +30,7 @@ import me.lucko.luckperms.common.config.ConfigKeys;
import me.lucko.luckperms.common.config.LuckPermsConfiguration; import me.lucko.luckperms.common.config.LuckPermsConfiguration;
import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.messaging.NoopMessagingService;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.Predicates;
@ -66,7 +67,7 @@ public class InfoCommand extends SingleCommand {
plugin.getStorage().getName(), plugin.getStorage().getName(),
c.get(ConfigKeys.SERVER), c.get(ConfigKeys.SERVER),
c.get(ConfigKeys.SYNC_TIME), c.get(ConfigKeys.SYNC_TIME),
plugin.getMessagingService() == null ? "None" : plugin.getMessagingService().getName(), plugin.getMessagingService() instanceof NoopMessagingService ? "None" : plugin.getMessagingService().getName(),
plugin.getPlayerCount(), plugin.getPlayerCount(),
plugin.getUserManager().getAll().size(), plugin.getUserManager().getAll().size(),
plugin.getGroupManager().getAll().size(), plugin.getGroupManager().getAll().size(),

View File

@ -28,6 +28,7 @@ import me.lucko.luckperms.common.commands.sender.Sender;
import me.lucko.luckperms.common.constants.Message; import me.lucko.luckperms.common.constants.Message;
import me.lucko.luckperms.common.constants.Permission; import me.lucko.luckperms.common.constants.Permission;
import me.lucko.luckperms.common.messaging.InternalMessagingService; import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.common.messaging.NoopMessagingService;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.Predicates; import me.lucko.luckperms.common.utils.Predicates;
@ -47,7 +48,7 @@ public class NetworkSyncCommand extends SingleCommand {
InternalMessagingService messagingService = plugin.getMessagingService(); InternalMessagingService messagingService = plugin.getMessagingService();
if (messagingService == null) { if (messagingService instanceof NoopMessagingService) {
Message.UPDATE_TASK_PUSH_FAILURE_NOT_SETUP.send(sender); Message.UPDATE_TASK_PUSH_FAILURE_NOT_SETUP.send(sender);
return CommandResult.FAILURE; return CommandResult.FAILURE;
} }

View File

@ -180,6 +180,7 @@ public class ConfigKeys {
.build(); .build();
})); }));
public static final ConfigKey<String> MESSAGING_SERVICE = EnduringKey.wrap(StringKey.of("messaging-service", "none")); public static final ConfigKey<String> MESSAGING_SERVICE = EnduringKey.wrap(StringKey.of("messaging-service", "none"));
public static final ConfigKey<Boolean> AUTO_PUSH_UPDATES = EnduringKey.wrap(BooleanKey.of("auto-push-updates", true));
public static final ConfigKey<Boolean> REDIS_ENABLED = EnduringKey.wrap(BooleanKey.of("redis.enabled", false)); public static final ConfigKey<Boolean> REDIS_ENABLED = EnduringKey.wrap(BooleanKey.of("redis.enabled", false));
public static final ConfigKey<String> REDIS_ADDRESS = EnduringKey.wrap(StringKey.of("redis.address", null)); public static final ConfigKey<String> REDIS_ADDRESS = EnduringKey.wrap(StringKey.of("redis.address", null));
public static final ConfigKey<String> REDIS_PASSWORD = EnduringKey.wrap(StringKey.of("redis.password", "")); public static final ConfigKey<String> REDIS_PASSWORD = EnduringKey.wrap(StringKey.of("redis.password", ""));

View File

@ -26,6 +26,7 @@ import lombok.Getter;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.BufferedRequest;
import java.util.Collections; import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
@ -40,6 +41,7 @@ import java.util.function.Consumer;
public abstract class AbstractMessagingService implements InternalMessagingService { public abstract class AbstractMessagingService implements InternalMessagingService {
public static final String CHANNEL = "lpuc"; public static final String CHANNEL = "lpuc";
@Getter
private final LuckPermsPlugin plugin; private final LuckPermsPlugin plugin;
@Getter @Getter
@ -47,6 +49,15 @@ public abstract class AbstractMessagingService implements InternalMessagingServi
private final Set<UUID> receivedMsgs = Collections.synchronizedSet(new HashSet<>()); private final Set<UUID> receivedMsgs = Collections.synchronizedSet(new HashSet<>());
@Getter
private final BufferedRequest<Void> updateBuffer = new BufferedRequest<Void>(10000L, r -> getPlugin().doAsync(r)) {
@Override
protected Void perform() {
pushUpdate();
return null;
}
};
protected abstract void sendMessage(String channel, String message); protected abstract void sendMessage(String channel, String message);
protected void onMessage(String channel, String msg, Consumer<UUID> callback) { protected void onMessage(String channel, String msg, Consumer<UUID> callback) {

View File

@ -23,10 +23,27 @@
package me.lucko.luckperms.common.messaging; package me.lucko.luckperms.common.messaging;
import me.lucko.luckperms.api.MessagingService; import me.lucko.luckperms.api.MessagingService;
import me.lucko.luckperms.common.utils.BufferedRequest;
public interface InternalMessagingService extends MessagingService { public interface InternalMessagingService extends MessagingService {
/**
* Gets the name of this messaging service
*
* @return the name of this messaging service
*/
String getName(); String getName();
/**
* Closes the messaging service
*/
void close(); void close();
/**
* Gets the buffer for sending updates to other servers
*
* @return the update buffer
*/
BufferedRequest<Void> getUpdateBuffer();
} }

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.messaging;
import me.lucko.luckperms.common.utils.BufferedRequest;
public class NoopMessagingService implements InternalMessagingService {
@Override
public String getName() {
return "No op";
}
@Override
public void close() {
}
@Override
public BufferedRequest<Void> getUpdateBuffer() {
return null;
}
@Override
public void pushUpdate() {
}
}

View File

@ -100,7 +100,7 @@ public interface LuckPermsPlugin {
Storage getStorage(); Storage getStorage();
/** /**
* Gets the redis messaging instance if present. Could return null if redis is not enabled. * Gets the redis messaging instance.
* *
* @return the redis messaging service * @return the redis messaging service
*/ */

View File

@ -49,6 +49,7 @@ import me.lucko.luckperms.common.locale.SimpleLocaleManager;
import me.lucko.luckperms.common.managers.TrackManager; import me.lucko.luckperms.common.managers.TrackManager;
import me.lucko.luckperms.common.managers.impl.GenericTrackManager; import me.lucko.luckperms.common.managers.impl.GenericTrackManager;
import me.lucko.luckperms.common.messaging.InternalMessagingService; import me.lucko.luckperms.common.messaging.InternalMessagingService;
import me.lucko.luckperms.common.messaging.NoopMessagingService;
import me.lucko.luckperms.common.messaging.RedisMessaging; import me.lucko.luckperms.common.messaging.RedisMessaging;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.plugin.LuckPermsScheduler; import me.lucko.luckperms.common.plugin.LuckPermsScheduler;
@ -222,6 +223,10 @@ public class LPSpongePlugin implements LuckPermsPlugin {
getLog().warn("Messaging service '" + messagingType + "' not recognised."); getLog().warn("Messaging service '" + messagingType + "' not recognised.");
} }
if (messagingService == null) {
messagingService = new NoopMessagingService();
}
// setup the update task buffer // setup the update task buffer
updateTaskBuffer = new BufferedRequest<Void>(1000L, this::doAsync) { updateTaskBuffer = new BufferedRequest<Void>(1000L, this::doAsync) {
@Override @Override

View File

@ -209,6 +209,9 @@ data {
# none ==> nothing # none ==> nothing
messaging-service="none" messaging-service="none"
# If LuckPerms should automatically push updates after a change has been made with a command.
auto-push-updates=true
# Settings for Redis. # Settings for Redis.
# Port 6379 is used by default; set address to "host:port" if differs # Port 6379 is used by default; set address to "host:port" if differs
redis { redis {