diff --git a/.gitlab/workflows/.gitlab-ci.main.yml b/.gitlab/workflows/.gitlab-ci.main.yml index b5d7efb..ef93ac3 100644 --- a/.gitlab/workflows/.gitlab-ci.main.yml +++ b/.gitlab/workflows/.gitlab-ci.main.yml @@ -1,5 +1,4 @@ stages: - - build - tag - publish - reset @@ -11,34 +10,9 @@ variables: before_script: - rm -rf $GRADLE_USER_HOME/.tmp || true -# ✅ Build 阶段 -build: - stage: build - rules: - - if: '$CI_COMMIT_BRANCH == "main"' - cache: - key: - files: - - gradle/libs.versions.toml - - "**/*.gradle.kts" - prefix: lab-agent - paths: - - .gradle/caches/ - - .gradle/wrapper/ - - .gradle/kotlin-profile/ - - .kotlin/ - policy: pull-push - script: - - echo "🔧 授予 gradlew 执行权限..." - - chmod +x gradlew - - ./gradlew clean build - tags: - - java - # 🏷️ 自动打标签 tag: stage: tag - needs: [ "build" ] image: alpine:latest rules: - if: '$CI_COMMIT_BRANCH == "main"' @@ -82,6 +56,18 @@ publish: needs: [ "tag" ] rules: - if: '$CI_COMMIT_BRANCH == "main"' + cache: + key: + files: + - gradle/libs.versions.toml + - "**/*.gradle.kts" + prefix: lab-agent + paths: + - .gradle/caches/ + - .gradle/wrapper/ + - .gradle/kotlin-profile/ + - .kotlin/ + policy: pull-push script: - echo "🔧 授予 gradlew 执行权限..." - chmod +x gradlew @@ -92,7 +78,7 @@ publish: # 🔄 重建 test 分支 reset: stage: reset - needs: [ "build" ] + needs: [ "publish" ] image: alpine:latest rules: - if: '$CI_COMMIT_BRANCH == "main"' @@ -111,7 +97,7 @@ reset: # Mirror to GitHub mirror-to-github: stage: mirror - needs: [ "build" ] + needs: [ "publish" ] image: alpine:latest rules: - if: '$CI_COMMIT_BRANCH == "main"' diff --git a/.gitlab/workflows/.gitlab-ci.test.yml b/.gitlab/workflows/.gitlab-ci.test.yml index aaeb1ee..b625b5c 100644 --- a/.gitlab/workflows/.gitlab-ci.test.yml +++ b/.gitlab/workflows/.gitlab-ci.test.yml @@ -1,23 +1,9 @@ stages: - - build - publish -# 🧪 test 构建 -build: - stage: build - rules: - - if: '$CI_COMMIT_BRANCH == "test"' - script: - - echo "🔧 授予 gradlew 执行权限..." - - chmod +x gradlew - - ./gradlew clean build - tags: - - java - # 🧪 test 发布 SNAPSHOT 包(允许覆盖) publish: stage: publish - needs: ["build"] rules: - if: '$CI_COMMIT_BRANCH == "test"' script: diff --git a/forgeboot-context/forgeboot-context-impl/src/main/kotlin/com/gewuyou/forgeboot/context/impl/utils/CoroutineContextUtils.kt b/forgeboot-context/forgeboot-context-impl/src/main/kotlin/com/gewuyou/forgeboot/context/impl/utils/CoroutineContextUtils.kt new file mode 100644 index 0000000..81c70b1 --- /dev/null +++ b/forgeboot-context/forgeboot-context-impl/src/main/kotlin/com/gewuyou/forgeboot/context/impl/utils/CoroutineContextUtils.kt @@ -0,0 +1,36 @@ +package com.gewuyou.forgeboot.context.impl.utils + +import com.gewuyou.forgeboot.context.api.ContextProcessor +import com.gewuyou.forgeboot.context.impl.ContextHolder +import com.gewuyou.forgeboot.context.impl.coroutine.ContextAwareCoroutineScope +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Job + +/** + * 协程上下文工具类,用于在特定上下文中启动协程任务。 + * + * @since 2025-07-18 11:17:44 + * @author gewuyou + */ +object CoroutineContextUtils { + /** + * 在指定的上下文中启动一个协程任务,并返回对应的 Job 对象。 + * + * @param contextHolder 上下文持有者,用于管理上下文数据的生命周期。 + * @param processors 上下文处理器列表,用于处理上下文相关的逻辑。 + * @param dispatcher 协程调度器,决定协程在哪个线程或线程池中执行,默认为 [Dispatchers.Default]。 + * @param block 要执行的协程代码块,接收一个挂起的 [CoroutineScope] 扩展函数。 + * + * @return 返回一个 [Job] 对象,可用于取消或跟踪协程任务的状态。 + */ + fun launchWithScopedContext( + contextHolder: ContextHolder, + processors: List, + dispatcher: CoroutineDispatcher = Dispatchers.Default, + block: suspend CoroutineScope.() -> Unit, + ): Job { + return ContextAwareCoroutineScope(contextHolder, processors).launchWithContext(dispatcher, block) + } +} \ No newline at end of file diff --git a/forgeboot-demo/forgeboot-trace-demo/build.gradle.kts b/forgeboot-demo/forgeboot-trace-demo/build.gradle.kts index bbb864c..fcb499b 100644 --- a/forgeboot-demo/forgeboot-trace-demo/build.gradle.kts +++ b/forgeboot-demo/forgeboot-trace-demo/build.gradle.kts @@ -1,8 +1,8 @@ - dependencies { implementation(libs.springBootStarter.web) implementation(project(Modules.TRACE.STARTER)) implementation(project(Modules.Context.STARTER)) + implementation(project(Modules.Webmvc.DTO)) implementation(libs.kotlinxCoroutines.reactor) implementation(libs.kotlinxCoroutines.core) } diff --git a/forgeboot-demo/forgeboot-trace-demo/src/main/kotlin/com/gewuyou/forgeboot/trace/demo/controller/TraceTestController.kt b/forgeboot-demo/forgeboot-trace-demo/src/main/kotlin/com/gewuyou/forgeboot/trace/demo/controller/TraceTestController.kt index 855d3d5..629208c 100644 --- a/forgeboot-demo/forgeboot-trace-demo/src/main/kotlin/com/gewuyou/forgeboot/trace/demo/controller/TraceTestController.kt +++ b/forgeboot-demo/forgeboot-trace-demo/src/main/kotlin/com/gewuyou/forgeboot/trace/demo/controller/TraceTestController.kt @@ -5,6 +5,7 @@ import com.gewuyou.forgeboot.context.impl.ContextHolder import com.gewuyou.forgeboot.context.impl.coroutine.ContextAwareCoroutineScope import com.gewuyou.forgeboot.core.extension.log import com.gewuyou.forgeboot.trace.api.RequestIdProvider +import com.gewuyou.forgeboot.webmvc.dto.R import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @@ -23,7 +24,7 @@ class TraceTestController( private val processors: List, ) { @GetMapping("/coroutine") - suspend fun coroutine(): String { + suspend fun coroutine(): R { val requestId = requestIdProvider.getRequestId() log.info("→ Controller RequestId: $requestId") @@ -32,7 +33,7 @@ class TraceTestController( scope.launchWithContext { log.info("RID: ${requestIdProvider.getRequestId()}") } - return "Main coroutine returned immediately with requestId: $requestId" + return R.success("Main coroutine returned immediately with requestId: $requestId",requestIdProvider = requestIdProvider) } @GetMapping("/servlet") diff --git a/forgeboot-trace/forgeboot-trace-impl/src/main/kotlin/com/gewuyou/forgeboot/trace/impl/provider/TraceRequestIdProvider.kt b/forgeboot-trace/forgeboot-trace-impl/src/main/kotlin/com/gewuyou/forgeboot/trace/impl/provider/TraceRequestIdProvider.kt index fa234fa..6835cd0 100644 --- a/forgeboot-trace/forgeboot-trace-impl/src/main/kotlin/com/gewuyou/forgeboot/trace/impl/provider/TraceRequestIdProvider.kt +++ b/forgeboot-trace/forgeboot-trace-impl/src/main/kotlin/com/gewuyou/forgeboot/trace/impl/provider/TraceRequestIdProvider.kt @@ -4,6 +4,7 @@ import com.gewuyou.forgeboot.context.api.extension.get import com.gewuyou.forgeboot.context.impl.ContextHolder import com.gewuyou.forgeboot.trace.api.RequestIdProvider import com.gewuyou.forgeboot.trace.api.config.TraceProperties +import org.slf4j.MDC /** @@ -14,8 +15,8 @@ import com.gewuyou.forgeboot.trace.api.config.TraceProperties */ class TraceRequestIdProvider( private val traceProperties: TraceProperties, - private val contextHolder: ContextHolder -): RequestIdProvider { + private val contextHolder: ContextHolder, +) : RequestIdProvider { /** * 获取请求ID * @@ -24,6 +25,7 @@ class TraceRequestIdProvider( * @return 请求ID的字符串表示 */ override fun getRequestId(): String { - return contextHolder[traceProperties.requestIdMdcKey] ?:throw RuntimeException("requestId is null") + return contextHolder[traceProperties.requestIdMdcKey] ?: MDC.get(traceProperties.requestIdMdcKey) + ?: throw RuntimeException("requestId is null") } } \ No newline at end of file