Change to a total task timeout instead of individual one

This commit is contained in:
games647 2016-06-29 17:48:33 +02:00
parent 6b13f9d370
commit a02c1c4e60

View File

@ -67,7 +67,6 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.messaging.Messenger; import org.bukkit.plugin.messaging.Messenger;
import org.bukkit.scheduler.BukkitScheduler; import org.bukkit.scheduler.BukkitScheduler;
import org.bukkit.scheduler.BukkitTask;
import java.io.File; import java.io.File;
import java.sql.SQLException; import java.sql.SQLException;
@ -80,6 +79,9 @@ import java.util.logging.Logger;
import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_ACCOUNT; import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_ACCOUNT;
import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_PASSWORD; import static fr.xephi.authme.settings.properties.EmailSettings.MAIL_PASSWORD;
import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS; import static fr.xephi.authme.settings.properties.EmailSettings.RECALL_PLAYERS;
import java.util.Iterator;
import java.util.logging.Level;
import org.bukkit.scheduler.BukkitWorker;
/** /**
* The AuthMe main class. * The AuthMe main class.
@ -452,35 +454,50 @@ public class AuthMe extends JavaPlugin {
if (newSettings != null) { if (newSettings != null) {
new PerformBackup(this, newSettings).doBackup(PerformBackup.BackupCause.STOP); new PerformBackup(this, newSettings).doBackup(PerformBackup.BackupCause.STOP);
} }
final AuthMe pluginInstance = this;
new Thread(new Runnable() { new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
List<Integer> pendingTasks = new ArrayList<>(); List<Integer> pendingTasks = new ArrayList<>();
for (BukkitTask pendingTask : getServer().getScheduler().getPendingTasks()) { //returns only the async takss
if (pendingTask.getOwner().equals(pluginInstance) && !pendingTask.isSync()) { for (BukkitWorker pendingTask : getServer().getScheduler().getActiveWorkers()) {
if (pendingTask.getOwner().equals(AuthMe.this)
//it's not a peridic task
&& !getServer().getScheduler().isQueued(pendingTask.getTaskId())) {
pendingTasks.add(pendingTask.getTaskId()); pendingTasks.add(pendingTask.getTaskId());
} }
} }
getLogger().info("Waiting for " + pendingTasks.size() + " tasks to finish");
getLogger().log(Level.INFO, "Waiting for {0} tasks to finish", pendingTasks.size());
int progress = 0; int progress = 0;
for (int taskId : pendingTasks) {
int maxTries = 5; //one minute + some time checking the running state
while (getServer().getScheduler().isCurrentlyRunning(taskId)) { int tries = 60;
if (maxTries <= 0) { while (!pendingTasks.isEmpty()) {
getLogger().info("Async task " + taskId + " times out after to many tries"); if (tries <= 0) {
break; getLogger().log(Level.INFO, "Async tasks times out after to many tries {0}", pendingTasks);
} break;
try {
Thread.sleep(1000);
} catch (InterruptedException ignored) {
}
maxTries--;
} }
progress++; try {
getLogger().info("Progress: " + progress + " / " + pendingTasks.size()); Thread.sleep(1000);
} catch (InterruptedException ignored) {
Thread.currentThread().interrupt();
break;
}
for (Iterator<Integer> iterator = pendingTasks.iterator(); iterator.hasNext();) {
int taskId = iterator.next();
if (!getServer().getScheduler().isCurrentlyRunning(taskId)) {
iterator.remove();
progress++;
getLogger().log(Level.INFO, "Progress: {0} / {1}", new Object[]{progress, pendingTasks.size()});
}
}
tries--;
} }
if (database != null) { if (database != null) {
database.close(); database.close();
} }