当前位置: 首页 > 面试题库 >

脚本块中的NonSerializableException

呼延靖
2023-03-14
问题内容

当我将test()调用放入脚本中时,我得到了 NonSerializableException ,当我注释掉 A 行而未注释掉 B
行时,它工作得很好。

test()在脚本内部和外部进行调用有什么区别?

我现在正在考虑的解决方法是使用when条件Jenkinsfile

更新 我怀疑该错误与需要“节点”块的命令有关

Jenkinsfile

#!/usr/bin/env groovy
import groovy.json.JsonSlurper

@NonCPS
def test() {
    sh "aws ecs update-service --cluster dev-cluster-name --service service-name --desired-count 0 --region us-west-2"
}

def deployDockerContainer() {
    script {
        def describeServices = sh(returnStdout: true, script: "aws ecs describe-services --region us-west-2 --cluster dev-cluster-name --services service-name").trim()
        def describeServicesJSON = new JsonSlurper().parseText(describeServices)

        if (describeServicesJSON.services.size() > 0 && describeServicesJSON.services[0].status.equals('ACTIVE')) {
            test() //A
        }
    }
    // test() //B
    sh "/usr/local/bin/ecs-cli ps --region us-west-2a --cluster dev-clustername"
}

pipeline {
    agent any

    environment {
        ENVIRONMENT = 'dev'
    }

    parameters {
        booleanParam(name: 'DEPLOY_TO_DEV', defaultValue: true, description: 'Deploy to Dev Environment?')
        booleanParam(name: 'DEPLOY_TO_TEST', defaultValue: false, description: 'Deploy to Test Environment?')
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: false, description: 'Deploy to Production Environment?')
    }

    stages {
        stage('DEV:Deploy') {
            when {
                expression {
                    return params.DEPLOY_TO_DEV
                }
            }
            environment {
                ENVIRONMENT = 'dev'
            }
            steps {
                deployDockerContainer()
            }
        }
    }
}

堆栈跟踪:

an exception which occurred:
    in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@4c090b96
    in field com.cloudbees.groovy.cps.impl.ProxyEnv.parent
    in object com.cloudbees.groovy.cps.impl.BlockScopeEnv@45444499
    in field com.cloudbees.groovy.cps.impl.CallEnv.caller
    in object com.cloudbees.groovy.cps.impl.FunctionCallEnv@2e3aa6ff
    in field com.cloudbees.groovy.cps.Continuable.e
    in object org.jenkinsci.plugins.workflow.cps.SandboxContinuable@8393d12
    in field org.jenkinsci.plugins.workflow.cps.CpsThread.program
    in object org.jenkinsci.plugins.workflow.cps.CpsThread@46045514
    in field org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.threads
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@420ee007
    in object org.jenkinsci.plugins.workflow.cps.CpsThreadGroup@420ee007
Caused: java.io.NotSerializableException: groovy.json.internal.LazyMap
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:860)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
    at java.util.HashMap.internalWriteEntries(HashMap.java:1790)
    at java.util.HashMap.writeObject(HashMap.java:1363)
    at sun.reflect.GeneratedMethodAccessor322.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:273)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:967)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.BlockMarshaller.doWriteObject(BlockMarshaller.java:65)
    at org.jboss.marshalling.river.BlockMarshaller.writeObject(BlockMarshaller.java:56)
    at org.jboss.marshalling.MarshallerObjectOutputStream.writeObjectOverride(MarshallerObjectOutputStream.java:50)
    at org.jboss.marshalling.river.RiverObjectOutputStream.writeObjectOverride(RiverObjectOutputStream.java:179)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:344)
    at java.util.TreeMap.writeObject(TreeMap.java:2438)
    at sun.reflect.GeneratedMethodAccessor336.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jboss.marshalling.reflect.SerializableClass.callWriteObject(SerializableClass.java:273)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:976)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteFields(RiverMarshaller.java:1032)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteSerializableObject(RiverMarshaller.java:988)
    at org.jboss.marshalling.river.RiverMarshaller.doWriteObject(RiverMarshaller.java:854)
    at org.jboss.marshalling.AbstractObjectOutput.writeObject(AbstractObjectOutput.java:58)
    at org.jboss.marshalling.AbstractMarshaller.writeObject(AbstractMarshaller.java:111)
    at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.lambda$writeObject$0(RiverWriter.java:144)
    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox.runInSandbox(GroovySandbox.java:108)
    at org.jenkinsci.plugins.workflow.support.pickles.serialization.RiverWriter.writeObject(RiverWriter.java:143)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:477)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgram(CpsThreadGroup.java:453)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.saveProgramIfPossible(CpsThreadGroup.java:440)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.run(CpsThreadGroup.java:367)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup.access$200(CpsThreadGroup.java:83)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:244)
    at org.jenkinsci.plugins.workflow.cps.CpsThreadGroup$2.call(CpsThreadGroup.java:232)
    at org.jenkinsci.plugins.workflow.cps.CpsVmExecutorService$2.call(CpsVmExecutorService.java:64)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at hudson.remoting.SingleLaneExecutorService$1.run(SingleLaneExecutorService.java:131)
    at jenkins.util.ContextResettingExecutorService$1.run(ContextResettingExecutorService.java:28)
    at jenkins.security.ImpersonatingExecutorService$1.run(ImpersonatingExecutorService.java:59)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

aws ecs update-service手动执行:

{
    "service": {
        "serviceArn": "XXXXXXXX",
        "serviceName": "XXXXXXXX",
        "clusterArn": "XXXXXXXX",
        "loadBalancers": [
            {
                "targetGroupArn": "XXXXXXXX",
                "containerName": "XXXXXXXX",
                "containerPort": 8080
            }
        ],
        "serviceRegistries": [],
        "status": "ACTIVE",
        "desiredCount": 0,
        "runningCount": 0,
        "pendingCount": 0,
        "launchType": "EC2",
        "taskDefinition": "XXXXXXXX",
        "deploymentConfiguration": {
            "maximumPercent": 200,
            "minimumHealthyPercent": 100
        },
        "deployments": [
            {
                "id": "XXXXXXXX",
                "status": "PRIMARY",
                "taskDefinition": "XXXXXXXX",
                "desiredCount": 0,
                "pendingCount": 0,
                "runningCount": 0,
                "createdAt": 1525230109.446,
                "updatedAt": 1525234874.41,
                "launchType": "EC2"
            }
        ],
        "roleArn": "XXXXXXXX",
        "events": [
            {
                "id": "XXXXXXXX",
                "createdAt": 1525230133.646,
                "message": "XXXXXXXX has reached a steady state."
            },
            {
                "id": "XXXXXXXX",
                "createdAt": 1525230121.278,
                "message": "XXXXXXXX has stopped 1 running tasks: (task XXXXXXXX)."
            },
            {
                "id": "XXXXXXXX",
                "createdAt": 1525230109.914,
                "message": "(service XXXXXXXX) has started 1 tasks: (task XXXXXXXX)."
            }
        ],
        "createdAt": 1525230109.446,
        "placementConstraints": [],
        "placementStrategy": [],
        "healthCheckGracePeriodSeconds": 0
    }
}

问题答案:

首先JsonSlurper是不可序列化的。如果要使用它,则必须将其封装到@NonCPS方法中。

其次,您不能在@NonCPS方法中使用任何cps方法,例如sh

第三,如果您不遵循这些规则,有时管道(cps代码)运行程序不会立即引发错误。有时,对脚本本身进行一些更改后会引发错误。



 类似资料:
  • 问题内容: 我试图使用以下代码执行构建,最后,在构建成功时执行构建后操作。仍然,我得到了MultipleCompilationErrorsException,说我的try块不是有效的节定义。请帮忙,我尝试了很多重组障碍,但似乎无法解决问题。 问题答案: 您使用的是声明式的方式来指定管道,因此您不得使用try / catch块(用于脚本化管道),而应使用post部分。参见:https : //jen

  • 包括目录中的所有模块,而中的模块通过包括 在运行时,上面的操作很好。但是,和也作为它们自己的脚本工作,但是运行会出现错误 从导入中删除可以使各个脚本正常工作,但主脚本找不到。 我如何使导入对主程序和单个模块都起作用?

  • Cocos Creator 允许你将代码拆分成多个脚本文件,并且让它们相互调用。要实现这点,你需要了解如何在 Cocos Creator 中定义和使用模块,这个步骤简称为 模块化。 如果你还不确定模块化究竟能做什么,模块化相当于: Java 和 Python 中的 import C# 中的 using C/C++ 中的 include HTML 中的 <link> 模块化使你可以在 Cocos C

  • 问题内容: 认为标题总结了问题:-) 问题答案: 脚本通常是直接运行的代码,由其本身运行。模块通常是由其他代码段导入的库。 请注意, 没有内部区别 -两者都是可执行的和可导入的,尽管直接执行时库代码通常不执行任何操作(或仅运行其单元测试),而导入设计为脚本的代码将导致其执行,因此通用测试。

  • 问题内容: 我试图通过和安装PYAHK 这两个错误均显示以下消息: 但是…所讨论的模块实际上确实存在 有模块无法在python3中命名的原因吗?我在安装过程中错过了什么吗? 问题答案: 在Python 2中是相对导入,但在Python 3中成为绝对导入;没有全局模块“脚本”,因此错误。在Py3中,它必须是或相对导入。

  • 问题内容: 有没有办法让python脚本加载和使用环境模块?不起作用,因为它在子shell中执行它们(至少,我认为这是正在发生的事情)。 问题答案: 我知道这个问题有点陈旧,但是它仍然足够相关,以至于我一直在寻找答案,因此我发布了我发现同样有效的内容: 至少在3.2.9+版本的源代码中,您可以包含python“ init”文件来获取模块的python函数版本: 有人告诉我早期版本可以在没有.py扩