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

0 downloads

dbbasic-queue/examples/cgi_script.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
cgi_script.py · 1.3 KB · 58 lines
1#!/usr/bin/env python3
2"""
3Example CGI script using dbbasic-queue
4
5This demonstrates how to use the queue in a traditional CGI environment.
6
7Place this in your cgi-bin directory and make it executable:
8 chmod +x cgi-bin/submit-form.cgi
9"""
10
11import cgi
12import sys
13import os
14
15# Add parent directory to path
16sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
17
18from dbbasic_queue import enqueue
19
20# Parse form data
21form = cgi.FieldStorage()
22
23# Queue email notification (don't block response)
24email = form.getvalue('email')
25name = form.getvalue('name', 'User')
26
27if 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>""")
47else:
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>""")