/
Agent.java
284 lines (195 loc) · 6.32 KB
/
Agent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* Copyright (C) 2010-2023 Structr GmbH
*
* This file is part of Structr <http://structr.org>.
*
* Structr is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* Structr is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Structr. If not, see <http://www.gnu.org/licenses/>.
*/
package org.structr.agent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.structr.common.error.FrameworkException;
import org.structr.core.Services;
import org.structr.core.app.StructrApp;
import org.structr.core.graph.Tx;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.structr.agent.ReturnValue.Retry;
/**
* Abstract base class for all agents.
*/
public abstract class Agent<T> extends Thread implements StatusInfo {
public static final String AVERAGE_EXECUTION_TIME = "average_execution_time";
public static final String EXECUTION_STATUS = "execution_status";
public static final String MAX_QUEUE_SIZE = "max_queue_size";
private static final Logger logger = LoggerFactory.getLogger(Agent.class.getName());
private final AtomicBoolean suspended = new AtomicBoolean(false);
private final Queue<Task<T>> taskQueue = new ConcurrentLinkedQueue<>();
private final AtomicBoolean acceptingTasks = new AtomicBoolean(true);
private AgentService agentService = null;
private long averageExecutionTime = 0;
private int maxAgents = 10;
private int maxQueueSize = 200;
/**
* This method will be called by the AgentService
* @param task
*/
public abstract ReturnValue processTask(final Task<T> task) throws Throwable;
public abstract Class getSupportedTaskType();
@Override
public final void run() {
agentService.notifyAgentStart(this);
do {
if (!Services.getInstance().isInitialized()) {
try { Thread.sleep(100); } catch (InterruptedException i) {}
// loop until we are stopped
continue;
}
while (suspended.get()) {
Thread.yield();
}
final Task<T> currentTask;
synchronized (taskQueue) {
currentTask = taskQueue.poll();
}
if (currentTask != null) {
ReturnValue ret = null;
// only execute process if Service layer is ready
// (and not shutting down right now)
if (Services.getInstance().isInitialized()) {
if (createEnclosingTransaction()) {
try (final Tx tx = StructrApp.getInstance().tx()) {
ret = processTask(currentTask);
tx.success();
} catch (FrameworkException fex) {
// task processing failed..
logger.error("Processing task {} failed: {}", currentTask.getType(), fex.toString());
} catch (Throwable t) {
// task processing failed..
logger.error("Processing task {} failed: {}", currentTask.getType(), t.getMessage());
}
} else {
try {
ret = processTask(currentTask);
} catch (FrameworkException fex) {
// task processing failed..
logger.error("Processing task {} failed: {}", currentTask.getType(), fex.toString());
} catch (Throwable t) {
// task processing failed..
logger.error("Processing task {} failed: {}", currentTask.getType(), t.getMessage());
}
}
}
if (ret != null && Retry.equals(ret) && currentTask.getRetryCount() < 2) {
// wait some time
try { Thread.sleep(2000); } catch (InterruptedException ex) {}
synchronized (taskQueue) {
currentTask.incrementRetryCount();
taskQueue.add(currentTask);
}
}
long endTime = System.nanoTime();
// calc. average execution time
averageExecutionTime += endTime;
averageExecutionTime /= 2;
} else {
// queue is empty, quit.
acceptingTasks.set(false);
}
} while (acceptingTasks.get());
// call beforeShutdown to allow agents to clean up
beforeShutdown();
agentService.notifyAgentStop(this);
}
public final boolean assignTask(final Task<T> task) {
if (canHandleMore() && acceptingTasks.get()) {
synchronized (taskQueue) {
taskQueue.add(task);
}
return true;
}
return false;
}
public final void killAgent() {
// stop accepting tasks
acceptingTasks.set(false);
// clear queue
taskQueue.clear();
// interrupt running process..
// not sure if this works... see Thread.interrupt()'s description!
// may not work if the processTask method itself catches the interrupt..
this.interrupt();
}
public final void suspendAgent() {
acceptingTasks.set(false);
suspended.set(true);
}
public final void resumeAgent() {
acceptingTasks.set(true);
suspended.set(false);
}
protected void beforeShutdown() {
// override me
}
protected boolean canHandleMore() {
int size = 0;
synchronized (taskQueue) {
// FIXME: size may not be a constant time operation! slow?
size = taskQueue.size();
}
// queue is empty, assume new agent
if (size == 0) {
return true;
}
return size < maxQueueSize;
}
public boolean createEnclosingTransaction() {
return true;
}
public final int getMaxQueueSize() {
return maxQueueSize;
}
public final long getAverageExecutionTime() {
return averageExecutionTime;
}
public final void setAgentService(AgentService service) {
this.setDaemon(true);
this.agentService = service;
}
public int getMaxAgents() {
return maxAgents;
}
// ----- interface StatusInfo -----
@Override
public Object getStatusProperty(String key) {
if (key.equals(AVERAGE_EXECUTION_TIME)) {
return (getAverageExecutionTime());
} else if (key.equals(MAX_QUEUE_SIZE)) {
return (getMaxQueueSize());
} else if (key.equals(EXECUTION_STATUS)) {
// TODO.
}
return null;
}
protected AgentService getBlackboardService() {
return agentService;
}
public final boolean isSuspended() {
return suspended.get();
}
public final boolean isAcceptingTasks() {
return acceptingTasks.get();
}
}