[+] Create task manager

This commit is contained in:
Hykilpikonna
2020-02-21 14:39:39 -05:00
parent bddc598e88
commit 2b27d3e03d
5 changed files with 259 additions and 0 deletions
@@ -0,0 +1,27 @@
package org.hydev.hyritone.task;
/**
* A task waiting to be executed.
* <p>
* Class created by the HyDEV Team on 2020-01-24!
*
* @author HyDEV Team (https://github.com/HyDevelop)
* @author Hykilpikonna (https://github.com/hykilpikonna)
* @author Vanilla (https://github.com/VergeDX)
* @since 2020-01-24 18:06
*/
public interface Task
{
/**
* Execute the task
*/
void execute(TaskRunState state);
/**
* Stop the task
*/
default void stop()
{
// Default nothing to do
}
}
@@ -0,0 +1,28 @@
package org.hydev.hyritone.task;
import lombok.AllArgsConstructor;
import java.util.ArrayList;
import java.util.Collections;
/**
* Instances of this class contains lists of tasks to be executed in order.
* <p>
* Class created by the HyDEV Team on 2020-01-24!
*
* @author HyDEV Team (https://github.com/HyDevelop)
* @author Hykilpikonna (https://github.com/hykilpikonna)
* @author Vanilla (https://github.com/VergeDX)
* @since 2020-01-24 18:05
*/
@AllArgsConstructor
public class TaskList extends ArrayList<Task>
{
public final String name;
public TaskList(String name, Task... tasks)
{
this(name);
Collections.addAll(this, tasks);
}
}
@@ -0,0 +1,76 @@
package org.hydev.hyritone.task;
import lombok.Getter;
/**
* This class is used to manage tasks.
* <p>
* Class created by the HyDEV Team on 2020-01-24!
*
* @author HyDEV Team (https://github.com/HyDevelop)
* @author Hykilpikonna (https://github.com/hykilpikonna)
* @author Vanilla (https://github.com/VergeDX)
* @since 2020-01-24 19:03
*/
@Getter
public class TaskManager
{
private TaskRunState runState = null;
/**
* Run a task list
*
* @param list Task list
* @return Successfully started or not
*/
public boolean run(TaskList list)
{
// Check already started
if (runState != null) return false;
// Reset values
runState = new TaskRunState(null, list, 0);
// Create thread
runState.thread = new Thread(() ->
{
// Execute step by step
while (runState.step < list.size())
{
list.get(runState.step).execute(runState);
runState.step++;
}
// Finished
stop();
}, "Hyritone Task");
// Start async
runState.thread.start();
// Success
return true;
}
/**
* Stop the task list
*/
public void stop()
{
// Not started
if (runState == null) return;
// Stop thread
runState.thread.interrupt();
// Stop task
if (runState.getStep() < runState.getTaskList().size())
{
runState.getTaskList().get(runState.getStep()).stop();
}
runState = null;
}
// TODO: pause
}
@@ -0,0 +1,98 @@
package org.hydev.hyritone.task;
import lombok.SneakyThrows;
import org.apache.commons.io.FileUtils;
import org.hydev.hyritone.Main;
import javax.script.ScriptException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import static org.hydev.hyritone.utils.ScriptUtils.getEngine;
/**
* This class contains preset tasks
* TODO: Make a UI for task list!
* <p>
* Class created by the HyDEV Team on 2020-01-24!
*
* @author HyDEV Team (https://github.com/HyDevelop)
* @author Hykilpikonna (https://github.com/hykilpikonna)
* @author Vanilla (https://github.com/VergeDX)
* @since 2020-01-24 18:23
*/
public class TaskPresets
{
// Map<name.toLowerCase(), taskList>
public static Map<String, TaskList> presets = new HashMap<>();
/**
* Register a task list
*
* @param list Task list
*/
public static void register(TaskList list)
{
presets.put(list.name.toLowerCase(), list);
}
/**
* Get preset by name
*
* @param name Lowercase name
* @return Task list by name, null if not found
*/
public static TaskList getPreset(String name)
{
if (presets.containsKey(name.toLowerCase()))
{
return presets.get(name.toLowerCase());
}
return null;
}
/**
* Load presets from file
*/
@SneakyThrows
public static void reloadPresets()
{
Path scriptDirPath = Main.taskPresetsDir.toPath();
presets.clear();
try
{
Files.walk(scriptDirPath, FileVisitOption.FOLLOW_LINKS)
.filter(Files::isRegularFile)
.forEach(path ->
{
try
{
String script = FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8);
TaskList list = (TaskList) getEngine().eval(script);
presets.put(list.name.toLowerCase(), list);
}
catch (IOException e)
{
throw new UncheckedIOException(e);
}
catch (ScriptException e)
{
e.printStackTrace();
}
});
}
catch (IOException | UncheckedIOException e)
{
System.err.println("Error when loading hyritone scripts.");
e.printStackTrace();
}
}
}
@@ -0,0 +1,30 @@
package org.hydev.hyritone.task;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* TODO: Write a description for this class!
* <p>
* Class created by the HyDEV Team on 2020-01-24!
*
* @author HyDEV Team (https://github.com/HyDevelop)
* @author Hykilpikonna (https://github.com/hykilpikonna)
* @author Vanilla (https://github.com/VergeDX)
* @since 2020-01-24 20:38
*/
@Data
@AllArgsConstructor
public class TaskRunState
{
public Thread thread;
public TaskList taskList;
public int step;
@Override
public String toString()
{
return "Task " + thread.getId() + " is executing step " + step + " of " + taskList.size() + " " +
"| Current Task: " + taskList.get(step).getClass().getSimpleName();
}
}