forked from BimberLab/DiscvrLabKeyModules
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractResumer.java
More file actions
300 lines (252 loc) · 9.04 KB
/
AbstractResumer.java
File metadata and controls
300 lines (252 loc) · 9.04 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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package org.labkey.api.sequenceanalysis.pipeline;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.Logger;
import org.labkey.api.pipeline.PipelineJob;
import org.labkey.api.pipeline.PipelineJobException;
import org.labkey.api.pipeline.RecordedAction;
import org.labkey.api.sequenceanalysis.SequenceOutputFile;
import org.labkey.api.util.Pair;
import org.labkey.vfs.FileSystemLike;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
/**
* Created by bimber on 4/2/2017.
*/
abstract public class AbstractResumer implements Serializable
{
transient Logger _log;
transient File _webserverJobDir;
protected TaskFileManager _fileManager;
protected LinkedHashSet<RecordedAction> _recordedActions = null;
protected boolean _isResume = false;
protected Map<File, File> _copiedInputs = new HashMap<>();
protected List<Pair<File, File>> _filesCopiedLocally = new ArrayList<>();
//for serialization
protected AbstractResumer()
{
}
protected AbstractResumer(File webserverJobDir, Logger log, TaskFileManager fileManager)
{
_webserverJobDir = webserverJobDir;
_log = log;
_fileManager = fileManager;
_recordedActions = new LinkedHashSet<>();
_filesCopiedLocally = new ArrayList<>();
}
public static File getSerializedJson(File outdir, String jsonName)
{
return new File(outdir, jsonName);
}
public static <RESUMER extends AbstractResumer> RESUMER readFromJson(File json, Class<RESUMER> clazz) throws PipelineJobException
{
try (BufferedInputStream bus = new BufferedInputStream(new FileInputStream(json)))
{
ObjectMapper objectMapper = PipelineJob.createObjectMapper();
return objectMapper.readValue(bus, clazz);
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
}
protected void writeToJson() throws PipelineJobException
{
writeToJson(_webserverJobDir);
}
abstract protected String getJsonName();
protected void logInfoBeforeSave()
{
_log.debug("total actions: " + _recordedActions.size());
_log.debug("total sequence outputs: " + getFileManager().getOutputsToCreate().size());
}
public void writeToJson(File outDir) throws PipelineJobException
{
_log.debug("saving job checkpoint to file");
logInfoBeforeSave();
if (outDir == null)
{
throw new PipelineJobException("output directory was null");
}
File output = getSerializedJson(outDir, getJsonName());
_log.debug("using file: " + output.getPath());
try
{
ObjectMapper objectMapper = PipelineJob.createObjectMapper();
objectMapper.writeValue(output, this);
}
catch (Throwable e)
{
throw new PipelineJobException(e);
}
}
public void markComplete()
{
markComplete(true);
}
public void markComplete(boolean deleteFile)
{
File file = getSerializedJson(_webserverJobDir, getJsonName());
if (file.exists())
{
_log.info("closing job resumer");
if (deleteFile)
file.delete();
else
_log.debug("delete of file will be deferred: " + file.getPath());
}
}
public void saveState() throws PipelineJobException
{
writeToJson();
}
public TaskFileManager getFileManager()
{
return _fileManager;
}
public void setFileManager(TaskFileManager fileManager)
{
_fileManager = fileManager;
}
public boolean isResume()
{
return _isResume;
}
public void setResume(boolean resume)
{
_isResume = resume;
}
public LinkedHashSet<RecordedAction> getRecordedActions()
{
return _recordedActions;
}
public void setRecordedActions(LinkedHashSet<RecordedAction> recordedActions)
{
_recordedActions = recordedActions;
}
public File getWebserverJobDir()
{
return _webserverJobDir;
}
public void setWebserverJobDir(File webserverJobDir)
{
_webserverJobDir = webserverJobDir;
}
public Logger getLogger()
{
return _log;
}
public void setLogger(Logger log)
{
_log = log;
}
public Map<File, File> getCopiedInputs()
{
return _copiedInputs;
}
public void setCopiedInputs(Map<File, File> copiedInputs)
{
_copiedInputs = copiedInputs;
}
public List<Pair<File, File>> getFilesCopiedLocally()
{
return _filesCopiedLocally;
}
public void setFilesCopiedLocally(List<Pair<File, File>> filesCopiedLocally)
{
_filesCopiedLocally = filesCopiedLocally;
}
public void addFileCopiedLocally(File orig, File copied)
{
_filesCopiedLocally.add(Pair.of(orig, copied));
}
public static <T extends AbstractResumer> T create(SequenceOutputHandler.JobContext ctx, String jsonName, Class<T> clazz) throws PipelineJobException
{
if (!(ctx instanceof SequenceOutputHandler.MutableJobContext))
{
throw new IllegalArgumentException("Expected JobContext to be instance of MutableJobContext");
}
T ret;
File json = getSerializedJson(ctx.getSourceDirectory(), jsonName);
if (!json.exists())
{
try
{
ret = clazz.getDeclaredConstructor(SequenceOutputHandler.JobContext.class).newInstance(ctx);
}
catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e)
{
throw new PipelineJobException(e);
}
}
else
{
ret = readFromJson(json, clazz);
ret.setResume(true);
ret.setLogger(ctx.getLogger());
ret.setWebserverJobDir(ctx.getSourceDirectory());
ret.getFileManager().onResume(ctx.getJob(), ctx.getWorkDir());
ctx.getLogger().debug("FileManagers initially equal: " + ctx.getFileManager().equals(ret.getFileManager()));
ctx.getLogger().debug("Replacing fileManager on JobContext");
((SequenceOutputHandler.MutableJobContext)ctx).setFileManager(ret.getFileManager());
try
{
if (!ret.getCopiedInputs().isEmpty())
{
for (File orig : ret.getCopiedInputs().keySet())
{
ctx.getWorkDir().inputFile(FileSystemLike.wrapFile(orig), FileSystemLike.wrapFile(ret._copiedInputs.get(orig)), false);
}
}
}
catch (IOException e)
{
throw new PipelineJobException(e);
}
//debugging:
ctx.getLogger().debug("loaded from file. total recorded actions: " + ret.getRecordedActions().size());
ctx.getLogger().debug("total sequence outputs: " + ret.getFileManager().getOutputsToCreate().size());
ctx.getLogger().debug("total intermediate files: " + ret.getFileManager().getIntermediateFiles().size());
for (RecordedAction a : ret.getRecordedActions())
{
ctx.getLogger().debug("action: " + a.getName() + ", inputs: " + a.getInputs().size() + ", outputs: " + a.getOutputs().size());
}
if (ret._recordedActions == null)
{
throw new PipelineJobException("Job read from file, but did not have any saved actions. This indicates a problem w/ serialization.");
}
}
if (ret.isResume())
{
ctx.getLogger().info("resuming previous job");
}
boolean fmEqual = ctx.getFileManager().equals(ret._fileManager);
ctx.getLogger().debug("FileManagers on resumer and JobContext equal: " + fmEqual);
return ret;
}
public void markComplete(SequenceOutputHandler.JobContext ctx)
{
// NOTE: due to the way the resumer is set up, the FileManager used by the Resumer is a different
// instance than the JobContext, meaning we need to manually pass information back to the primary FileManager
ctx.getLogger().debug("total sequence outputs tracked in resumer: " + getFileManager().getOutputsToCreate().size());
for (SequenceOutputFile so : getFileManager().getOutputsToCreate())
{
ctx.addSequenceOutput(so);
}
ctx.getLogger().debug("total actions tracked in resumer: " + getRecordedActions().size());
for (RecordedAction a : getRecordedActions())
{
ctx.addActions(a);
}
ctx.getFileManager().addIntermediateFiles(getFileManager().getIntermediateFiles());
markComplete();
}
}