Skip to content

Commit

Permalink
Merge branch 'fixIndexAliasNaming' of https://github.com/hbz/lobid-re…
Browse files Browse the repository at this point in the history
  • Loading branch information
dr0i committed May 4, 2021
2 parents 6c6564f + 0d5e195 commit cec2cd7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
87 changes: 54 additions & 33 deletions web/app/controllers/resources/Webhook.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
* @author Pascal Christoph (dr0i)
*/
public class Webhook extends Controller {
private static final String FILENAME_UPDATE=
private static final String FILENAME_UPDATE =
Application.CONFIG.getString("webhook.alma.update.filename");
private static final String FILENAME_BASEDUMP=
private static final String FILENAME_BASEDUMP =
Application.CONFIG.getString("webhook.alma.basedump.filename");
private static final String INDEX_NAME =
Application.CONFIG.getString("webhook.alma.indexname");
private static final String INDEX_NAME_OF_BASEDUMP =
Application.CONFIG.getString("webhook.alma.basedump.indexname");
private static final String INDEX_NAME_OF_UPDATE =
Application.CONFIG.getString("webhook.alma.update.indexname");
private static final String TOKEN =
Application.CONFIG.getString("webhook.alma.token");
private static final String EMAIL =
Expand All @@ -30,79 +32,98 @@ public class Webhook extends Controller {
private static final String INDEX_BASEDUMP_ALIAS_SUFFIX = "-staging";
private static final String UPDATE_NEWEST_INDEX = "exact";
private static final String CREATE_INDEX = "create";
private static final String MSG_ETL_PROCESS_IS_ALREADY_RUNNING =
" because an ETL process is already running. Try again later!";
private static final String MSG_UPDATE_ALREADY_RUNNING =
"Couldn't update index '" + INDEX_NAME_OF_UPDATE
+ MSG_ETL_PROCESS_IS_ALREADY_RUNNING;

private static final String MORPH_FILENAME = "alma.xml";
// If null, create default values from Global settings
public static String clusterHost = null;
public static String clusterName = null;
private static String msgWrongToken = "'%s' is the wrong token. Declining to ETL %s.";
private static String msgWrongToken =
"'%s' is the wrong token. Declining to ETL %s.";
private static String msgStartEtl = "Starting ETL of '%s'...";
private static final String MSG_ALREADY_RUNNING ="An ETL is already running. Only one at a time is allowed. Please try again later.";

/**
* Triggers ETL of updates.
*
* @param token the token to authorize updating
* @return "200 ok" or "403 forbidden" (depending on token) or "423 locked"
* in case of an already triggered process that was not yet finished
* @param GIVEN_TOKEN the token to authorize updating
* @return "200 ok" or "403 forbidden" (depending on token) or "423 locked" in
* case of an already triggered process that was not yet finished
*/
public static Result updateAlma(final String GIVEN_TOKEN) {
final String KIND="update";
final String KIND = "update";
if (!GIVEN_TOKEN.equals(TOKEN)) {
return wrongToken(KIND, GIVEN_TOKEN);
}
if (AlmaMarcXml2lobidJsonEs.threadAlreadyStarted) {
sendMail(KIND, false, "Couldn't update index '"+ INDEX_NAME + " because an ETL process is already running. Try again later!");
return status(423, MSG_ALREADY_RUNNING);
sendMail(KIND, false, MSG_UPDATE_ALREADY_RUNNING);
return status(423, MSG_UPDATE_ALREADY_RUNNING);
}
Logger.info(String.format(msgStartEtl, KIND));
AlmaMarcXml2lobidJsonEs.setKindOfEtl(KIND);
AlmaMarcXml2lobidJsonEs.setEmail(EMAIL);
AlmaMarcXml2lobidJsonEs.main(FILENAME_UPDATE, INDEX_NAME,INDEX_UPDATE_ALIAS_SUFFIX,
clusterHost, clusterName, UPDATE_NEWEST_INDEX, MORPH_FILENAME);
sendMail(KIND, true, "Going to update index '"+ INDEX_NAME + "'");
AlmaMarcXml2lobidJsonEs.main(FILENAME_UPDATE, INDEX_NAME_OF_UPDATE,
INDEX_UPDATE_ALIAS_SUFFIX, clusterHost, clusterName,
UPDATE_NEWEST_INDEX, MORPH_FILENAME);
sendMail(KIND, true,
"Going to update index '" + INDEX_NAME_OF_UPDATE + "'");
return ok("... started ETL " + KIND);
}

/**
/**
* Triggers ETL of basedump.
*
* @param token the token to authorize updating
* @return "200 ok" or "403 forbidden" (depending on token) or "423 locked"
* in case of an already triggered process that was not yet finished
* @param GIVEN_TOKEN the token to authorize updating
* @return "200 ok" or "403 forbidden" (depending on token) or "423 locked" in
* case of an already triggered process that was not yet finished
*/
public static Result basedumpAlma(final String GIVEN_TOKEN) {
final String KIND="basedump";
final String KIND = "basedump";
if (!GIVEN_TOKEN.equals(TOKEN)) {
return wrongToken(KIND, GIVEN_TOKEN);
}
String CREATE_INDEX_NAME = INDEX_NAME + "-" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd-kkmm"));
if (AlmaMarcXml2lobidJsonEs.threadAlreadyStarted){
sendMail(KIND, false, "Couldn't created new index with name "+ CREATE_INDEX_NAME + " because an ETL process is already running. Try again later!");
return status(423, MSG_ALREADY_RUNNING);
final String CREATE_INDEX_NAME_OF_BASEDUMP =
INDEX_NAME_OF_BASEDUMP + "-" + LocalDateTime.now()
.format(DateTimeFormatter.ofPattern("yyyyMMdd-kkmm"));
final String MSG_CREATE_INDEX_ALREADY_RUNNING =
"Couldn't created new index with name " + CREATE_INDEX_NAME_OF_BASEDUMP
+ MSG_ETL_PROCESS_IS_ALREADY_RUNNING;
if (AlmaMarcXml2lobidJsonEs.threadAlreadyStarted) {
sendMail(KIND, false, MSG_CREATE_INDEX_ALREADY_RUNNING);
return status(423, MSG_CREATE_INDEX_ALREADY_RUNNING);
}
Logger.info(String.format(msgStartEtl, KIND));
AlmaMarcXml2lobidJsonEs.setKindOfEtl(KIND);
AlmaMarcXml2lobidJsonEs.setEmail(EMAIL);
AlmaMarcXml2lobidJsonEs.main(FILENAME_BASEDUMP, CREATE_INDEX_NAME,
INDEX_BASEDUMP_ALIAS_SUFFIX, clusterHost, clusterName, CREATE_INDEX,
MORPH_FILENAME);
sendMail(KIND, true, "Going to created new index with name "+ CREATE_INDEX_NAME + " , adding " + INDEX_BASEDUMP_ALIAS_SUFFIX +" to alias of index");
AlmaMarcXml2lobidJsonEs.main(FILENAME_BASEDUMP,
CREATE_INDEX_NAME_OF_BASEDUMP, INDEX_BASEDUMP_ALIAS_SUFFIX, clusterHost,
clusterName, CREATE_INDEX, MORPH_FILENAME);
sendMail(KIND, true,
"Going to created new index with name " + CREATE_INDEX_NAME_OF_BASEDUMP

+ " , adding " + INDEX_BASEDUMP_ALIAS_SUFFIX
+ " to alias of index");
return ok("... started ETL " + KIND);
}

private static Result wrongToken (final String KIND, final String TOKEN) {
private static Result wrongToken(final String KIND, final String TOKEN) {
String msg = String.format(msgWrongToken, TOKEN, KIND);
Logger.error(msg);
sendMail(KIND, false, msg);
return forbidden(msg);
}

private static void sendMail(final String KIND, final boolean SUCCESS, final String MESSAGE) {
private static void sendMail(final String KIND, final boolean SUCCESS,
final String MESSAGE) {
try {
Email.sendEmail("hduser", EMAIL, "Webhook ETL of " + KIND + " triggered:" + (SUCCESS ? "success :)" : "fails :("), MESSAGE);
Email.sendEmail("hduser", EMAIL, "Webhook ETL of " + KIND + " triggered:"
+ (SUCCESS ? "success :)" : "fails :("), MESSAGE);
} catch (Exception e) {
Logger.error("Couldn't send email", e.toString());
Logger.error("Couldn't send email", e.toString());
}
}

}
}
3 changes: 2 additions & 1 deletion web/conf/resources.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ webhook = {
alma = {
update = {
filename = "/data/other/datenportal/export/alma/prod/update.xml.bgzf"
indexname = "almaresources-staging"
}
basedump = {
filename = "/data/other/datenportal/export/alma/prod/baseline.xml.bgzf"
indexname = "almaresources"
}
token = "123"
indexname = "almaresources"
}
email = "change@me"
}
Expand Down

0 comments on commit cec2cd7

Please sign in to comment.