Skip to content

Commit

Permalink
1.修复日志乱码问题;
Browse files Browse the repository at this point in the history
2.timeout参数校验;
3.跳转链接优化。
  • Loading branch information
jsonwan committed Jun 23, 2021
1 parent 6dadfe5 commit 86a9a3d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
28 changes: 18 additions & 10 deletions src/main/kotlin/com/tencent/bk/devops/atom/task/JobScriptAtom.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import com.tencent.bk.devops.atom.task.utils.JobUtils
import com.tencent.bk.devops.atom.task.utils.Keys
import com.tencent.bk.devops.atom.utils.json.JsonUtil
import org.apache.commons.lang3.StringUtils
import java.nio.charset.Charset
import java.util.Base64
import org.slf4j.LoggerFactory
import java.lang.IndexOutOfBoundsException
import java.nio.charset.Charset
import java.util.*

@AtomService(paramClass = InnerJobParam::class)
class JobScriptAtom : TaskAtom<InnerJobParam> {
Expand Down Expand Up @@ -54,6 +53,13 @@ class JobScriptAtom : TaskAtom<InnerJobParam> {
fastExecuteScript(param, result)
}

private fun printLink(bizId: String, taskInstanceId: Long, jobHost: String) {
logger.info("对接蓝鲸企业版3.x/社区版6.x中的作业平台请点击以下链接查看详情:")
logger.info(JobUtils.getV3DetailUrl(bizId, taskInstanceId, jobHost))
logger.info("对接蓝鲸企业版2.x/社区版5.x中的作业平台请点击以下链接查看详情:")
logger.info(JobUtils.getV2DetailUrl(bizId, taskInstanceId, jobHost))
}

private fun fastExecuteScript(
param: InnerJobParam,
result: AtomResult
Expand All @@ -63,6 +69,11 @@ class JobScriptAtom : TaskAtom<InnerJobParam> {
val taskId = param.pipelineTaskId
val targetAccount = param.account
val timeout = 0L + (param.timeout ?: 1000)
if (timeout < 60L || timeout > 72000) {
result.status = Status.failure
result.message = "timeout参数不合法,必须为60-72000之间的整数"
return
}
var operator = param.pipelineStartUserName
var scriptContent = when (param.scriptType) {
"1" -> {
Expand Down Expand Up @@ -139,7 +150,7 @@ class JobScriptAtom : TaskAtom<InnerJobParam> {

try {
val taskInstanceId = JobUtils.fastExecuteScript(fastExecuteScriptReq, this.esbApiHost)
logger.info(JobUtils.getDetailUrl(bizId, taskInstanceId, jobHost))
printLink(bizId, taskInstanceId, jobHost)
val startTime = System.currentTimeMillis()
checkStatus(
bizId = bizId,
Expand All @@ -148,11 +159,9 @@ class JobScriptAtom : TaskAtom<InnerJobParam> {
taskInstanceId = taskInstanceId,
operator = operator,
buildId = buildId,
jobHost = esbApiHost,
result = result
)

logger.info(JobUtils.getDetailUrl(bizId, taskInstanceId, jobHost))
printLink(bizId, taskInstanceId, jobHost)
} catch (e: Exception) {
logger.error("Job API invoke failed", e)
result.status = Status.failure
Expand All @@ -175,16 +184,15 @@ class JobScriptAtom : TaskAtom<InnerJobParam> {
operator: String,
buildId: String,
taskId: String,
jobHost: String,
result: AtomResult
) {

var needContinue = true

while (needContinue) {
Thread.sleep(2000)
val taskResult = JobUtils.getTaskResult(appId, appSecret, bizId, taskInstanceId, operator, jobHost)
logger.info(JobUtils.getDetailUrl(bizId, taskInstanceId, jobHost))
val taskResult = JobUtils.getTaskResult(appId, appSecret, bizId, taskInstanceId, operator, esbApiHost)
printLink(bizId, taskInstanceId, jobHost)
if (taskResult.isFinish) {
needContinue = false
if (taskResult.success) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ class JobResourceApi {
bizId: String,
taskInstanceId: Long,
operator: String,
jobHost: String
esbHost: String
): TaskResult {
try {
val url =
"$jobHost/api/c/compapi/v2/job/get_job_instance_status/?bk_app_code=$appId&bk_app_secret=$appSecret&bk_username=$operator&bk_biz_id=$bizId&job_instance_id=$taskInstanceId"
"$esbHost/api/c/compapi/v2/job/get_job_instance_status/?bk_app_code=$appId&bk_app_secret=$appSecret&bk_username=$operator&bk_biz_id=$bizId&job_instance_id=$taskInstanceId"
val urlWithoutSecret =
"$jobHost/api/c/compapi/v2/job/get_job_instance_status/?bk_app_code=$appId&bk_app_secret=***&bk_username=$operator&bk_biz_id=$bizId&job_instance_id=$taskInstanceId"
"$esbHost/api/c/compapi/v2/job/get_job_instance_status/?bk_app_code=$appId&bk_app_secret=***&bk_username=$operator&bk_biz_id=$bizId&job_instance_id=$taskInstanceId"
logger.info("Get request url: $urlWithoutSecret")
OkhttpUtils.doGet(url).use { resp ->
val responseStr = resp.body()!!.string()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ object JobUtils {
bizId: String,
taskInstanceId: Long,
operator: String,
jobHost: String
esbHost: String
): JobResourceApi.TaskResult {
return jobResourceApi.getTaskResult(appId, appSecret, bizId, taskInstanceId, operator, jobHost)
return jobResourceApi.getTaskResult(appId, appSecret, bizId, taskInstanceId, operator, esbHost)
}

fun getDetailUrl(appId: String, taskInstanceId: Long, jobHost: String): String {
return "<a target='_blank' href='$jobHost/?taskInstanceList&appId=$appId#taskInstanceId=$taskInstanceId'>到作业平台V2查看详情(Go to JobV2 for Detail, click this if using BlueKing5.x)</a> <a target='_blank' href='$jobHost/api_execute/$taskInstanceId'>到作业平台V3查看详情(Go to JobV3 for Detail, click this if using BlueKing6.x)</a>"
fun getV2DetailUrl(appId: String, taskInstanceId: Long, jobHost: String): String {
return "JobV2: <a target='_blank' href='$jobHost/?taskInstanceList&appId=$appId#taskInstanceId=$taskInstanceId'>到作业平台V2查看详情(Go to JobV2 for Detail, click this if using BlueKing5.x)</a>";
}

fun getV3DetailUrl(appId: String, taskInstanceId: Long, jobHost: String): String {
return "JobV3: <a target='_blank' href='$jobHost/api_execute/$taskInstanceId'>到作业平台V3查看详情(Go to JobV3 for Detail, click this if using BlueKing6.x)</a>"
}
}
4 changes: 2 additions & 2 deletions task.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"language": "java",
"minimumVersion": "1.8",
"demands": [],
"target": "$bk_java_path -jar executeJobScript-jar-with-dependencies.jar -Dfile.encoding=utf8"
"target": "$bk_java_path -Dfile.encoding=utf8 -jar executeJobScript-jar-with-dependencies.jar"
},
"input": {
"bizId": {
Expand Down Expand Up @@ -175,7 +175,7 @@
"required": false
},
"timeout": {
"label": "超时时间(Script Timeout)",
"label": "超时时间(Timeout,单位为秒,范围60-72000)",
"default": "1000",
"type": "vuex-input",
"desc": "请输入脚本执行超时时间,单位为秒,范围60-72000,默认1000(Please input script timeout, time unit:second, range:60-72000, default:1000)",
Expand Down

0 comments on commit 86a9a3d

Please sign in to comment.