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.Util;
\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 compbio.engine.client.Util.writeMarker(workDirectory,
\r
60 JobStatus.CANCELLED);
\r
61 log.debug("Cancelling local job from work directory " + workDirectory);
\r
62 return future.cancel(true);
\r
65 public static JobStatus getJobStatus(Future<ConfiguredExecutable<?>> future) {
\r
66 // Order is important here as cancelled tasks also considered done!
\r
67 if (future == null) {
\r
68 throw new NullPointerException("Future must be provided!");
\r
70 if (future.isCancelled()) {
\r
71 return JobStatus.CANCELLED;
\r
73 if (future.isDone()) {
\r
74 return JobStatus.FINISHED;
\r
76 return JobStatus.RUNNING;
\r
79 public static JobStatus getRecordedJobStatus(String jobId) {
\r
80 // job has been removed from the task list
\r
81 // but there may be status written to the disk
\r
82 String workDir = Configurator.getWorkDirectory(jobId);
\r
83 if (Util.isMarked(workDir, JobStatus.FINISHED)
\r
84 || Util.isMarked(workDir, JobStatus.COLLECTED)) {
\r
85 return JobStatus.FINISHED;
\r
87 if (Util.isMarked(workDir, JobStatus.CANCELLED)) {
\r
88 return JobStatus.CANCELLED;
\r
90 if (Util.isMarked(workDir, JobStatus.FAILED)) {
\r
91 return JobStatus.FAILED;
\r
93 return JobStatus.UNDEFINED;
\r
96 public static boolean cleanup(ConfiguredExecutable<?> confExecutable) {
\r
97 if (confExecutable == null) {
\r
98 throw new NullPointerException("Future must be provided!");
\r
100 return Cleaner.deleteFiles(confExecutable);
\r
103 public static ConfiguredExecutable<?> getResults(
\r
104 Future<ConfiguredExecutable<?>> future, final String taskId)
\r
105 throws ResultNotAvailableException {
\r
106 ConfiguredExecutable<?> exec = null;
\r
108 exec = future.get();
\r
109 if (exec == null) {
\r
110 throw new ResultNotAvailableException(
\r
111 "Job return null as a Result! Job work directory is "
\r
112 + Configurator.getWorkDirectory(taskId)
\r
113 + " Job id is " + taskId);
\r
115 compbio.engine.client.Util.writeMarker(Configurator
\r
116 .getWorkDirectory(taskId), JobStatus.COLLECTED);
\r
117 } catch (InterruptedException e) {
\r
118 // reassert threads interrupted status
\r
119 Thread.currentThread().interrupt();
\r
120 compbio.engine.client.Util.writeMarker(Configurator
\r
121 .getWorkDirectory(taskId), JobStatus.FAILED);
\r
123 log.debug("Cancelling job due to Interruption");
\r
124 future.cancel(true);
\r
125 // do not clean up leave files untouched
\r
126 // this.cleanup(taskId);
\r
127 } catch (ExecutionException e) {
\r
128 // this.cleanup(taskId);
\r
129 compbio.engine.client.Util.writeMarker(Configurator
\r
130 .getWorkDirectory(taskId), JobStatus.FAILED);
\r
131 log.debug("Job execution exception: " + e.getLocalizedMessage(), e
\r
133 // ExecutionException returned as thus Throwable needs unwrapping
\r
134 LocalEngineUtil.launderThrowable(e.getCause());
\r
136 future.cancel(true);// harmless if the task already completed
\r
137 // whatever happens remove task from list
\r
138 SubmissionManager.removeTask(taskId);
\r