1 /* Copyright (c) 2009 Peter Troshin
\r
3 * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0
\r
5 * This library is free software; you can redistribute it and/or modify it under the terms of the
\r
6 * Apache License version 2 as published by the Apache Software Foundation
\r
8 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
\r
9 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache
\r
10 * License for more details.
\r
12 * A copy of the license is in apache_license.txt. It is also available here:
\r
13 * @see: http://www.apache.org/licenses/LICENSE-2.0.txt
\r
15 * Any republication or derived work distributed in source code form
\r
16 * must include this copyright and license notice.
\r
19 package compbio.engine.local;
\r
21 import java.util.concurrent.ExecutionException;
\r
22 import java.util.concurrent.Future;
\r
24 import org.apache.log4j.Logger;
\r
26 import compbio.engine.Cleaner;
\r
27 import compbio.engine.Configurator;
\r
28 import compbio.engine.SubmissionManager;
\r
29 import compbio.engine.client.ConfiguredExecutable;
\r
30 import compbio.engine.client.EngineUtil;
\r
31 import compbio.metadata.JobStatus;
\r
32 import compbio.metadata.ResultNotAvailableException;
\r
34 public final class LocalEngineUtil {
\r
36 private static final Logger log = Logger.getLogger(LocalEngineUtil.class);
\r
39 * Coerce an unchecked Throwable to a RuntimeException or Error
\r
44 static RuntimeException launderThrowable(Throwable throwable) {
\r
45 if (throwable instanceof RuntimeException) {
\r
46 return (RuntimeException) throwable;
\r
47 } else if (throwable instanceof Error) {
\r
48 throw (Error) throwable;
\r
51 throw new IllegalStateException(
\r
52 "Checked exception being thrown and unwrapped by LocalRunner.launderThrowable method",
\r
57 public static boolean cancelJob(Future<ConfiguredExecutable<?>> future,
\r
58 String workDirectory) {
\r
59 EngineUtil.writeMarker(workDirectory, JobStatus.CANCELLED);
\r
60 log.debug("Cancelling local job from work directory " + workDirectory);
\r
61 return future.cancel(true);
\r
64 public static JobStatus getJobStatus(Future<ConfiguredExecutable<?>> future) {
\r
65 // Order is important here as cancelled tasks also considered done!
\r
66 if (future == null) {
\r
67 throw new NullPointerException("Future must be provided!");
\r
69 if (future.isCancelled()) {
\r
70 return JobStatus.CANCELLED;
\r
72 if (future.isDone()) {
\r
73 return JobStatus.FINISHED;
\r
75 return JobStatus.RUNNING;
\r
78 public static JobStatus getRecordedJobStatus(String jobId) {
\r
79 // job has been removed from the task list
\r
80 // but there may be status written to the disk
\r
81 String workDir = Configurator.getWorkDirectory(jobId);
\r
82 if (EngineUtil.isMarked(workDir, JobStatus.FINISHED)
\r
83 || EngineUtil.isMarked(workDir, JobStatus.COLLECTED)) {
\r
84 return JobStatus.FINISHED;
\r
86 if (EngineUtil.isMarked(workDir, JobStatus.CANCELLED)) {
\r
87 return JobStatus.CANCELLED;
\r
89 if (EngineUtil.isMarked(workDir, JobStatus.FAILED)) {
\r
90 return JobStatus.FAILED;
\r
92 return JobStatus.UNDEFINED;
\r
95 public static boolean cleanup(ConfiguredExecutable<?> confExecutable) {
\r
96 if (confExecutable == null) {
\r
97 throw new NullPointerException("Future must be provided!");
\r
99 return Cleaner.deleteFiles(confExecutable);
\r
102 public static ConfiguredExecutable<?> getResults(
\r
103 Future<ConfiguredExecutable<?>> future, final String taskId)
\r
104 throws ResultNotAvailableException {
\r
105 ConfiguredExecutable<?> exec = null;
\r
107 exec = future.get();
\r
108 if (exec == null) {
\r
109 throw new ResultNotAvailableException(
\r
110 "Job return null as a Result! Job work directory is "
\r
111 + Configurator.getWorkDirectory(taskId)
\r
112 + " Job id is " + taskId);
\r
114 EngineUtil.writeMarker(Configurator.getWorkDirectory(taskId), JobStatus.COLLECTED);
\r
115 } catch (InterruptedException e) {
\r
116 // reassert threads interrupted status
\r
117 Thread.currentThread().interrupt();
\r
118 EngineUtil.writeMarker(Configurator.getWorkDirectory(taskId), JobStatus.FAILED);
\r
120 log.debug("Cancelling job due to Interruption");
\r
121 future.cancel(true);
\r
122 // do not clean up leave files untouched
\r
123 // this.cleanup(taskId);
\r
124 } catch (ExecutionException e) {
\r
125 // this.cleanup(taskId);
\r
126 EngineUtil.writeMarker(Configurator .getWorkDirectory(taskId), JobStatus.FAILED);
\r
127 log.debug("Job execution exception: " + e.getLocalizedMessage(), e.getCause());
\r
128 // ExecutionException returned as thus Throwable needs unwrapping
\r
129 LocalEngineUtil.launderThrowable(e.getCause());
\r
131 future.cancel(true);// harmless if the task already completed
\r
132 // whatever happens remove task from list
\r
133 SubmissionManager.removeTask(taskId);
\r