Repository navigation
feat(aws-marketplace): bill a listing's LLM usage to AWS through a per-period metering tally #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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
34ce8d6
feat(aws-marketplace): bill a listing's LLM usage to AWS hourly throu…
waleedlatif1 7a7f562
fix(aws-marketplace): meter what changed in the books per billing per…
waleedlatif1 87f5256
fix(aws-marketplace): fix a record's quantity once it is first sent, …
waleedlatif1 60b4fb6
fix(aws-marketplace): count rows while a long run can still raise the…
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,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 } | ||
| ) | ||
|
waleedlatif1 marked this conversation as resolved.
Resolved
|
||
| }) | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.