feat: show failed deliveries in the API, CLI and UI

This commit is contained in:
tgiachi 2026-11-09 20:34:37 +01:00
commit afd067688c
4 changed files with 109 additions and 14 deletions

View file

@ -63,11 +63,14 @@ export interface QueueItem {
last_error: string
next_attempt: string
created_at: string
/** Given up for good: never retried, kept for a week so the admin can see it. */
failed: boolean
}
export interface Queue {
pending: number
retrying: number
failed: number
oldest: string | null
items: QueueItem[]
}

View file

@ -11,6 +11,7 @@ const item = (id: number, over: Partial<QueueItem> = {}): QueueItem => ({
last_error: '',
next_attempt: '2030-01-02T03:04:05Z',
created_at: '2030-01-01T00:00:00Z',
failed: false,
...over,
})
@ -51,7 +52,7 @@ describe('queue page', () => {
expect(within(fresh).getByText('—')).toBeInTheDocument()
expect(screen.getByText('Pending').nextElementSibling).toHaveTextContent('2')
expect(screen.getByText('Retrying').nextElementSibling).toHaveTextContent('1')
expect(screen.getByText('Retrying', { selector: 'div' }).nextElementSibling).toHaveTextContent('1')
expect(screen.getByText('Oldest').nextElementSibling).toHaveTextContent('2030-01-01 00:00 UTC')
})
@ -68,6 +69,26 @@ describe('queue page', () => {
expect(screen.queryByText(/showing/i)).not.toBeInTheDocument()
})
it('shows failed deliveries apart from pending ones, with the reason', async () => {
await setup([
item(1),
item(2, { attempts: 2, last_error: 'connection refused' }),
item(3, { attempts: 1, failed: true, last_error: 'refused by the relay: 554 policy rejection' }),
])
const failed = (await screen.findByText('user3@example.com')).closest('tr')!
expect(within(failed).getByText('Failed')).toBeInTheDocument()
expect(within(failed).getByText(/refused by the relay: 554 policy rejection/)).toBeInTheDocument()
expect(within(failed).getByText('—', { selector: 'td' })).toBeInTheDocument() // never retried: no next attempt
expect(within(screen.getByText('user2@example.com').closest('tr')!).getByText('Retrying')).toBeInTheDocument()
expect(within(screen.getByText('user1@example.com').closest('tr')!).getByText('Waiting')).toBeInTheDocument()
expect(screen.getByText('Pending').nextElementSibling).toHaveTextContent('2')
expect(screen.getByText('Retrying', { selector: 'div' }).nextElementSibling).toHaveTextContent('1')
expect(screen.getByText('Failed', { selector: 'div' }).nextElementSibling).toHaveTextContent('1')
const rows = screen.getAllByRole('row').slice(1).map((r) => r.textContent ?? '')
expect(rows[0]).toContain('user3@example.com') // failed first
})
it('refreshes on demand', async () => {
const { user, state } = await setup([item(1)])
await screen.findByText('user1@example.com')

View file

@ -10,6 +10,7 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { Card, CardContent } from '@/components/ui/card'
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table'
@ -38,9 +39,10 @@ export function QueuePage() {
{q && (
<>
<div className="grid gap-4 sm:grid-cols-3">
<div className="grid gap-4 sm:grid-cols-4">
<Stat label="Pending" value={String(q.pending)} />
<Stat label="Retrying" value={String(q.retrying)} />
<Stat label="Failed" value={String(q.failed)} />
<Stat label="Oldest" value={q.oldest ? formatDate(q.oldest) : '—'} />
</div>
{q.items.length === 0 ? (
@ -50,6 +52,7 @@ export function QueuePage() {
<TableHeader>
<TableRow>
<TableHead>Recipient</TableHead>
<TableHead>State</TableHead>
<TableHead>Attempts</TableHead>
<TableHead>Last error</TableHead>
<TableHead>Next attempt</TableHead>
@ -60,9 +63,18 @@ export function QueuePage() {
{q.items.map((i) => (
<TableRow key={i.id}>
<TableCell className="font-medium">{i.recipient}</TableCell>
<TableCell>
{i.failed ? (
<Badge variant="destructive">Failed</Badge>
) : i.attempts > 0 ? (
<Badge variant="secondary">Retrying</Badge>
) : (
<Badge variant="outline">Waiting</Badge>
)}
</TableCell>
<TableCell>{i.attempts}</TableCell>
<TableCell>{i.last_error || '—'}</TableCell>
<TableCell>{formatDate(i.next_attempt)}</TableCell>
<TableCell className="max-w-xs whitespace-normal">{i.last_error || '—'}</TableCell>
<TableCell>{i.failed ? '—' : formatDate(i.next_attempt)}</TableCell>
<TableCell className="text-right">
<Button size="sm" variant="outline" onClick={() => setDiscarding(i)}>
Discard

View file

@ -71,10 +71,12 @@ export function installFakeApi(state: FakeState) {
let m: RegExpMatchArray | null
if (path === '/queue' && method === 'GET') {
const items = [...state.queue].sort((a, b) => b.attempts - a.attempts).slice(0, state.queueLimit)
const retrying = state.queue.filter((i) => i.attempts > 0).length
const oldest = state.queue.map((i) => i.created_at).sort()[0] ?? null
return json(200, { pending: state.queue.length, retrying, oldest, items })
const items = [...state.queue].sort((a, b) => Number(b.failed) - Number(a.failed) || b.attempts - a.attempts).slice(0, state.queueLimit)
const waiting = state.queue.filter((i) => !i.failed)
const retrying = waiting.filter((i) => i.attempts > 0).length
const failed = state.queue.length - waiting.length
const oldest = waiting.map((i) => i.created_at).sort()[0] ?? null
return json(200, { pending: waiting.length, retrying, failed, oldest, items })
}
if ((m = path.match(/^\/queue\/(\d+)$/)) && method === 'DELETE') {
const id = Number(m[1])