Merge branch 'develop' into update_212_Dec_merge_with_21125_chamges
[jalview.git] / src / jalview / ws / jws2 / jabaws2 / JabawsServiceInstance.java
1 package jalview.ws.jws2.jabaws2;
2
3 import jalview.bin.Cache;
4 import jalview.bin.Console;
5 import jalview.gui.WebserviceInfo;
6 import jalview.util.MessageManager;
7 import jalview.ws.gui.WsJob;
8 import jalview.ws.gui.WsJob.JobState;
9 import jalview.ws.jws2.JabaParamStore;
10 import jalview.ws.jws2.JabaPreset;
11 import jalview.ws.jws2.dm.JabaWsParamSet;
12 import jalview.ws.params.WsParamSetI;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import compbio.metadata.Argument;
20 import compbio.metadata.ChunkHolder;
21 import compbio.metadata.JobStatus;
22 import compbio.metadata.Preset;
23
24 /**
25  * Base class for JABAWS service instances. Provides helper methods for
26  * interfacing with Jalview.
27  * 
28  * @author jprocter
29  *
30  */
31 public class JabawsServiceInstance<T extends compbio.data.msa.JManagement>
32         implements
33         jalview.ws.api.JalviewWebServiceI, jalview.ws.api.CancellableI
34 {
35   /**
36    * our service instance handler generated by the discoverer
37    */
38   Jws2Instance our;
39   protected T service;
40   protected Map<JobStatus, JobState> jwsState = new HashMap<>();
41
42   @Override
43   public boolean cancel(WsJob job)
44   {
45     service.cancelJob(job.getJobId());
46     // if the Jaba server indicates the job can't be cancelled, its
47     // because its running on the server's local execution engine
48     // so we just close the window anyway.
49   
50     return true;
51   }
52
53
54   public JabawsServiceInstance(Jws2Instance handle)
55   {
56     our = handle;
57     service = (T) handle.service;
58   }
59
60   @Override
61   public void updateStatus(WsJob job)
62   {
63     JobStatus jwsstatus = service.getJobStatus(job.getJobId());
64     job.setState(jwsState.get(jwsstatus));
65   }
66
67   @Override
68   public boolean updateJobProgress(WsJob job) throws Exception
69   {
70     StringBuilder response = new StringBuilder(job.getStatus());
71     long lastchunk = job.getNextChunk();
72     if (lastchunk == -1)
73     {
74       Console.debug("No more status messages for job " + job.getJobId());
75       return false;
76     }
77     boolean changed = false;
78     do
79     {
80       ChunkHolder chunk = service.pullExecStatistics(job.getJobId(),
81               lastchunk);
82       if (chunk != null)
83       {
84         changed |= chunk.getChunk().length() > 0;
85         response.append(chunk.getChunk());
86         lastchunk = chunk.getNextPosition();
87         try
88         {
89           Thread.sleep(50);
90         } catch (InterruptedException x)
91         {
92         }
93         ;
94       }
95       ;
96       job.setnextChunk(lastchunk);
97     } while (lastchunk >= 0 && job.getNextChunk() != lastchunk);
98     if (job instanceof WsJob)
99     {
100       // TODO decide if WsJob will be the bean for all ng-webservices
101       job.setStatus(response.toString());
102     }
103     return changed;
104   }
105
106   {
107     jwsState.put(JobStatus.CANCELLED, JobState.CANCELLED);
108     jwsState.put(JobStatus.COLLECTED, JobState.FINISHED);
109     jwsState.put(JobStatus.FAILED, JobState.FAILED);
110     jwsState.put(JobStatus.FINISHED, JobState.FINISHED);
111     jwsState.put(JobStatus.PENDING, JobState.QUEUED);
112     jwsState.put(JobStatus.RUNNING, JobState.RUNNING);
113     jwsState.put(JobStatus.STARTED, JobState.RUNNING);
114     jwsState.put(JobStatus.SUBMITTED, JobState.SUBMITTED);
115     jwsState.put(JobStatus.UNDEFINED, JobState.UNKNOWN);
116   }
117
118   public boolean isPresetJob(WsJob job)
119   {
120     return job.getPreset() != null && job.getPreset() instanceof JabaPreset;
121   }
122
123   public Preset getServerPreset(WsJob job)
124   {
125     return (isPresetJob(job))
126             ? ((JabaPreset) job.getPreset()).getJabaPreset()
127             : null;
128   }
129
130   public List<Argument> getJabaArguments(WsParamSetI preset)
131   {
132     List<Argument> newargs = new ArrayList<>();
133     if (preset != null)
134     {
135       if (preset instanceof JabaWsParamSet)
136       {
137         newargs.addAll(((JabaWsParamSet) preset).getjabaArguments());
138       }
139       else
140       {
141         newargs.addAll(
142                 JabaParamStore.getJabafromJwsArgs(preset.getArguments()));
143       }
144     }
145     return newargs;
146   }
147
148   @Override
149   public boolean handleSubmitError(Throwable _lex, WsJob j,
150           WebserviceInfo wsInfo) throws Exception, Error
151   {
152     if (_lex instanceof compbio.metadata.UnsupportedRuntimeException)
153     {
154       wsInfo.appendProgressText(MessageManager.formatMessage(
155               "info.job_couldnt_be_run_server_doesnt_support_program",
156               new String[]
157               { _lex.getMessage() }));
158       wsInfo.warnUser(_lex.getMessage(),
159               MessageManager.getString("warn.service_not_supported"));
160       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_SERVERERROR);
161       wsInfo.setStatus(j.getJobnum(),
162               WebserviceInfo.STATE_STOPPED_SERVERERROR);
163       return true;
164     }
165     if (_lex instanceof compbio.metadata.LimitExceededException)
166     {
167       wsInfo.appendProgressText(MessageManager.formatMessage(
168               "info.job_couldnt_be_run_exceeded_hard_limit", new String[]
169               { _lex.getMessage() }));
170       wsInfo.warnUser(_lex.getMessage(),
171               MessageManager.getString("warn.input_is_too_big"));
172       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
173       wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
174       return true;
175     }
176     if (_lex instanceof compbio.metadata.WrongParameterException)
177     {
178       wsInfo.warnUser(_lex.getMessage(),
179               MessageManager.getString("warn.invalid_job_param_set"));
180       wsInfo.appendProgressText(MessageManager.formatMessage(
181               "info.job_couldnt_be_run_incorrect_param_setting",
182               new String[]
183               { _lex.getMessage() }));
184       wsInfo.setStatus(WebserviceInfo.STATE_STOPPED_ERROR);
185       wsInfo.setStatus(j.getJobnum(), WebserviceInfo.STATE_STOPPED_ERROR);
186       return true;
187     }
188     // pass on to generic error handler
189     return false;
190   }
191
192   @Override
193   public boolean handleCollectionException(Exception ex, WsJob msjob,
194           WebserviceInfo wsInfo)
195   {
196     if (ex instanceof compbio.metadata.ResultNotAvailableException)
197     {
198       // job has failed for some reason - probably due to invalid
199       // parameters
200       Console.debug(
201               "Results not available for finished job - marking as broken job.",
202               ex);
203       String status = msjob.getStatus();
204
205       msjob.setStatus(status
206               + "\nResult not available. Probably due to invalid input or parameter settings. Server error message below:\n\n"
207               + ex.getLocalizedMessage());
208       msjob.setState(WsJob.JobState.BROKEN);
209       return true;
210     }
211     return false;
212   }
213 }