Fabric: كيف تقرأ النتائج في ThreadingGroup أثناء حالات الفشل الجزئي؟

تم إنشاؤها على ١٤ مايو ٢٠١٨  ·  5تعليقات  ·  مصدر: fabric/fabric

سيكون من الجيد تضمين مثال على ذلك في المستندات أيضًا. لنفترض أن لدي هذا الرمز.

from fabric import ThreadingGroup as Group
from fabric.exceptions import GroupException

hosts = ['web1', 'web2', 'web3']
g = Group(*hosts)

try:
    results = g.run('date', hide='both')
except GroupException as e:
    print e.args # I read from code that arguments are passed to exception but I'm unable to use that. `args` argument in my case is an empty tuple. 

لنفترض أن web1 و web3 نجحا بينما فشل اتصال web2 أو عاد الأمر برمز خروج غير صفري.

  • كيف أحصل على هذه التفاصيل بعد إثارة الاستثناء؟
  • هل يضمن عدم رفع الاستثناء إلا بعد الانتهاء من جميع العمليات؟

هو موضع تقدير أي مساعدة.

التعليق الأكثر فائدة

أهلا،

بالإضافة إلى تعليق شاديابي ، ها هي طريقة أستخدمها للتعامل مع GroupException.
آمل أن يساعد هذا !!

import logging, socket, paramiko.ssh_exception
from fabric import Connection, Config, ThreadingGroup, exceptions, runners

...

g = ThreadingGroup.from_connections(c)
try:
    result = g.run('hostname' )  # (just a basic command)
except exceptions.GroupException as e:
    i = 0
    for c, r in e.result.items():
        print("Host[{}] = [{}]".format(i,c.host) )
        if isinstance(r,runners.Result) :
            print("ok, fine")
        elif isinstance(r,socket.gaierror) :
            print("Network error")
        elif isinstance(r,paramiko.ssh_exception.AuthenticationException) :
            print("Auth failed")
        else:
            print("something else")
        i+=1



(حصلت عليه من دعم ActivCloud )

ال 5 كومينتر

مرحبًا shadyabhi ،

لست متأكدًا مما إذا كنت تحاول التقاط استثناء معين أو إذا كنت تريد فقط التقاط جميع الاستثناءات التي تحدث في سلاسل منفصلة. أعتقد أن كائن ThreadingGroup يمر بالفعل حول كائنات الاستثناء باستخدام قوائم الانتظار ويرفع إذا تمت مصادفة أي منها أثناء التنفيذ. تمكنت من رؤية تتبعات المكدس باستخدام مقتطف الشفرة التالي:

def run_data(c):
    c.run('date', hide='both')

<strong i="8">@task</strong>
def thread_exception_example(c):
    hosts = ['localhost', 'fakehost', 'localhost']

    # python list comprehension
    {c: run_data(c) for c in Group(*hosts)}

قمت بتشغيل سطر الأوامر: fab thread-exception-example

مرحبًا bossjones ،

شكرا على الرد. ومع ذلك ، فأنا لا أستخدم الأمر fab ولكني أقوم بتنفيذ كل هذا باستخدام النسيج كمكتبة. لذلك ، لا تزال الإجابة معلقة وسيكون موضع تقدير.

بمعنى آخر ، يتعامل fab مع ما أريد التعامل معه في الكود الخاص بي بنفسي. هناك أسباب تجعلني لا أرغب في استخدام الأمر fab مباشرة لأنني أرغب في الحصول على مزيد من المرونة في كيفية استدعاء المهام.

شكرا

مرحبا جميعا،

أنا آسف ، لقد أغفلت التفاصيل. فحصت رمز النسيج وحصلت على التفاصيل.

الوسيطة مع GroupException عبارة عن قاموس باستخدام المفتاح fabric.connection.Connection ككائن وقيمة fabric.runners.Result object.

except GroupException as e:
         for c, r in e.result.items():
             print "Connection: {}, Result: {}".format(c, r)

أهلا،

بالإضافة إلى تعليق شاديابي ، ها هي طريقة أستخدمها للتعامل مع GroupException.
آمل أن يساعد هذا !!

import logging, socket, paramiko.ssh_exception
from fabric import Connection, Config, ThreadingGroup, exceptions, runners

...

g = ThreadingGroup.from_connections(c)
try:
    result = g.run('hostname' )  # (just a basic command)
except exceptions.GroupException as e:
    i = 0
    for c, r in e.result.items():
        print("Host[{}] = [{}]".format(i,c.host) )
        if isinstance(r,runners.Result) :
            print("ok, fine")
        elif isinstance(r,socket.gaierror) :
            print("Network error")
        elif isinstance(r,paramiko.ssh_exception.AuthenticationException) :
            print("Auth failed")
        else:
            print("something else")
        i+=1



(حصلت عليه من دعم ActivCloud )

شكرًا akiuni على مثالك! أنا جديد على Python / Fabric وقد ساعدني ذلك في فهم كيفية تقديم الطلبات باستخدام ThreadedGroup ، وجمع النتائج عند وجود استثناء. ها هي النسخة الموسعة من مثالك. مقدر جدا! لما يستحق ، لقد قمت بنشر هذا على Stackoverflow أيضًا.

# requires fabric 2.x - run 'pip install fabric' to install it
import logging, socket, paramiko.ssh_exception
from fabric import Connection, Config, SerialGroup, ThreadingGroup, exceptions, runners
from fabric.exceptions import GroupException


# Note: You need to supply your own valid servers here to ssh to of course!
def main():
    testHosts("All should succeed", "validServer1,validServer2,validServer3")
    testHosts("Some should fail", "validServer1,validServer2,BADSERVER1,validServer3,BADSERVER2")

def testHosts(message, hostsAsString):
    print("")
    print(message)

    # Get list of hosts from somewhere, and convert them to connections
    hosts = hostsAsString.split(",")
    servers = [Connection(host=host) for host in hosts]

    # Create a thread group to run requests in parallel
    g = ThreadingGroup.from_connections(servers)
    try:
        command = "df -h / | tail -n1 | awk '{print $5}'"
        results = g.run(command, hide=True)
        for r in results:
            connection = results[r]
            print("{}".format(r.host) )
            print("  SUCCESS, " + connection.stdout.strip())
    except GroupException as e:
        # If an exception occurred, at least one request failed. 
        # Iterate through results here
        for c, r in e.result.items():
            print("{}".format(c.host) )
            if isinstance(r,runners.Result) :
                print("  SUCCESS, " + r.stdout.strip())
            elif isinstance(r,socket.gaierror) :
                print("  FAILED,  Network error")
            elif isinstance(r,paramiko.ssh_exception.AuthenticationException) :
                print("  FAILED,  Auth failed")
            else:
                print("  FAILED,  Something other reason")

main()

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات