|
2 | 2 | Workflow examples - function-based and class-based styles. |
3 | 3 | """ |
4 | 4 |
|
5 | | -from fastloop import FastLoop, LoopContext, LoopEvent, Workflow, WorkflowBlock |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from fastloop import ( |
| 8 | + BlockPlan, |
| 9 | + FastLoop, |
| 10 | + LoopContext, |
| 11 | + LoopEvent, |
| 12 | + ScheduleType, |
| 13 | + Workflow, |
| 14 | + WorkflowBlock, |
| 15 | +) |
6 | 16 |
|
7 | 17 | app = FastLoop(name="workflow-demo") |
8 | 18 |
|
@@ -127,6 +137,95 @@ async def execute( |
127 | 137 | print(f"Survey results: {answers}") |
128 | 138 |
|
129 | 139 |
|
| 140 | +@app.event("start_email_monitor") |
| 141 | +class StartEmailMonitor(LoopEvent): |
| 142 | + pass |
| 143 | + |
| 144 | + |
| 145 | +async def email_monitor_plan( |
| 146 | + _ctx: LoopContext, |
| 147 | + _blocks: list[WorkflowBlock], |
| 148 | + current_block: WorkflowBlock, |
| 149 | + block_output: Any, |
| 150 | +) -> BlockPlan: |
| 151 | + """Plan function decides next block and scheduling based on block output.""" |
| 152 | + match current_block.type: |
| 153 | + case "search_emails": |
| 154 | + if block_output and block_output.get("found_emails"): |
| 155 | + return BlockPlan(schedule_type=ScheduleType.IMMEDIATE) |
| 156 | + return BlockPlan( |
| 157 | + next_block_index=0, |
| 158 | + schedule_type=ScheduleType.DELAY, |
| 159 | + delay_seconds=600, |
| 160 | + reason="No emails found, retry in 10 minutes", |
| 161 | + ) |
| 162 | + case "notify": |
| 163 | + return BlockPlan(schedule_type=ScheduleType.STOP) |
| 164 | + case _: |
| 165 | + return BlockPlan(schedule_type=ScheduleType.IMMEDIATE) |
| 166 | + |
| 167 | + |
| 168 | +@app.workflow( |
| 169 | + name="email_monitor", |
| 170 | + start_event=StartEmailMonitor, |
| 171 | + plan=email_monitor_plan, |
| 172 | +) |
| 173 | +async def email_monitor_workflow( |
| 174 | + ctx: LoopContext, |
| 175 | + _blocks: list[WorkflowBlock], |
| 176 | + current_block: WorkflowBlock, |
| 177 | +) -> dict[str, Any]: |
| 178 | + """Workflow that returns output for the plan function to decide next steps.""" |
| 179 | + print(f"[{ctx.block_index + 1}/{ctx.block_count}] {current_block.type}") |
| 180 | + |
| 181 | + match current_block.type: |
| 182 | + case "search_emails": |
| 183 | + emails = [] # simulate search |
| 184 | + return {"found_emails": len(emails) > 0, "emails": emails} |
| 185 | + case "summarize": |
| 186 | + return {"summary": "Email summary"} |
| 187 | + case "notify": |
| 188 | + return {"notified": True} |
| 189 | + case _: |
| 190 | + return {} |
| 191 | + |
| 192 | + |
| 193 | +@app.event("start_retry_demo") |
| 194 | +class StartRetryDemo(LoopEvent): |
| 195 | + pass |
| 196 | + |
| 197 | + |
| 198 | +@app.workflow(name="retry_demo", start_event=StartRetryDemo) |
| 199 | +class RetryDemoWorkflow(Workflow): |
| 200 | + """Class-based workflow with plan method for rate limit handling.""" |
| 201 | + |
| 202 | + async def plan( |
| 203 | + self, |
| 204 | + ctx: LoopContext, |
| 205 | + _blocks: list[WorkflowBlock], |
| 206 | + _current_block: WorkflowBlock, |
| 207 | + block_output: Any, |
| 208 | + ) -> BlockPlan | None: |
| 209 | + if block_output and block_output.get("rate_limited"): |
| 210 | + return BlockPlan( |
| 211 | + next_block_index=ctx.block_index, |
| 212 | + schedule_type=ScheduleType.DELAY, |
| 213 | + delay_seconds=block_output.get("retry_after", 60), |
| 214 | + ) |
| 215 | + if ctx.block_index >= ctx.block_count - 1: |
| 216 | + return BlockPlan(schedule_type=ScheduleType.STOP) |
| 217 | + return None |
| 218 | + |
| 219 | + async def execute( |
| 220 | + self, |
| 221 | + ctx: LoopContext, |
| 222 | + _blocks: list[WorkflowBlock], |
| 223 | + current_block: WorkflowBlock, |
| 224 | + ) -> dict[str, Any]: |
| 225 | + print(f"[{ctx.block_index + 1}/{ctx.block_count}] {current_block.type}") |
| 226 | + return {"success": True} |
| 227 | + |
| 228 | + |
130 | 229 | if __name__ == "__main__": |
131 | 230 | app.run(port=8111) |
132 | 231 |
|
@@ -156,3 +255,21 @@ async def execute( |
156 | 255 | # curl http://localhost:8111/onboarding/<workflow_id> |
157 | 256 | # |
158 | 257 | # 5. If service restarts, workflow resumes from current block automatically |
| 258 | +# |
| 259 | +# 6. Start email monitor workflow with plan function: |
| 260 | +# curl -X POST http://localhost:8111/email_monitor \ |
| 261 | +# -H "Content-Type: application/json" \ |
| 262 | +# -d '{ |
| 263 | +# "type": "start_email_monitor", |
| 264 | +# "blocks": [ |
| 265 | +# {"type": "search_emails", "text": "Search for customer emails"}, |
| 266 | +# {"type": "summarize", "text": "Summarize found emails"}, |
| 267 | +# {"type": "notify", "text": "Send notification"} |
| 268 | +# ] |
| 269 | +# }' |
| 270 | +# |
| 271 | +# The plan function will: |
| 272 | +# - Retry the search block with a 10-minute delay if no emails found |
| 273 | +# - Proceed immediately when emails are found |
| 274 | +# - Stop the workflow after notification |
| 275 | +# - Delays use Redis scheduling, so the workflow survives restarts |
0 commit comments