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