feat: support webhook #79

This commit is contained in:
KFCFans 2020-11-14 11:18:04 +08:00
parent 19112db038
commit 528920379d
2 changed files with 49 additions and 5 deletions

View File

@ -1,9 +1,6 @@
package com.github.kfcfans.powerjob.common.utils;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.*;
import java.io.IOException;
import java.util.Objects;
@ -34,7 +31,12 @@ public class HttpUtils {
.build();
try (Response response = client.newCall(request).execute()) {
if (response.code() == HTTP_SUCCESS_CODE) {
return Objects.requireNonNull(response.body()).string();
ResponseBody body = response.body();
if (body == null) {
return null;
}else {
return body.string();
}
}
}
return null;

View File

@ -0,0 +1,42 @@
package com.github.kfcfans.powerjob.server.service.alarm.impl;
import com.github.kfcfans.powerjob.common.utils.HttpUtils;
import com.github.kfcfans.powerjob.server.persistence.core.model.UserInfoDO;
import com.github.kfcfans.powerjob.server.service.alarm.Alarm;
import com.github.kfcfans.powerjob.server.service.alarm.Alarmable;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.List;
/**
* http 回调报警
*
* @author tjq
* @since 11/14/20
*/
@Slf4j
@Service
public class WebHookAlarmService implements Alarmable {
@Override
public void onFailed(Alarm alarm, List<UserInfoDO> targetUserList) {
if (CollectionUtils.isEmpty(targetUserList)) {
return;
}
targetUserList.forEach(user -> {
String webHook = user.getWebHook();
if (StringUtils.isEmpty(webHook)) {
return;
}
try {
String response = HttpUtils.get(webHook);
log.info("[WebHookAlarmService] invoke webhook[url={}] successfully, response is {}", webHook, response);
}catch (Exception e) {
log.warn("[WebHookAlarmService] invoke webhook[url={}] failed!", webHook, e);
}
});
}
}