Skip to content

Commit 1fb0075

Browse files
committed
Added support for blocking tasks in task manager.
1 parent 78d8f5b commit 1fb0075

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

src/main/java/com/github/bustedearlobes/themis/commands/ClearCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void onCall(Matcher fullCommand, Message message, JDA jda, Themis themis)
3131
targetChannel,
3232
numOfMessages,
3333
message.getTextChannel());
34-
themis.getTaskManager().addTaskToScheduler(cmt);
34+
themis.getTaskManager().addTask(cmt);
3535
}
3636

3737
@Override

src/main/java/com/github/bustedearlobes/themis/commands/MuteCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void onCall(Matcher parsedCommand, Message message, JDA jda, Themis themi
2929
channel,
3030
message.getTextChannel(),
3131
true);
32-
themis.getTaskManager().addTaskToScheduler(muteTask);
32+
themis.getTaskManager().addTask(muteTask);
3333
if(parsedCommand.group(4) != null) {
3434
TimeUnit timeUnit = parseTimeUnit(parsedCommand.group(6));
3535
long time = Integer.parseInt(parsedCommand.group(5));
@@ -40,7 +40,7 @@ public void onCall(Matcher parsedCommand, Message message, JDA jda, Themis themi
4040
false,
4141
time,
4242
timeUnit);
43-
themis.getTaskManager().addTaskToScheduler(unmuteTask);
43+
themis.getTaskManager().addTask(unmuteTask);
4444
}
4545
}
4646
}

src/main/java/com/github/bustedearlobes/themis/commands/UnmuteCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void onCall(Matcher parsedCommand, Message message, JDA jda, Themis themi
3232
false,
3333
0,
3434
TimeUnit.SECONDS);
35-
themis.getTaskManager().addTaskToScheduler(unmuteTask);
35+
themis.getTaskManager().addTask(unmuteTask);
3636
}
3737
}
3838

src/main/java/com/github/bustedearlobes/themis/taskmanager/ScheduledTask.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.Serializable;
44
import java.util.concurrent.TimeUnit;
5+
import java.util.concurrent.atomic.AtomicBoolean;
56
import java.util.logging.Level;
67
import java.util.logging.Logger;
78

@@ -22,13 +23,15 @@ public abstract class ScheduledTask extends ListenerAdapter implements Runnable,
2223
private long repeat;
2324
private long numberOfRuns;
2425
private long timeOfNextRun;
26+
private AtomicBoolean isComplete;
2527
private transient JDA jda;
2628

2729
public ScheduledTask(long delay, long periodicity, TimeUnit timeUnit, long repeat) {
2830
this.periodicity = timeUnit.toMillis(periodicity);
2931
this.timeOfNextRun = System.currentTimeMillis() + timeUnit.toMillis(delay);
3032
this.repeat = repeat;
3133
this.numberOfRuns = 0;
34+
this.isComplete = new AtomicBoolean(false);
3235
}
3336

3437
protected boolean taskIsReady() {
@@ -105,6 +108,7 @@ public final void run() {
105108
} finally {
106109
jda.removeEventListener(this);
107110
}
111+
isComplete.set(true);
108112
}
109113

110114
public final void incrementRun() {
@@ -113,6 +117,10 @@ public final void incrementRun() {
113117
numberOfRuns++;
114118
}
115119
}
120+
121+
public boolean isComplete() {
122+
return isComplete.get();
123+
}
116124

117125
protected abstract void runTask();
118126
}

src/main/java/com/github/bustedearlobes/themis/taskmanager/TaskManager.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,21 @@ public TaskManager(JDA jda) {
3636
STATE_FILE.delete();
3737
}
3838

39-
public void addTaskToScheduler(ScheduledTask task) {
39+
public void addTask(ScheduledTask task) {
4040
task.setJDA(jda);
4141
synchronized(scheduledTasks) {
4242
scheduledTasks.add(task);
4343
}
4444
saveState();
4545
}
4646

47+
public void addTaskBlocked(ScheduledTask task) throws InterruptedException {
48+
addTask(task);
49+
while(!task.isComplete()) {
50+
Thread.sleep(100);
51+
}
52+
}
53+
4754
@Override
4855
public void run() {
4956
isRunning = new AtomicBoolean(true);
@@ -98,7 +105,7 @@ private void loadOldState() {
98105
ObjectInputStream ois = new ObjectInputStream(bis)) {
99106
int numberOfTasks = ois.readInt();
100107
for(int count = 0; count < numberOfTasks; count ++) {
101-
addTaskToScheduler((ScheduledTask)ois.readObject());
108+
addTask((ScheduledTask)ois.readObject());
102109
count ++;
103110
}
104111
LOG.log(Level.INFO, "Task manager state loaded " + numberOfTasks + " tasks from " + STATE_FILE.getCanonicalPath());

0 commit comments

Comments
 (0)