File size: 3,597 Bytes
4595394
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
# =====================================================
# Apckeyl Framework
# Version 1.0
# task_manager.py
# =====================================================

"""
Apckeyl Task Manager.

Version 1.0

Создаёт и хранит задачи Control Plane.

На этом этапе Task Manager работает только
в памяти процесса.

Постоянное хранение, очередь и внешняя база
будут добавлены позже.
"""

from datetime import datetime, timezone
from itertools import count

from control_plane import control_plane


# =====================================================
# Task Manager
# =====================================================

class TaskManager:

    def __init__(self, control_plane_instance=None):

        if control_plane_instance is None:
            control_plane_instance = control_plane

        self.control_plane = control_plane_instance

        self._tasks = {}

        self._counter = count(1)


    # =================================================
    # Create Task
    # =================================================

    def create_task(
        self,
        task_type,
        payload=None,
    ):

        if not task_type:
            raise ValueError(
                "task_type is required"
            )

        module = self.control_plane.resolve_task(
            task_type
        )

        number = next(
            self._counter
        )

        task_id = (
            f"task_{number:06d}"
        )

        task = {

            "task_id": task_id,

            "task_type": task_type,

            "status": "created",

            "module_id": module[
                "module_id"
            ],

            "module_name": module[
                "module_name"
            ],

            "payload": payload,

            "created_at": (
                datetime.now(
                    timezone.utc
                ).isoformat()
            ),
        }

        self._tasks[
            task_id
        ] = task

        return dict(task)


    # =================================================
    # Get Task
    # =================================================

    def get_task(
        self,
        task_id,
    ):

        task = self._tasks.get(
            task_id
        )

        if task is None:
            return None

        return dict(task)


    # =================================================
    # Update Status
    # =================================================

    def update_status(
        self,
        task_id,
        status,
    ):

        task = self._tasks.get(
            task_id
        )

        if task is None:

            raise KeyError(
                f"Unknown task: {task_id}"
            )

        task["status"] = status

        return dict(task)


    # =================================================
    # List Tasks
    # =================================================

    def list_tasks(self):

        return [
            dict(task)
            for task
            in self._tasks.values()
        ]


    # =================================================
    # Delete Task
    # =================================================

    def delete_task(
        self,
        task_id,
    ):

        if task_id in self._tasks:

            del self._tasks[
                task_id
            ]


# =====================================================
# Default Task Manager
# =====================================================

task_manager = TaskManager()