-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_workflow.rb
More file actions
177 lines (146 loc) · 5.25 KB
/
dynamic_workflow.rb
File metadata and controls
177 lines (146 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env ruby
# frozen_string_literal: true
# Dynamic Workflow Example
# =========================
#
# Demonstrates creating and executing workflows at runtime without pre-registration.
#
# What it does:
# -------------
# - Creates a workflow programmatically using Ruby code
# - Defines two workers: get_user_email and send_email
# - Chains tasks together using the >> operator
# - Executes the workflow with input data
#
# Use Cases:
# ----------
# - Workflows that cannot be defined statically (structure depends on runtime data)
# - Programmatic workflow generation based on business rules
# - Testing workflows without registering definitions
# - Rapid prototyping and development
#
# Key Concepts:
# -------------
# - ConductorWorkflow: Build workflows in code
# - Task chaining: Use >> operator to define task sequence
# - Dynamic execution: Create and run workflows on-the-fly
# - Worker tasks: Ruby methods/blocks that execute task logic
#
# Usage:
# bundle exec ruby examples/dynamic_workflow.rb
#
# Prerequisites:
# - Conductor server running (set CONDUCTOR_SERVER_URL)
# - For Orkes: Set CONDUCTOR_AUTH_KEY and CONDUCTOR_AUTH_SECRET
require_relative '../lib/conductor'
# Include workflow DSL for shorter class names
include Conductor::Workflow
# ============================================================================
# WORKERS - Define task implementations
# ============================================================================
# Worker 1: Get user email by user ID
class GetUserEmailWorker
include Conductor::Worker::WorkerModule
worker_task 'get_user_email'
def execute(task)
userid = get_input(task, 'userid', 'unknown')
email = "#{userid}@example.com"
puts "[GetUserEmailWorker] Generated email for userid=#{userid}: #{email}"
# Return the email as output
{ 'result' => email }
end
end
# Worker 2: Send email
class SendEmailWorker
include Conductor::Worker::WorkerModule
worker_task 'send_email'
def execute(task)
email = get_input(task, 'email', '')
subject = get_input(task, 'subject', 'No Subject')
body = get_input(task, 'body', '')
puts "[SendEmailWorker] Sending email to #{email}"
puts " Subject: #{subject}"
puts " Body: #{body}"
# Simulate sending email
{ 'status' => 'sent', 'to' => email }
end
end
def main
# Configuration from environment variables
# CONDUCTOR_SERVER_URL: Conductor server URL (e.g., https://developer.orkescloud.com/api)
# CONDUCTOR_AUTH_KEY: API Authentication Key (optional for OSS)
# CONDUCTOR_AUTH_SECRET: API Auth Secret (optional for OSS)
config = Conductor::Configuration.new
puts '=' * 70
puts 'Conductor Ruby SDK - Dynamic Workflow Example'
puts '=' * 70
puts
puts "Server: #{config.server_url}"
puts
# Create clients using OrkesClients factory
clients = Conductor::Orkes::OrkesClients.new(config)
workflow_executor = clients.get_workflow_executor
# Start workers in the background
task_handler = Conductor::Worker::TaskRunner.new(config)
task_handler.register_worker(GetUserEmailWorker.new)
task_handler.register_worker(SendEmailWorker.new)
task_handler.start
puts 'Workers started...'
puts
# ============================================================================
# BUILD WORKFLOW DYNAMICALLY
# ============================================================================
# Create workflow with executor for dynamic execution
workflow = ConductorWorkflow.new(
clients.get_workflow_client,
'dynamic_workflow_ruby',
version: 1,
executor: workflow_executor
)
# Define tasks
# Task 1: Get user email - uses workflow input for userid
get_email = SimpleTask.new('get_user_email', 'get_user_email_ref')
.input('userid', workflow.input('userid'))
# Task 2: Send email - uses output from get_email task
sendmail = SimpleTask.new('send_email', 'send_email_ref')
.input('email', get_email.output('result'))
.input('subject', 'Hello from Conductor Ruby SDK')
.input('body', 'This is a test email from a dynamic workflow')
# Chain tasks: workflow >> task1 >> task2
workflow >> get_email >> sendmail
# Configure the output of the workflow
workflow.output_parameter('email', get_email.output('result'))
# ============================================================================
# EXECUTE WORKFLOW
# ============================================================================
puts 'Executing dynamic workflow...'
puts
# Execute workflow synchronously with input
workflow_run = workflow.execute(
input: { 'userid' => 'user_a' },
wait_for_seconds: 30
)
puts
puts 'Workflow completed!'
puts '-' * 70
puts "Workflow ID: #{workflow_run.workflow_id}"
puts "Status: #{workflow_run.status}"
puts "Output: #{workflow_run.output.inspect}"
puts
puts "Check the workflow execution at: #{config.ui_host}/execution/#{workflow_run.workflow_id}"
# Stop workers
task_handler.stop
puts
puts 'Workers stopped. Goodbye!'
end
if __FILE__ == $PROGRAM_NAME
begin
main
rescue Conductor::ApiError => e
puts "API Error: #{e.message}"
puts e.backtrace.first(5).join("\n")
rescue StandardError => e
puts "Error: #{e.message}"
puts e.backtrace.first(5).join("\n")
end
end