File size: 3,319 Bytes
0242ab2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from executor.action_registry import (
    ACTION_REGISTRY
)

# Importing action modules automatically registers them
# into ACTION_REGISTRY during module initialization.
import executor.actions.goto_action
import executor.actions.click_action
import executor.actions.fill_action
import executor.actions.wait_action
import executor.actions.extract_action
import executor.actions.save_results_action
import executor.actions.search_action

from models.execution_log import (
    ExecutionLog
)


class WorkflowExecutor:

    async def execute(
        self,
        page,
        workflow,
        db=None,
        run_id=None
    ):

        # Store results returned by each workflow step
        results = []

        # Execute workflow steps one by one in the defined order
        for index, step in enumerate(
            workflow["steps"],
            start=1
        ):

            action = step["action"]

            print(
                "ACTION_REGISTRY =",
                ACTION_REGISTRY
            )

            print(
                "ACTION =",
                action
            )

            # Find the function corresponding to this action
            handler = ACTION_REGISTRY.get(
                action
            )

            if not handler:

                # Log failure if action is unsupported
                if db and run_id:

                    db.add(
                        ExecutionLog(
                            run_id=run_id,
                            step_number=index,
                            action=action,
                            status="failed",
                            message=f"Unknown action: {action}"
                        )
                    )

                    db.commit()

                raise Exception(
                    f"Unknown action: {action}"
                )

            try:

                # Execute the Playwright action
                result = await handler(
                    page,
                    step
                )

                results.append(
                    result
                )

                # Store successful execution log
                if db and run_id:

                    db.add(
                        ExecutionLog(
                            run_id=run_id,
                            step_number=index,
                            action=action,
                            status=result.get(
                                "status",
                                "success"
                            ),
                            message=f"{action} executed successfully"
                        )
                    )

                    db.commit()

            except Exception as e:

                # Log step failure before stopping workflow execution
                if db and run_id:

                    db.add(
                        ExecutionLog(
                            run_id=run_id,
                            step_number=index,
                            action=action,
                            status="failed",
                            message=str(e)
                        )
                    )

                    db.commit()

                raise e

        # Return results collected from all workflow steps
        return results