Skip to content

Commit

Permalink
Merge pull request #3388 from wangyu096/issue_3369
Browse files Browse the repository at this point in the history
feat: Job 支持多租户 #3369
  • Loading branch information
wangyu096 authored Jan 20, 2025
2 parents 37426ae + 29d08a7 commit f964b5c
Show file tree
Hide file tree
Showing 31 changed files with 1,057 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class JobApplicationAvailabilityBean extends ApplicationAvailabilityBean
public void onApplicationEvent(AvailabilityChangeEvent<?> event) {
super.onApplicationEvent(event);
if (ReadinessState.REFUSING_TRAFFIC == event.getState()) {
// SpringCloud负载均衡缓存默认为35s,等待调用方缓存刷新后再真正关闭Spring容器
int waitSeconds = 40;
// SpringCloud负载均衡缓存设置为20s,等待调用方缓存刷新后再真正关闭Spring容器
int waitSeconds = 30;
while (waitSeconds > 0) {
ThreadUtils.sleep(1000);
log.info("wait for GracefulShutdown, {}s left", waitSeconds--);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,50 @@
package com.tencent.bk.job.common.web.config;

import com.tencent.bk.job.common.web.filter.RepeatableReadWriteServletRequestResponseFilter;
import com.tencent.bk.job.common.web.filter.WebRepeatableReadServletRequestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

/**
* 给/esb/api/*, /service/* 用的过滤器,包装request和response
*
*/
@Bean
public FilterRegistrationBean repeatableRSRRFilterRegister() {
FilterRegistrationBean<RepeatableReadWriteServletRequestResponseFilter> registration =
new FilterRegistrationBean<>();
registration.setFilter(repeatableRRRFilter());
registration.addUrlPatterns("/esb/api/*", "/service/*", "/web/*");
registration.addUrlPatterns("/esb/api/*", "/service/*");
registration.setName("repeatableReadRequestResponseFilter");
registration.setOrder(0);
return registration;
}

/**
* 给/web/* 用的过滤器,仅包装request
*
*/
@Bean
public FilterRegistrationBean webRepeatableRRFilterRegister() {
FilterRegistrationBean<WebRepeatableReadServletRequestFilter> registration = new FilterRegistrationBean<>();
registration.setFilter(webRepeatableReadRequestFilter());
registration.addUrlPatterns("/web/*");
registration.setName("webRepeatableReadRequestFilter");
registration.setOrder(1);
return registration;
}

@Bean(name = "repeatableReadRequestResponseFilter")
public RepeatableReadWriteServletRequestResponseFilter repeatableRRRFilter() {
return new RepeatableReadWriteServletRequestResponseFilter();
}

@Bean(name = "webRepeatableReadRequestFilter")
public WebRepeatableReadServletRequestFilter webRepeatableReadRequestFilter() {
return new WebRepeatableReadServletRequestFilter();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@

package com.tencent.bk.job.common.web.filter;

import com.tencent.bk.job.common.web.utils.ServletUtil;
import com.tencent.bk.job.common.web.model.RepeatableReadHttpServletResponse;
import com.tencent.bk.job.common.web.model.RepeatableReadWriteHttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
Expand All @@ -49,35 +49,15 @@ public void init(FilterConfig filterConfig) {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
ServletException {
ServletRequest servletRequest = request;
ServletResponse servletResponse = response;
if (isJsonRequest(request)) {
// 仅处理 ContentType: application/json 请求
servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request);
}
if (isJsonResponse(response)) {
// 仅处理 ContentType: application/json 响应
servletResponse = new RepeatableReadHttpServletResponse((HttpServletResponse) response);
if (!ServletUtil.isJsonRequest(request)) {
chain.doFilter(request, response);
return;
}
ServletRequest servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request);
ServletResponse servletResponse = new RepeatableReadHttpServletResponse((HttpServletResponse) response);
chain.doFilter(servletRequest, servletResponse);
}

private boolean isJsonRequest(ServletRequest request) {
return isJsonContentType(request.getContentType());
}

private boolean isJsonResponse(ServletResponse response) {
return isJsonContentType(response.getContentType());
}

private boolean isJsonContentType(String contentType) {
if (StringUtils.isBlank(contentType)) {
return false;
}
contentType = contentType.trim().toLowerCase();
return contentType.startsWith("application/json");
}

@Override
public void destroy() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.web.filter;

import com.tencent.bk.job.common.web.utils.ServletUtil;
import com.tencent.bk.job.common.web.model.RepeatableReadWriteHttpServletRequest;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
public class WebRepeatableReadServletRequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// do nothing
}

/**
* 仅包装ServletRequest,给/web使用
*
*/
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
ServletRequest servletRequest = request;

if (ServletUtil.isJsonRequest(request)) {
// 仅处理 ContentType: application/json 请求
servletRequest = new RepeatableReadWriteHttpServletRequest((HttpServletRequest) request);
}

chain.doFilter(servletRequest, response);
}

@Override
public void destroy() {
// do nothing
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.web.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletRequest;


/**
* 处理servlet请求和响应的工具类
*/
public class ServletUtil {

/**
* 判断 servlet 请求的 Content-Type 是否是 application/json
* @param request 请求
* @return 请求的 Content-Type 是否是 application/json
*/
public static boolean isJsonRequest(ServletRequest request) {
return isJsonContentType(request.getContentType());
}

private static boolean isJsonContentType(String contentType) {
if (StringUtils.isBlank(contentType)) {
return false;
}
contentType = contentType.trim().toLowerCase();
return contentType.startsWith("application/json");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ PageData<CronJobInfoDTO> listPageCronJobsWithoutVarsByCondition(CronJobInfoDTO c
* @param cronJobIdList 定时任务 IDs
* @return 定时任务信息
*/
List<CronJobInfoDTO> getCronJobByIds(List<Long> cronJobIdList);
List<CronJobInfoDTO> listCronJobByIds(List<Long> cronJobIdList);

/**
* 根据定时任务 ID 查询定时任务信息
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ public CronJobInfoDTO getCronJobById(long cronJobId) {
}

@Override
public List<CronJobInfoDTO> getCronJobByIds(List<Long> cronJobIdList) {
public List<CronJobInfoDTO> listCronJobByIds(List<Long> cronJobIdList) {
List<Condition> conditions = new ArrayList<>();
conditions.add(TABLE.ID.in(cronJobIdList.stream().map(ULong::valueOf).collect(Collectors.toList())));
conditions.add(TABLE.IS_DELETED.equal(UByte.valueOf(0)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.tencent.bk.job.crontab.listener.event.CrontabEvent;
import com.tencent.bk.job.crontab.model.dto.CronJobInfoDTO;
import com.tencent.bk.job.crontab.service.CronJobService;
import com.tencent.bk.job.crontab.service.QuartzService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -41,10 +42,12 @@
public class CrontabEventListener {

private final CronJobService cronJobService;
private final QuartzService quartzService;

@Autowired
public CrontabEventListener(CronJobService cronJobService) {
public CrontabEventListener(CronJobService cronJobService, QuartzService quartzService) {
this.cronJobService = cronJobService;
this.quartzService = quartzService;
}


Expand Down Expand Up @@ -86,7 +89,7 @@ private void refreshCronJobInQuartz(CronJobInfoDTO cronJobInfoDTO) {
}
if (cronJobInfoDTO.getEnable()) {
// 开启定时任务
boolean result = cronJobService.addJobToQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
boolean result = cronJobService.checkAndAddJobToQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
log.info(
"add cronJob({},{}) to quartz, result={}",
cronJobInfoDTO.getAppId(),
Expand All @@ -95,7 +98,7 @@ private void refreshCronJobInQuartz(CronJobInfoDTO cronJobInfoDTO) {
);
} else {
// 关闭定时任务
boolean result = cronJobService.deleteJobFromQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
boolean result = quartzService.deleteJobFromQuartz(cronJobInfoDTO.getAppId(), cronJobInfoDTO.getId());
log.info(
"delete cronJob({},{}) from quartz, result={}",
cronJobInfoDTO.getAppId(),
Expand All @@ -107,7 +110,7 @@ private void refreshCronJobInQuartz(CronJobInfoDTO cronJobInfoDTO) {

private void deleteCronJobFromQuartz(long appId, long cronJobId) {
// 删除定时任务
boolean result = cronJobService.deleteJobFromQuartz(appId, cronJobId);
boolean result = quartzService.deleteJobFromQuartz(appId, cronJobId);
log.info(
"delete cronJob({},{}) from quartz, result={}",
appId,
Expand Down
Loading

0 comments on commit f964b5c

Please sign in to comment.