Dùng boto3 gọi start_crawler để chạy crawler ngay lập tức. Ném CrawlerRunningException nếu crawler đã chạy.
Quy trình
- Import boto3, ClientError.
- Hàm nhận
crawler_name. - Tạo session, client Glue.
- Gọi
glue_client.start_crawler(Name=...). - Trả về response metadata.
- Xử lý ngoại lệ.
Code mẫu
import boto3
from botocore.exceptions import ClientError
def khoi_chay_crawler(crawler_name):
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.start_crawler(Name=crawler_name)
return response
except ClientError as e:
raise Exception("Lỗi boto3 client: " + str(e))
except Exception as e:
raise Exception("Lỗi không mong đợi: " + str(e))
# Lần 1: chạy bình thường
print(khoi_chay_crawler("Data Dimension"))
# Lần 2: crawler chưa xong -> ném exception
print(khoi_chay_crawler("Data Dimension"))
Kết quả
# Lần 1: {'ResponseMetadata': {'RequestId': '73e50130-...', 'HTTPStatusCode': 200, ...}}
# Lần 2: Exception: boto3 client error ... CrawlerRunningException: Crawler with name Data Dimension has already started