A job queue in TSV files for background tasks, with retries and exponential backoff.

0 downloads

dbbasic-queue/examples/flask_app.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-queue.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-queue.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-queue.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Update README with PyPI information and badges 89cd41e 1 year, 1 month ago History
flask_app.py · 5.4 KB · 232 lines
1#!/usr/bin/env python3
2"""
3Example Flask web application using dbbasic-queue
4
5Install dependencies:
6 pip install flask
7
8Run:
9 python3 examples/flask_app.py
10
11Then test:
12 curl -X POST http://localhost:5000/api/send-email \
13 -H "Content-Type: application/json" \
14 -d '{"to":"user@example.com","subject":"Test","body":"Hello!"}'
15"""
16
17import sys
18import os
19
20# Add parent directory to path
21sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
22
23from flask import Flask, request, jsonify
24from dbbasic_queue import enqueue, get_job, cancel_job
25
26app = Flask(__name__)
27
28
29@app.route('/')
30def index():
31 return '''
32 <h1>DBBasic Queue Example</h1>
33 <p>Try these endpoints:</p>
34 <ul>
35 <li>POST /api/send-email - Queue an email job</li>
36 <li>GET /api/jobs/&lt;job_id&gt; - Check job status</li>
37 <li>DELETE /api/jobs/&lt;job_id&gt; - Cancel job</li>
38 </ul>
39 '''
40
41
42@app.route('/api/send-email', methods=['POST'])
43def send_email():
44 """
45 API endpoint that queues email job
46
47 Request body:
48 {
49 "to": "user@example.com",
50 "subject": "Welcome",
51 "body": "Thanks for signing up!"
52 }
53 """
54 data = request.json
55
56 # Validate input
57 if not data or 'to' not in data or 'subject' not in data:
58 return jsonify({'error': 'Missing required fields'}), 400
59
60 # Queue job instead of sending immediately
61 job_id = enqueue('send_email', {
62 'to': data['to'],
63 'subject': data['subject'],
64 'body': data.get('body', '')
65 })
66
67 # Return immediately (don't wait for email to send)
68 return jsonify({
69 'job_id': job_id,
70 'status': 'queued',
71 'message': 'Email queued for delivery'
72 }), 202
73
74
75@app.route('/api/process-video', methods=['POST'])
76def process_video():
77 """
78 API endpoint that queues video processing job
79
80 Request body:
81 {
82 "video_id": 42
83 }
84 """
85 data = request.json
86
87 if not data or 'video_id' not in data:
88 return jsonify({'error': 'Missing video_id'}), 400
89
90 job_id = enqueue('process_video', {
91 'video_id': data['video_id']
92 })
93
94 return jsonify({
95 'job_id': job_id,
96 'status': 'queued',
97 'message': 'Video queued for processing'
98 }), 202
99
100
101@app.route('/api/schedule-report', methods=['POST'])
102def schedule_report():
103 """
104 API endpoint that schedules delayed report generation
105
106 Request body:
107 {
108 "user_id": 99,
109 "delay": 3600 // seconds (optional)
110 }
111 """
112 import time
113 data = request.json
114
115 if not data or 'user_id' not in data:
116 return jsonify({'error': 'Missing user_id'}), 400
117
118 delay = data.get('delay', 0)
119 run_at = int(time.time()) + delay if delay else None
120
121 job_id = enqueue('generate_report',
122 {'user_id': data['user_id']},
123 run_at=run_at
124 )
125
126 return jsonify({
127 'job_id': job_id,
128 'status': 'scheduled' if delay else 'queued',
129 'run_at': run_at,
130 'message': f"Report {'scheduled' if delay else 'queued'}"
131 }), 202
132
133
134@app.route('/api/jobs/<job_id>')
135def job_status(job_id):
136 """
137 Check job status
138
139 Returns:
140 {
141 "id": "abc123",
142 "type": "send_email",
143 "status": "completed",
144 "result": {...}
145 }
146 """
147 job = get_job(job_id)
148 if not job:
149 return jsonify({'error': 'Job not found'}), 404
150
151 return jsonify(job)
152
153
154@app.route('/api/jobs/<job_id>', methods=['DELETE'])
155def cancel_job_endpoint(job_id):
156 """
157 Cancel pending job
158
159 Returns:
160 {
161 "cancelled": true
162 }
163 """
164 cancelled = cancel_job(job_id)
165
166 if cancelled:
167 return jsonify({'cancelled': True, 'message': 'Job cancelled'})
168 else:
169 return jsonify({
170 'cancelled': False,
171 'message': 'Job not found or already processing/completed'
172 }), 400
173
174
175@app.route('/api/queue/stats')
176def queue_stats():
177 """
178 Get queue statistics
179
180 Returns:
181 {
182 "pending": 5,
183 "processing": 1,
184 "completed": 42,
185 "failed": 2
186 }
187 """
188 # Read queue file and count statuses
189 import os
190 queue_file = 'data/queue.tsv'
191
192 if not os.path.exists(queue_file):
193 return jsonify({
194 'pending': 0,
195 'processing': 0,
196 'completed': 0,
197 'failed': 0,
198 'cancelled': 0
199 })
200
201 stats = {
202 'pending': 0,
203 'processing': 0,
204 'completed': 0,
205 'failed': 0,
206 'cancelled': 0
207 }
208
209 with open(queue_file, 'r') as f:
210 lines = f.readlines()
211 for line in lines[1:]: # Skip header
212 if line.strip():
213 parts = line.strip().split('\t')
214 if len(parts) >= 4:
215 status = parts[3]
216 if status in stats:
217 stats[status] += 1
218
219 return jsonify(stats)
220
221
222if __name__ == '__main__':
223 print("Starting Flask app...")
224 print("API Endpoints:")
225 print(" POST http://localhost:5000/api/send-email")
226 print(" POST http://localhost:5000/api/process-video")
227 print(" POST http://localhost:5000/api/schedule-report")
228 print(" GET http://localhost:5000/api/jobs/<job_id>")
229 print(" DELETE http://localhost:5000/api/jobs/<job_id>")
230 print(" GET http://localhost:5000/api/queue/stats")
231 print()
232 app.run(debug=True, port=5000)