Initial commit

This commit is contained in:
Gregor Schulte
2026-02-02 08:41:48 +01:00
commit 43b4910a63
30 changed files with 4898 additions and 0 deletions

136
examples/basic/main.go Normal file
View File

@@ -0,0 +1,136 @@
// Basic example demonstrating how to create and run flows with the Prefect Go client.
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/gregor/prefect-go/pkg/client"
"github.com/gregor/prefect-go/pkg/models"
)
func main() {
// Create a new Prefect client
c, err := client.NewClient(
client.WithBaseURL("http://localhost:4200/api"),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// Create a new flow
fmt.Println("Creating flow...")
flow, err := c.Flows.Create(ctx, &models.FlowCreate{
Name: "example-flow",
Tags: []string{"example", "go-client", "basic"},
Labels: map[string]interface{}{
"environment": "development",
"version": "1.0",
},
})
if err != nil {
log.Fatalf("Failed to create flow: %v", err)
}
fmt.Printf("✓ Flow created: %s (ID: %s)\n", flow.Name, flow.ID)
// Get the flow by ID
fmt.Println("\nRetrieving flow...")
retrievedFlow, err := c.Flows.Get(ctx, flow.ID)
if err != nil {
log.Fatalf("Failed to get flow: %v", err)
}
fmt.Printf("✓ Flow retrieved: %s\n", retrievedFlow.Name)
// Create a flow run
fmt.Println("\nCreating flow run...")
flowRun, err := c.FlowRuns.Create(ctx, &models.FlowRunCreate{
FlowID: flow.ID,
Name: "example-run-1",
Parameters: map[string]interface{}{
"param1": "value1",
"param2": 42,
},
Tags: []string{"manual-run"},
})
if err != nil {
log.Fatalf("Failed to create flow run: %v", err)
}
fmt.Printf("✓ Flow run created: %s (ID: %s)\n", flowRun.Name, flowRun.ID)
fmt.Printf(" State: %v\n", flowRun.StateType)
// Set the flow run state to RUNNING
fmt.Println("\nUpdating flow run state to RUNNING...")
runningState := models.StateTypeRunning
updatedRun, err := c.FlowRuns.SetState(ctx, flowRun.ID, &models.StateCreate{
Type: runningState,
Message: strPtr("Flow run started"),
})
if err != nil {
log.Fatalf("Failed to set state: %v", err)
}
fmt.Printf("✓ State updated to: %v\n", updatedRun.StateType)
// Simulate some work
fmt.Println("\nSimulating work...")
time.Sleep(2 * time.Second)
// Set the flow run state to COMPLETED
fmt.Println("Updating flow run state to COMPLETED...")
completedState := models.StateTypeCompleted
completedRun, err := c.FlowRuns.SetState(ctx, flowRun.ID, &models.StateCreate{
Type: completedState,
Message: strPtr("Flow run completed successfully"),
})
if err != nil {
log.Fatalf("Failed to set state: %v", err)
}
fmt.Printf("✓ State updated to: %v\n", completedRun.StateType)
// List all flows
fmt.Println("\nListing all flows...")
flowsPage, err := c.Flows.List(ctx, nil, 0, 10)
if err != nil {
log.Fatalf("Failed to list flows: %v", err)
}
fmt.Printf("✓ Found %d flows (total: %d)\n", len(flowsPage.Results), flowsPage.Count)
for i, f := range flowsPage.Results {
fmt.Printf(" %d. %s (ID: %s)\n", i+1, f.Name, f.ID)
}
// List flow runs for this flow
fmt.Println("\nListing flow runs...")
runsPage, err := c.FlowRuns.List(ctx, &models.FlowRunFilter{
FlowID: &flow.ID,
}, 0, 10)
if err != nil {
log.Fatalf("Failed to list flow runs: %v", err)
}
fmt.Printf("✓ Found %d flow runs\n", len(runsPage.Results))
for i, run := range runsPage.Results {
fmt.Printf(" %d. %s - State: %v\n", i+1, run.Name, run.StateType)
}
// Clean up: delete the flow run and flow
fmt.Println("\nCleaning up...")
if err := c.FlowRuns.Delete(ctx, flowRun.ID); err != nil {
log.Printf("Warning: Failed to delete flow run: %v", err)
} else {
fmt.Println("✓ Flow run deleted")
}
if err := c.Flows.Delete(ctx, flow.ID); err != nil {
log.Printf("Warning: Failed to delete flow: %v", err)
} else {
fmt.Println("✓ Flow deleted")
}
fmt.Println("\n✓ Example completed successfully!")
}
func strPtr(s string) *string {
return &s
}

135
examples/deployment/main.go Normal file
View File

@@ -0,0 +1,135 @@
// Deployment example demonstrating how to create and manage deployments.
package main
import (
"context"
"fmt"
"log"
"github.com/gregor/prefect-go/pkg/client"
"github.com/gregor/prefect-go/pkg/models"
)
func main() {
// Create a new Prefect client
c, err := client.NewClient(
client.WithBaseURL("http://localhost:4200/api"),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// Create a flow first
fmt.Println("Creating flow...")
flow, err := c.Flows.Create(ctx, &models.FlowCreate{
Name: "scheduled-flow",
Tags: []string{"scheduled", "production"},
})
if err != nil {
log.Fatalf("Failed to create flow: %v", err)
}
fmt.Printf("✓ Flow created: %s (ID: %s)\n", flow.Name, flow.ID)
// Create a deployment
fmt.Println("\nCreating deployment...")
workPoolName := "default-pool"
deployment, err := c.Deployments.Create(ctx, &models.DeploymentCreate{
Name: "my-deployment",
FlowID: flow.ID,
WorkPoolName: &workPoolName,
Parameters: map[string]interface{}{
"default_param": "value",
},
Tags: []string{"automated"},
})
if err != nil {
log.Fatalf("Failed to create deployment: %v", err)
}
fmt.Printf("✓ Deployment created: %s (ID: %s)\n", deployment.Name, deployment.ID)
fmt.Printf(" Status: %v\n", deployment.Status)
fmt.Printf(" Paused: %v\n", deployment.Paused)
// Get the deployment
fmt.Println("\nRetrieving deployment...")
retrievedDeployment, err := c.Deployments.Get(ctx, deployment.ID)
if err != nil {
log.Fatalf("Failed to get deployment: %v", err)
}
fmt.Printf("✓ Deployment retrieved: %s\n", retrievedDeployment.Name)
// Get deployment by name
fmt.Println("\nRetrieving deployment by name...")
deploymentByName, err := c.Deployments.GetByName(ctx, flow.Name, deployment.Name)
if err != nil {
log.Fatalf("Failed to get deployment by name: %v", err)
}
fmt.Printf("✓ Deployment retrieved by name: %s\n", deploymentByName.Name)
// Pause the deployment
fmt.Println("\nPausing deployment...")
if err := c.Deployments.Pause(ctx, deployment.ID); err != nil {
log.Fatalf("Failed to pause deployment: %v", err)
}
fmt.Println("✓ Deployment paused")
// Check paused status
pausedDeployment, err := c.Deployments.Get(ctx, deployment.ID)
if err != nil {
log.Fatalf("Failed to get deployment: %v", err)
}
fmt.Printf(" Paused: %v\n", pausedDeployment.Paused)
// Resume the deployment
fmt.Println("\nResuming deployment...")
if err := c.Deployments.Resume(ctx, deployment.ID); err != nil {
log.Fatalf("Failed to resume deployment: %v", err)
}
fmt.Println("✓ Deployment resumed")
// Create a flow run from the deployment
fmt.Println("\nCreating flow run from deployment...")
flowRun, err := c.Deployments.CreateFlowRun(ctx, deployment.ID, map[string]interface{}{
"custom_param": "custom_value",
})
if err != nil {
log.Fatalf("Failed to create flow run: %v", err)
}
fmt.Printf("✓ Flow run created: %s (ID: %s)\n", flowRun.Name, flowRun.ID)
fmt.Printf(" Deployment ID: %v\n", flowRun.DeploymentID)
// Update deployment description
fmt.Println("\nUpdating deployment...")
description := "Updated deployment description"
updatedDeployment, err := c.Deployments.Update(ctx, deployment.ID, &models.DeploymentUpdate{
Description: &description,
})
if err != nil {
log.Fatalf("Failed to update deployment: %v", err)
}
fmt.Printf("✓ Deployment updated\n")
fmt.Printf(" Description: %v\n", updatedDeployment.Description)
// Clean up
fmt.Println("\nCleaning up...")
if err := c.FlowRuns.Delete(ctx, flowRun.ID); err != nil {
log.Printf("Warning: Failed to delete flow run: %v", err)
} else {
fmt.Println("✓ Flow run deleted")
}
if err := c.Deployments.Delete(ctx, deployment.ID); err != nil {
log.Printf("Warning: Failed to delete deployment: %v", err)
} else {
fmt.Println("✓ Deployment deleted")
}
if err := c.Flows.Delete(ctx, flow.ID); err != nil {
log.Printf("Warning: Failed to delete flow: %v", err)
} else {
fmt.Println("✓ Flow deleted")
}
fmt.Println("\n✓ Deployment example completed successfully!")
}

136
examples/monitoring/main.go Normal file
View File

@@ -0,0 +1,136 @@
// Monitoring example demonstrating how to monitor flow runs until completion.
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/gregor/prefect-go/pkg/client"
"github.com/gregor/prefect-go/pkg/models"
)
func main() {
// Create a new Prefect client
c, err := client.NewClient(
client.WithBaseURL("http://localhost:4200/api"),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// Create a flow
fmt.Println("Creating flow...")
flow, err := c.Flows.Create(ctx, &models.FlowCreate{
Name: "monitored-flow",
Tags: []string{"monitoring-example"},
})
if err != nil {
log.Fatalf("Failed to create flow: %v", err)
}
fmt.Printf("✓ Flow created: %s (ID: %s)\n", flow.Name, flow.ID)
// Create a flow run
fmt.Println("\nCreating flow run...")
flowRun, err := c.FlowRuns.Create(ctx, &models.FlowRunCreate{
FlowID: flow.ID,
Name: "monitored-run",
})
if err != nil {
log.Fatalf("Failed to create flow run: %v", err)
}
fmt.Printf("✓ Flow run created: %s (ID: %s)\n", flowRun.Name, flowRun.ID)
// Start monitoring in a goroutine
go func() {
// Simulate state transitions
time.Sleep(2 * time.Second)
// Set to RUNNING
fmt.Println("\n[Simulator] Setting state to RUNNING...")
runningState := models.StateTypeRunning
_, err := c.FlowRuns.SetState(ctx, flowRun.ID, &models.StateCreate{
Type: runningState,
Message: strPtr("Flow run started"),
})
if err != nil {
log.Printf("Error setting state: %v", err)
}
// Simulate work
time.Sleep(5 * time.Second)
// Set to COMPLETED
fmt.Println("[Simulator] Setting state to COMPLETED...")
completedState := models.StateTypeCompleted
_, err = c.FlowRuns.SetState(ctx, flowRun.ID, &models.StateCreate{
Type: completedState,
Message: strPtr("Flow run completed successfully"),
})
if err != nil {
log.Printf("Error setting state: %v", err)
}
}()
// Monitor the flow run until completion
fmt.Println("\nMonitoring flow run (polling every 2 seconds)...")
fmt.Println("Waiting for completion...")
// Create a context with timeout
monitorCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
// Wait for the flow run to complete
finalRun, err := c.FlowRuns.Wait(monitorCtx, flowRun.ID, 2*time.Second)
if err != nil {
log.Fatalf("Failed to wait for flow run: %v", err)
}
fmt.Printf("\n✓ Flow run completed!\n")
fmt.Printf(" Final state: %v\n", finalRun.StateType)
if finalRun.State != nil && finalRun.State.Message != nil {
fmt.Printf(" Message: %s\n", *finalRun.State.Message)
}
if finalRun.StartTime != nil {
fmt.Printf(" Start time: %v\n", finalRun.StartTime)
}
if finalRun.EndTime != nil {
fmt.Printf(" End time: %v\n", finalRun.EndTime)
}
fmt.Printf(" Total run time: %.2f seconds\n", finalRun.TotalRunTime)
// Query flow runs with a filter
fmt.Println("\nQuerying completed flow runs...")
completedState := models.StateTypeCompleted
runsPage, err := c.FlowRuns.List(ctx, &models.FlowRunFilter{
FlowID: &flow.ID,
StateType: &completedState,
}, 0, 10)
if err != nil {
log.Fatalf("Failed to list flow runs: %v", err)
}
fmt.Printf("✓ Found %d completed flow runs\n", len(runsPage.Results))
// Clean up
fmt.Println("\nCleaning up...")
if err := c.FlowRuns.Delete(ctx, flowRun.ID); err != nil {
log.Printf("Warning: Failed to delete flow run: %v", err)
} else {
fmt.Println("✓ Flow run deleted")
}
if err := c.Flows.Delete(ctx, flow.ID); err != nil {
log.Printf("Warning: Failed to delete flow: %v", err)
} else {
fmt.Println("✓ Flow deleted")
}
fmt.Println("\n✓ Monitoring example completed successfully!")
}
func strPtr(s string) *string {
return &s
}

152
examples/pagination/main.go Normal file
View File

@@ -0,0 +1,152 @@
// Pagination example demonstrating how to iterate through large result sets.
package main
import (
"context"
"fmt"
"log"
"github.com/gregor/prefect-go/pkg/client"
"github.com/gregor/prefect-go/pkg/models"
)
func main() {
// Create a new Prefect client
c, err := client.NewClient(
client.WithBaseURL("http://localhost:4200/api"),
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
ctx := context.Background()
// Create multiple flows for demonstration
fmt.Println("Creating sample flows...")
createdFlows := make([]models.Flow, 0)
for i := 1; i <= 15; i++ {
flow, err := c.Flows.Create(ctx, &models.FlowCreate{
Name: fmt.Sprintf("pagination-flow-%d", i),
Tags: []string{"pagination-example", fmt.Sprintf("batch-%d", (i-1)/5+1)},
})
if err != nil {
log.Fatalf("Failed to create flow %d: %v", i, err)
}
createdFlows = append(createdFlows, *flow)
fmt.Printf("✓ Created flow %d: %s\n", i, flow.Name)
}
// Example 1: Manual pagination with List
fmt.Println("\n--- Example 1: Manual Pagination ---")
pageSize := 5
page := 0
totalSeen := 0
for {
fmt.Printf("\nFetching page %d (offset: %d, limit: %d)...\n", page+1, page*pageSize, pageSize)
result, err := c.Flows.List(ctx, &models.FlowFilter{
Tags: []string{"pagination-example"},
}, page*pageSize, pageSize)
if err != nil {
log.Fatalf("Failed to list flows: %v", err)
}
fmt.Printf("✓ Page %d: Found %d flows\n", page+1, len(result.Results))
for i, flow := range result.Results {
fmt.Printf(" %d. %s (tags: %v)\n", totalSeen+i+1, flow.Name, flow.Tags)
}
totalSeen += len(result.Results)
if !result.HasMore {
fmt.Println("\n✓ No more pages")
break
}
page++
}
fmt.Printf("\nTotal flows seen: %d\n", totalSeen)
// Example 2: Automatic pagination with Iterator
fmt.Println("\n--- Example 2: Automatic Pagination with Iterator ---")
iter := c.Flows.ListAll(ctx, &models.FlowFilter{
Tags: []string{"pagination-example"},
})
count := 0
for iter.Next(ctx) {
flow := iter.Value()
if flow != nil {
count++
fmt.Printf("%d. %s\n", count, flow.Name)
}
}
if err := iter.Err(); err != nil {
log.Fatalf("Iterator error: %v", err)
}
fmt.Printf("\n✓ Total flows iterated: %d\n", count)
// Example 3: Collect all results at once
fmt.Println("\n--- Example 3: Collect All Results ---")
iter2 := c.Flows.ListAll(ctx, &models.FlowFilter{
Tags: []string{"pagination-example"},
})
allFlows, err := iter2.Collect(ctx)
if err != nil {
log.Fatalf("Failed to collect flows: %v", err)
}
fmt.Printf("✓ Collected %d flows at once\n", len(allFlows))
for i, flow := range allFlows {
fmt.Printf(" %d. %s\n", i+1, flow.Name)
}
// Example 4: Collect with limit
fmt.Println("\n--- Example 4: Collect With Limit ---")
iter3 := c.Flows.ListAll(ctx, &models.FlowFilter{
Tags: []string{"pagination-example"},
})
maxItems := 7
limitedFlows, err := iter3.CollectWithLimit(ctx, maxItems)
if err != nil {
log.Fatalf("Failed to collect flows: %v", err)
}
fmt.Printf("✓ Collected first %d flows (limit: %d)\n", len(limitedFlows), maxItems)
for i, flow := range limitedFlows {
fmt.Printf(" %d. %s\n", i+1, flow.Name)
}
// Example 5: Filter by specific tag batch
fmt.Println("\n--- Example 5: Filter by Specific Tag ---")
batch2Filter := &models.FlowFilter{
Tags: []string{"batch-2"},
}
batch2Page, err := c.Flows.List(ctx, batch2Filter, 0, 10)
if err != nil {
log.Fatalf("Failed to list flows: %v", err)
}
fmt.Printf("✓ Found %d flows with tag 'batch-2'\n", len(batch2Page.Results))
for i, flow := range batch2Page.Results {
fmt.Printf(" %d. %s (tags: %v)\n", i+1, flow.Name, flow.Tags)
}
// Clean up all created flows
fmt.Println("\n--- Cleaning Up ---")
for i, flow := range createdFlows {
if err := c.Flows.Delete(ctx, flow.ID); err != nil {
log.Printf("Warning: Failed to delete flow %d: %v", i+1, err)
} else {
fmt.Printf("✓ Deleted flow %d: %s\n", i+1, flow.Name)
}
}
fmt.Println("\n✓ Pagination example completed successfully!")
}