Repository navigation
feat(aws-marketplace): bill a listing's LLM usage to AWS hourly through a metering ledger #9921
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staging
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { generateShortId } from '@sim/utils/id' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { verifyCronAuth } from '@/lib/auth/internal' | ||
| import { isAwsMarketplaceEnabled } from '@/lib/billing/aws-marketplace/config' | ||
| import { meterAwsMarketplaceUsage } from '@/lib/billing/aws-marketplace/metering' | ||
| import { acquireLock, releaseLock } from '@/lib/core/config/redis' | ||
| import { runDetached } from '@/lib/core/utils/background' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
|
|
||
| const logger = createLogger('AwsMarketplaceMeterCron') | ||
|
|
||
| const LOCK_KEY = 'aws-marketplace-meter-lock' | ||
| /** Outlasts a run comfortably; the schedule runs every 15 minutes. */ | ||
| const LOCK_TTL_SECONDS = 10 * 60 | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| /** | ||
| * Records newly closed hours of usage owed through AWS Marketplace and sends AWS what it has not | ||
| * accepted yet. Acknowledges the cron call at once and works in the background; a Redis lock keeps | ||
| * runs from overlapping, and the ledger makes a replayed run harmless. A no-op on deployments | ||
| * where no configured listing bills usage. | ||
| */ | ||
| export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| const authError = verifyCronAuth(request, 'AWS Marketplace metering') | ||
| if (authError) { | ||
| return authError | ||
| } | ||
|
|
||
| if (!isAwsMarketplaceEnabled()) { | ||
| return NextResponse.json({ | ||
| success: true, | ||
| message: 'AWS Marketplace is not configured – skipped', | ||
| status: 'skip', | ||
| }) | ||
| } | ||
|
|
||
| const lockValue = generateShortId() | ||
| const locked = await acquireLock(LOCK_KEY, lockValue, LOCK_TTL_SECONDS, { | ||
| reclaimOnFailure: true, | ||
| }) | ||
| if (!locked) { | ||
| return NextResponse.json( | ||
| { | ||
| success: true, | ||
| message: 'AWS Marketplace metering already in progress – skipped', | ||
| status: 'skip', | ||
| }, | ||
| { status: 202 } | ||
| ) | ||
| } | ||
|
|
||
| runDetached('aws-marketplace-meter', async () => { | ||
| try { | ||
| const summary = await meterAwsMarketplaceUsage() | ||
| if (summary) logger.info('AWS Marketplace metering completed', { ...summary }) | ||
| } finally { | ||
| await releaseLock(LOCK_KEY, lockValue).catch(() => {}) | ||
| } | ||
| }) | ||
|
|
||
| return NextResponse.json( | ||
| { success: true, message: 'AWS Marketplace metering started', status: 'started' }, | ||
| { status: 202 } | ||
| ) | ||
|
Comment on lines
+63
to
+66
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The new route defines its JSON responses locally. The repository requires every API route's request and response shapes to live under Context Used: CLAUDE.md (source) Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| }) | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: This handler is not scheduled by the Helm chart, so Marketplace usage metering never runs in the standard deployment. Add a
cronjobs.jobsentry for/api/cron/aws-marketplace-meterwith the intended 15-minute schedule.Prompt for AI agents