forked from comixed/comixed
-
Notifications
You must be signed in to change notification settings - Fork 2
/
MonitorTaskQueueWorkerTask.java
94 lines (86 loc) · 3.44 KB
/
MonitorTaskQueueWorkerTask.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
/*
* ComiXed - A digital comic book library management application.
* Copyright (C) 2020, The ComiXed Project
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses>
*/
package org.comixedproject.task.model;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.comixedproject.model.state.messaging.TaskCountMessage;
import org.comixedproject.model.tasks.Task;
import org.comixedproject.task.TaskException;
import org.comixedproject.task.adaptors.WorkerTaskAdaptor;
import org.comixedproject.task.encoders.WorkerTaskEncoder;
import org.comixedproject.task.runner.TaskManager;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;
/**
* <code>MonitorTaskQueueWorkerTask</code> is a task that continuous runs,looking for other tasks
* that have been queued. When it finds them it loads them, decodes them and adds them to the {@link
* TaskManager} for execution.
*
* @author Darryl L. Pierce
*/
@Component
@Log4j2
public class MonitorTaskQueueWorkerTask extends AbstractWorkerTask implements InitializingBean {
public static final String TASK_UPDATE_TARGET = "/topic/taskcount";
@Autowired private TaskManager taskManager;
@Autowired private WorkerTaskAdaptor workerTaskAdaptor;
@Autowired private SimpMessagingTemplate messagingTemplate;
@Override
protected String createDescription() {
return "Task queue monitor";
}
@Override
public void afterPropertiesSet() throws Exception {
log.debug("Scheduling task queue monitor");
this.taskManager.runTask(this);
}
@Override
public void startTask() throws WorkerTaskException {
log.debug("Updating topic: task count");
this.messagingTemplate.convertAndSend(
TASK_UPDATE_TARGET, new TaskCountMessage(this.workerTaskAdaptor.getTaskCount()));
log.debug("Checking queue for waiting tasks");
final List<Task> taskQueue = this.workerTaskAdaptor.getNextTask();
for (int index = 0; index < taskQueue.size(); index++) {
Task task = taskQueue.get(index);
log.debug("Rehydrating queued task");
WorkerTaskEncoder<?> decoder;
try {
decoder = this.workerTaskAdaptor.getEncoder(task.getTaskType());
} catch (TaskException error) {
throw new WorkerTaskException("failed to get task decoder", error);
}
WorkerTask nextTask = decoder.decode(task);
log.debug("Passing task to task manager");
this.taskManager.runTask(nextTask, task);
}
try {
Thread.sleep(1000L);
} catch (InterruptedException error) {
log.error("monitor task queue interrupted", error);
Thread.currentThread().interrupt();
}
}
@Override
public void afterExecution() {
log.debug("Rescheduling task queue monitor");
this.taskManager.runTask(this);
}
}