reorganized app shutdown

This commit is contained in:
gnembon committed 2020-10-24 22:59:16 -03:00
1 parent 26a15d7c8d
commit a6286850b4
2 files changed
+20 -45

No files matched your search

@@ -337,46 +337,34 @@ public class CarpetScriptHost extends ScriptHost
@Override @Override
public void onClose() public void onClose()
{ {
inTermination = true; super.onClose();
FunctionValue closing = getFunction("__on_close"); FunctionValue closing = getFunction("__on_close");
if (closing != null) if (closing != null)
{ {
ServerPlayerEntity player = (user==null)?null:scriptServer.server.getPlayerManager().getPlayer(user);
ServerCommandSource source = (player != null)?player.getCommandSource():scriptServer.server.getCommandSource();
try try
{ {
callUDF(BlockPos.ORIGIN, scriptServer.server.getCommandSource(), closing, Collections.emptyList()); callUDF(BlockPos.ORIGIN, source, closing, Collections.emptyList());
} }
catch (InvalidCallbackException ignored) catch (InvalidCallbackException ignored)
{ {
} }
} }
userHosts.forEach((key, value) -> if (user == null)
{ {
FunctionValue userClosing = value.getFunction("__on_close");
if (userClosing != null)
{
ServerPlayerEntity player = scriptServer.server.getPlayerManager().getPlayer(key);
ServerCommandSource source = (player != null)?player.getCommandSource():scriptServer.server.getCommandSource();
try
{
((CarpetScriptHost) value).callUDF(BlockPos.ORIGIN, source, userClosing, Collections.emptyList());
}
catch (InvalidCallbackException ignored)
{
}
}
});
String markerName = Auxiliary.MARKER_STRING+"_"+((getName()==null)?"":getName()); String markerName = Auxiliary.MARKER_STRING + "_" + ((getName() == null) ? "" : getName());
for (ServerWorld world : scriptServer.server.getWorlds()) for (ServerWorld world : scriptServer.server.getWorlds())
{
for (Entity e : world.getEntitiesByType(EntityType.ARMOR_STAND, (as) -> as.getScoreboardTags().contains(markerName)))
{ {
e.remove(); for (Entity e : world.getEntitiesByType(EntityType.ARMOR_STAND, (as) -> as.getScoreboardTags().contains(markerName)))
{
e.remove();
}
} }
if (this.saveTimeout > 0)
dumpState();
} }
if (this.saveTimeout > 0)
dumpState();
super.onClose();
} }
private void dumpState() private void dumpState()
+7 -20
View File
@@ -90,6 +90,7 @@ public abstract class ScriptHost
protected ScriptHost parent; protected ScriptHost parent;
protected boolean perUser; protected boolean perUser;
protected String user;
public String getName() {return main ==null?null: main.getName();} public String getName() {return main ==null?null: main.getName();}
@@ -102,6 +103,7 @@ public abstract class ScriptHost
this.parent = parent; this.parent = parent;
this.main = code; this.main = code;
this.perUser = perUser; this.perUser = perUser;
this.user = null;
ModuleData moduleData = new ModuleData(code); ModuleData moduleData = new ModuleData(code);
initializeModuleGlobals(moduleData); initializeModuleGlobals(moduleData);
this.moduleData.put(code, moduleData); this.moduleData.put(code, moduleData);
@@ -313,15 +315,10 @@ public abstract class ScriptHost
if (oldUserHost != null) return oldUserHost; if (oldUserHost != null) return oldUserHost;
ScriptHost userHost = this.duplicate(); ScriptHost userHost = this.duplicate();
userHost.modules.putAll(this.modules); userHost.modules.putAll(this.modules);
for (Map.Entry<Module, ScriptHost.ModuleData> e : this.moduleData.entrySet()) this.moduleData.forEach((key, value) -> userHost.moduleData.put(key, new ModuleData(key, value)));
{
userHost.moduleData.put(e.getKey(), new ModuleData(e.getKey(), e.getValue()));
}
// fixing imports // fixing imports
userHost.moduleData.forEach((module, data) -> userHost.moduleData.forEach((module, data) -> data.setImportsBasedOn(userHost, this.moduleData.get(data.parent)));
{ userHost.user = user;
data.setImportsBasedOn(userHost, this.moduleData.get(data.parent));
});
userHosts.put(user, userHost); userHosts.put(user, userHost);
return userHost; return userHost;
} }
@@ -361,18 +358,8 @@ public abstract class ScriptHost
public void onClose() public void onClose()
{ {
inTermination = true; inTermination = true;
List<ExecutorService> toStop = new ArrayList<>(executorServices.values()); for (ScriptHost uh : userHosts.values()) uh.onClose();
for (ScriptHost uh : userHosts.values()) toStop.addAll(uh.executorServices.values()); executorServices.values().forEach(e -> {e.shutdown(); e.shutdownNow();});
if (!toStop.isEmpty())
{
ThreadPoolExecutor shutdownService = (ThreadPoolExecutor) Executors.newCachedThreadPool();
List<CompletableFuture<?>> terminators = toStop.stream().map(exec ->
CompletableFuture.supplyAsync(() -> { exec.shutdown(); exec.shutdownNow(); return null; }, shutdownService)).collect(Collectors.toList()
);
terminators.forEach(CompletableFuture::join);
shutdownService.shutdown();
shutdownService.shutdownNow();
}
} }
public void setPerPlayer(boolean isPerUser) public void setPerPlayer(boolean isPerUser)