Integrating Dagster with Django

เมื่อปีที่แล้วเห็นคำถามนี้ใน Stack Overflow เลยไปตอบดู คิดว่าเอามาแชร์ในนี้ด้วยน่าจะมีประโยชน์เผื่อใครแวะเข้ามาครับผม

ด้านล่างนี้เป็นโค้ดที่ผมใช้ครับ

import os
import sys

import django
from dagster import execute_pipeline, pipeline, solid


# Add the project to sys.path, so that Python can find packages
PROJECT_ROOT = os.path.join(os.path.dirname(__file__), 'demo')
sys.path.append(PROJECT_ROOT)

# Set up the Django environment
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'demo.settings')
django.setup()


from customers.models import Customer


@solid
def hello_django_orm(context):
    number_of_customers = Customer.objects.count()
    message = f'Found {number_of_customers} customers!'
    context.log.info(message)
    return number_of_customers


@pipeline
def hello_django_orm_pipeline():
    hello_django_orm()


if __name__ == '__main__':
    result = execute_pipeline(hello_django_orm_pipeline)
    assert result.success

Ref: hello-dagster/hello_django_orm.py at main · zkan/hello-dagster · GitHub

หมายเหตุไว้สักหน่อย โค้ดที่ใช้ด้านบนเป็นโค้ด Dagster เวอร์ชั่นเก่านะครับ

ตอนนี้เค้าเปลี่ยนคอนเซป และเปลี่ยนจากคำว่า solid ไปเป็นคำว่า ops เปลี่ยน pipeline ไปเป็น graph แล้ว~