diff --git a/src/main/java/io/kyligence/notebook/console/notification/NotificationService.java b/src/main/java/io/kyligence/notebook/console/notification/NotificationService.java index 5b2aba5..856cdde 100644 --- a/src/main/java/io/kyligence/notebook/console/notification/NotificationService.java +++ b/src/main/java/io/kyligence/notebook/console/notification/NotificationService.java @@ -21,14 +21,14 @@ @Slf4j public class NotificationService { - private final String NOTIFICATION_SQL = "select 1 as col1 as a;RUN a as FeishuMessageExt.`` where text=\"%s\" AND webhook = \"%s\" as A2;"; + private static final String NOTIFICATION_SQL = "select 1 as col1 as a;RUN a as FeishuMessageExt.`` where text=\"%s\" AND webhook = \"%s\" as A2;"; private static final NotebookConfig config = NotebookConfig.getInstance(); @Autowired EngineService engineService; - public void notification(String scheduleName, long duration, String user, int status) { + public void notification(String notebookName, String scheduleName, long duration, String user, int status) { String webHook = config.getNitificationWebhook(); String header = config.getNitificationMsgHeader(); @@ -55,14 +55,15 @@ public void notification(String scheduleName, long duration, String user, int st String durString = String.format("%02d:%02d:%02d", hours, minutes, seconds); String time = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(Calendar.getInstance().getTime()); - String body = String.format("- Schedule Name: %s\n" + + String body = String.format("- Notebook Name: %s\n" + + "- Schedule Name: %s\n" + "- Schedule Time: %s\n" + "- Duration: %s \n" + "- Execute User: %s\n" + - "- Status: %s", scheduleName, time, durString, user, jobStatusStr); + "- Status: %s", notebookName, scheduleName, time, durString, user, jobStatusStr); String msg = header + "\n" + body; String sql = String.format(NOTIFICATION_SQL, msg, webHook); - String responseBody = engineService.runScript(new EngineService.RunScriptParams() + engineService.runScript(new EngineService.RunScriptParams() .withSql(sql)); } catch (Exception ex) { log.warn("[NotificationService] Exceptions occurred when sending IM notifications." + ex.getStackTrace()); diff --git a/src/main/java/io/kyligence/notebook/console/service/SchedulerService.java b/src/main/java/io/kyligence/notebook/console/service/SchedulerService.java index fdb0c41..4d3f143 100644 --- a/src/main/java/io/kyligence/notebook/console/service/SchedulerService.java +++ b/src/main/java/io/kyligence/notebook/console/service/SchedulerService.java @@ -66,6 +66,17 @@ public void initSchedulers() { } } + public boolean isSentAtFailedLevel() { + return NotificationLevel.valueByLevel(config.getNotificationLevel()).getCode() == NotificationLevel.FAILED.getCode(); + } + + public boolean isSentAtAllLevel() { + return NotificationLevel.valueByLevel(config.getNotificationLevel()).getCode() == NotificationLevel.ALL.getCode(); + } + + public boolean isNeededIMNotification(int status) { + return (isSentAtAllLevel() || (isSentAtFailedLevel() && status == JobInfo.JobStatus.FAILED)); + } public void callback(String token, String scheduleOwner, String entityType, String entityId, String commitId, Integer timeout) { @@ -116,18 +127,12 @@ public void callback(String token, String scheduleOwner, String entityType, jobInfo.setStatus(status); jobService.updateByJobId(jobInfo); long duration = jobInfo.getFinishTime().getTime() - jobInfo.getCreateTime().getTime(); - if (NotificationLevel.valueByLevel( - config.getNotificationLevel()).getCode() == NotificationLevel.FAILED.getCode() - && status == JobInfo.JobStatus.FAILED) { - // send IM when failed - notificationService.notification(jobInfo.getName(), duration, user, status); - } else if (NotificationLevel.valueByLevel(config.getNotificationLevel()).getCode() == NotificationLevel.ALL.getCode()) { - // send IM whenerver job is failed or successed - notificationService.notification(jobInfo.getName(), duration, user, status); + if (isNeededIMNotification(status)) { + // send IM when failed when the notification level is at failed + //or send IM whenever job is failed or successed if the notification level is set as all + notificationService.notification(getEntityName(entityType, Integer.parseInt(entityId)), jobInfo.getName(), duration, scheduleOwner, status); } } - - } public boolean isEnabled() {