mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
feat: support webhook #79
This commit is contained in:
parent
19112db038
commit
528920379d
@ -1,9 +1,6 @@
|
|||||||
package com.github.kfcfans.powerjob.common.utils;
|
package com.github.kfcfans.powerjob.common.utils;
|
||||||
|
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.*;
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import okhttp3.Response;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@ -34,7 +31,12 @@ public class HttpUtils {
|
|||||||
.build();
|
.build();
|
||||||
try (Response response = client.newCall(request).execute()) {
|
try (Response response = client.newCall(request).execute()) {
|
||||||
if (response.code() == HTTP_SUCCESS_CODE) {
|
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;
|
return null;
|
||||||
|
@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user