2
2
3
3
import java .io .Serializable ;
4
4
import java .util .concurrent .TimeUnit ;
5
- import java .util .concurrent .atomic .AtomicBoolean ;
6
5
import java .util .logging .Level ;
7
6
import java .util .logging .Logger ;
8
7
16
15
import net .dv8tion .jda .core .hooks .ListenerAdapter ;
17
16
18
17
public abstract class ScheduledTask extends ListenerAdapter implements Runnable , Serializable {
19
- private static final long serialVersionUID = 1L ;
18
+ private static final long serialVersionUID = 2L ;
20
19
private static final Logger LOG = Logger .getLogger ("Themis" );
21
20
21
+ private final String NAME = this .getClass ().getName ();;
22
+
22
23
private long periodicity ;
23
24
private long repeat ;
24
- private long numberOfRuns ;
25
- private AtomicBoolean isComplete ;
26
- private AtomicBoolean isInProgress ;
25
+ private long numberOfRuns = 0 ;
26
+ private TaskState state ;
27
+
27
28
private transient long timeOfNextRun ;
28
29
private transient JDA jda ;
30
+
29
31
30
32
public ScheduledTask (long delay , long periodicity , TimeUnit timeUnit , long repeat ) {
31
33
this .periodicity = timeUnit .toMillis (periodicity );
32
34
this .timeOfNextRun = System .currentTimeMillis () + timeUnit .toMillis (delay );
33
35
this .repeat = repeat ;
34
- this .numberOfRuns = 0 ;
35
- this .isComplete = new AtomicBoolean (false );
36
- this .isInProgress = new AtomicBoolean (false );
36
+ setState (TaskState .QUEUED );
37
37
}
38
38
39
- protected boolean taskIsReady () {
40
- return (System .currentTimeMillis () >= timeOfNextRun ) && !isExpired ();
39
+ @ Override
40
+ public final void run () {
41
+ setState (TaskState .RUNNING );
42
+ jda .addEventListener (this );
43
+ try {
44
+ runTask ();
45
+ } catch (Exception e ) {
46
+ StackTraceElement [] st = e .getStackTrace ();
47
+ if (st .length > 0 ) {
48
+ StackTraceElement lastStackTrace = st [st .length - 1 ];
49
+ LOG .logp (Level .WARNING , lastStackTrace .getClassName (), lastStackTrace .getMethodName (),
50
+ "Exception occured in scheduled task." , e );
51
+ } else {
52
+ LOG .log (Level .WARNING , "Exception occured in scheduled task." , e );
53
+ }
54
+ } finally {
55
+ jda .removeEventListener (this );
56
+ this .cleanUpTask ();
57
+ }
58
+ if (hasMoreRuns ()) {
59
+ setState (TaskState .QUEUED );
60
+ } else {
61
+ setState (TaskState .CLEANUP );
62
+ }
63
+ }
64
+
65
+ public final boolean isSameTaskType (ScheduledTask task ) {
66
+ return (this .NAME == task .NAME );
67
+ }
68
+
69
+ public boolean isState (TaskState state ) {
70
+ synchronized (this .state ) {
71
+ return (this .state == state );
72
+ }
73
+ }
74
+
75
+ public TaskState getState () {
76
+ synchronized (state ) {
77
+ return state ;
78
+ }
79
+ }
80
+
81
+ protected void cleanUpTask () {
82
+ cleanUpJDAChanges ();
83
+ setState (TaskState .DEAD );
84
+ }
85
+
86
+ protected final long getTimeUntilNextRun () {
87
+ return timeOfNextRun - System .currentTimeMillis ();
88
+ }
89
+
90
+ protected final void incrementRun () {
91
+ timeOfNextRun = System .currentTimeMillis () + periodicity ;
92
+ if (repeat != Long .MAX_VALUE ) {
93
+ numberOfRuns ++;
94
+ }
95
+ }
96
+
97
+ protected final boolean taskIsReady () {
98
+ return (System .currentTimeMillis () >= timeOfNextRun );
41
99
}
42
100
43
- protected boolean isExpired () {
44
- return (( numberOfRuns > repeat ) && isComplete () );
101
+ protected final boolean hasMoreRuns () {
102
+ return (numberOfRuns > repeat );
45
103
}
46
104
47
- protected JDA getJDA () {
105
+ protected final JDA getJDA () {
48
106
if (jda == null ) {
49
107
LOG .log (Level .SEVERE , "JDA not set in task" );
50
108
}
51
109
return jda ;
52
110
}
53
111
54
- protected void setJDA (JDA jda ) {
112
+ protected final void setJDA (JDA jda ) {
55
113
this .jda = jda ;
56
114
}
57
115
58
- protected Guild getGuildById (String guildId ) {
116
+ protected final Guild getGuildById (String guildId ) {
59
117
Guild guild = getJDA ().getGuildById (guildId );
60
118
if (guild == null ) {
61
119
throw new EntityNotFoundException ("Could not find group from id " + guildId );
62
120
}
63
121
return guild ;
64
122
}
65
123
66
- protected Member getMemberById (String memberId , Guild guild ) {
124
+ protected final Member getMemberById (String memberId , Guild guild ) {
67
125
Member member = guild .getMemberById (memberId );
68
126
if (member == null ) {
69
127
throw new EntityNotFoundException ("Could not find member from id "
@@ -74,7 +132,7 @@ protected Member getMemberById(String memberId, Guild guild) {
74
132
return member ;
75
133
}
76
134
77
- protected TextChannel getTextChannelById (String textChannelId , Guild guild ) {
135
+ protected final TextChannel getTextChannelById (String textChannelId , Guild guild ) {
78
136
TextChannel textChannel = guild .getTextChannelById (textChannelId );
79
137
if (textChannel == null ) {
80
138
throw new EntityNotFoundException ("Could not find textChannel from id "
@@ -85,58 +143,22 @@ protected TextChannel getTextChannelById(String textChannelId, Guild guild) {
85
143
return textChannel ;
86
144
}
87
145
88
- protected User getUserById (String userId ) {
146
+ protected final User getUserById (String userId ) {
89
147
User user = jda .getUserById (userId );
90
148
if (user == null ) {
91
149
throw new EntityNotFoundException ("Could not find user from id " + userId );
92
150
}
93
151
return user ;
94
152
}
95
-
96
- @ Override
97
- public final void run () {
98
- isInProgress .set (true );
99
- jda .addEventListener (this );
100
- try {
101
- runTask ();
102
- } catch (Exception e ) {
103
- StackTraceElement [] st = e .getStackTrace ();
104
- if (st .length > 0 ) {
105
- StackTraceElement lastStackTrace = st [st .length - 1 ];
106
- LOG .logp (Level .WARNING , lastStackTrace .getClassName (), lastStackTrace .getMethodName (),
107
- "Exception occured in scheduled task." , e );
108
- } else {
109
- LOG .log (Level .WARNING , "Exception occured in scheduled task." , e );
110
- }
111
- } finally {
112
- jda .removeEventListener (this );
113
- }
114
- isComplete .set (true );
115
- isInProgress .set (false );
116
- }
117
-
118
- public final void incrementRun () {
119
- timeOfNextRun = System .currentTimeMillis () + periodicity ;
120
- if (repeat != Long .MAX_VALUE ) {
121
- numberOfRuns ++;
122
- }
123
- }
124
153
125
- public boolean isComplete () {
126
- return isComplete .get ();
127
- }
154
+ protected void cleanUpJDAChanges () { }
128
155
129
- public void recalculateRunTime (long timeRemaining ) {
130
- timeOfNextRun = System .currentTimeMillis () + timeRemaining ;
131
- }
156
+ protected abstract void runTask ();
132
157
133
- public long getTimeUntilNextRun () {
134
- return timeOfNextRun - System .currentTimeMillis ();
158
+ private void setState (TaskState state ) {
159
+ synchronized (this .state ) {
160
+ this .state = state ;
161
+ }
135
162
}
136
163
137
- protected abstract void runTask ();
138
-
139
- public boolean isInProgress () {
140
- return isInProgress .get ();
141
- }
142
164
}
0 commit comments