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
+14 -39

No files matched your search

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