Skip to content

Commit

Permalink
Add worker tags table and insert/update rest API (#159)
Browse files Browse the repository at this point in the history
* - Added sql table
- Added Insert/Update REST API for worker tags

* Added line feed
  • Loading branch information
jamesfwood authored Jul 13, 2023
1 parent 5b6cc4b commit 969d9bc
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
33 changes: 33 additions & 0 deletions cws-core/src/main/java/jpl/cws/core/db/SchedulerDbService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,39 @@ public List<Map<String,Object>> getProcInstStatus(String procInstId, Integer lim
}


/**
*
*/
public void updateWorkerTag(String workerId, String name, String value) throws Exception {

// If we are adding a tag, then first try inserting a new row.
// If the insert actually inserted a new row, then we are done.
// If the row already existed, then update the row.
//
int numUpdated = jdbcTemplate.update(
"INSERT IGNORE INTO cws_worker_tags " +
"(worker_id, name, value) " +
"VALUES (?, ?, ?)",
new Object[] {workerId, name, value});
if (numUpdated == 0) {
// row was already there, so just update it
numUpdated = jdbcTemplate.update(
"UPDATE cws_worker_tags " +
"SET value=?, updated_time=? " +
"WHERE worker_id=? AND name=?",
new Object[] {
value,
new Timestamp(DateTime.now().getMillis()),
workerId,
name});
log.debug("Updated " + numUpdated + " row(s) in the cws_worker_tags table...");
}
else {
log.debug("Inserted " + numUpdated + " row(s) into the cws_worker_tags table...");
}
}


/**
*
*/
Expand Down
5 changes: 4 additions & 1 deletion cws-core/src/main/java/jpl/cws/core/web/JsonResponse.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jpl.cws.core.web;

import com.google.gson.GsonBuilder;
import org.apache.commons.lang.StringEscapeUtils;

public class JsonResponse {
public enum Status {
Expand All @@ -25,6 +26,8 @@ public String getMessage() {
}

public String toString() {
return new GsonBuilder().setPrettyPrinting().create().toJson(this);
String json = new GsonBuilder().setPrettyPrinting().create().toJson(this);

return StringEscapeUtils.unescapeJava(json);
}
}
31 changes: 29 additions & 2 deletions cws-service/src/main/java/jpl/cws/controller/RestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,35 @@ public GsonUTCDateAdapter() {

return "success";
}




/**
* Inserts or updates worker tag with name and value
*
*/
@RequestMapping(value = "/worker/{workerId}/updateTag/{name}", method = POST, produces="application/json")
public @ResponseBody String updateWorkerTag(
HttpServletResponse response,
@PathVariable String workerId,
@PathVariable String name,
@RequestParam(value = "value") String value) {

try {
dbService.updateWorkerTag(workerId, name, value);
} catch (Exception e) {
log.error("Unexpected error", e);

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

return new JsonResponse(JsonResponse.Status.FAIL, e.getMessage()).toString();
}

response.setStatus(HttpServletResponse.SC_OK);

return new JsonResponse(JsonResponse.Status.SUCCESS, "Updated worker '" + workerId + "' tag: " + name + "='" + value + "'").toString();
}


/**
* Checks if procDefKey is deployed (exists)
*
Expand Down
12 changes: 11 additions & 1 deletion install/sql/core.sql.template
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ CREATE TABLE IF NOT EXISTS `cws_worker` (
UNIQUE KEY (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `cws_worker_tags` (
`worker_id` varchar(255) COLLATE utf8_bin NOT NULL,
`name` varchar(255) COLLATE utf8_bin NOT NULL,
`value` varchar(1000) COLLATE utf8_bin DEFAULT NULL,
`created_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
`updated_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP(),
PRIMARY KEY (`worker_id`, `name`),
CONSTRAINT `FK_cws_worker_tags` FOREIGN KEY (`worker_id`) REFERENCES `cws_worker` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE IF NOT EXISTS `cws_external_worker` (
`id` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',
Expand All @@ -36,7 +45,8 @@ CREATE TABLE IF NOT EXISTS `cws_log_usage` (
`worker_id` varchar(255) COLLATE utf8_bin NOT NULL,
`filename` varchar(255) COLLATE utf8_bin NOT NULL,
`size_bytes` bigint NOT NULL,
PRIMARY KEY (`worker_id`, `filename`)
PRIMARY KEY (`worker_id`, `filename`),
CONSTRAINT `FK_cws_worker_log_usage` FOREIGN KEY (`worker_id`) REFERENCES `cws_worker` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;


Expand Down

0 comments on commit 969d9bc

Please sign in to comment.