Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parallel script loading #3924

Merged
merged 17 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,290 changes: 773 additions & 517 deletions src/main/java/ch/njol/skript/ScriptLoader.java

Large diffs are not rendered by default.

131 changes: 40 additions & 91 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import java.util.jar.JarFile;
import java.util.logging.Filter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
Expand All @@ -71,7 +70,6 @@
import org.eclipse.jdt.annotation.Nullable;

import com.google.gson.Gson;

import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.bukkitutil.BukkitUnsafe;
import ch.njol.skript.bukkitutil.BurgerHelper;
Expand All @@ -87,6 +85,7 @@
import ch.njol.skript.classes.data.JavaClasses;
import ch.njol.skript.classes.data.SkriptClasses;
import ch.njol.skript.command.Commands;
import ch.njol.skript.config.Config;
import ch.njol.skript.doc.Documentation;
import ch.njol.skript.events.EvtSkript;
import ch.njol.skript.hooks.Hook;
Expand All @@ -101,8 +100,6 @@
import ch.njol.skript.lang.SyntaxElementInfo;
import ch.njol.skript.lang.Trigger;
import ch.njol.skript.lang.TriggerItem;
import ch.njol.skript.lang.VariableString;
import ch.njol.skript.lang.function.Functions;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.localization.Language;
import ch.njol.skript.localization.Message;
Expand Down Expand Up @@ -138,6 +135,7 @@
import ch.njol.util.Closeable;
import ch.njol.util.Kleenean;
import ch.njol.util.NullableChecker;
import ch.njol.util.OpenCloseable;
import ch.njol.util.StringUtils;
import ch.njol.util.coll.CollectionUtils;
import ch.njol.util.coll.iterator.CheckedIterator;
Expand Down Expand Up @@ -504,18 +502,14 @@ public void run() {
info("Loading variables...");
final long vls = System.currentTimeMillis();

final LogHandler h = SkriptLogger.startLogHandler(new ErrorDescLogHandler() {
// private final List<LogEntry> log = new ArrayList<LogEntry>();

LogHandler h = SkriptLogger.startLogHandler(new ErrorDescLogHandler() {
@Override
public LogResult log(final LogEntry entry) {
super.log(entry);
if (entry.level.intValue() >= Level.SEVERE.intValue()) {
logEx(entry.message); // no [Skript] prefix
return LogResult.DO_NOT_LOG;
} else {
// log.add(entry);
// return LogResult.CACHED;
return LogResult.LOG;
}
}
Expand All @@ -533,20 +527,13 @@ protected void afterErrors() {
logEx("Skript will work properly, but old variables might not be available at all and new ones may or may not be saved until Skript is able to create a backup of the old file and/or is able to connect to the database (which requires a restart of Skript)!");
logEx();
}

@Override
protected void onStop() {
super.onStop();
// SkriptLogger.logAll(log);
}
});
final CountingLogHandler c = SkriptLogger.startLogHandler(new CountingLogHandler(SkriptLogger.SEVERE));
try {

try (CountingLogHandler c = new CountingLogHandler(SkriptLogger.SEVERE).start()) {
if (!Variables.load())
if (c.getCount() == 0)
error("(no information available)");
} finally {
c.stop();
h.stop();
}

Expand All @@ -562,10 +549,11 @@ protected void onStop() {
@SuppressWarnings("null")
CountingLogHandler errorCounter = new CountingLogHandler(Level.SEVERE);
try {
SkriptLogger.startLogHandler(errorCounter);
errorCounter.start();
File testDir = TestMode.TEST_DIR.toFile();
assert testDir != null;
ScriptLoader.loadScripts(ScriptLoader.loadStructures(testDir));
List<Config> configs = ScriptLoader.loadStructures(testDir);
ScriptLoader.runScriptsLoad(configs, errorCounter).join();
} finally {
errorCounter.stop();
}
Expand Down Expand Up @@ -598,13 +586,7 @@ protected void onStop() {
if (logNormal())
info("Loaded " + Variables.numVariables() + " variables in " + ((vld / 100) / 10.) + " seconds");

ScriptLoader.loadScripts();

Skript.info(m_finished_loading.toString());

EvtSkript.onSkriptStart();

final Metrics metrics = new Metrics(Skript.this);
Metrics metrics = new Metrics(Skript.this);

metrics.addCustomChart(new Metrics.SimplePie("pluginLanguage") {

Expand Down Expand Up @@ -727,24 +709,36 @@ public String getValue() {

Skript.metrics = metrics;

// suppresses the "can't keep up" warning after loading all scripts
final Filter f = new Filter() {
@Override
public boolean isLoggable(final @Nullable LogRecord record) {
if (record == null)
return false;
if (record.getMessage() != null && record.getMessage().toLowerCase(Locale.ENGLISH).startsWith("can't keep up!"))
return false;
return true;
}
};
BukkitLoggerFilter.addFilter(f);
Bukkit.getScheduler().scheduleSyncDelayedTask(Skript.this, new Runnable() {
@Override
public void run() {
BukkitLoggerFilter.removeFilter(f);
}
}, 1);
/*
* Start loading scripts
*/
ScriptLoader.runScriptsLoad(OpenCloseable.EMPTY)
.thenAccept(unused -> {
Skript.info(m_finished_loading.toString());

// EvtSkript.onSkriptStart should be called on main server thread
if (!ScriptLoader.isAsync()) {
EvtSkript.onSkriptStart();

// Suppresses the "can't keep up" warning after loading all scripts
// Only for non-asynchronous loading
Filter filter = record -> {
if (record == null)
return false;
return record.getMessage() == null
|| !record.getMessage().toLowerCase(Locale.ENGLISH).startsWith("can't keep up!");
};
BukkitLoggerFilter.addFilter(filter);
Bukkit.getScheduler().scheduleSyncDelayedTask(
Skript.this,
() -> BukkitLoggerFilter.removeFilter(filter),
1);
} else {
Bukkit.getScheduler().scheduleSyncDelayedTask(Skript.this,
EvtSkript::onSkriptStart);
}
});

}
});

Expand Down Expand Up @@ -964,51 +958,6 @@ public static Metrics getMetrics() {
return metrics;
}

/**
* Clears triggers, commands, functions and variable names
*/
static void disableScripts() {
VariableString.variableNames.clear();
SkriptEventHandler.removeAllTriggers();
Commands.clearCommands();
Functions.clearFunctions();
}

/**
* Prints errors from reloading the config & scripts
*/
static void reload() {
if (!ScriptLoader.loadAsync)
disableScripts();
reloadMainConfig();
reloadAliases();
ScriptLoader.loadScripts();
}

/**
* Prints errors
*/
static void reloadScripts() {
if (!ScriptLoader.loadAsync)
disableScripts();
ScriptLoader.loadScripts();
}

/**
* Prints errors
*/
static void reloadMainConfig() {
SkriptConfig.load();
}

/**
* Prints errors
*/
static void reloadAliases() {
Aliases.clear();
Aliases.load();
}

@SuppressWarnings("null")
private final static Collection<Closeable> closeOnDisable = Collections.synchronizedCollection(new ArrayList<Closeable>());

Expand All @@ -1031,7 +980,7 @@ public void onDisable() {

EvtSkript.onSkriptStop(); // TODO [code style] warn user about delays in Skript stop events

disableScripts();
ScriptLoader.disableScripts();

Bukkit.getScheduler().cancelTasks(this);

Expand Down
Loading