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.concurrent.TimeUnit;
21  import org.apache.commons.lang.time.DurationFormatUtils;
22  
23  /**
24   * Represents a RunDeck execution, usually triggered by an API call. An execution could be a {@link RundeckJob}
25   * execution or an "ad-hoc" execution.
26   * 
27   * @author Vincent Behar
28   */
29  public class RundeckExecution implements Serializable {
30  
31      private static final long serialVersionUID = 1L;
32  
33      private Long id;
34  
35      private String url;
36  
37      private ExecutionStatus status;
38  
39      /** Optional - only if it is a job execution */
40      private RundeckJob job;
41  
42      private String startedBy;
43  
44      private Date startedAt;
45  
46      /** only if the execution has ended */
47      private Date endedAt;
48  
49      /** only if the execution was aborted */
50      private String abortedBy;
51  
52      private String description;
53  
54      /**
55       * @return the duration of the execution in milliseconds (or null if the duration is still running, or has been
56       *         aborted)
57       */
58      public Long getDurationInMillis() {
59          if (startedAt == null || endedAt == null) {
60              return null;
61          }
62          return endedAt.getTime() - startedAt.getTime();
63      }
64  
65      /**
66       * @return the duration of the execution in seconds (or null if the execution is still running, or has been aborted)
67       */
68      public Long getDurationInSeconds() {
69          Long durationInMillis = getDurationInMillis();
70          return durationInMillis != null ? TimeUnit.MILLISECONDS.toSeconds(durationInMillis) : null;
71      }
72  
73      /**
74       * @return the duration of the execution, as a human-readable string : "3 minutes 34 seconds" (or null if the
75       *         execution is still running, or has been aborted)
76       */
77      public String getDuration() {
78          Long durationInMillis = getDurationInMillis();
79          return durationInMillis != null ? DurationFormatUtils.formatDurationWords(durationInMillis, true, true) : null;
80      }
81  
82      /**
83       * @return the duration of the execution, as a "short" human-readable string : "0:03:34.187" (or null if the
84       *         execution is still running, or has been aborted)
85       */
86      public String getShortDuration() {
87          Long durationInMillis = getDurationInMillis();
88          return durationInMillis != null ? DurationFormatUtils.formatDurationHMS(durationInMillis) : null;
89      }
90  
91      public Long getId() {
92          return id;
93      }
94  
95      public void setId(Long id) {
96          this.id = id;
97      }
98  
99      public String getUrl() {
100         return url;
101     }
102 
103     public void setUrl(String url) {
104         this.url = url;
105     }
106 
107     /**
108      * @return the status of the execution - see {@link ExecutionStatus}
109      */
110     public ExecutionStatus getStatus() {
111         return status;
112     }
113 
114     public void setStatus(ExecutionStatus status) {
115         this.status = status;
116     }
117 
118     /**
119      * @return the {@link RundeckJob} associated with this execution, or null in the case of an ad-hoc execution
120      *         (command or script)
121      */
122     public RundeckJob getJob() {
123         return job;
124     }
125 
126     public void setJob(RundeckJob job) {
127         this.job = job;
128     }
129 
130     public String getStartedBy() {
131         return startedBy;
132     }
133 
134     public void setStartedBy(String startedBy) {
135         this.startedBy = startedBy;
136     }
137 
138     public Date getStartedAt() {
139         return (startedAt != null) ? new Date(startedAt.getTime()) : null;
140     }
141 
142     public void setStartedAt(Date startedAt) {
143         this.startedAt = ((startedAt != null) ? new Date(startedAt.getTime()) : null);
144     }
145 
146     public Date getEndedAt() {
147         return (endedAt != null) ? new Date(endedAt.getTime()) : null;
148     }
149 
150     public void setEndedAt(Date endedAt) {
151         this.endedAt = ((endedAt != null) ? new Date(endedAt.getTime()) : null);
152     }
153 
154     public String getAbortedBy() {
155         return abortedBy;
156     }
157 
158     public void setAbortedBy(String abortedBy) {
159         this.abortedBy = abortedBy;
160     }
161 
162     public String getDescription() {
163         return description;
164     }
165 
166     public void setDescription(String description) {
167         this.description = description;
168     }
169 
170     @Override
171     public String toString() {
172         return "RundeckExecution [id=" + id + ", description=" + description + ", url=" + url + ", status=" + status
173                + ", startedBy=" + startedBy + ", startedAt=" + startedAt + ", endedAt=" + endedAt
174                + ", durationInSeconds=" + getDurationInSeconds() + ", abortedBy=" + abortedBy + ", job=" + job + "]";
175     }
176 
177     @Override
178     public int hashCode() {
179         final int prime = 31;
180         int result = 1;
181         result = prime * result + ((abortedBy == null) ? 0 : abortedBy.hashCode());
182         result = prime * result + ((description == null) ? 0 : description.hashCode());
183         result = prime * result + ((endedAt == null) ? 0 : endedAt.hashCode());
184         result = prime * result + ((id == null) ? 0 : id.hashCode());
185         result = prime * result + ((job == null) ? 0 : job.hashCode());
186         result = prime * result + ((startedAt == null) ? 0 : startedAt.hashCode());
187         result = prime * result + ((startedBy == null) ? 0 : startedBy.hashCode());
188         result = prime * result + ((status == null) ? 0 : status.hashCode());
189         result = prime * result + ((url == null) ? 0 : url.hashCode());
190         return result;
191     }
192 
193     @Override
194     public boolean equals(Object obj) {
195         if (this == obj)
196             return true;
197         if (obj == null)
198             return false;
199         if (getClass() != obj.getClass())
200             return false;
201         RundeckExecution other = (RundeckExecution) obj;
202         if (abortedBy == null) {
203             if (other.abortedBy != null)
204                 return false;
205         } else if (!abortedBy.equals(other.abortedBy))
206             return false;
207         if (description == null) {
208             if (other.description != null)
209                 return false;
210         } else if (!description.equals(other.description))
211             return false;
212         if (endedAt == null) {
213             if (other.endedAt != null)
214                 return false;
215         } else if (!endedAt.equals(other.endedAt))
216             return false;
217         if (id == null) {
218             if (other.id != null)
219                 return false;
220         } else if (!id.equals(other.id))
221             return false;
222         if (job == null) {
223             if (other.job != null)
224                 return false;
225         } else if (!job.equals(other.job))
226             return false;
227         if (startedAt == null) {
228             if (other.startedAt != null)
229                 return false;
230         } else if (!startedAt.equals(other.startedAt))
231             return false;
232         if (startedBy == null) {
233             if (other.startedBy != null)
234                 return false;
235         } else if (!startedBy.equals(other.startedBy))
236             return false;
237         if (status == null) {
238             if (other.status != null)
239                 return false;
240         } else if (!status.equals(other.status))
241             return false;
242         if (url == null) {
243             if (other.url != null)
244                 return false;
245         } else if (!url.equals(other.url))
246             return false;
247         return true;
248     }
249 
250     /**
251      * The status of an execution
252      */
253     public static enum ExecutionStatus {
254         RUNNING, SUCCEEDED, FAILED, ABORTED;
255     }
256 
257 }