-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Celery Queue for subscribe send email workflow
- Setups a celery queue backed by redis - Updates the subscribe feature in nextjs to do the following depending on error_boundary=true/false -- Send a enqueue request to flask -- Showcase error boundary - Adds enqueue endpoint
- Loading branch information
Showing
7 changed files
with
160 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from celery import Celery, signals | ||
import sentry_sdk, os, dotenv | ||
import os | ||
|
||
dotenv.load_dotenv() | ||
|
||
redis_host = os.environ.get("REDISHOST", "localhost") | ||
redis_port = int(os.environ.get("REDISPORT")) | ||
|
||
redis_url = f"redis://{redis_host}:{redis_port}/1" | ||
|
||
# app = Celery( | ||
# 'src.tasks', | ||
# backend='redis://localhost:6379/1', | ||
# broker='redis://localhost:6379/1',) | ||
|
||
|
||
|
||
|
||
app = Celery('subscribe', | ||
broker=redis_url, | ||
backend=redis_url, | ||
include=['src.queues.tasks']) | ||
|
||
# Optional configuration, see the application user guide. | ||
app.conf.update( | ||
result_expires=3600, | ||
) | ||
|
||
# Initialize Sentry SDK on Celery startup | ||
@signals.celeryd_init.connect | ||
def init_sentry(**_kwargs): | ||
dotenv.load_dotenv() | ||
RELEASE = os.environ["RELEASE"] | ||
DSN = os.environ["FLASK_APP_DSN"] | ||
ENVIRONMENT = os.environ["FLASK_ENV"] | ||
sentry_sdk.init( | ||
dsn=DSN, | ||
release=RELEASE, | ||
environment=ENVIRONMENT, | ||
traces_sample_rate=1.0, | ||
# Set profiles_sample_rate to 1.0 to profile 100% | ||
# of sampled transactions. | ||
# We recommend adjusting this value in production. | ||
profiles_sample_rate=1.0, | ||
) | ||
|
||
if __name__ == '__main__': | ||
app.start() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
import sentry_sdk | ||
from .celery import app | ||
import time, random | ||
|
||
|
||
@app.task | ||
def sendEmail(email): | ||
try: | ||
time.sleep(random.randrange(5)) # Simulate a delay | ||
if random.randrange(5) % 2 == 0: # Check if the random number is even | ||
raise Exception("sending email error") | ||
else: | ||
print("Sending email to: " + email) | ||
except Exception as e: | ||
# Log the exception to Sentry | ||
sentry_sdk.capture_exception(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
|
||
import { NextResponse } from "next/server"; | ||
import { getProductsOnly } from '@/lib/data.js'; | ||
import { | ||
determineBackendUrl, | ||
} from '@/src/utils/backendrouter'; | ||
|
||
export async function POST(request) { | ||
try { | ||
console.log("Here in POST"); | ||
|
||
// Parse the incoming request body to extract the email | ||
const { email } = await request.json(); | ||
|
||
if (!email) { | ||
return NextResponse.json({ error: 'Email is required' }, { status: 400 }); | ||
} | ||
|
||
// Determine the backend URL | ||
const backendUrl = determineBackendUrl('flask'); | ||
|
||
// Send the email to the backend | ||
const resp = await fetch(`${backendUrl}/enqueue`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify({ email }), // Include the extracted email | ||
}); | ||
|
||
if (!resp.ok) { | ||
const errorText = await resp.text(); | ||
console.error('Backend error:', errorText); | ||
return NextResponse.json({ error: 'Failed to enqueue email' }, { status: resp.status }); | ||
} | ||
|
||
const data = await resp.json(); | ||
|
||
return NextResponse.json({ response: data }, { status: 200 }); | ||
} catch (err) { | ||
console.error('Error handling POST request:', err); | ||
return NextResponse.json({ error: 'Internal server error' }, { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters