Skip to content

Commit c39f6ea

Browse files
committed
feat(deployment): notify users when deployment to OTP finishes or fails
refs catalogueglobal/datatools-ui#18
1 parent c81db6f commit c39f6ea

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

src/main/java/com/conveyal/datatools/common/status/MonitorableJob.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
88

9+
import java.io.Serializable;
910
import java.time.LocalDateTime;
1011
import java.time.format.DateTimeFormatter;
1112
import java.util.*;
@@ -14,8 +15,8 @@
1415
/**
1516
* Created by landon on 6/13/16.
1617
*/
17-
public abstract class MonitorableJob implements Runnable {
18-
18+
public abstract class MonitorableJob implements Runnable, Serializable {
19+
private static final long serialVersionUID = 1L;
1920
private static final Logger LOG = LoggerFactory.getLogger(MonitorableJob.class);
2021
protected final String owner;
2122

@@ -204,8 +205,8 @@ public void addNextJob(MonitorableJob ...jobs) {
204205
/**
205206
* Represents the current status of this job.
206207
*/
207-
public static class Status implements Cloneable {
208-
208+
public static class Status implements Serializable {
209+
private static final long serialVersionUID = 1L;
209210
/** What message (defined in messages.<lang>) should be displayed to the user? */
210211
public String message;
211212

src/main/java/com/conveyal/datatools/manager/jobs/DeployJob.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import java.io.FileNotFoundException;
1313
import java.io.IOException;
1414
import java.io.InputStream;
15+
import java.io.Serializable;
1516
import java.net.HttpURLConnection;
1617
import java.net.MalformedURLException;
1718
import java.net.URL;
@@ -24,6 +25,7 @@
2425
import com.conveyal.datatools.common.status.MonitorableJob;
2526
import com.conveyal.datatools.manager.models.Deployment;
2627
import com.conveyal.datatools.manager.persistence.FeedStore;
28+
import com.fasterxml.jackson.annotation.JsonProperty;
2729
import org.slf4j.Logger;
2830
import org.slf4j.LoggerFactory;
2931

@@ -58,6 +60,11 @@ public class DeployJob extends MonitorableJob {
5860
/** This hides the status field on the parent class, providing additional fields. */
5961
public DeployStatus status;
6062

63+
@JsonProperty
64+
public String getDeploymentId () {
65+
return deployment.id;
66+
}
67+
6168
public DeployJob(Deployment deployment, String owner, List<String> targets, String publicUrl, String s3Bucket, String s3CredentialsFilename) {
6269
// TODO add new job type or get rid of enum in favor of just using class names
6370
super(owner, "Deploying " + deployment.name, JobType.DEPLOY_TO_OTP);
@@ -92,7 +99,7 @@ public void jobLogic () {
9299

93100
LOG.info("Created deployment bundle file: " + deploymentTempFile.getAbsolutePath());
94101

95-
// dump the deployment bundle
102+
// Dump the deployment bundle to the temp file.
96103
try {
97104
status.message = "Creating OTP Bundle";
98105
this.deployment.dump(deploymentTempFile, true, true, true);
@@ -106,10 +113,10 @@ public void jobLogic () {
106113
}
107114

108115
status.percentComplete = 100.0 * (double) tasksCompleted / totalTasks;
109-
System.out.println("pctComplete = " + status.percentComplete);
116+
LOG.info("Deployment pctComplete = {}", status.percentComplete);
110117
status.built = true;
111118

112-
// upload to S3, if applicable
119+
// Upload to S3, if applicable
113120
if(this.s3Bucket != null) {
114121
status.message = "Uploading to S3";
115122
status.uploadingS3 = true;
@@ -147,7 +154,7 @@ public void jobLogic () {
147154
status.uploadingS3 = false;
148155
}
149156

150-
// if no OTP targets (i.e. we're only deploying to S3), we're done
157+
// If there are no OTP targets (i.e. we're only deploying to S3), we're done.
151158
if(this.targets == null) {
152159
status.completed = true;
153160
return;
@@ -156,7 +163,7 @@ public void jobLogic () {
156163
// figure out what router we're using
157164
String router = deployment.routerId != null ? deployment.routerId : "default";
158165

159-
// load it to OTP
166+
// Send the deployment file over the wire to each OTP server.
160167
for (String rawUrl : this.targets) {
161168
status.message = "Deploying to " + rawUrl;
162169
status.uploading = true;
@@ -253,6 +260,7 @@ public void jobLogic () {
253260
input.close();
254261
} catch (IOException e) {
255262
// do nothing
263+
LOG.warn("Could not close input stream for deployment file.");
256264
}
257265

258266
status.uploading = false;
@@ -301,32 +309,26 @@ public void jobFinished () {
301309
if (!deleted) {
302310
LOG.error("Deployment {} not deleted! Disk space in danger of filling up.", deployment.id);
303311
}
304-
312+
String message;
305313
if (!status.error) {
306314
// Update status with successful completion state only if no error was encountered.
307315
status.update(false, "Deployment complete!", 100, true);
316+
message = String.format("Deployment %s successfully deployed to %s", deployment.name, publicUrl);
317+
} else {
318+
message = String.format("WARNING: Deployment %s failed to deploy to %s", deployment.name, publicUrl);
308319
}
320+
// Send notification to those subscribed to updates for the deployment.
321+
NotifyUsersForSubscriptionJob.createNotification("deployment-updated", deployment.id, message);
309322
}
310323

311324
/**
312325
* Represents the current status of this job.
313326
*/
314327
public static class DeployStatus extends Status {
315-
// /** What error message (defined in messages.<lang>) should be displayed to the user? */
316-
// public String message;
317-
//
318-
// /** Is this deployment completed (successfully or unsuccessfully) */
319-
// public boolean completed;
320-
321-
// /** Was there an error? */
322-
// public boolean error;
323-
328+
private static final long serialVersionUID = 1L;
324329
/** Did the manager build the bundle successfully */
325330
public boolean built;
326331

327-
// /** Is the bundle currently being uploaded to the server? */
328-
// public boolean uploading;
329-
330332
/** Is the bundle currently being uploaded to an S3 bucket? */
331333
public boolean uploadingS3;
332334

src/main/java/com/conveyal/datatools/manager/jobs/NotifyUsersForSubscriptionJob.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.conveyal.datatools.manager.jobs;
22

33
import com.conveyal.datatools.manager.DataManager;
4+
import com.conveyal.datatools.manager.models.Deployment;
45
import com.conveyal.datatools.manager.models.FeedSource;
56
import com.conveyal.datatools.manager.models.Project;
67
import com.conveyal.datatools.manager.persistence.Persistence;
@@ -106,6 +107,21 @@ private void notifyUsersForSubscription() {
106107
// Add action text.
107108
html += String.format("<p>View <a href='%s/project/%s'>this project</a>.</p>", APPLICATION_URL, p.id);
108109
break;
110+
case "deployment":
111+
Deployment deployment = Persistence.deployments.getById(this.target);
112+
// Format subject header
113+
subject = String.format(
114+
"%s Notification: %s (%s)",
115+
applicationName,
116+
subscriptionToString,
117+
deployment.name);
118+
// Add action text.
119+
html += String.format(
120+
"<p>View <a href='%s/project/%s/deployments/%s'>this deployment</a>.</p>",
121+
APPLICATION_URL,
122+
deployment.projectId,
123+
deployment.id);
124+
break;
109125
default:
110126
LOG.warn("Notifications not supported for subscription type {}", subType[0]);
111127
return;

0 commit comments

Comments
 (0)