Fabric收到错误时如何继续执行任务

编程入门 行业动态 更新时间:2024-10-28 06:26:15
本文介绍了Fabric收到错误时如何继续执行任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我定义一个要在多个远程服务器上运行的任务时,如果该任务在一个服务器上运行并退出并出现错误,Fabric将停止并中止该任务.但是我想让Fabric忽略该错误并在下一台服务器上运行该任务.我怎样才能做到这一点?

When I define a task to run on several remote servers, if the task runs on server one and exits with an error, Fabric will stop and abort the task. But I want to make fabric ignore the error and run the task on the next server. How can I make it do this?

例如:

$ fab site1_service_gw [site1rpt1] Executing task 'site1_service_gw' [site1fep1] run: echo 'Nm123!@#' | sudo -S route [site1fep1] err: [site1fep1] err: We trust you have received the usual lecture from the local System [site1fep1] err: Administrator. It usually boils down to these three things: [site1fep1] err: [site1fep1] err: #1) Respect the privacy of others. [site1fep1] err: #2) Think before you type. [site1fep1] err: #3) With great power comes great responsibility. [site1fep1] err: root's password: [site1fep1] err: sudo: route: command not found Fatal error: run() encountered an error (return code 1) while executing 'echo 'Nm123!@#' | sudo -S route ' Aborting.

推荐答案

来自文档:

... Fabric默认为快速失败"行为模式:如果出现任何错误,例如远程程序返回非零返回值或fabfile的Python代码遇到异常,执行将立即停止.

... Fabric defaults to a "fail-fast" behavior pattern: if anything goes wrong, such as a remote program returning a nonzero return value or your fabfile’s Python code encountering an exception, execution will halt immediately.

这通常是所需的行为,但是该规则有很多例外,因此Fabric提供了布尔值env.warn_only.默认为False,这意味着错误情况将导致程序立即中止.但是,如果在发生故障时将env.warn_only设置为True(例如使用设置上下文管理器),则Fabric将发出警告消息,但会继续执行.

This is typically the desired behavior, but there are many exceptions to the rule, so Fabric provides env.warn_only, a Boolean setting. It defaults to False, meaning an error condition will result in the program aborting immediately. However, if env.warn_only is set to True at the time of failure – with, say, the settings context manager – Fabric will emit a warning message but continue executing.

看起来您可以通过使用 settings上下文管理器,如下所示:

Looks like you can exercise fine-grained control over where errors are ignored by using the settings context manager, something like so:

from fabric.api import settings sudo('mkdir tmp') # can't fail with settings(warn_only=True): sudo('touch tmp/test') # can fail sudo('rm tmp') # can't fail

更多推荐

Fabric收到错误时如何继续执行任务

本文发布于:2023-10-24 07:49:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   Fabric

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!