Skip to content

Commit

Permalink
feature: Job 支持容器执行 - 脚本任务 TencentBlueKing#2631
Browse files Browse the repository at this point in the history
  • Loading branch information
wangyu096 committed Feb 4, 2024
2 parents aae02b8 + 7aa8283 commit 75280c8
Show file tree
Hide file tree
Showing 60 changed files with 301 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ public IamAppTransferAspect(AppScopeMappingService appScopeMappingService) {
this.appScopeMappingService = appScopeMappingService;
}

@Pointcut("execution (* com.tencent.bk.job.common.iam.service.impl.BusinessAuthServiceImpl.auth*(..))")
@Pointcut("within(com.tencent.bk.job.common.iam.service.impl.BusinessAuthServiceImpl) " +
"&& execution (* com.tencent.bk.job.common.iam.service.impl.BusinessAuthServiceImpl.auth*(..))")
public void processAuthBusinessAction() {
}

@Pointcut("execution (* com.tencent.bk.job.*.auth..impl.*.auth*(..))")
@Pointcut("within(com.tencent.bk.job..*) " +
"&& execution (* com.tencent.bk.job.*.auth..impl.*.auth*(..))")
public void processAuthResourceAction() {
}

@Pointcut("execution (* com.tencent.bk.job.*.auth..impl.*.batchAuth*(..))")
@Pointcut("within(com.tencent.bk.job..*) " +
"&& execution (* com.tencent.bk.job.*.auth..impl.*.batchAuth*(..))")
public void processBatchAuthResourceAction() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class IamCallbackAspect {
public IamCallbackAspect() {
}

@Pointcut("execution (* com.tencent.bk.job.*.api.iam.impl.*.callback(..))")
@Pointcut("within(com.tencent.bk.job..*) && execution (* com.tencent.bk.job.*.api.iam.impl.*.callback(..))")
public void processCallbackRequest() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
@Aspect
public class IamExceptionHandleAspect {

@Pointcut("execution (* com.tencent.bk.sdk.iam.service.impl.PolicyServiceImpl.*(..))")
@Pointcut("within(com.tencent.bk.sdk.iam.service.impl.PolicyServiceImpl) " +
"&& execution (* com.tencent.bk.sdk.iam.service.impl.PolicyServiceImpl.*(..))")
public void processPolicyServiceAction() {
}

@Pointcut("execution (* com.tencent.bk.sdk.iam.helper.AuthHelper.isAllowed(..))")
@Pointcut("within(com.tencent.bk.sdk.iam.helper.AuthHelper) " +
"&& execution (* com.tencent.bk.sdk.iam.helper.AuthHelper.isAllowed(..))")
public void processIsAllowedAction() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@
* IN THE SOFTWARE.
*/

package com.tencent.bk.job.common.mysql.config;
package com.tencent.bk.job.common.mysql;

import org.jooq.DSLContext;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AliasFor;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
/**
* Job 自定义事务注解
*/
@Transactional
public @interface JobTransactional {
@AliasFor(annotation = Transactional.class)
String transactionManager();

@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(DSLContext.class)
@ConditionalOnBean(DataSource.class)
public class JobMySQLAutoConfiguration {
@AliasFor(annotation = Transactional.class)
Class<? extends Throwable>[] rollbackFor() default {Throwable.class};

@AliasFor(annotation = Transactional.class)
int timeout() default TransactionDefinition.TIMEOUT_DEFAULT;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public EsbAppResourceScopeReqAspect(AppScopeMappingService appScopeMappingServic
this.appScopeMappingService = appScopeMappingService;
}

@Before("execution(* com.tencent.bk.job.*.api.esb..*.*(..)))")
@Before("within(com.tencent.bk.job..*) && execution(* com.tencent.bk.job.*.api.esb..*.*(..)))")
public void handleAppScopeReq(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
*
* * 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;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.info.BuildProperties;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class VersionInfoLogApplicationRunner implements ApplicationRunner {
@Value("${spring.application.name:bk-job}")
private String serviceName;
private final BuildProperties buildProperties;

@Autowired
public VersionInfoLogApplicationRunner(BuildProperties buildProperties) {
this.buildProperties = buildProperties;
}

@Override
public void run(ApplicationArguments args){
String version = buildProperties.getVersion();
log.info("Service name: '{}', Version: '{}'", serviceName, version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static Tag exception(Throwable exception) {
return EXCEPTION_NONE;
}

@Around("execution (@com.tencent.bk.job.common.esb.metrics.EsbApiTimed * *.*(..))")
@Around("within(com.tencent.bk.job..*) && execution (@com.tencent.bk.job.common.esb.metrics.EsbApiTimed * *.*(..))")
public Object timedMethod(ProceedingJoinPoint pjp) throws Throwable {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest request = null;
Expand Down
1 change: 1 addition & 0 deletions src/backend/job-crontab/service-job-crontab/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dependencies {
api project(":commons:common-redis")
api project(":commons:common-web")
api project(':commons:common-security')
api project(':commons:common-mysql')
api project(":job-execute:api-job-execute")
api project(":job-manage:api-job-manage")
api project(":job-crontab:api-job-crontab")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.tencent.bk.job.common.exception.InvalidParamException;
import com.tencent.bk.job.common.model.BaseSearchCondition;
import com.tencent.bk.job.common.model.InternalResponse;
import com.tencent.bk.job.common.mysql.JobTransactional;
import com.tencent.bk.job.common.util.JobContextUtil;
import com.tencent.bk.job.common.util.date.DateUtils;
import com.tencent.bk.job.common.util.json.JsonUtils;
Expand All @@ -43,7 +44,6 @@
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
Expand Down Expand Up @@ -116,7 +116,7 @@ public InternalResponse<Map<Long, List<CronJobVO>>> batchListCronJobByPlanIds(Lo
}

@Override
@Transactional(value = "jobCrontabTransactionManager", rollbackFor = {Throwable.class})
@JobTransactional(transactionManager = "jobCrontabTransactionManager")
public InternalResponse<Long> saveCronJobWithId(String username, Long appId, Long cronJobId, Long createTime,
Long lastModifyTime, String lastModifyUser,
CronJobCreateUpdateReq cronJobCreateUpdateReq) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@

package com.tencent.bk.job.crontab.service.impl;

import com.tencent.bk.job.common.mysql.JobTransactional;
import com.tencent.bk.job.crontab.model.dto.CronJobBasicInfoDTO;
import com.tencent.bk.job.crontab.service.CronJobBatchLoadService;
import com.tencent.bk.job.crontab.service.CronJobService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -48,7 +48,7 @@ public CronJobBatchLoadServiceImpl(CronJobService cronJobService) {
}

@Override
@Transactional(rollbackFor = {Exception.class, Error.class}, timeout = 30)
@JobTransactional(transactionManager = "jobCrontabTransactionManager", timeout = 30)
public CronLoadResult batchLoadCronToQuartz(int start, int limit) {
int successNum = 0;
int failedNum = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.tencent.bk.job.common.model.PageData;
import com.tencent.bk.job.common.model.dto.AppResourceScope;
import com.tencent.bk.job.common.model.dto.HostDTO;
import com.tencent.bk.job.common.mysql.JobTransactional;
import com.tencent.bk.job.common.util.JobContextUtil;
import com.tencent.bk.job.common.util.date.DateUtils;
import com.tencent.bk.job.common.util.json.JsonUtils;
Expand Down Expand Up @@ -84,7 +85,6 @@
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.time.Instant;
Expand Down Expand Up @@ -200,7 +200,7 @@ public boolean updateCronJobErrorById(CronJobInfoDTO cronJobErrorInfo) {
}

@Override
@Transactional(value = "jobCrontabTransactionManager", rollbackFor = {Exception.class, Error.class})
@JobTransactional(transactionManager = "jobCrontabTransactionManager")
@ActionAuditRecord(
actionId = ActionId.CREATE_CRON,
instance = @AuditInstanceRecord(
Expand All @@ -227,7 +227,7 @@ public CronJobInfoDTO createCronJobInfo(String username, CronJobInfoDTO cronJobI
}

@Override
@Transactional(rollbackFor = {Exception.class, Error.class})
@JobTransactional(transactionManager = "jobCrontabTransactionManager")
@ActionAuditRecord(
actionId = ActionId.MANAGE_CRON,
instance = @AuditInstanceRecord(
Expand Down Expand Up @@ -393,7 +393,7 @@ public Boolean deleteCronJobInfo(String username, Long appId, Long cronJobId) {
}

@Override
@Transactional(value = "jobCrontabTransactionManager", rollbackFor = {Exception.class, Error.class})
@JobTransactional(transactionManager = "jobCrontabTransactionManager")
@ActionAuditRecord(
actionId = ActionId.MANAGE_CRON,
instance = @AuditInstanceRecord(
Expand Down Expand Up @@ -452,7 +452,7 @@ public Boolean changeCronJobEnableStatus(String username, Long appId, Long cronJ
}

@Override
@Transactional(value = "jobCrontabTransactionManager", rollbackFor = {Exception.class, Error.class})
@JobTransactional(transactionManager = "jobCrontabTransactionManager")
public Boolean disableExpiredCronJob(Long appId, Long cronJobId, String lastModifyUser, Long lastModifyTime) {
CronJobInfoDTO cronJobInfo = new CronJobInfoDTO();
cronJobInfo.setAppId(appId);
Expand Down Expand Up @@ -508,7 +508,7 @@ public Map<Long, List<CronJobInfoDTO>> listCronJobByPlanIds(Long appId, List<Lon
}

@Override
@Transactional(value = "jobCrontabTransactionManager", rollbackFor = {Error.class, Exception.class})
@JobTransactional(transactionManager = "jobCrontabTransactionManager")
public Boolean addInnerJob(ServiceAddInnerCronJobRequestDTO request) {
if (!request.validate()) {
return false;
Expand Down Expand Up @@ -600,7 +600,7 @@ public Boolean deleteInnerCronJob(String systemId, String jobKey) {
}

@Override
@Transactional(value = "jobCrontabTransactionManager", rollbackFor = {Exception.class, Error.class})
@JobTransactional(transactionManager = "jobCrontabTransactionManager")
@ActionAuditRecord(
actionId = ActionId.MANAGE_CRON,
instance = @AuditInstanceRecord(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InternalException;
import com.tencent.bk.job.common.util.file.FileUtil;
import com.tencent.bk.job.common.util.JobUUID;
import com.tencent.bk.job.common.util.file.FileUtil;
import com.tencent.bk.job.execute.engine.consts.FileDirTypeConf;
import com.tencent.bk.job.execute.engine.util.NFSUtils;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

import com.tencent.bk.job.common.artifactory.model.dto.NodeDTO;
import com.tencent.bk.job.common.artifactory.sdk.ArtifactoryClient;
import com.tencent.bk.job.common.util.file.FileUtil;
import com.tencent.bk.job.common.util.TimeUtil;
import com.tencent.bk.job.common.util.file.FileUtil;
import com.tencent.bk.job.common.util.file.PathUtil;
import com.tencent.bk.job.execute.constants.Consts;
import com.tencent.bk.job.execute.model.FileDetailDTO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,14 @@ private void addScriptLogsAndRefreshPullProgress(List<ServiceExecuteObjectScript
return;
}
int offset = executeObjectTask.getScriptLogOffset();
int contentSizeBytes = 0;
if (StringUtils.isNotEmpty(content)) {
int bytes = content.getBytes(StandardCharsets.UTF_8).length;
offset += bytes;
contentSizeBytes = content.getBytes(StandardCharsets.UTF_8).length;
offset += contentSizeBytes;
executeObjectTask.setScriptLogOffset(offset);
}
logs.add(logService.buildScriptLog(stepInstance, executeObject,
executeObjectTaskResult.getScreen(), offset));
executeObjectTaskResult.getScreen(), contentSizeBytes, offset));
}
// 刷新日志拉取偏移量
refreshPullLogProgress(executeObjectTaskResult.getScreen(), executeObjectGseKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,17 @@ ServiceExecuteObjectScriptLogDTO buildSystemScriptLog(StepInstanceBaseDTO stepIn
/**
* 构造脚本日志
*
* @param stepInstance 步骤实例
* @param executeObject 执行对象
* @param content 日志原始内容
* @param offset 日志偏移 - 字节
* @param stepInstance 步骤实例
* @param executeObject 执行对象
* @param content 日志原始内容
* @param contentSizeBytes 日志内容大小(单位byte)
* @param offset 日志偏移 - 字节
* @return 系统日志
*/
ServiceExecuteObjectScriptLogDTO buildScriptLog(StepInstanceBaseDTO stepInstance,
ExecuteObject executeObject,
String content,
int contentSizeBytes,
int offset);

/**
Expand Down
Loading

0 comments on commit 75280c8

Please sign in to comment.