Fix delayed executor.

This commit is contained in:
Aria 2023-05-08 02:43:28 -07:00
parent abed7cd895
commit c30a980ea0
4 changed files with 60 additions and 33 deletions

View file

@ -12,7 +12,7 @@ plugins {
id 'maven-publish' id 'maven-publish'
} }
version = "1.1.8" version = "1.1.9"
group = "dev.zontreck" group = "dev.zontreck"
archivesBaseName = "LibAC" archivesBaseName = "LibAC"
java.toolchain.languageVersion = JavaLanguageVersion.of(17) java.toolchain.languageVersion = JavaLanguageVersion.of(17)

View file

@ -13,12 +13,24 @@ public abstract class Task implements Runnable
public static final String CHECK = ""; public static final String CHECK = "";
public static final String FAIL = "X"; public static final String FAIL = "X";
// Else use the progress spinner from the Progress class // Else use the progress spinner from the Progress class
private boolean isSilent=false;
public Task(String name) public Task(String name)
{ {
TASK_NAME=name; TASK_NAME=name;
} }
/**
* This constructor is meant to be used to create silent tasks that do not output to the console. (Example usage: DelayedExecutionService)
* @param name Task name
* @param silent Whether to print to the terminal
*/
public Task(String name, boolean silent)
{
this(name);
isSilent=silent;
}
public boolean isComplete(){ public boolean isComplete(){
return token.get(); return token.get();
@ -26,13 +38,14 @@ public abstract class Task implements Runnable
public void startTask() public void startTask()
{ {
DelayedExecutorService.getInstance().schedule(this, 1); DelayedExecutorService.scheduleTask(this, 1);
DelayedExecutorService.getInstance().schedule(new SpinnerTask(token), 1); if(!isSilent)
DelayedExecutorService.scheduleTask(new SpinnerTask(token), 1);
} }
public void stopTask() public void stopTask()
{ {
if(token.get()) if(token.get() && !isSilent)
{ {
System.out.printf("\r"+TASK_NAME+"\t\t["+token.status+"]\n"); System.out.printf("\r"+TASK_NAME+"\t\t["+token.status+"]\n");
} }
@ -62,6 +75,7 @@ public abstract class Task implements Runnable
try { try {
Thread.sleep(1000L); Thread.sleep(1000L);
if(!isSilent)
System.out.printf("\r"+TASK_NAME+"\t\t"+spinner.getSpinnerTick()); System.out.printf("\r"+TASK_NAME+"\t\t"+spinner.getSpinnerTick());
}catch(Exception e) }catch(Exception e)
{ {

View file

@ -1,13 +1,13 @@
package dev.zontreck.ariaslib.terminal; package dev.zontreck.ariaslib.terminal;
import com.google.common.collect.Lists;
import dev.zontreck.ariaslib.util.DelayedExecutorService; import dev.zontreck.ariaslib.util.DelayedExecutorService;
import java.util.List; import java.util.*;
import java.util.Stack;
public class TaskBus implements Runnable public class TaskBus implements Runnable
{ {
public static Stack<Task> tasks = new Stack<>(); public static List<Task> tasks = new ArrayList<>();
public static Task current = null; public static Task current = null;
@Override @Override
@ -17,7 +17,7 @@ public class TaskBus implements Runnable
if(TaskBus.current == null) if(TaskBus.current == null)
{ {
current = tasks.pop(); current = tasks.get(0);
current.startTask(); current.startTask();
}else { }else {

View file

@ -1,11 +1,10 @@
package dev.zontreck.ariaslib.util; package dev.zontreck.ariaslib.util;
import java.util.ArrayList; import java.time.Instant;
import java.util.List; import java.util.*;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import dev.zontreck.ariaslib.terminal.Task;
import dev.zontreck.ariaslib.terminal.Terminal; import dev.zontreck.ariaslib.terminal.Terminal;
@ -29,6 +28,20 @@ public class DelayedExecutorService {
} }
private DelayedExecutorService(){} private DelayedExecutorService(){}
/**
* This function is designed to set back up the executor if it was previously stopped and restarted.
*/
public static void setup()
{
stopRepeatingThread();
repeater.schedule(new TimerTask() {
@Override
public void run() {
DelayedExecutorService.getInstance().onTick();
}
}, 1000L, 1000L);
}
/** /**
* Stops accepting new tasks, and current ones will abort * Stops accepting new tasks, and current ones will abort
*/ */
@ -53,32 +66,32 @@ public class DelayedExecutorService {
} }
public class DelayedExecution public class DelayedExecution
{ {
public DelayedExecution(Runnable run, long unix) { public DelayedExecution(Task run, long unix) {
scheduled=run; scheduled=run;
unix_time=unix; unix_time=unix;
} }
public Runnable scheduled; public Task scheduled;
public long unix_time; public long unix_time;
} }
public List<DelayedExecution> EXECUTORS = new ArrayList<>(); public List<DelayedExecution> EXECUTORS = new ArrayList<>();
public void schedule(final Runnable run, int seconds) public static void scheduleTask(final Task run, int seconds)
{
DelayedExecutorService.getInstance().schedule(run,seconds);
}
public static void scheduleRepeatingTask(final Task run, int seconds)
{
DelayedExecutorService.getInstance().scheduleRepeating(run,seconds);
}
public void schedule(final Task run, int seconds)
{ {
if(!isRunning()){ if(!isRunning()){
return; return;
} }
//long unix = Instant.now().getEpochSecond()+ (seconds); long unix = Instant.now().getEpochSecond()+ (seconds);
TimerTask task = new TimerTask() { EXECUTORS.add(new DelayedExecution(run, unix));
@Override
public void run()
{
run.run();
}
};
repeater.schedule(task, seconds*1000L);
//DelayedExecution exe = new DelayedExecution(run,unix);
//EXECUTORS.add(exe);
} }
public static boolean isRunning() public static boolean isRunning()
@ -86,19 +99,19 @@ public class DelayedExecutorService {
return RUN.get(); return RUN.get();
} }
public void scheduleRepeating(final Runnable run, int seconds) public void scheduleRepeating(final Task run, int seconds)
{ {
if(!isRunning()) return; if(!isRunning()) return;
TimerTask task = new TimerTask() { long unix = Instant.now().getEpochSecond()+ (seconds);
Task repeater = new Task("Repeating:"+run.TASK_NAME, true) {
@Override @Override
public void run() { public void run() {
run.run(); run.run();
scheduleRepeating(run, seconds); scheduleRepeating(run, seconds);
} }
}; };
EXECUTORS.add(new DelayedExecution(repeater, unix));
repeater.schedule(task, seconds*1000L);
} }
private static void stopRepeatingThread() private static void stopRepeatingThread()
@ -112,7 +125,7 @@ public class DelayedExecutorService {
{ {
DelayedExecutorService.stopRepeatingThread(); DelayedExecutorService.stopRepeatingThread();
} }
/*Iterator<DelayedExecution> it = EXECUTORS.iterator(); Iterator<DelayedExecution> it = EXECUTORS.iterator();
while(it.hasNext()) while(it.hasNext())
{ {
DelayedExecution e = it.next(); DelayedExecution e = it.next();
@ -123,7 +136,7 @@ public class DelayedExecutorService {
tx.setName("DelayedExecutorTask-"+String.valueOf(DelayedExecutorService.getNext())); tx.setName("DelayedExecutorTask-"+String.valueOf(DelayedExecutorService.getNext()));
tx.start(); tx.start();
} }
}*/ }
} }
public static int getNext() public static int getNext()