2017-06-05 31 views
0

我正在使用this mongodb3 cookbook来安装mongodb实例。厨师:mmake确定一项服务正在网络上侦听

我想,当服务开始执行脚本:

execute "Add Mongo Users" do 
    command "mongo #{host}:#{port} /tmp/mongo.setup.users.js" 
    subscribes :run, 'service[mongod]', :delayed 
end 

我得到这个错误:

==> Expected process to exit with [0], but received '1' 
==> ---- Begin output of mongo localhost:30158 /tmp/mongo.setup.users.js ---- 
==> STDOUT: MongoDB shell version v3.4.2 
==> connecting to: localhost:30158 
==> 2017-06-05T07:39:48.136+0000 W NETWORK [thread1] Failed to connect to 127.0.0.1:30158, in(checking soc 
ket for error after poll), reason: Connection refused 
==> 2017-06-05T07:39:48.136+0000 E QUERY [thread1] Error: couldn't connect to server localhost:30158, co 
nnection attempt failed : 

所以,你可以看到,服务尚未运行时命令执行(我收到网络错误)。

我看了一下cookbook的存储库代码。根据this peace of code,服务开始...

任何想法?

+0

可能是该命令有点太快。触发服务启动并不意味着它已准备好接受连接。 – StephenKing

+0

请注意您的主题中的拼写。 – StephenKing

+0

也许更新'command'参数的值并预先输入'ss -ntl;'先输出监听端口列表(检查我是否正确)。 – StephenKing

回答

0

由于触发服务的启动并不意味着它立即准备好接受连接,您需要等待一段时间。

  • 一种常见的方法是添加一个额外的,硬编码的延迟,例如

    command "sleep 3; mongo.." 
    

    下行:3秒可能就足够了机器A,而不是在计算机B(所以把20秒,你已经解决了它的情况下,99.9% - 但并没有成为一个更好的程序员; - ))。

  • 更好的方法可以是例如出现在jenkins cookbook's helper library

    def wait_until_ready! 
        Timeout.timeout(timeout, JenkinsTimeout) do 
        begin 
         open(endpoint) 
        rescue SocketError, 
         Errno::ECONNREFUSED, 
         Errno::ECONNRESET, 
         Errno::ENETUNREACH, 
         Timeout::Error, 
         OpenURI::HTTPError => e 
         # If authentication has been enabled, the server will return an HTTP 
         # 403. This is "OK", since it means that the server is actually 
         # ready to accept requests. 
         return if e.message =~ /^403/ 
    
         Chef::Log.debug("Jenkins is not accepting requests - #{e.message}") 
         sleep(0.5) 
         retry 
        end 
        end 
    rescue JenkinsTimeout 
        raise JenkinsNotReady.new(endpoint, timeout) 
    end 
    

    此代码反复尝试连接到詹金斯,直到其作品或达到超时。你应该能够适应这个mongo(你可以调用这个命令并检查它是否成功,或者直接连接到TCP端口,就像詹金斯代码一样)。

  • 之间的一种方法是使用Chef的retry参数:

    execute "Add Mongo Users" do 
        command "mongo #{host}:#{port} /tmp/mongo.setup.users.js" 
        retry 10 
        subscribes :run, 'service[mongod]', :delayed 
    end 
    

    我不能完全肯定,如果它会工作,为mongo命令可能返回的故障很快,留下一点时间长达启动服务(使之与万能的sleep结合? )另外,根据你执行的命令,确保你不会造成负面的副作用(当它可以连接到服务,但在后面的步骤中失败 - 结果是它再次运行。想想这里的数据损坏和朋友)。

相关问题