public JobStateTransitions stateTransitions() { final JobStateTransitions transitions = new JobStateTransitions(); for (JobInstance job : this) { transitions.addAll(job.getTransitions()); } return transitions; }
public JobStateTransitions(JobStateTransition... list) { for (JobStateTransition jobStateTransition : list) { this.add(jobStateTransition); } }
public JobStateTransition getTransition(JobState state) { return stateTransitions.byState(state); }
@Test public void shouldReturnBuildingTransitionTimeAsStartBuildingDate() { final Date date = new Date(); JobInstance instance = JobInstanceMother.scheduled("jobConfig1"); JobStateTransitions transitions = new JobStateTransitions( new JobStateTransition(JobState.Building, date)); instance.setTransitions(transitions); assertThat(instance.getStartedDateFor(JobState.Building), is(date)); }
private void assertCopiedJob(JobInstance newJava, final long originalId) { assertThat(newJava.getId(), is(-1l)); assertThat(newJava.getTransitions().isEmpty(), is(false)); assertThat(newJava.getResult(), is(Passed)); assertThat(newJava.getState(), is(Completed)); assertThat(newJava.getTransitions().byState(Scheduled).getId(), is(-1l)); assertThat(newJava.getTransitions().byState(Completed).getId(), is(-1l)); assertThat(newJava.getOriginalJobId(), is(originalId)); assertThat(newJava.isRerun(), is(false)); assertThat(newJava.isCopy(), is(true)); }
private JobStateTransition assertNewJob(JobInstance newRails) { assertThat(newRails.getId(), is(-1l)); assertThat(newRails.getTransitions().size(), is(1)); JobStateTransition newSchedulingTransition = newRails.getTransitions().byState(JobState.Scheduled); assertThat(newSchedulingTransition.getId(), is(-1l)); assertThat(newRails.getResult(), is(Unknown)); assertThat(newRails.getState(), is(Scheduled)); assertThat(newRails.isRerun(), is(true)); return newSchedulingTransition; }
public long latestTransitionId() { return stateTransitions.latestTransitionId(); }
@Test public void shouldCreateATransitionOnStateChange() throws Exception { JobInstance instance = JobInstanceMother.scheduled("jobConfig1"); instance.completing(JobResult.Passed); final JobStateTransition scheduledState = new JobStateTransition(JobState.Scheduled, new Date()); final JobStateTransition completedState = new JobStateTransition(JobState.Completing, new Date()); assertThat(instance.getTransitions(), hasItem(scheduledState)); assertThat(instance.getTransitions(), hasItem(completedState)); assertThat(instance.getTransitions().first(), not(isTransitionWithState(JobState.Preparing))); }
public int hashCode() { int result1; result1 = (int) (stageId ^ (stageId >>> 32)); result1 = 31 * result1 + (name != null ? name.hashCode() : 0); result1 = 31 * result1 + (state != null ? state.hashCode() : 0); result1 = 31 * result1 + (result != null ? result.hashCode() : 0); result1 = 31 * result1 + (agentUuid != null ? agentUuid.hashCode() : 0); result1 = 31 * result1 + (stateTransitions != null ? stateTransitions.hashCode() : 0); result1 = 31 * result1 + (scheduledDate != null ? scheduledDate.hashCode() : 0); result1 = 31 * result1 + (timeProvider != null ? timeProvider.hashCode() : 0); result1 = 31 * result1 + (ignored ? 1 : 0); result1 = 31 * result1 + (identifier != null ? identifier.hashCode() : 0); result1 = 31 * result1 + (plan != null ? plan.hashCode() : 0); return result1; }
return false; if (stateTransitions != null ? !stateTransitions.equals( instance.stateTransitions) : instance.stateTransitions != null) { return false;
public static void toJSON(OutputWriter jsonWriter, JobInstance jobInstance) { jsonWriter.add("id", jobInstance.getId()); jsonWriter.add("name", jobInstance.getName()); if (jobInstance.getState() != null) { jsonWriter.add("state", jobInstance.getState().toString()); } if (jobInstance.getResult() != null) { jsonWriter.add("result", jobInstance.getResult().toString()); } if (jobInstance.getScheduledDate() != null) { jsonWriter.add("scheduled_date", jobInstance.getScheduledDate().getTime()); } jsonWriter.add("rerun", jobInstance.isRerun()); if (jobInstance.getOriginalJobId() == null) { jsonWriter.add("original_job_id", (String) null); } else { jsonWriter.add("original_job_id", jobInstance.getOriginalJobId()); } jsonWriter.addWithDefaultIfBlank("agent_uuid", jobInstance.getAgentUuid(), (String) null); jsonWriter.add("pipeline_name", (String) null); jsonWriter.add("pipeline_counter", (String) null); jsonWriter.add("stage_name", (String) null); jsonWriter.add("stage_counter", (String) null); jsonWriter.addChildList("job_state_transitions", jobStateTransitionsWriter -> jobInstance.getTransitions().forEach( jobStateTransition -> jobStateTransitionsWriter.addChild( jobStateTransitionWriter -> JobStateTransitionRepresenter.toJSON(jobStateTransitionWriter, jobStateTransition)))); } }
public static JobInstance buildEndingWithState(JobState endState, JobResult result, String jobConfig) { final JobInstance instance = new JobInstance(jobConfig); instance.setAgentUuid("1234"); instance.setId(1L); instance.setIdentifier(defaultJobIdentifier(jobConfig)); instance.setState(endState); instance.setTransitions(new JobStateTransitions()); DateTime now = new DateTime(); Date scheduledDate = now.minusMinutes(5).toDate(); instance.setScheduledDate(scheduledDate); List<JobState> orderedStates = orderedBuildStates(); DateTime stateDate = new DateTime(scheduledDate); for (JobState stateToCompareTo : orderedStates) { if (endState.compareTo(stateToCompareTo) >= 0) { instance.changeState(stateToCompareTo, stateDate.toDate()); stateDate = stateDate.plusMinutes(1); } } if (endState.equals(JobState.Completed)) { instance.setResult(result); } return instance; }
public static Stage stageWithNBuildsHavingEndState(JobState endState, JobResult result, String stageName, List<String> buildNames) { JobInstances builds = new JobInstances(); for (String buildName : buildNames) { builds.add(JobInstanceMother.buildEndingWithState(endState, result, buildName)); } Stage stage = new Stage(stageName, builds, DEFAULT_APPROVED_BY, null, null, new TimeProvider()); stage.calculateResult(); stage.setCompletedByTransitionId(stage.getJobInstances().last().getTransitions().latestTransitionId()); return stage; }
@Test public void shouldNotCreateATransitionWhenPreviousStateIsTheSame() throws Exception { JobInstance instance = JobInstanceMother.scheduled("jobConfig1"); instance.changeState(JobState.Scheduled); final JobStateTransition scheduledState = new JobStateTransition(JobState.Scheduled, new Date()); assertThat(instance.getTransitions(), hasItem(scheduledState)); assertThat(instance.getTransitions(), iterableWithSize(1)); assertThat(instance.getTransitions().first(), not(isTransitionWithState(JobState.Preparing))); }
public Date getStartedDateFor(JobState state) { JobStateTransition transition = this.stateTransitions.byState(state); return transition == null ? null : transition.getStateChangeTime(); }
public void changeState(JobState newState, Date stateChangeTime) { if (this.state != newState) { this.state = newState; stateTransitions.add(new JobStateTransition(newState, stateChangeTime)); } }
@Test public void shouldReturnMostRecentCompletedTransitionAsCompletedDateIfLatestTransitionIdIsNot() { firstJob.assign("AGENT-1", time1.toDate()); firstJob.completing(JobResult.Passed, time2.toDate()); firstJob.completed(time2.toDate()); secondJob.assign("AGENT-2", time3.toDate()); secondJob.completing(JobResult.Passed, time4.toDate()); secondJob.completed(time4.toDate()); secondJob.getTransitions().byState(JobState.Completed).setId(1); stage.calculateResult(); assertThat(stage.completedDate(), is(time4.toDate())); }
public static JobInstance completed(String jobConfigName, JobResult result, Date completedDate, Date startbuilding) { JobInstance instance = new JobInstance(jobConfigName); instance.completing(result, completedDate); instance.completed(completedDate); instance.getTransitions().add(new JobStateTransition(JobState.Building, startbuilding)); return instance; }
@Test public void shouldChangeStatus() throws Exception { JobInstance instance = JobInstanceMother.scheduled("jobConfig1"); instance.assign("1234", timeProvider.currentTime()); assertThat(instance.getState(), is(JobState.Assigned)); assertThat(instance.getTransitions().byState(JobState.Assigned), not(nullValue())); }