Skip to content

Commit

Permalink
#168 revise the IM info when scheduled task failed (#171)
Browse files Browse the repository at this point in the history
* revise the IM info when scheduled task failed

* refactor the if statement to determine whether to send the IM notification
  • Loading branch information
ckeys authored Jul 22, 2022
1 parent 07fe954 commit 57f1050
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 57f1050

Please sign in to comment.