askrobots / dbbasic-queue
Public
A job queue in TSV files for background tasks, with retries and exponential backoff.
cgi_script.py · 1.3 KB · 58 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | Example CGI script using dbbasic-queue |
| 4 | |
| 5 | This demonstrates how to use the queue in a traditional CGI environment. |
| 6 | |
| 7 | Place this in your cgi-bin directory and make it executable: |
| 8 | chmod +x cgi-bin/submit-form.cgi |
| 9 | """ |
| 10 | |
| 11 | import cgi |
| 12 | import sys |
| 13 | import os |
| 14 | |
| 15 | # Add parent directory to path |
| 16 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 17 | |
| 18 | from dbbasic_queue import enqueue |
| 19 | |
| 20 | # Parse form data |
| 21 | form = cgi.FieldStorage() |
| 22 | |
| 23 | # Queue email notification (don't block response) |
| 24 | email = form.getvalue('email') |
| 25 | name = form.getvalue('name', 'User') |
| 26 | |
| 27 | if email: |
| 28 | job_id = enqueue('send_email', { |
| 29 | 'to': email, |
| 30 | 'subject': 'Form submitted', |
| 31 | 'body': f'Thanks for your submission, {name}!' |
| 32 | }) |
| 33 | |
| 34 | # Return immediately |
| 35 | print("Content-Type: text/html\n") |
| 36 | print(f"""<!DOCTYPE html> |
| 37 | <html> |
| 38 | <head> |
| 39 | <title>Form Submitted</title> |
| 40 | </head> |
| 41 | <body> |
| 42 | <h1>Thanks, {name}!</h1> |
| 43 | <p>You'll receive a confirmation email at <strong>{email}</strong> shortly.</p> |
| 44 | <p><small>Job ID: {job_id}</small></p> |
| 45 | </body> |
| 46 | </html>""") |
| 47 | else: |
| 48 | print("Content-Type: text/html\n") |
| 49 | print("""<!DOCTYPE html> |
| 50 | <html> |
| 51 | <head> |
| 52 | <title>Error</title> |
| 53 | </head> |
| 54 | <body> |
| 55 | <h1>Error</h1> |
| 56 | <p>Please provide an email address.</p> |
| 57 | </body> |
| 58 | </html>""") |