askrobots / dbbasic-object-primitive
Public
Python functions as REST objects, with clustering, load balancing and state replication; no containers or orchestration.
06_task_queue.py · 5.9 KB · 245 lines
| 1 | """ |
| 2 | 06_task_queue.py - Task queue with priorities |
| 3 | """ |
| 4 | |
| 5 | import json |
| 6 | from datetime import datetime |
| 7 | from typing import List, Dict, Any |
| 8 | |
| 9 | _logger = None |
| 10 | _state_manager = None |
| 11 | |
| 12 | |
| 13 | def POST(request): |
| 14 | """ |
| 15 | Add a task to the queue. |
| 16 | |
| 17 | Request: |
| 18 | { |
| 19 | 'title': 'Task title', |
| 20 | 'priority': 'HIGH' | 'NORMAL' | 'LOW', # Optional, default: NORMAL |
| 21 | 'description': 'Task description' # Optional |
| 22 | } |
| 23 | """ |
| 24 | |
| 25 | # Validate |
| 26 | if 'title' not in request: |
| 27 | return {'status': 'error', 'message': 'Missing field: title'} |
| 28 | |
| 29 | title = request['title'] |
| 30 | priority = request.get('priority', 'NORMAL') |
| 31 | description = request.get('description', '') |
| 32 | |
| 33 | # Validate priority |
| 34 | valid_priorities = ['HIGH', 'NORMAL', 'LOW'] |
| 35 | if priority not in valid_priorities: |
| 36 | return { |
| 37 | 'status': 'error', |
| 38 | 'message': f'Invalid priority: {priority}. Valid: {valid_priorities}', |
| 39 | } |
| 40 | |
| 41 | # Create task |
| 42 | tasks = _get_tasks() |
| 43 | task_id = _get_next_task_id() |
| 44 | |
| 45 | task = { |
| 46 | 'task_id': task_id, |
| 47 | 'title': title, |
| 48 | 'description': description, |
| 49 | 'priority': priority, |
| 50 | 'status': 'PENDING', |
| 51 | 'created_at': datetime.now().isoformat(), |
| 52 | } |
| 53 | |
| 54 | tasks.append(task) |
| 55 | _save_tasks(tasks) |
| 56 | |
| 57 | if _logger: |
| 58 | _logger.info('Task added', task_id=task_id, title=title, priority=priority) |
| 59 | |
| 60 | return { |
| 61 | 'status': 'ok', |
| 62 | 'message': f'Task added: {task_id}', |
| 63 | 'task': task, |
| 64 | } |
| 65 | |
| 66 | |
| 67 | def GET(request): |
| 68 | """ |
| 69 | Get tasks. |
| 70 | |
| 71 | Request: |
| 72 | {} # Get all tasks |
| 73 | {'status': 'PENDING'} # Filter by status |
| 74 | {'priority': 'HIGH'} # Filter by priority |
| 75 | {'task_id': 1} # Get specific task |
| 76 | """ |
| 77 | |
| 78 | tasks = _get_tasks() |
| 79 | |
| 80 | # Filter by task_id |
| 81 | if 'task_id' in request: |
| 82 | task_id = int(request['task_id']) |
| 83 | task = _find_task(tasks, task_id) |
| 84 | |
| 85 | if not task: |
| 86 | return {'status': 'error', 'message': f'Task not found: {task_id}'} |
| 87 | |
| 88 | return {'status': 'ok', 'task': task} |
| 89 | |
| 90 | # Filter by status |
| 91 | if 'status' in request: |
| 92 | tasks = [t for t in tasks if t['status'] == request['status']] |
| 93 | |
| 94 | # Filter by priority |
| 95 | if 'priority' in request: |
| 96 | tasks = [t for t in tasks if t['priority'] == request['priority']] |
| 97 | |
| 98 | # Sort by priority (HIGH > NORMAL > LOW) |
| 99 | priority_order = {'HIGH': 0, 'NORMAL': 1, 'LOW': 2} |
| 100 | tasks_sorted = sorted(tasks, key=lambda t: priority_order[t['priority']]) |
| 101 | |
| 102 | if _logger: |
| 103 | _logger.info('Tasks retrieved', count=len(tasks_sorted), filters=request) |
| 104 | |
| 105 | return { |
| 106 | 'status': 'ok', |
| 107 | 'tasks': tasks_sorted, |
| 108 | 'count': len(tasks_sorted), |
| 109 | } |
| 110 | |
| 111 | |
| 112 | def PUT(request): |
| 113 | """ |
| 114 | Update task status. |
| 115 | |
| 116 | Request: |
| 117 | { |
| 118 | 'task_id': 1, |
| 119 | 'status': 'IN_PROGRESS' | 'COMPLETED' | 'CANCELLED' |
| 120 | } |
| 121 | """ |
| 122 | |
| 123 | if 'task_id' not in request: |
| 124 | return {'status': 'error', 'message': 'Missing field: task_id'} |
| 125 | |
| 126 | if 'status' not in request: |
| 127 | return {'status': 'error', 'message': 'Missing field: status'} |
| 128 | |
| 129 | task_id = int(request['task_id']) |
| 130 | new_status = request['status'] |
| 131 | |
| 132 | # Validate status |
| 133 | valid_statuses = ['PENDING', 'IN_PROGRESS', 'COMPLETED', 'CANCELLED'] |
| 134 | if new_status not in valid_statuses: |
| 135 | return { |
| 136 | 'status': 'error', |
| 137 | 'message': f'Invalid status: {new_status}. Valid: {valid_statuses}', |
| 138 | } |
| 139 | |
| 140 | # Find task |
| 141 | tasks = _get_tasks() |
| 142 | task = _find_task(tasks, task_id) |
| 143 | |
| 144 | if not task: |
| 145 | return {'status': 'error', 'message': f'Task not found: {task_id}'} |
| 146 | |
| 147 | # Update status |
| 148 | old_status = task['status'] |
| 149 | task['status'] = new_status |
| 150 | task['updated_at'] = datetime.now().isoformat() |
| 151 | |
| 152 | if new_status == 'COMPLETED': |
| 153 | task['completed_at'] = datetime.now().isoformat() |
| 154 | |
| 155 | _save_tasks(tasks) |
| 156 | |
| 157 | if _logger: |
| 158 | _logger.info( |
| 159 | 'Task status updated', |
| 160 | task_id=task_id, |
| 161 | old_status=old_status, |
| 162 | new_status=new_status, |
| 163 | ) |
| 164 | |
| 165 | return { |
| 166 | 'status': 'ok', |
| 167 | 'message': f'Task {task_id} status: {old_status} → {new_status}', |
| 168 | 'task': task, |
| 169 | } |
| 170 | |
| 171 | |
| 172 | def DELETE(request): |
| 173 | """ |
| 174 | Delete a task. |
| 175 | |
| 176 | Request: |
| 177 | {'task_id': 1} |
| 178 | """ |
| 179 | |
| 180 | if 'task_id' not in request: |
| 181 | return {'status': 'error', 'message': 'Missing field: task_id'} |
| 182 | |
| 183 | task_id = int(request['task_id']) |
| 184 | |
| 185 | # Find and remove task |
| 186 | tasks = _get_tasks() |
| 187 | task = _find_task(tasks, task_id) |
| 188 | |
| 189 | if not task: |
| 190 | return {'status': 'error', 'message': f'Task not found: {task_id}'} |
| 191 | |
| 192 | tasks = [t for t in tasks if t['task_id'] != task_id] |
| 193 | _save_tasks(tasks) |
| 194 | |
| 195 | if _logger: |
| 196 | _logger.warning('Task deleted', task_id=task_id, title=task['title']) |
| 197 | |
| 198 | return { |
| 199 | 'status': 'ok', |
| 200 | 'message': f'Task deleted: {task_id}', |
| 201 | 'task': task, |
| 202 | } |
| 203 | |
| 204 | |
| 205 | def _get_tasks() -> List[Dict[str, Any]]: |
| 206 | """Get all tasks from state""" |
| 207 | if _state_manager: |
| 208 | tasks_json = _state_manager.get('tasks', '[]') |
| 209 | return json.loads(tasks_json) |
| 210 | else: |
| 211 | return [] |
| 212 | |
| 213 | |
| 214 | def _save_tasks(tasks: List[Dict[str, Any]]) -> None: |
| 215 | """Save tasks to state""" |
| 216 | if _state_manager: |
| 217 | tasks_json = json.dumps(tasks) |
| 218 | _state_manager.set('tasks', tasks_json) |
| 219 | |
| 220 | |
| 221 | def _get_next_task_id() -> int: |
| 222 | """Get next task ID""" |
| 223 | if _state_manager: |
| 224 | next_id = _state_manager.get('next_task_id', 1) |
| 225 | _state_manager.set('next_task_id', next_id + 1) |
| 226 | return next_id |
| 227 | else: |
| 228 | return 1 |
| 229 | |
| 230 | |
| 231 | def _find_task(tasks: List[Dict[str, Any]], task_id: int) -> Dict[str, Any]: |
| 232 | """Find task by ID""" |
| 233 | for task in tasks: |
| 234 | if task['task_id'] == task_id: |
| 235 | return task |
| 236 | return None |
| 237 | |
| 238 | |
| 239 | __endpoint__ = { |
| 240 | 'name': 'task_queue', |
| 241 | 'description': 'Task queue with priorities and status tracking', |
| 242 | 'version': '1.0.0', |
| 243 | 'author': 'tutorial', |
| 244 | 'methods': ['GET', 'POST', 'PUT', 'DELETE'], |
| 245 | } |