草庐IT

go - Golang扫描函数中 "ERROR: IOError: closed stream"如何处理?

coder 2024-07-09 原文

我在 Golang 代码中使用 knife bootstrap 命令来引导节点。有时食谱中有等待,等待超过 10 分钟。在这种情况下,我收到错误“错误:IOStream 关​​闭”。有什么办法可以增加时间限制吗?我也在不断地从 cmd 的输出和错误流中读取日志。

func executeCMDWorkstation(cmd *exec.Cmd, projectId, cr_id string) bool {

stdout, err3 := cmd.StdoutPipe()
if err3 != nil {
    utils.Info.Println(err3)
    return false
}
defer stdout.Close()
stderr, err4 := cmd.StderrPipe()
if err4 != nil {
    utils.Info.Println(err4)
    return false
}
defer stderr.Close()

//cmd.Stdout = os.Stdout
//cmd.Stderr = os.Stderr
multi := io.MultiReader(stdout, stderr)
inp := bufio.NewScanner(multi)
err := cmd.Start()
if err != nil {
    utils.Info.Println("Error while creating client", err)
    return false
}
fmt.Println("Generating logs**************")
for inp.Scan() {

    line := inp.Text()
    if cr_id == "" {
        lg.SendDebugLogs(line, "projects/logs", projectId, "chef")
    } else {
        lg.SendDebugLogs(line, "changeRequests/logs", cr_id, "chef")
    }
}
exitStatus := cmd.Wait()
if exiterr, ok := exitStatus.(*exec.ExitError); ok {
    utils.Info.Println("************ Exit Status is:", exiterr, "************")
    return false
}

return true

我们将不胜感激任何有助于改进此过程的帮助。

最佳答案

在查看了一些论坛、Go Docs 并尝试了一些代码更改之后,最终将 bufio 中的扫描方法替换为对我有用的 textproto 的 ReadLine。

我替换了下面的代码:

inp := bufio.NewScanner(multi)
err := cmd.Start()
if err != nil {
    utils.Info.Println("Error while creating client", err)
    return false
}
fmt.Println("Generating logs**************")
for inp.Scan() {
    line := inp.Text()
    if cr_id == "" {
        lg.SendDebugLogs(line, "projects/logs", projectId, "chef")
    } else {
        lg.SendDebugLogs(line, "changeRequests/logs", cr_id, "chef")
    }
}

使用以下代码:

    inp1 := bufio.NewReader(multi)

    tp := textproto.NewReader( inp1 )

    err := cmd.Start()
    if err != nil {
        utils.Info.Println("Error while creating client", err)
        return false
    }
    fmt.Println("Generating logs**************")

    /*
    New code for logs starts here
     */


    for {
        line, readErr := tp.ReadLine()

        if readErr != nil {
                break
        }

        if cr_id == "" {
            lg.SendDebugLogs(line, "projects/logs", projectId, "chef")
        } else {
            lg.SendDebugLogs(line, "changeRequests/logs", cr_id, "chef")
        }
    }

最后,通过处理立即重启以及厨师食谱或脚本中的长时间 sleep /等待,此修改对我来说非常有效。基本上我改变了流阅读器我希望这对面临同样问题的其他人有帮助:)

关于go - Golang扫描函数中 "ERROR: IOError: closed stream"如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47012475/

有关go - Golang扫描函数中 "ERROR: IOError: closed stream"如何处理?的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - Ruby on Rails : . 常量化 : wrong constant name error? - 2

    我正在使用这个:4.times{|i|assert_not_equal("content#{i+2}".constantize,object.first_content)}我之前声明过局部变量content1content2content3content4content5我得到的错误NameError:wrongconstantnamecontent2这个错误是什么意思?我很确定我想要content2=\ 最佳答案 你必须用一个大字母来调用ruby​​常量:Content2而不是content2。Aconstantnamestart

  4. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  5. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  6. ruby-on-rails - Enumerator.new 如何处理已通过的 block ? - 2

    我在理解Enumerator.new方法的工作原理时遇到了一些困难。假设文档中的示例:fib=Enumerator.newdo|y|a=b=1loopdoy[1,1,2,3,5,8,13,21,34,55]循环中断条件在哪里,它如何知道循环应该迭代多少次(因为它没有任何明确的中断条件并且看起来像无限循环)? 最佳答案 Enumerator使用Fibers在内部。您的示例等效于:require'fiber'fiber=Fiber.newdoa=b=1loopdoFiber.yieldaa,b=b,a+bendend10.times.m

  7. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  8. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  9. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

  10. 使用 ACL 调用 upload_file 时出现 Ruby S3 "Access Denied"错误 - 2

    我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file

随机推荐