Skip to content
This repository was archived by the owner on Sep 12, 2024. It is now read-only.

Commit c7898e2

Browse files
committed
Merge pull request #70 from spotify/rculbertson/temp-jobs-default-expire
Set the expires value each time we create a TempJob
2 parents 1f438a6 + cd1d893 commit c7898e2

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

helios-testing/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
<artifactId>junit</artifactId>
2727
<version>4.11</version>
2828
</dependency>
29+
<dependency>
30+
<groupId>joda-time</groupId>
31+
<artifactId>joda-time</artifactId>
32+
<version>2.3</version>
33+
</dependency>
2934

3035
<!--test deps-->
3136
<dependency>

helios-testing/src/main/java/com/spotify/helios/testing/TemporaryJobBuilder.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@
3333
import com.spotify.helios.common.descriptors.ServiceEndpoint;
3434
import com.spotify.helios.common.descriptors.ServicePorts;
3535

36+
import org.joda.time.DateTime;
37+
3638
import java.io.File;
3739
import java.io.IOException;
3840
import java.net.MalformedURLException;
3941
import java.net.URL;
4042
import java.nio.file.Path;
43+
import java.util.Date;
4144
import java.util.List;
4245
import java.util.Map;
4346
import java.util.Set;
@@ -56,6 +59,7 @@
5659
public class TemporaryJobBuilder {
5760

5861
private static final Pattern JOB_NAME_FORBIDDEN_CHARS = Pattern.compile("[^0-9a-zA-Z-_.]+");
62+
private static final int DEFAULT_EXPIRES_MINUTES = 30;
5963

6064
private final List<String> hosts = Lists.newArrayList();
6165
private final Job.Builder builder = Job.newBuilder();
@@ -149,6 +153,19 @@ public TemporaryJobBuilder hostFilter(final String hostFilter) {
149153
return this;
150154
}
151155

156+
/**
157+
* The Helios master will undeploy and delete the job at the specified date, if it has not
158+
* already been removed. If not set, jobs will be removed after 30 minutes. This is for the
159+
* case when a TemporaryJob is not cleaned up properly, perhaps because the process terminated
160+
* prematurely.
161+
* @param expires the Date when the job should be removed
162+
* @return the TemporaryJobBuilder
163+
*/
164+
public TemporaryJobBuilder expires(final Date expires) {
165+
this.builder.setExpires(expires);
166+
return this;
167+
}
168+
152169
/**
153170
* Deploys the job to the specified hosts. If no hosts are specified, a host will be chosen at
154171
* random from the current Helios cluster. If the HELIOS_HOST_FILTER environment variable is set,
@@ -181,6 +198,12 @@ public TemporaryJob deploy(final List<String> hosts) {
181198
builder.setVersion(randomVersion());
182199
}
183200

201+
// Set job to expires value, if it's not already set. This ensures temporary jobs which
202+
// aren't cleaned up properly by the test will be removed by the master.
203+
if (builder.getExpires() == null) {
204+
builder.setExpires(new DateTime().plusMinutes(DEFAULT_EXPIRES_MINUTES).toDate());
205+
}
206+
184207
if (this.hosts.isEmpty()) {
185208
if (isNullOrEmpty(hostFilter)) {
186209
hostFilter = getenv("HELIOS_HOST_FILTER");

helios-testing/src/test/java/com/spotify/helios/testing/TemporaryJobsTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,27 @@
2727
import com.spotify.helios.common.descriptors.JobStatus;
2828
import com.spotify.helios.system.SystemTestBase;
2929

30+
import org.joda.time.DateTime;
3031
import org.junit.Before;
3132
import org.junit.Rule;
3233
import org.junit.Test;
3334

3435
import java.io.File;
3536
import java.net.Socket;
3637
import java.nio.file.Path;
38+
import java.util.Date;
3739
import java.util.Map;
3840

3941
import static com.google.common.base.Charsets.UTF_8;
4042
import static com.google.common.collect.Iterables.getOnlyElement;
4143
import static com.spotify.helios.common.descriptors.HostStatus.Status.UP;
4244
import static java.util.concurrent.TimeUnit.MINUTES;
4345
import static java.util.concurrent.TimeUnit.SECONDS;
46+
import static org.hamcrest.Matchers.equalTo;
4447
import static org.hamcrest.Matchers.hasKey;
4548
import static org.hamcrest.Matchers.is;
4649
import static org.hamcrest.Matchers.not;
50+
import static org.hamcrest.Matchers.notNullValue;
4751
import static org.junit.Assert.assertArrayEquals;
4852
import static org.junit.Assert.assertEquals;
4953
import static org.junit.Assert.assertFalse;
@@ -202,17 +206,45 @@ public static class JobNamePrefixTest {
202206
.prefixDirectory(prefixDirectory.toString())
203207
.build();
204208

209+
private final Date expires = new DateTime().plusHours(1).toDate();
210+
205211
private TemporaryJob job1;
212+
private TemporaryJob job2;
206213

207214
@Before
208215
public void setup() {
209216
job1 = temporaryJobs.job()
210217
.image(BUSYBOX)
211218
.command(IDLE_COMMAND)
212219
.deploy(testHost);
220+
221+
job2 = temporaryJobs.job()
222+
.image(BUSYBOX)
223+
.command(IDLE_COMMAND)
224+
.expires(expires)
225+
.deploy(testHost);
213226
}
214227

215228
@Test public void testJobPrefixFile() throws Exception {
229+
// Verify a default expires values was set on job1
230+
assertThat(job1.job().getExpires(), is(notNullValue()));
231+
232+
// Verify expires was set correctly on job2
233+
assertThat(job2.job().getExpires(), is(equalTo(expires)));
234+
235+
// Get all jobs from master to make sure values are set correctly there
236+
final Map<JobId, Job> jobs = client.jobs().get();
237+
238+
// Verify job1 was set correctly on master
239+
final Job remoteJob1 = jobs.get(job1.job().getId());
240+
assertThat(remoteJob1, is(notNullValue()));
241+
assertThat(remoteJob1.getExpires(), is(equalTo(job1.job().getExpires())));
242+
243+
// Verify job2 was set correctly on master
244+
final Job remoteJob2 = jobs.get(job2.job().getId());
245+
assertThat(remoteJob2, is(notNullValue()));
246+
assertThat(remoteJob2.getExpires(), equalTo(expires));
247+
216248
// Set jobPrefixFile so we can verify it was deleted after test completed
217249
jobPrefixFile = temporaryJobs.jobPrefixFile();
218250
}

0 commit comments

Comments
 (0)