Repository navigation
Expand file tree
/
Copy pathtemporal_worker_fixed.py
More file actions
75 lines (64 loc) · 2.6 KB
/
Copy pathtemporal_worker_fixed.py
File metadata and controls
75 lines (64 loc) · 2.6 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
# temporal_version/worker.py
import asyncio
import logging
from temporalio.worker import Worker
from temporalio.client import Client
# Import from the same directory
import temporal_workflows_fixed as temporal_workflows
import temporal_activities
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
async def main():
"""
Start the Temporal worker.
The worker is responsible for:
1. Connecting to the Temporal server
2. Polling for workflow and activity tasks
3. Executing workflows and activities
4. Reporting results back to Temporal
Key benefits of Temporal workers:
- Horizontal scalability (run multiple workers)
- Automatic load balancing
- Graceful shutdown handling
- Built-in metrics and monitoring
"""
# Connect to Temporal server
# In production, you would configure authentication and other settings
client = await Client.connect("localhost:7233")
logger.info("Connected to Temporal server")
# Create and configure the worker
worker = Worker(
client,
task_queue="order-processing-queue", # Task queue name
workflows=[
temporal_workflows.OrderProcessingWorkflow, # Register workflow classes
temporal_workflows.OrderApprovalWorkflow,
],
activities=[
# Register activity methods
temporal_activities.OrderProcessingActivities.check_inventory,
temporal_activities.OrderProcessingActivities.process_payment,
temporal_activities.OrderProcessingActivities.reserve_inventory,
temporal_activities.OrderProcessingActivities.ship_order,
temporal_activities.OrderProcessingActivities.send_notification,
temporal_activities.OrderProcessingActivities.rollback_payment,
temporal_activities.OrderProcessingActivities.rollback_inventory,
temporal_activities.OrderProcessingActivities.get_order_status,
],
max_concurrent_activities=10, # Limit concurrent activities
)
logger.info("Worker configured successfully")
logger.info("Task queue: order-processing-queue")
logger.info("Registered workflows: OrderProcessingWorkflow, OrderApprovalWorkflow")
logger.info("Registered activities: 8 activities")
# Start the worker
logger.info("Starting worker... (Press Ctrl+C to stop)")
try:
await worker.run()
except KeyboardInterrupt:
logger.info("Shutting down worker...")
finally:
await client.close()
if __name__ == "__main__":
asyncio.run(main())