Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ใช้ Boto3 หยุด Workflow ใน AWS Glue Data Catalog

ภาพรวม

สาธิตการหยุด workflow run ที่กำลังดำเนินการอยู่โดยใช้ stop_workflow_run ต้องระบุ workflow name และ run ID

ตัวอย่างโค้ด

import boto3
from botocore.exceptions import ClientError

def stop_workflow(workflow_name, run_id):
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        response = glue_client.stop_workflow_run(
            Name=workflow_name,
            RunId=run_id
        )
        return response
    except ClientError as e:
        raise Exception(f"boto3 client error: {e}")
    except Exception as e:
        raise Exception(f"Unexpected error: {e}")

# ใช้งาน (ต้องมี run_id จากการเริ่ม workflow)
# run_id = start_workflow(...)['RunId']
# print(stop_workflow("test-daily", run_id))

ผลลัพธ์ตัวอย่าง

{
    'RunId': 'wr_64e880240692fddd5e1b19aed587f856bc20a96f54bc',
    'ResponseMetadata': {
        'RequestId': '782e953b-8ee3-4876-9b2c-cd35e147b513',
        'HTTPStatusCode': 200,
        ...
    }
}

ข้อสังเกต

  • ต้องมี RunId ของ workflow run ที่ต้องการหยุด (ได้จาก start_workflow_run หรือ get_workflow_runs)
  • หยุดได้เฉพาะ workflow ที่อยู่ในสถานะ RUNNING
  • ต้องมี permission glue:StopWorkflowRun