diff --git a/src/backend/commons/common-i18n/src/main/java/com/tencent/bk/job/common/i18n/locale/BkConsts.java b/src/backend/commons/common-i18n/src/main/java/com/tencent/bk/job/common/i18n/locale/BkConsts.java new file mode 100644 index 0000000000..13603e7f0f --- /dev/null +++ b/src/backend/commons/common-i18n/src/main/java/com/tencent/bk/job/common/i18n/locale/BkConsts.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.i18n.locale; + +/** + * 蓝鲸公共协议涉及的常量 + */ +public class BkConsts { + + // 国际化语言Header取值 + public static final String HEADER_VALUE_LANG_ZH = "zh"; + public static final String HEADER_VALUE_LANG_ZH_CN = "zh-cn"; + public static final String HEADER_VALUE_LANG_EN = "en"; + public static final String HEADER_VALUE_LANG_EN_US = "en-us"; + +} diff --git a/src/backend/commons/common-i18n/src/main/java/com/tencent/bk/job/common/i18n/locale/LocaleUtils.java b/src/backend/commons/common-i18n/src/main/java/com/tencent/bk/job/common/i18n/locale/LocaleUtils.java index 9ea4b7ff19..f3bc1e2112 100644 --- a/src/backend/commons/common-i18n/src/main/java/com/tencent/bk/job/common/i18n/locale/LocaleUtils.java +++ b/src/backend/commons/common-i18n/src/main/java/com/tencent/bk/job/common/i18n/locale/LocaleUtils.java @@ -38,6 +38,7 @@ public class LocaleUtils { public static final String LANG_ZH = "zh"; public static final String LANG_EN = "en"; public static final String LANG_EN_US = "en_US"; + /** * 蓝鲸通用的LANG HEADER */ @@ -50,19 +51,28 @@ public class LocaleUtils { private static final Map localeMap = new HashMap<>(); static { - localeMap.put("zh", LANG_ZH); - localeMap.put("zh-cn", LANG_ZH_CN); + localeMap.put(BkConsts.HEADER_VALUE_LANG_ZH, LANG_ZH); + localeMap.put(BkConsts.HEADER_VALUE_LANG_ZH_CN, LANG_ZH_CN); localeMap.put("zh_cn", LANG_ZH_CN); - localeMap.put("en", LANG_EN); - localeMap.put("en-us", LANG_EN_US); + localeMap.put(BkConsts.HEADER_VALUE_LANG_EN, LANG_EN); + localeMap.put(BkConsts.HEADER_VALUE_LANG_EN_US, LANG_EN_US); localeMap.put("en_us", LANG_EN_US); } + private static final Map bkLangMap = new HashMap<>(); + + static { + bkLangMap.put(LANG_ZH, BkConsts.HEADER_VALUE_LANG_ZH); + bkLangMap.put(LANG_ZH_CN, BkConsts.HEADER_VALUE_LANG_ZH_CN); + bkLangMap.put(LANG_EN, BkConsts.HEADER_VALUE_LANG_EN); + bkLangMap.put(LANG_EN_US, BkConsts.HEADER_VALUE_LANG_EN_US); + } + /** * 对不同形式的language Header值进行统一 * - * @param lang - * @return + * @param lang 语言Header取值 + * @return 标准化后的语言Header取值 */ public static String getNormalLang(String lang) { lang = lang.toLowerCase(); @@ -72,6 +82,19 @@ public static String getNormalLang(String lang) { return lang; } + /** + * 根据标准语言取值获取蓝鲸协议语言参数取值 + * + * @param normalLang 标准语言取值 + * @return 蓝鲸协议语言参数取值 + */ + public static String getBkLang(String normalLang) { + if (bkLangMap.containsKey(normalLang)) { + return bkLangMap.get(normalLang); + } + return null; + } + public static Locale getLocale(String lang) { if (StringUtils.isBlank(lang)) { return Locale.SIMPLIFIED_CHINESE; diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message.properties index af68f58073..4e4ebc7734 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message.properties @@ -77,6 +77,10 @@ #第三方API请求错误 1216001=第三方 API 请求错误 +#消息通知中心异常 +1217001=消息通知中心API不存在:{0} +1217002=消息通知中心接口数据异常 + ##业务错误-通用 1241001=请求参数缺失 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en.properties index 38a6616fe5..ce7cf7bfb3 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en.properties @@ -77,6 +77,10 @@ #第三方API请求错误 1216001=Third API data unexpected +#消息通知中心异常 +1217001=BK-Notice API not found:{0} +1217002=BK-Notice API data unexpected + ## Business error - common 1241001=Missing request parameters 1241002=Invalid request parameters diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en_US.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en_US.properties index 38a6616fe5..ce7cf7bfb3 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en_US.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_en_US.properties @@ -77,6 +77,10 @@ #第三方API请求错误 1216001=Third API data unexpected +#消息通知中心异常 +1217001=BK-Notice API not found:{0} +1217002=BK-Notice API data unexpected + ## Business error - common 1241001=Missing request parameters 1241002=Invalid request parameters diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh.properties index af68f58073..4e4ebc7734 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh.properties @@ -77,6 +77,10 @@ #第三方API请求错误 1216001=第三方 API 请求错误 +#消息通知中心异常 +1217001=消息通知中心API不存在:{0} +1217002=消息通知中心接口数据异常 + ##业务错误-通用 1241001=请求参数缺失 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh_CN.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh_CN.properties index 27f6afa0b8..b058a7153e 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh_CN.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/exception/message_zh_CN.properties @@ -77,6 +77,10 @@ #第三方API请求错误 1216001=第三方 API 请求错误 +#消息通知中心异常 +1217001=消息通知中心API不存在:{0} +1217002=消息通知中心接口数据异常 + ##业务错误-通用 1241001=请求参数缺失 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties index 9829fb0fa1..55e374504e 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages.properties @@ -73,3 +73,5 @@ validation.constraints.AccountDescription_tooLong.message=账号描述不能超 validation.constraints.InvalidJobInstanceId.message=任务实例ID必须为正整数 validation.constraints.InvalidStepInstanceId.message=步骤实例ID必须为正整数 validation.constraints.queryAgentInfoHostIds_tooMany.message=单次查询主机数量不可超过5000 +validation.constraints.InvalidAnnouncementsOffset.message=公告偏移量必须大于等于0 +validation.constraints.InvalidAnnouncementsLimit.message=公告获取数量必须大于等于1 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties index 5b05cece31..c908964129 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en.properties @@ -72,3 +72,5 @@ validation.constraints.AccountDescription_tooLong.message=Account description le validation.constraints.InvalidJobInstanceId.message=Job instance id must be a positive number validation.constraints.InvalidStepInstanceId.message=Step instance id must be a positive number validation.constraints.queryAgentInfoHostIds_tooMany.message=HostIds size can not be over 5000 +validation.constraints.InvalidAnnouncementsOffset.message=Announcements offset must be equal or greater than 0 +validation.constraints.InvalidAnnouncementsLimit.message=Announcements limit must be equal or greater than 1 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties index 5b05cece31..c908964129 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_en_US.properties @@ -72,3 +72,5 @@ validation.constraints.AccountDescription_tooLong.message=Account description le validation.constraints.InvalidJobInstanceId.message=Job instance id must be a positive number validation.constraints.InvalidStepInstanceId.message=Step instance id must be a positive number validation.constraints.queryAgentInfoHostIds_tooMany.message=HostIds size can not be over 5000 +validation.constraints.InvalidAnnouncementsOffset.message=Announcements offset must be equal or greater than 0 +validation.constraints.InvalidAnnouncementsLimit.message=Announcements limit must be equal or greater than 1 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties index 4114a706e0..3340848b6b 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh.properties @@ -72,3 +72,5 @@ validation.constraints.AccountDescription_tooLong.message=账号描述不能超 validation.constraints.InvalidJobInstanceId.message=任务实例ID必须为正整数 validation.constraints.InvalidStepInstanceId.message=步骤实例ID必须为正整数 validation.constraints.queryAgentInfoHostIds_tooMany.message=单次查询主机数量不可超过5000 +validation.constraints.InvalidAnnouncementsOffset.message=公告偏移量必须大于等于0 +validation.constraints.InvalidAnnouncementsLimit.message=公告获取数量必须大于等于1 diff --git a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties index 2191591ec2..e62ef5c1c1 100644 --- a/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties +++ b/src/backend/commons/common-i18n/src/main/resources/i18n/validation/ValidationMessages_zh_CN.properties @@ -72,3 +72,5 @@ validation.constraints.AccountDescription_tooLong.message=账号描述不能超 validation.constraints.InvalidJobInstanceId.message=任务实例ID必须为正整数 validation.constraints.InvalidStepInstanceId.message=步骤实例ID必须为正整数 validation.constraints.queryAgentInfoHostIds_tooMany.message=单次查询主机数量不可超过5000 +validation.constraints.InvalidAnnouncementsOffset.message=公告偏移量必须大于等于0 +validation.constraints.InvalidAnnouncementsLimit.message=公告获取数量必须大于等于1 diff --git a/src/backend/commons/common-service/src/main/resources/logback-default.xml b/src/backend/commons/common-service/src/main/resources/logback-default.xml index 97bf863197..93935e0c53 100644 --- a/src/backend/commons/common-service/src/main/resources/logback-default.xml +++ b/src/backend/commons/common-service/src/main/resources/logback-default.xml @@ -126,6 +126,7 @@ Default job logback configuration provided for import + ${AUDIT_EVENT_LOG_FILE} diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ErrorCode.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ErrorCode.java index 95c5495700..d4ce0f7006 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ErrorCode.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/constant/ErrorCode.java @@ -38,6 +38,90 @@ public class ErrorCode { */ public static final int RESULT_OK = 0; + // ==================================== 系统级错误 ================================================// + + // ======= 系统错误-平台服务错误 =======// + // LICENSE 不可用 + public static final int LICENSE_ERROR = 1210101; + // GSE 不可用 + public static final int GSE_ERROR = 1210001; + // GSE数据异常:{0} + public static final int GSE_API_DATA_ERROR = 1210002; + + // CMDB错误 + // CMDB服务状态不可达 - 地址配置错误或者地址无法正确解析 + public static final int CMDB_UNREACHABLE_SERVER = 1211001; + // CMDB接口返回数据结构异常- 一般是被网关防火墙重定向返回统一登录页面 + public static final int CMDB_API_DATA_ERROR = 1211002; + // 根据动态分组ID查找主机失败,动态分组ID:{0},原因:{1},请确认指定的动态分组在业务下是否存在 + public static final int FAIL_TO_FIND_HOST_BY_DYNAMIC_GROUP = 1211003; + // 根据业务ID查找动态分组失败,业务ID:{0},原因:{1},请确认指定的业务是否存在动态分组 + public static final int FAIL_TO_FIND_DYNAMIC_GROUP_BY_BIZ = 1211004; + + // PaaS异常 + // CMSI接口访问异常 + public static final int CMSI_API_ACCESS_ERROR = 1213001; + // 用户管理接口访问异常 + public static final int USER_MANAGE_API_ACCESS_ERROR = 1213002; + // 调用CMSI接口获取通知渠道数据异常 + public static final int CMSI_MSG_CHANNEL_DATA_ERROR = 1213003; + // 调用CMSI接口发送通知失败,错误码:{0},错误信息:{1} + public static final int CMSI_FAIL_TO_SEND_MSG = 1213004; + + // 制品库异常 + // Artifactory接口返回数据结构异常 + public static final int ARTIFACTORY_API_DATA_ERROR = 1214001; + // 制品库中找不到节点:{0},请到制品库核实 + public static final int CAN_NOT_FIND_NODE_IN_ARTIFACTORY = 1214002; + + // IAM接口数据异常- 一般是被网关防火墙重定向返回统一登录页面 + public static final int IAM_API_DATA_ERROR = 1215001; + // 用户({0})账号已被冻结,请处理后再重试 + public static final int IAM_USER_ACCOUNT_FROZEN = 1215002; + + // 第三方API请求错误 + public static final int API_ERROR = 1216001; + + // 消息通知中心异常 + // 消息通知中心API不存在:{0} + public static final int BK_NOTICE_API_NOT_FOUND = 1217001; + // 消息通知中心接口数据异常 + public static final int BK_NOTICE_API_DATA_ERROR = 1217002; + + // ======== 系统错误-权限错误 ==================// + // 用户({0})权限不足,请前往权限中心确认并申请补充后重试 + public static final int PERMISSION_DENIED = 1238001; + // 蓝鲸统一权限错误码,用户({0})权限不足,请前往权限中心确认并申请补充后重试 + public static final int BK_PERMISSION_DENIED = 9900403; + + // ========= 系统错误-请求 ====================// + // 内部服务异常 + public static final int INTERNAL_ERROR = 1240002; + // 错误的请求 + public static final int BAD_REQUEST = 1240003; + // Cookie过期或者不存在 + public static final int COOKIE_ILLEGAL = 1240004; + // 服务不可用 + public static final int SERVICE_UNAVAILABLE = 1240001; + // 服务认证失败 + public static final int SERVICE_AUTH_FAIL = 1240005; + // 配置异常:{0} + public static final int INVALID_CONFIG = 1240006; + // 命令行参数异常:{0} + public static final int INVALID_CMD_ARGS = 1240007; + + // ======= 系统错误-公共组件错误 =======// + // Redis服务不可用,连接不上 - IP不存在或者配置错误 + public static final int REDIS_CONNECT_FAIL = 1250001; + // Redis服务内存满或者其他问题 - 内存不足够 + public static final int REDIS_DATA_EXCEPTION = 1250002; + // DB 不可用 + public static final int DB_ERROR = 1252001; + // MQ 不可用 + public static final int MQ_ERROR = 1255001; + // NFS存储 不可用 + public static final int NFS_ERROR = 1259001; + // ==================================== 业务级错误 ================================================// /* * 业务通用-1241xxx @@ -291,6 +375,10 @@ public class ErrorCode { public static final int FAIL_TO_DOWNLOAD_NODE_FROM_ARTIFACTORY = 1249002; // 备份服务 end + // ======= 系统错误-公共组件错误 =======// + // 1250xxx-1259xxx被【系统错误-公共组件错误】占用 + // ======= 系统错误-公共组件错误 =======// + // 文件网关 start // 文件源不存在:{0} public static final int FILE_SOURCE_NOT_EXIST = 1260001; @@ -354,81 +442,4 @@ public class ErrorCode { // 迁移失败,任务: {0}, 详情: {1} public static final int MIGRATION_FAIL = 1263001; - // ==================================== 系统级错误 ================================================// - // ======== 系统错误-权限错误 ==================// - // 用户({0})权限不足,请前往权限中心确认并申请补充后重试 - public static final int PERMISSION_DENIED = 1238001; - // 蓝鲸统一权限错误码,用户({0})权限不足,请前往权限中心确认并申请补充后重试 - public static final int BK_PERMISSION_DENIED = 9900403; - - // ========= 系统错误-请求 ====================// - // 内部服务异常 - public static final int INTERNAL_ERROR = 1240002; - // 错误的请求 - public static final int BAD_REQUEST = 1240003; - // Cookie过期或者不存在 - public static final int COOKIE_ILLEGAL = 1240004; - // 服务不可用 - public static final int SERVICE_UNAVAILABLE = 1240001; - // 服务认证失败 - public static final int SERVICE_AUTH_FAIL = 1240005; - // 配置异常:{0} - public static final int INVALID_CONFIG = 1240006; - // 命令行参数异常:{0} - public static final int INVALID_CMD_ARGS = 1240007; - - // ======= 系统错误-公共组件错误 =======// - // Redis服务不可用,连接不上 - IP不存在或者配置错误 - public static final int REDIS_CONNECT_FAIL = 1250001; - // Redis服务内存满或者其他问题 - 内存不足够 - public static final int REDIS_DATA_EXCEPTION = 1250002; - // NFS存储 不可用 - public static final int NFS_ERROR = 1259001; - // DB 不可用 - public static final int DB_ERROR = 1252001; - // MQ 不可用 - public static final int MQ_ERROR = 1255001; - - // ======= 系统错误-平台服务错误 =======// - // LICENSE 不可用 - public static final int LICENSE_ERROR = 1210101; - // GSE 不可用 - public static final int GSE_ERROR = 1210001; - // GSE数据异常:{0} - public static final int GSE_API_DATA_ERROR = 1210002; - - // CMDB错误 - // CMDB服务状态不可达 - 地址配置错误或者地址无法正确解析 - public static final int CMDB_UNREACHABLE_SERVER = 1211001; - // CMDB接口返回数据结构异常- 一般是被网关防火墙重定向返回统一登录页面 - public static final int CMDB_API_DATA_ERROR = 1211002; - // 根据动态分组ID查找主机失败,动态分组ID:{0},原因:{1},请确认指定的动态分组在业务下是否存在 - public static final int FAIL_TO_FIND_HOST_BY_DYNAMIC_GROUP = 1211003; - // 根据业务ID查找动态分组失败,业务ID:{0},原因:{1},请确认指定的业务是否存在动态分组 - public static final int FAIL_TO_FIND_DYNAMIC_GROUP_BY_BIZ = 1211004; - - // PaaS异常 - // CMSI接口访问异常 - public static final int CMSI_API_ACCESS_ERROR = 1213001; - // 用户管理接口访问异常 - public static final int USER_MANAGE_API_ACCESS_ERROR = 1213002; - // 调用CMSI接口获取通知渠道数据异常 - public static final int CMSI_MSG_CHANNEL_DATA_ERROR = 1213003; - // 调用CMSI接口发送通知失败,错误码:{0},错误信息:{1} - public static final int CMSI_FAIL_TO_SEND_MSG = 1213004; - - // 制品库异常 - // Artifactory接口返回数据结构异常 - public static final int ARTIFACTORY_API_DATA_ERROR = 1214001; - // 制品库中找不到节点:{0},请到制品库核实 - public static final int CAN_NOT_FIND_NODE_IN_ARTIFACTORY = 1214002; - - // IAM接口数据异常- 一般是被网关防火墙重定向返回统一登录页面 - public static final int IAM_API_DATA_ERROR = 1215001; - // 用户({0})账号已被冻结,请处理后再重试 - public static final int IAM_USER_ACCOUNT_FROZEN = 1215002; - - // 第三方API请求错误 - public static final int API_ERROR = 1216001; - } diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/exception/HttpStatusException.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/exception/HttpStatusException.java new file mode 100644 index 0000000000..3c51b3d38f --- /dev/null +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/exception/HttpStatusException.java @@ -0,0 +1,46 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.exception; + +import lombok.Getter; +import lombok.ToString; + +/** + * 服务异常 + */ +@Getter +@ToString +public class HttpStatusException extends RuntimeException { + private final String uri; + private final int httpStatus; + private final String reasonPhrase; + + public HttpStatusException(String uri, int httpStatus, String reasonPhrase) { + super("http status not ok, uri=" + uri + ", statusCode=" + httpStatus + ", reasonPhrase=" + reasonPhrase); + this.uri = uri; + this.httpStatus = httpStatus; + this.reasonPhrase = reasonPhrase; + } +} diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/metrics/CommonMetricNames.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/metrics/CommonMetricNames.java index 771c68f787..0fba47495d 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/metrics/CommonMetricNames.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/metrics/CommonMetricNames.java @@ -76,6 +76,15 @@ public class CommonMetricNames { */ public static final String IAM_API = "job.client.iam.api"; + /** + * 仅统计调用消息通知中心后台 API的HTTP请求过程 + */ + public static final String BK_NOTICE_API_HTTP = "job.client.bknotice.api.http"; + /** + * 统计调用消息通知中心后台 API整个过程,含反序列化 + */ + public static final String BK_NOTICE_API = "job.client.bknotice.api"; + /** * 被调用ESB API diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/BaseHttpHelper.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/BaseHttpHelper.java index 462f11d547..9cc6b30d31 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/BaseHttpHelper.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/http/BaseHttpHelper.java @@ -26,6 +26,7 @@ import com.tencent.bk.job.common.constant.ErrorCode; import com.tencent.bk.job.common.constant.HttpMethodEnum; +import com.tencent.bk.job.common.exception.HttpStatusException; import com.tencent.bk.job.common.exception.InternalException; import com.tencent.bk.job.common.exception.NotImplementedException; import lombok.extern.slf4j.Slf4j; @@ -173,17 +174,18 @@ private HttpResponse execute(HttpRequestBase httpClientRequest, HttpContext cont if (entity != null && entity.getContent() != null) { respStr = new String(EntityUtils.toByteArray(entity), CHARSET); } - if (httpStatusCode != HttpStatus.SC_OK) { - String message = httpResponse.getStatusLine().getReasonPhrase(); + // 状态码>=400判定为失败 + if (httpStatusCode >= HttpStatus.SC_BAD_REQUEST) { + String reasonPhrase = httpResponse.getStatusLine().getReasonPhrase(); log.warn( "Request fail, method: {}, url={}, httpStatusCode={}, errorReason={}, body={},", httpClientRequest.getMethod(), httpClientRequest.getURI().getPath(), httpStatusCode, - message, + reasonPhrase, respStr ); - throw new InternalException(message, ErrorCode.API_ERROR); + throw new HttpStatusException(httpClientRequest.getURI().toString(), httpStatusCode, reasonPhrase); } else { return new HttpResponse(httpStatusCode, respStr); } diff --git a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/config/BkApiGatewayProperties.java b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/config/BkApiGatewayProperties.java index eb68b7452b..896e487106 100644 --- a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/config/BkApiGatewayProperties.java +++ b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/config/BkApiGatewayProperties.java @@ -35,12 +35,14 @@ public class BkApiGatewayProperties { - private GseApiGwConfig gse; + private ApiGwConfig gse; + + private ApiGwConfig bkNotice; @Getter @Setter @ToString - public static class GseApiGwConfig { + public static class ApiGwConfig { /** * 蓝鲸Api Gateway url */ diff --git a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/model/EsbResp.java b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/model/EsbResp.java index c232135c87..a6722407e1 100644 --- a/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/model/EsbResp.java +++ b/src/backend/commons/esb-sdk/src/main/java/com/tencent/bk/job/common/esb/model/EsbResp.java @@ -137,6 +137,6 @@ public static EsbResp buildValidateFailResp(ErrorDetailDTO errorDetail) { @JsonIgnore public boolean isSuccess() { - return this.code == ErrorCode.RESULT_OK; + return (this.result != null && this.result) || (this.code != null && this.code == ErrorCode.RESULT_OK); } } diff --git a/src/backend/commons/notice-sdk/build.gradle b/src/backend/commons/notice-sdk/build.gradle new file mode 100644 index 0000000000..907fb546e8 --- /dev/null +++ b/src/backend/commons/notice-sdk/build.gradle @@ -0,0 +1,41 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +dependencies { + api project(':commons:common') + api project(':commons:esb-sdk') + api project(':commons:common-i18n') + implementation 'com.fasterxml.jackson.core:jackson-core' + implementation 'com.fasterxml.jackson.core:jackson-databind' + implementation 'com.fasterxml.jackson.core:jackson-annotations' + implementation 'org.apache.commons:commons-lang3' + implementation "net.sf.dozer:dozer" + implementation 'io.micrometer:micrometer-registry-prometheus' + implementation 'org.apache.commons:commons-collections4' + implementation 'org.apache.httpcomponents:httpclient' + compileOnly 'org.projectlombok:lombok' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.junit.jupiter:junit-jupiter' + testImplementation 'org.springframework.boot:spring-boot-starter-test' +} diff --git a/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/IBkNoticeClient.java b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/IBkNoticeClient.java new file mode 100644 index 0000000000..3b14bbab83 --- /dev/null +++ b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/IBkNoticeClient.java @@ -0,0 +1,38 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.notice; + +import com.tencent.bk.job.common.notice.model.AnnouncementDTO; +import com.tencent.bk.job.common.notice.model.BkNoticeApp; + +import java.util.List; + +public interface IBkNoticeClient { + + BkNoticeApp registerApplication(); + + List getCurrentAnnouncements(String bkLanguage, Integer offset, Integer limit); + +} diff --git a/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/config/BkNoticeProperties.java b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/config/BkNoticeProperties.java new file mode 100644 index 0000000000..60abda52fd --- /dev/null +++ b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/config/BkNoticeProperties.java @@ -0,0 +1,41 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.notice.config; + +import lombok.Getter; +import lombok.Setter; +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * 消息通知中心配置 + */ +@ConfigurationProperties(prefix = "bk-notice") +@Getter +@Setter +public class BkNoticeProperties { + + private boolean enabled = true; + +} diff --git a/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/config/NoticeAutoConfiguration.java b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/config/NoticeAutoConfiguration.java new file mode 100644 index 0000000000..a51a34904a --- /dev/null +++ b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/config/NoticeAutoConfiguration.java @@ -0,0 +1,48 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.notice.config; + +import com.tencent.bk.job.common.esb.config.AppProperties; +import com.tencent.bk.job.common.esb.config.BkApiGatewayProperties; +import com.tencent.bk.job.common.notice.impl.BkNoticeClient; +import io.micrometer.core.instrument.MeterRegistry; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Slf4j +@Configuration(proxyBeanMethods = false) +@EnableConfigurationProperties({BkNoticeProperties.class}) +public class NoticeAutoConfiguration { + + @Bean + public BkNoticeClient bkNoticeClient(MeterRegistry meterRegistry, + AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties) { + return new BkNoticeClient(meterRegistry, appProperties, bkApiGatewayProperties); + } + +} diff --git a/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/exception/BkNoticeException.java b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/exception/BkNoticeException.java new file mode 100644 index 0000000000..14addf24b7 --- /dev/null +++ b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/exception/BkNoticeException.java @@ -0,0 +1,25 @@ +package com.tencent.bk.job.common.notice.exception; + +import com.tencent.bk.job.common.exception.InternalException; +import lombok.Getter; +import lombok.ToString; + +/** + * 调用蓝鲸消息通知中心异常 + */ +@Getter +@ToString +public class BkNoticeException extends InternalException { + + public BkNoticeException(Throwable cause, Integer errorCode, Object[] errorParams) { + super(cause, errorCode, errorParams); + } + + public BkNoticeException(String message, Integer errorCode) { + super(message, errorCode); + } + + public BkNoticeException(String message, Throwable cause, Integer errorCode) { + super(message, cause, errorCode); + } +} diff --git a/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/impl/BkNoticeClient.java b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/impl/BkNoticeClient.java new file mode 100644 index 0000000000..dc8913520e --- /dev/null +++ b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/impl/BkNoticeClient.java @@ -0,0 +1,170 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.notice.impl; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.tencent.bk.job.common.constant.ErrorCode; +import com.tencent.bk.job.common.constant.HttpMethodEnum; +import com.tencent.bk.job.common.esb.config.AppProperties; +import com.tencent.bk.job.common.esb.config.BkApiGatewayProperties; +import com.tencent.bk.job.common.esb.model.BkApiAuthorization; +import com.tencent.bk.job.common.esb.model.EsbReq; +import com.tencent.bk.job.common.esb.model.EsbResp; +import com.tencent.bk.job.common.esb.model.OpenApiRequestInfo; +import com.tencent.bk.job.common.esb.sdk.AbstractBkApiClient; +import com.tencent.bk.job.common.exception.HttpStatusException; +import com.tencent.bk.job.common.exception.InternalException; +import com.tencent.bk.job.common.metrics.CommonMetricNames; +import com.tencent.bk.job.common.notice.IBkNoticeClient; +import com.tencent.bk.job.common.notice.exception.BkNoticeException; +import com.tencent.bk.job.common.notice.model.AnnouncementDTO; +import com.tencent.bk.job.common.notice.model.BkNoticeApp; +import com.tencent.bk.job.common.util.http.HttpHelperFactory; +import com.tencent.bk.job.common.util.http.HttpMetricUtil; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Tag; +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpStatus; + +import java.util.List; + +@SuppressWarnings("SameParameterValue") +public class BkNoticeClient extends AbstractBkApiClient implements IBkNoticeClient { + + private static final String URI_REGISTER_APPLICATION = "/apigw/v1/register/"; + private static final String URI_GET_CURRENT_ANNOUNCEMENTS = "/apigw/v1/announcement/get_current_announcements/"; + + private final AppProperties appProperties; + private final BkApiAuthorization authorization; + + public BkNoticeClient(MeterRegistry meterRegistry, + AppProperties appProperties, + BkApiGatewayProperties bkApiGatewayProperties) { + super( + meterRegistry, + CommonMetricNames.BK_NOTICE_API, + getBkNoticeUrlSafely(bkApiGatewayProperties), + HttpHelperFactory.getDefaultHttpHelper() + ); + this.appProperties = appProperties; + authorization = BkApiAuthorization.appAuthorization(appProperties.getCode(), appProperties.getSecret()); + } + + private static String getBkNoticeUrlSafely(BkApiGatewayProperties bkApiGatewayProperties) { + if (bkApiGatewayProperties == null || bkApiGatewayProperties.getBkNotice() == null) { + return null; + } + return bkApiGatewayProperties.getBkNotice().getUrl(); + } + + @Override + public BkNoticeApp registerApplication() { + EsbResp resp = requestBkNoticeApi( + HttpMethodEnum.POST, + URI_REGISTER_APPLICATION, + null, + new TypeReference>() { + }, + true + ); + return resp.getData(); + } + + @Override + public List getCurrentAnnouncements(String bkLanguage, Integer offset, Integer limit) { + EsbResp> resp = requestBkNoticeApi( + HttpMethodEnum.GET, + buildUriWithParams(bkLanguage, offset, limit), + null, + new TypeReference>>() { + }, + true + ); + return resp.getData(); + } + + private String buildUriWithParams(String bkLanguage, Integer offset, Integer limit) { + StringBuilder sb = new StringBuilder(); + sb.append(URI_GET_CURRENT_ANNOUNCEMENTS); + sb.append("?platform="); + sb.append(appProperties.getCode()); + if (StringUtils.isNotBlank(bkLanguage)) { + sb.append("&language="); + sb.append(bkLanguage); + } + if (offset != null) { + sb.append("&offset="); + sb.append(offset); + } + if (limit != null) { + sb.append("&limit="); + sb.append(limit); + } + return sb.toString(); + } + + /** + * 通过ESB请求消息通知中心API的统一入口,监控数据埋点位置 + * + * @param method Http方法 + * @param uri 请求地址 + * @param reqBody 请求体内容 + * @param typeReference 指定了返回值类型的EsbResp TypeReference对象 + * @param 泛型:返回值类型 + * @return 返回值类型实例 + */ + private EsbResp requestBkNoticeApi(HttpMethodEnum method, + String uri, + EsbReq reqBody, + TypeReference> typeReference, + Boolean idempotent) { + try { + HttpMetricUtil.setHttpMetricName(CommonMetricNames.BK_NOTICE_API_HTTP); + HttpMetricUtil.addTagForCurrentMetric(Tag.of("api_name", uri)); + OpenApiRequestInfo requestInfo = OpenApiRequestInfo + .builder() + .method(method) + .uri(uri) + .body(reqBody) + .authorization(authorization) + .setIdempotent(idempotent) + .build(); + return doRequest(requestInfo, typeReference); + } catch (InternalException e) { + // 接口不存在的场景需要使用指定错误码以便前端兼容处理 + if (e.getCause() instanceof HttpStatusException) { + HttpStatusException httpStatusException = (HttpStatusException) e.getCause(); + if (httpStatusException.getHttpStatus() == HttpStatus.SC_NOT_FOUND) { + throw new BkNoticeException(e, ErrorCode.BK_NOTICE_API_NOT_FOUND, new String[]{uri}); + } + } + throw new BkNoticeException(e, ErrorCode.BK_NOTICE_API_DATA_ERROR, null); + } catch (Exception e) { + throw new BkNoticeException(e, ErrorCode.BK_NOTICE_API_DATA_ERROR, null); + } finally { + HttpMetricUtil.clearHttpMetric(); + } + } +} diff --git a/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/model/AnnouncementDTO.java b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/model/AnnouncementDTO.java new file mode 100644 index 0000000000..fc02fe7e37 --- /dev/null +++ b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/model/AnnouncementDTO.java @@ -0,0 +1,89 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.notice.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import java.util.List; + +@Slf4j +@NoArgsConstructor +@Data +public class AnnouncementDTO { + + private Long id; + + /** + * 标题 + */ + private String title; + + /** + * 内容 + */ + private String content; + + /** + * 内容列表(国际化,各种语言的内容) + */ + @JsonProperty("content_list") + private List contentList; + + /** + * 公告类型: event(活动通知)/announce(平台公告) + */ + @JsonProperty("announce_type") + private String announceType; + + /** + * 开始时间 + */ + @JsonProperty("start_time") + private String startTime; + + /** + * 结束时间 + */ + @JsonProperty("end_time") + private String endTime; + + @NoArgsConstructor + @Data + public static class ContentWithLanguage { + /** + * 内容 + */ + private String content; + + /** + * 语言 + */ + private String language; + } + +} diff --git a/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/model/BkNoticeApp.java b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/model/BkNoticeApp.java new file mode 100644 index 0000000000..6942ad961d --- /dev/null +++ b/src/backend/commons/notice-sdk/src/main/java/com/tencent/bk/job/common/notice/model/BkNoticeApp.java @@ -0,0 +1,37 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.common.notice.model; + +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@Data +public class BkNoticeApp { + + private String code; + + private String name; +} diff --git a/src/backend/commons/notice-sdk/src/main/resources/META-INF/spring.factories b/src/backend/commons/notice-sdk/src/main/resources/META-INF/spring.factories new file mode 100644 index 0000000000..dde166dd38 --- /dev/null +++ b/src/backend/commons/notice-sdk/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +com.tencent.bk.job.common.notice.config.NoticeAutoConfiguration diff --git a/src/backend/job-assemble/src/test/resources/application-test.yml b/src/backend/job-assemble/src/test/resources/application-test.yml index ab8ea613d1..b818e915d9 100644 --- a/src/backend/job-assemble/src/test/resources/application-test.yml +++ b/src/backend/job-assemble/src/test/resources/application-test.yml @@ -143,6 +143,8 @@ app: bk-api-gateway: gse: url: gse.apigw.com + bkNotice: + url: bk-notice.apigw.com esb: service: url: esb.service diff --git a/src/backend/job-manage/api-job-manage/build.gradle b/src/backend/job-manage/api-job-manage/build.gradle index 978ed6be8f..6f876179b8 100644 --- a/src/backend/job-manage/api-job-manage/build.gradle +++ b/src/backend/job-manage/api-job-manage/build.gradle @@ -27,6 +27,7 @@ dependencies { api project(':commons:common-i18n') api project(':commons:common-iam') api project(':commons:cmdb-sdk') + api project(':commons:notice-sdk') api project(':job-execute:api-job-execute') api(project(":commons:common-api")) implementation "org.springframework:spring-web" diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebNoticeResource.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebNoticeResource.java new file mode 100644 index 0000000000..229e428fd4 --- /dev/null +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/WebNoticeResource.java @@ -0,0 +1,67 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.api.web; + +import com.tencent.bk.job.common.annotation.WebAPI; +import com.tencent.bk.job.common.model.Response; +import com.tencent.bk.job.manage.model.web.vo.notice.AnnouncementVO; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiParam; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.validation.constraints.Min; +import java.util.List; + +/** + * 对接蓝鲸消息中心相关的 WEB API + */ +@Validated +@Api(tags = {"job-manage:web:Notice"}) +@RequestMapping("/web/notice") +@RestController +@WebAPI +public interface WebNoticeResource { + + @ApiOperation(value = "获获取公告列表(返回码1217001表示消息通知中心API不存在,该环境未对接消息通知中心,需要兼容处理)", produces = "application/json") + @GetMapping(value = {"/announcement/currentAnnouncements"}) + Response> getCurrentAnnouncements( + @ApiParam("用户名,网关自动传入") + @RequestHeader("username") + String username, + @Min(message = "{validation.constraints.InvalidAnnouncementsOffset.message}", value = 0L) + @RequestParam(value = "offset", required = false) + Integer offset, + @Min(message = "{validation.constraints.InvalidAnnouncementsLimit.message}", value = 1L) + @RequestParam(value = "limit", required = false) + Integer limit + ); + +} diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/common/consts/globalsetting/GlobalSettingKeys.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/common/consts/globalsetting/GlobalSettingKeys.java index ae6a211a3b..19e90d6638 100644 --- a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/common/consts/globalsetting/GlobalSettingKeys.java +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/common/consts/globalsetting/GlobalSettingKeys.java @@ -54,4 +54,8 @@ public class GlobalSettingKeys { public static final String KEY_IS_BIZSET_MIGRATED_TO_CMDB = "IS_BIZSET_MIGRATED_TO_CMDB"; // 助手信息 public static final String KEY_BK_HELPER = "BK_HELPER"; + // 是否对接消息通知中心 + public static final String KEY_ENABLE_BK_NOTICE = "ENABLE_BK_NOTICE"; + // 消息通知中心是否注册成功 + public static final String KEY_BK_NOTICE_REGISTERED_SUCCESS = "BK_NOTICE_REGISTERED_SUCCESS"; } diff --git a/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/vo/notice/AnnouncementVO.java b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/vo/notice/AnnouncementVO.java new file mode 100644 index 0000000000..4a71aed891 --- /dev/null +++ b/src/backend/job-manage/api-job-manage/src/main/java/com/tencent/bk/job/manage/model/web/vo/notice/AnnouncementVO.java @@ -0,0 +1,74 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.model.web.vo.notice; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.tencent.bk.job.common.notice.model.AnnouncementDTO; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +@NoArgsConstructor +@AllArgsConstructor +@ApiModel("公告内容") +@Data +public class AnnouncementVO { + + @ApiModelProperty("ID") + private Long id; + + @ApiModelProperty("标题") + private String title; + + @ApiModelProperty("内容") + private String content; + + @ApiModelProperty("公告类型: event(活动通知)/announce(平台公告)") + @JsonProperty("announce_type") + private String announceType; + + @ApiModelProperty("开始时间") + @JsonProperty("start_time") + private String startTime; + + @ApiModelProperty("结束时间") + @JsonProperty("end_time") + private String endTime; + + public static AnnouncementVO fromDTO(AnnouncementDTO announcementDTO) { + AnnouncementVO announcementVO = new AnnouncementVO(); + announcementVO.setId(announcementDTO.getId()); + announcementVO.setTitle(announcementDTO.getTitle()); + announcementVO.setContent(announcementDTO.getContent()); + announcementVO.setAnnounceType(announcementDTO.getAnnounceType()); + announcementVO.setStartTime(announcementDTO.getStartTime()); + announcementVO.setEndTime(announcementDTO.getEndTime()); + return announcementVO; + } +} diff --git a/src/backend/job-manage/boot-job-manage/src/main/resources/logback-spring.xml b/src/backend/job-manage/boot-job-manage/src/main/resources/logback-spring.xml index 2ac5449b8f..3f0df06198 100644 --- a/src/backend/job-manage/boot-job-manage/src/main/resources/logback-spring.xml +++ b/src/backend/job-manage/boot-job-manage/src/main/resources/logback-spring.xml @@ -8,6 +8,7 @@ + @@ -25,26 +26,60 @@ + + + ${NOTICE_LOG_FILE} + + ${NOTICE_LOG_FILE}-%d{yyyyMMdd}.log.%i + 1GB + 7 + 2GB + + + ${FILE_LOG_PATTERN} + UTF-8 + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/backend/job-manage/boot-job-manage/src/test/resources/application-test.yml b/src/backend/job-manage/boot-job-manage/src/test/resources/application-test.yml index c3815727d0..324219d057 100644 --- a/src/backend/job-manage/boot-job-manage/src/test/resources/application-test.yml +++ b/src/backend/job-manage/boot-job-manage/src/test/resources/application-test.yml @@ -29,6 +29,8 @@ job: bk-api-gateway: gse: url: gse.apigw.com + bkNotice: + url: bk-notice.apigw.com esb: service: url: esb.service diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebNoticeResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebNoticeResourceImpl.java new file mode 100644 index 0000000000..3c77f1097d --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebNoticeResourceImpl.java @@ -0,0 +1,73 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.api.web.impl; + +import com.tencent.bk.job.common.i18n.locale.BkConsts; +import com.tencent.bk.job.common.i18n.locale.LocaleUtils; +import com.tencent.bk.job.common.model.Response; +import com.tencent.bk.job.common.notice.IBkNoticeClient; +import com.tencent.bk.job.common.notice.config.BkNoticeProperties; +import com.tencent.bk.job.common.util.JobContextUtil; +import com.tencent.bk.job.manage.api.web.WebNoticeResource; +import com.tencent.bk.job.manage.model.web.vo.notice.AnnouncementVO; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +@RestController +@Slf4j +public class WebNoticeResourceImpl implements WebNoticeResource { + + private final IBkNoticeClient bkNoticeClient; + private final BkNoticeProperties bkNoticeProperties; + + @Autowired + public WebNoticeResourceImpl(@Autowired(required = false) IBkNoticeClient bkNoticeClient, + BkNoticeProperties bkNoticeProperties) { + this.bkNoticeClient = bkNoticeClient; + this.bkNoticeProperties = bkNoticeProperties; + } + + @Override + public Response> getCurrentAnnouncements(String username, Integer offset, Integer limit) { + if (!bkNoticeProperties.isEnabled()) { + log.info("bkNotice not enabled, please check config value: bkNotice.enabled"); + return Response.buildSuccessResp(Collections.emptyList()); + } + String userLang = JobContextUtil.getUserLang(); + String bkLang = LocaleUtils.getBkLang(userLang); + if (bkLang == null) { + bkLang = BkConsts.HEADER_VALUE_LANG_EN; + } + List resultList = bkNoticeClient.getCurrentAnnouncements(bkLang, offset, limit).stream() + .map(AnnouncementVO::fromDTO) + .collect(Collectors.toList()); + return Response.buildSuccessResp(resultList); + } +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/config/ExecutorConfiguration.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/config/ExecutorConfiguration.java index c23617d22c..c2a21982f3 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/config/ExecutorConfiguration.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/config/ExecutorConfiguration.java @@ -129,6 +129,19 @@ public ThreadPoolExecutor adminAuthExecutor(MeterRegistry meterRegistry) { ); } + @Bean("initRunnerExecutor") + public ThreadPoolExecutor initRunnerExecutor(MeterRegistry meterRegistry) { + return new WatchableThreadPoolExecutor( + meterRegistry, + "initRunnerExecutor", + 0, + 5, + 1, + TimeUnit.SECONDS, + new LinkedBlockingQueue<>() + ); + } + private ThreadFactory getThreadFactoryByNameAndSeq(String namePrefix, AtomicInteger seq) { return r -> { Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/ClearDeletedHostsRunner.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/ClearDeletedHostsRunner.java index f8aeceb892..b01fe48024 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/ClearDeletedHostsRunner.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/ClearDeletedHostsRunner.java @@ -27,9 +27,12 @@ import com.tencent.bk.job.manage.task.ClearDeletedHostsTask; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; +import java.util.concurrent.ThreadPoolExecutor; + /** * 进程启动时立即执行一次无效主机清理 */ @@ -38,14 +41,17 @@ public class ClearDeletedHostsRunner implements CommandLineRunner { private final ClearDeletedHostsTask clearDeletedHostsTask; + private final ThreadPoolExecutor initRunnerExecutor; @Autowired - public ClearDeletedHostsRunner(ClearDeletedHostsTask clearDeletedHostsTask) { + public ClearDeletedHostsRunner(ClearDeletedHostsTask clearDeletedHostsTask, + @Qualifier("initRunnerExecutor") ThreadPoolExecutor initRunnerExecutor) { this.clearDeletedHostsTask = clearDeletedHostsTask; + this.initRunnerExecutor = initRunnerExecutor; } @Override public void run(String... args) { - new Thread(clearDeletedHostsTask::execute).start(); + initRunnerExecutor.submit(clearDeletedHostsTask::execute); } } diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/InitArtifactoryDataRunner.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/InitArtifactoryDataRunner.java index fdbadf374f..ea712a93dd 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/InitArtifactoryDataRunner.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/InitArtifactoryDataRunner.java @@ -30,27 +30,37 @@ import com.tencent.bk.job.manage.config.LocalFileConfigForManage; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; +import java.util.concurrent.ThreadPoolExecutor; + @Slf4j @Component("jobManageInitArtifactoryDataRunner") public class InitArtifactoryDataRunner implements CommandLineRunner { private final ArtifactoryConfig artifactoryConfig; private final LocalFileConfigForManage localFileConfigForManage; + private final ThreadPoolExecutor initRunnerExecutor; @Autowired public InitArtifactoryDataRunner( ArtifactoryConfig artifactoryConfig, - LocalFileConfigForManage localFileConfigForManage + LocalFileConfigForManage localFileConfigForManage, + @Qualifier("initRunnerExecutor") ThreadPoolExecutor initRunnerExecutor ) { this.artifactoryConfig = artifactoryConfig; this.localFileConfigForManage = localFileConfigForManage; + this.initRunnerExecutor = initRunnerExecutor; } @Override public void run(String... args) { + initRunnerExecutor.submit(this::initArtifactoryData); + } + + public void initArtifactoryData() { if (!JobConstants.FILE_STORAGE_BACKEND_ARTIFACTORY.equals(localFileConfigForManage.getStorageBackend())) { //不使用制品库作为后端存储时不初始化 return; diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/RegisterBkNoticeRunner.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/RegisterBkNoticeRunner.java new file mode 100644 index 0000000000..09f77f36ff --- /dev/null +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/runner/RegisterBkNoticeRunner.java @@ -0,0 +1,107 @@ +/* + * Tencent is pleased to support the open source community by making BK-JOB蓝鲸智云作业平台 available. + * + * Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved. + * + * BK-JOB蓝鲸智云作业平台 is licensed under the MIT License. + * + * License for BK-JOB蓝鲸智云作业平台: + * -------------------------------------------------------------------- + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and + * to permit persons to whom the Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of + * the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO + * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF + * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +package com.tencent.bk.job.manage.runner; + +import com.tencent.bk.job.common.notice.IBkNoticeClient; +import com.tencent.bk.job.common.notice.model.BkNoticeApp; +import com.tencent.bk.job.common.util.ThreadUtils; +import com.tencent.bk.job.common.util.TimeUtil; +import com.tencent.bk.job.manage.common.consts.globalsetting.GlobalSettingKeys; +import com.tencent.bk.job.manage.dao.globalsetting.GlobalSettingDAO; +import com.tencent.bk.job.manage.model.dto.GlobalSettingDTO; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.helpers.MessageFormatter; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Component; + +import java.util.concurrent.ThreadPoolExecutor; + +/** + * 进程启动时向消息中心注册平台信息(幂等操作) + */ +@SuppressWarnings("ConstantConditions") +@Slf4j +@Component +@ConditionalOnProperty(value = "bkNotice.enabled", havingValue = "true", matchIfMissing = true) +public class RegisterBkNoticeRunner implements CommandLineRunner { + + private final IBkNoticeClient bkNoticeClient; + private final ThreadPoolExecutor initRunnerExecutor; + private final GlobalSettingDAO globalSettingDAO; + + @Autowired + public RegisterBkNoticeRunner(IBkNoticeClient bkNoticeClient, + @Qualifier("initRunnerExecutor") ThreadPoolExecutor initRunnerExecutor, + GlobalSettingDAO globalSettingDAO) { + this.bkNoticeClient = bkNoticeClient; + this.initRunnerExecutor = initRunnerExecutor; + this.globalSettingDAO = globalSettingDAO; + } + + @Override + public void run(String... args) { + initRunnerExecutor.submit(() -> { + boolean registerSuccess = false; + // 最多重试30min,覆盖整个蓝鲸部署时间 + int maxRetryTimes = 180; + int retryTimes = 0; + while (!registerSuccess && retryTimes < maxRetryTimes) { + try { + BkNoticeApp bkNoticeApp = bkNoticeClient.registerApplication(); + log.info("registerApplication result:{}", bkNoticeApp); + registerSuccess = true; + } catch (Exception e) { + retryTimes++; + if (retryTimes < maxRetryTimes) { + String msg = MessageFormatter.format( + "Fail to registerApplication, retry {}", + retryTimes + ).getMessage(); + log.warn(msg, e); + ThreadUtils.sleep(10000); + } else { + log.warn("Fail to registerApplication finally", e); + } + } + } + // 将注册结果写入DB中 + if (registerSuccess) { + int affectedNum = globalSettingDAO.upsertGlobalSetting(buildRegisterResult(registerSuccess)); + log.info("Write to db, registerSuccess={}, affectedNum={}", registerSuccess, affectedNum); + } + }); + } + + private GlobalSettingDTO buildRegisterResult(boolean registerSuccess) { + GlobalSettingDTO globalSettingDTO = new GlobalSettingDTO(); + globalSettingDTO.setKey(GlobalSettingKeys.KEY_BK_NOTICE_REGISTERED_SUCCESS); + globalSettingDTO.setValue(String.valueOf(registerSuccess)); + globalSettingDTO.setDescription("Updated at " + TimeUtil.getCurrentTimeStr("yyyy-MM-dd HH:mm:ss.SSS")); + return globalSettingDTO; + } +} diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/GlobalSettingsServiceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/GlobalSettingsServiceImpl.java index 1494877092..c0577cb9c2 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/GlobalSettingsServiceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/service/impl/GlobalSettingsServiceImpl.java @@ -33,6 +33,7 @@ import com.tencent.bk.job.common.i18n.locale.LocaleUtils; import com.tencent.bk.job.common.i18n.service.MessageI18nService; import com.tencent.bk.job.common.iam.constant.ActionId; +import com.tencent.bk.job.common.notice.config.BkNoticeProperties; import com.tencent.bk.job.common.util.JobContextUtil; import com.tencent.bk.job.common.util.StringUtil; import com.tencent.bk.job.common.util.TimeUtil; @@ -126,6 +127,7 @@ public class GlobalSettingsServiceImpl implements GlobalSettingsService { private final MessageI18nService i18nService; private final JobManageConfig jobManageConfig; private final LocalFileConfigForManage localFileConfigForManage; + private final BkNoticeProperties bkNoticeProperties; private final NotifyTemplateConverter notifyTemplateConverter; private final BuildProperties buildProperties; @Value("${job.manage.upload.filesize.max:5GB}") @@ -142,6 +144,7 @@ public GlobalSettingsServiceImpl(NotifyEsbChannelDAO notifyEsbChannelDAO, MessageI18nService i18nService, JobManageConfig jobManageConfig, LocalFileConfigForManage localFileConfigForManage, + BkNoticeProperties bkNoticeProperties, NotifyTemplateConverter notifyTemplateConverter, BuildProperties buildProperties) { this.notifyEsbChannelDAO = notifyEsbChannelDAO; @@ -154,6 +157,7 @@ public GlobalSettingsServiceImpl(NotifyEsbChannelDAO notifyEsbChannelDAO, this.i18nService = i18nService; this.jobManageConfig = jobManageConfig; this.localFileConfigForManage = localFileConfigForManage; + this.bkNoticeProperties = bkNoticeProperties; this.notifyTemplateConverter = notifyTemplateConverter; this.buildProperties = buildProperties; } @@ -670,12 +674,26 @@ private void addEnableUploadToArtifactoryConfig(Map configMap) { ); } + private void addEnableBkNoticeConfig(Map configMap) { + configMap.put( + GlobalSettingKeys.KEY_ENABLE_BK_NOTICE, + bkNoticeProperties.isEnabled() && bkNoticeRegisteredSuccess() + ); + } + + private boolean bkNoticeRegisteredSuccess() { + GlobalSettingDTO globalSettingDTO = + globalSettingDAO.getGlobalSetting(GlobalSettingKeys.KEY_BK_NOTICE_REGISTERED_SUCCESS); + return globalSettingDTO != null && "true".equals(globalSettingDTO.getValue().toLowerCase()); + } + @Override public Map getJobConfig(String username) { Map configMap = new HashMap<>(); addFileUploadConfig(configMap); addEnableFeatureFileManageConfig(configMap); addEnableUploadToArtifactoryConfig(configMap); + addEnableBkNoticeConfig(configMap); return configMap; } diff --git a/src/backend/settings.gradle b/src/backend/settings.gradle index 58340448d1..700490ecef 100644 --- a/src/backend/settings.gradle +++ b/src/backend/settings.gradle @@ -30,6 +30,7 @@ include 'commons:esb-sdk' include 'commons:cmdb-sdk' include 'commons:paas-sdk' include 'commons:gse-sdk' +include 'commons:notice-sdk' include 'commons:common-iam' include 'commons:common-jwt' include 'commons:common-utils' diff --git a/support-files/kubernetes/charts/bk-job/VALUES_LOG.md b/support-files/kubernetes/charts/bk-job/VALUES_LOG.md index 25d88d5b49..1f25b62a73 100644 --- a/support-files/kubernetes/charts/bk-job/VALUES_LOG.md +++ b/support-files/kubernetes/charts/bk-job/VALUES_LOG.md @@ -1,5 +1,17 @@ # chart values 更新日志 +## 0.5.6 +1. 增加消息通知中心配置 + +```yaml +# 蓝鲸消息通知中心 API Gateway url +bkNoticeApiGatewayUrl: "http://bkapi.example.com/api/bk-notice" +# 消息通知中心配置 +bkNotice: + # 是否对接消息通知中心 + enabled: true +``` + ## 0.5.5 1. 增加权限中心web地址配置 diff --git a/support-files/kubernetes/charts/bk-job/templates/configmap-common.yaml b/support-files/kubernetes/charts/bk-job/templates/configmap-common.yaml index d92f168fbf..6a6d86bfbc 100644 --- a/support-files/kubernetes/charts/bk-job/templates/configmap-common.yaml +++ b/support-files/kubernetes/charts/bk-job/templates/configmap-common.yaml @@ -39,6 +39,8 @@ data: bkApiGateway: gse: url: {{ .Values.bkGseApiGatewayUrl }} + bkNotice: + url: {{ .Values.bkNoticeApiGatewayUrl }} gse: enabled: {{ .Values.gse.enabled }} cache: @@ -88,6 +90,8 @@ data: {{- else }} url: {{ .Values.bkLoginUrl }} {{- end }} + bkNotice: + enabled: {{ .Values.bkNotice.enabled }} job: security: service: diff --git a/support-files/kubernetes/charts/bk-job/values.yaml b/support-files/kubernetes/charts/bk-job/values.yaml index 735bbfa048..57b2555d04 100644 --- a/support-files/kubernetes/charts/bk-job/values.yaml +++ b/support-files/kubernetes/charts/bk-job/values.yaml @@ -425,6 +425,8 @@ bkRepoUrl: "http://bkrepo.example.com" bkNodemanUrl: "http://nodeman.example.com" # 蓝鲸 GSE API Gateway url bkGseApiGatewayUrl: "http://bkapi.example.com" +# 蓝鲸消息通知中心 API Gateway url +bkNoticeApiGatewayUrl: "http://bkapi.example.com/api/bk-notice" # 文档中心 url bkDocsCenterUrl: "https://bk.tencent.com/docs" # 问题反馈 url @@ -569,6 +571,11 @@ login: # 完成页面登录后前端通过Cookie提交的凭据的Key,也是后台向apiUrl获取用户信息提交的凭据的Key tokenName: "bk_token" +# 消息通知中心配置 +bkNotice: + # 是否对接消息通知中心 + enabled: true + ## Job Common Config job: # 作业平台运行所有环境(prod,dev)与模式(kubernetes),默认生产环境(prod)、K8s容器模式(kubernetes) diff --git a/support-files/templates/#etc#job#job-common#application.yml b/support-files/templates/#etc#job#job-common#application.yml index 8eadbd8319..aec7ae04a8 100644 --- a/support-files/templates/#etc#job#job-common#application.yml +++ b/support-files/templates/#etc#job#job-common#application.yml @@ -35,6 +35,9 @@ bkApiGateway: gse: # 蓝鲸 GSE API Gateway url。格式:{网关访问地址}/{网关环境},网关访问地址、网关环境的取值见bk-gse网关API文档。 url: __BK_API_GATEWAY_GSE_URL__ + bkNotice: + # 蓝鲸消息通知中心 API Gateway url。格式:{网关访问地址}/{网关环境},网关访问地址、网关环境的取值见bk-notice网关API文档。 + url: __BK_API_GATEWAY_BK_NOTICE_URL__ gse: # 是否初始化 GSE1.0 Client。如果需要对接GSE1.0(job.features.gseV2.enabled=false), 必须设置gse.enabled=true enabled: true @@ -76,6 +79,9 @@ paas: url: __BK_PAAS_PUBLIC_URL__ login: url: __BK_PAAS_PUBLIC_URL__/login/ +bkNotice: + # 是否对接消息通知中心 + enabled: true job: security: service: diff --git a/support-files/templates/job.env b/support-files/templates/job.env index a7c0a90dda..ce9d3f1e5d 100644 --- a/support-files/templates/job.env +++ b/support-files/templates/job.env @@ -67,6 +67,10 @@ BK_GSE_CACHE_APISERVER_PORT= BK_GSE_SSL_KEYSTORE_PASSWORD= BK_GSE_SSL_TRUSTSTORE_PASSWORD= BK_GSE_PUBLIC_KEY_BASE64= +BK_API_GATEWAY_GSE_URL= + +# 依赖的消息通知中心服务 +BK_API_GATEWAY_BK_NOTICE_URL= ### job-gateway BK_JOB_GATEWAY_SERVER_HTTPS_PORT=10503