View Javadoc

1   /*
2    * Copyright 2011 Vincent Behar
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.rundeck.api.domain;
17  
18  import java.io.Serializable;
19  import java.util.Date;
20  import java.util.Set;
21  import java.util.concurrent.TimeUnit;
22  import org.apache.commons.lang.time.DurationFormatUtils;
23  
24  /**
25   * Represents a Rundeck execution, usually triggered by an API call. An execution could be a {@link RundeckJob}
26   * execution or an "ad-hoc" execution.
27   * 
28   * @author Vincent Behar
29   */
30  public class RundeckExecution implements Serializable {
31  
32      private static final long serialVersionUID = 1L;
33  
34      private Long id;
35  
36      private String url;
37  
38      private ExecutionStatus status;
39  
40      /** Optional - only if it is a job execution */
41      private RundeckJob job;
42  
43      private String startedBy;
44  
45      private Date startedAt;
46  
47      /** only if the execution has ended */
48      private Date endedAt;
49  
50      /** only if the execution was aborted */
51      private String abortedBy;
52  
53      private String description;
54      private String argstring;
55      private String project;
56      private Set<RundeckNodeIdentity> successfulNodes;
57      private Set<RundeckNodeIdentity> failedNodes;
58  
59      /**
60       * @return the duration of the execution in milliseconds (or null if the duration is still running, or has been
61       *         aborted)
62       */
63      public Long getDurationInMillis() {
64          if (startedAt == null || endedAt == null) {
65              return null;
66          }
67          return endedAt.getTime() - startedAt.getTime();
68      }
69  
70      /**
71       * @return the duration of the execution in seconds (or null if the execution is still running, or has been aborted)
72       */
73      public Long getDurationInSeconds() {
74          Long durationInMillis = getDurationInMillis();
75          return durationInMillis != null ? TimeUnit.MILLISECONDS.toSeconds(durationInMillis) : null;
76      }
77  
78      /**
79       * @return the duration of the execution, as a human-readable string : "3 minutes 34 seconds" (or null if the
80       *         execution is still running, or has been aborted)
81       */
82      public String getDuration() {
83          Long durationInMillis = getDurationInMillis();
84          return durationInMillis != null ? DurationFormatUtils.formatDurationWords(durationInMillis, true, true) : null;
85      }
86  
87      /**
88       * @return the duration of the execution, as a "short" human-readable string : "0:03:34.187" (or null if the
89       *         execution is still running, or has been aborted)
90       */
91      public String getShortDuration() {
92          Long durationInMillis = getDurationInMillis();
93          return durationInMillis != null ? DurationFormatUtils.formatDurationHMS(durationInMillis) : null;
94      }
95  
96      public Long getId() {
97          return id;
98      }
99  
100     public void setId(Long id) {
101         this.id = id;
102     }
103 
104     public String getUrl() {
105         return url;
106     }
107 
108     public void setUrl(String url) {
109         this.url = url;
110     }
111 
112     /**
113      * @return the status of the execution - see {@link ExecutionStatus}
114      */
115     public ExecutionStatus getStatus() {
116         return status;
117     }
118 
119     public void setStatus(ExecutionStatus status) {
120         this.status = status;
121     }
122 
123     /**
124      * @return the {@link RundeckJob} associated with this execution, or null in the case of an ad-hoc execution
125      *         (command or script)
126      */
127     public RundeckJob getJob() {
128         return job;
129     }
130 
131     public void setJob(RundeckJob job) {
132         this.job = job;
133     }
134 
135     public String getStartedBy() {
136         return startedBy;
137     }
138 
139     public void setStartedBy(String startedBy) {
140         this.startedBy = startedBy;
141     }
142 
143     public Date getStartedAt() {
144         return (startedAt != null) ? new Date(startedAt.getTime()) : null;
145     }
146 
147     public void setStartedAt(Date startedAt) {
148         this.startedAt = ((startedAt != null) ? new Date(startedAt.getTime()) : null);
149     }
150 
151     public Date getEndedAt() {
152         return (endedAt != null) ? new Date(endedAt.getTime()) : null;
153     }
154 
155     public void setEndedAt(Date endedAt) {
156         this.endedAt = ((endedAt != null) ? new Date(endedAt.getTime()) : null);
157     }
158 
159     public String getAbortedBy() {
160         return abortedBy;
161     }
162 
163     public void setAbortedBy(String abortedBy) {
164         this.abortedBy = abortedBy;
165     }
166 
167     public String getDescription() {
168         return description;
169     }
170 
171     public void setDescription(String description) {
172         this.description = description;
173     }
174 
175     @Override
176     public String toString() {
177         return "RundeckExecution [id=" + id + ", description=" + description + ", url=" + url + ", status=" + status
178                + ", argstring=" + argstring
179                + ", startedBy=" + startedBy + ", startedAt=" + startedAt + ", endedAt=" + endedAt
180                + ", durationInSeconds=" + getDurationInSeconds() + ", abortedBy=" + abortedBy + ", job=" + job
181                + ", successfulNodes=" + getSuccessfulNodes()
182                 + ", failedNodes=" + getFailedNodes()
183                 + "]";
184     }
185 
186     @Override
187     public int hashCode() {
188         final int prime = 31;
189         int result = 1;
190         result = prime * result + ((abortedBy == null) ? 0 : abortedBy.hashCode());
191         result = prime * result + ((argstring == null) ? 0 : argstring.hashCode());
192         result = prime * result + ((description == null) ? 0 : description.hashCode());
193         result = prime * result + ((endedAt == null) ? 0 : endedAt.hashCode());
194         result = prime * result + ((id == null) ? 0 : id.hashCode());
195         result = prime * result + ((job == null) ? 0 : job.hashCode());
196         result = prime * result + ((startedAt == null) ? 0 : startedAt.hashCode());
197         result = prime * result + ((startedBy == null) ? 0 : startedBy.hashCode());
198         result = prime * result + ((status == null) ? 0 : status.hashCode());
199         result = prime * result + ((url == null) ? 0 : url.hashCode());
200         result = prime * result + ((project == null) ? 0 : project.hashCode());
201         return result;
202     }
203 
204     @Override
205     public boolean equals(Object obj) {
206         if (this == obj)
207             return true;
208         if (obj == null)
209             return false;
210         if (getClass() != obj.getClass())
211             return false;
212         RundeckExecution other = (RundeckExecution) obj;
213         if (abortedBy == null) {
214             if (other.abortedBy != null)
215                 return false;
216         } else if (!abortedBy.equals(other.abortedBy))
217             return false;
218         if (argstring == null) {
219             if (other.argstring != null)
220                 return false;
221         } else if (!argstring.equals(other.argstring))
222             return false;
223         if (description == null) {
224             if (other.description != null)
225                 return false;
226         } else if (!description.equals(other.description))
227             return false;
228         if (endedAt == null) {
229             if (other.endedAt != null)
230                 return false;
231         } else if (!endedAt.equals(other.endedAt))
232             return false;
233         if (id == null) {
234             if (other.id != null)
235                 return false;
236         } else if (!id.equals(other.id))
237             return false;
238         if (job == null) {
239             if (other.job != null)
240                 return false;
241         } else if (!job.equals(other.job))
242             return false;
243         if (startedAt == null) {
244             if (other.startedAt != null)
245                 return false;
246         } else if (!startedAt.equals(other.startedAt))
247             return false;
248         if (startedBy == null) {
249             if (other.startedBy != null)
250                 return false;
251         } else if (!startedBy.equals(other.startedBy))
252             return false;
253         if (status == null) {
254             if (other.status != null)
255                 return false;
256         } else if (!status.equals(other.status))
257             return false;
258         if (url == null) {
259             if (other.url != null)
260                 return false;
261         } else if (!url.equals(other.url))
262             return false;
263         if (project == null) {
264             if (other.project != null)
265                 return false;
266         } else if (!project.equals(other.project))
267             return false;
268         return true;
269     }
270 
271     /**
272      * the argument string for the execution
273      */
274     public String getArgstring() {
275         return argstring;
276     }
277 
278     public void setArgstring(String argstring) {
279         this.argstring = argstring;
280     }
281 
282     public String getProject() {
283         return project;
284     }
285 
286     public void setProject(String project) {
287         this.project = project;
288     }
289 
290     public Set<RundeckNodeIdentity> getSuccessfulNodes() {
291         return successfulNodes;
292     }
293 
294     public void setSuccessfulNodes(Set<RundeckNodeIdentity> successfulNodes) {
295         this.successfulNodes = successfulNodes;
296     }
297 
298     public Set<RundeckNodeIdentity> getFailedNodes() {
299         return failedNodes;
300     }
301 
302     public void setFailedNodes(Set<RundeckNodeIdentity> failedNodes) {
303         this.failedNodes = failedNodes;
304     }
305 
306     /**
307      * The status of an execution
308      */
309     public static enum ExecutionStatus {
310         RUNNING, SUCCEEDED, FAILED, ABORTED, FAILED_WITH_RETRY,TIMEDOUT;
311     }
312 
313 }