-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTaskLog.java
More file actions
140 lines (129 loc) · 3.75 KB
/
Copy pathTaskLog.java
File metadata and controls
140 lines (129 loc) · 3.75 KB
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
package javaforce.webui.tasks;
import java.io.*;
import java.util.*;
import javaforce.*;
/** Task Log.
*
* Historical log of tasks completed.
*
* @author pquiring
*/
public class TaskLog {
private String folder;
private ArrayList<TaskEvent> processing = new ArrayList<>();
private ArrayList<TaskEvent> completed = new ArrayList<>();
private Object lock = new Object();
public TaskLog(String folder) {
this.folder = folder;
}
public void add(TaskEvent event) {
synchronized (lock) {
processing.add(event);
}
}
public void complete(TaskEvent event) {
//move from processing to disk
try {
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
int month = now.get(Calendar.MONTH) + 1;
synchronized (lock) {
completed.add(event);
processing.remove(event);
String filename = getFilename(year, month);
RandomAccessFile file_io = new RandomAccessFile(filename, "rw");
byte[] data = event.toByteArray();
file_io.seek(file_io.length());
file_io.write(data);
file_io.close();
}
} catch (Exception e) {
JFLog.log(e);
}
}
private static final long time_cut_time = 24 * 60 * 60 * 1000; //24 hrs
void purge() {
synchronized (lock) {
long time_now = System.currentTimeMillis();
long time_cut = time_now - time_cut_time;
ArrayList<TaskEvent> remove = new ArrayList<>();
synchronized (lock) {
for(TaskEvent event : completed) {
if (event.time_complete == 0) continue;
if (event.time_complete < time_cut) {
remove.add(event);
}
}
for(TaskEvent event : remove) {
completed.remove(event);
}
}
}
}
public boolean getTaskCompleted(long task_id) {
//check taskList (24 hrs window)
TaskEvent event = getTaskEvent(task_id);
return event.time_complete != 0;
}
public TaskEvent getTaskEvent(long task_id) {
synchronized (lock) {
for(TaskEvent event : processing) {
if (event.task_id == task_id) {
return event;
}
}
for(TaskEvent event : completed) {
if (event.task_id == task_id) {
return event;
}
}
}
return null;
}
private String getFilename(int year, int month) {
return String.format("%s/%d-%02d.tasks", folder, year, month);
}
/** Get all currently processing tasks. */
public TaskEvent[] getEventsInProcess() {
TaskEvent[] list;
synchronized (lock) {
list = processing.toArray(TaskEvent.ArrayType);
}
return list;
}
/** Get all completed task events from year/month. */
public TaskEvent[] getEvents(int year, int month) {
byte[] data = new byte[4096];
ArrayList<TaskEvent> events = new ArrayList<>();
RandomAccessFile file_io = null;
try {
synchronized (lock) {
String filename = getFilename(year, month);
if (!new File(filename).exists()) {
return TaskEvent.ArrayType;
}
file_io = new RandomAccessFile(filename, "rw");
long pos = 0;
long length = file_io.length();
while (pos < length) {
file_io.readFully(data, 0, 4);
pos += 4;
int event_length = BE.getuint32(data, 0);
while (event_length > data.length) {
data = new byte[data.length << 1];
}
file_io.readFully(data, 0, event_length);
events.add(TaskEvent.fromByteArray(data, 0, event_length));
pos += event_length;
}
file_io.close();
}
} catch (Exception e) {
if (file_io != null) {
try {file_io.close();} catch (Exception e2) {}
}
JFLog.log(e);
}
return events.toArray(TaskEvent.ArrayType);
}
}