feat: retry all failed messages from emails page
Add "Repetir todas as falhadas" with batched API calls and progress feedback; raise retry-batch limit to 100 and support all=true on API.
This commit is contained in:
1 parent
bd2d04aba7
commit
ea88e8833d
3 files changed
+103
-30
No files matched your search
@@ -150,9 +150,10 @@ export const api = {
|
||||
return request<MessageList>(`/messages${q.toString() ? '?' + q : ''}`)
|
||||
},
|
||||
retry: (id: number) => request<Message>(`/messages/${id}/retry`, { method: 'POST' }),
|
||||
retryBatch: (opts?: { limit?: number; account_id?: number }) => {
|
||||
retryBatch: (opts?: { limit?: number; account_id?: number; all?: boolean }) => {
|
||||
const q = new URLSearchParams()
|
||||
if (opts?.limit != null) q.set('limit', String(opts.limit))
|
||||
if (opts?.all) q.set('all', 'true')
|
||||
else if (opts?.limit != null) q.set('limit', String(opts.limit))
|
||||
if (opts?.account_id != null) q.set('account_id', String(opts.account_id))
|
||||
return request<RetryBatchResult>(`/messages/retry-batch${q.toString() ? '?' + q : ''}`, {
|
||||
method: 'POST',
|
||||
|
||||
@@ -74,6 +74,57 @@ export default function Emails() {
|
||||
}
|
||||
}
|
||||
|
||||
const handleRetryAll = async () => {
|
||||
let failedCount = filterStatus === 'failed' ? total : 0
|
||||
if (failedCount === 0) {
|
||||
try {
|
||||
const stats = await api.stats.get()
|
||||
failedCount = stats.messages_failed
|
||||
} catch {
|
||||
failedCount = 0
|
||||
}
|
||||
}
|
||||
if (failedCount === 0) {
|
||||
setActionMsg({ ok: true, text: 'Não há mensagens falhadas para repetir.' })
|
||||
return
|
||||
}
|
||||
const ok = confirm(
|
||||
`Repetir todas as ${failedCount} mensagens falhadas?\n\nIsto pode demorar vários minutos. Mantém a conta inactiva durante o processo.`,
|
||||
)
|
||||
if (!ok) return
|
||||
|
||||
setBatchRetrying(true)
|
||||
setActionMsg({ ok: false, text: `A repetir ${failedCount} mensagens…` })
|
||||
let totalSucceeded = 0
|
||||
let totalFailed = 0
|
||||
let totalAttempted = 0
|
||||
try {
|
||||
while (true) {
|
||||
const r = await api.messages.retryBatch({ limit: 50 })
|
||||
totalSucceeded += r.succeeded
|
||||
totalFailed += r.failed
|
||||
totalAttempted += r.attempted
|
||||
setActionMsg({
|
||||
ok: false,
|
||||
text: `A repetir… ${totalSucceeded} OK, ${totalFailed} falhas (${totalAttempted} processadas)`,
|
||||
})
|
||||
if (r.attempted === 0) break
|
||||
}
|
||||
setActionMsg({
|
||||
ok: totalFailed === 0,
|
||||
text: `Concluído: ${totalSucceeded} reencaminhadas, ${totalFailed} falharam (${totalAttempted} processadas).`,
|
||||
})
|
||||
reload()
|
||||
} catch (e) {
|
||||
setActionMsg({
|
||||
ok: false,
|
||||
text: e instanceof Error ? e.message : 'Retry de todas falhou',
|
||||
})
|
||||
} finally {
|
||||
setBatchRetrying(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
@@ -83,14 +134,24 @@ export default function Emails() {
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
{(filterStatus === 'failed' || filterStatus === '') && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleRetryBatch(5)}
|
||||
disabled={batchRetrying || loading}
|
||||
className="text-sm px-3 py-1.5 border border-amber-300 text-amber-800 bg-amber-50 rounded-md hover:bg-amber-100 disabled:opacity-40"
|
||||
>
|
||||
{batchRetrying ? 'A repetir…' : 'Repetir 5 falhadas'}
|
||||
</button>
|
||||
<>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleRetryBatch(5)}
|
||||
disabled={batchRetrying || loading}
|
||||
className="text-sm px-3 py-1.5 border border-amber-300 text-amber-800 bg-amber-50 rounded-md hover:bg-amber-100 disabled:opacity-40"
|
||||
>
|
||||
{batchRetrying ? 'A repetir…' : 'Repetir 5 falhadas'}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleRetryAll}
|
||||
disabled={batchRetrying || loading}
|
||||
className="text-sm px-3 py-1.5 border border-indigo-300 text-indigo-800 bg-indigo-50 rounded-md hover:bg-indigo-100 disabled:opacity-40"
|
||||
>
|
||||
Repetir todas as falhadas
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
<select
|
||||
value={filterStatus}
|
||||
|
||||
@@ -86,34 +86,45 @@ def list_messages(
|
||||
def retry_batch(
|
||||
db: DbSession,
|
||||
_user: CurrentUser,
|
||||
limit: int = Query(default=5, ge=1, le=50),
|
||||
limit: int = Query(default=5, ge=1, le=100),
|
||||
account_id: int | None = Query(default=None),
|
||||
all_failed: bool = Query(default=False, alias="all"),
|
||||
) -> dict:
|
||||
"""Retry up to N failed messages (for testing after a forward fix)."""
|
||||
query = db.query(Message).filter(Message.status == MessageStatus.failed)
|
||||
if account_id is not None:
|
||||
query = query.filter(Message.account_id == account_id)
|
||||
messages = query.order_by(Message.id.desc()).limit(limit).all()
|
||||
|
||||
"""Retry failed messages. Use all=true to process every failed message in batches."""
|
||||
batch_size = 100 if all_failed else limit
|
||||
succeeded = 0
|
||||
failed = 0
|
||||
attempted = 0
|
||||
errors: list[str] = []
|
||||
|
||||
for message in messages:
|
||||
account = db.get(Account, message.account_id)
|
||||
if account is None:
|
||||
failed += 1
|
||||
errors.append(f"#{message.id}: account missing")
|
||||
continue
|
||||
try:
|
||||
retry_single_message(db, message.id)
|
||||
succeeded += 1
|
||||
except Exception as exc:
|
||||
failed += 1
|
||||
errors.append(f"#{message.id}: {exc}")
|
||||
while True:
|
||||
query = db.query(Message).filter(Message.status == MessageStatus.failed)
|
||||
if account_id is not None:
|
||||
query = query.filter(Message.account_id == account_id)
|
||||
messages = query.order_by(Message.id.asc()).limit(batch_size).all()
|
||||
if not messages:
|
||||
break
|
||||
|
||||
for message in messages:
|
||||
account = db.get(Account, message.account_id)
|
||||
if account is None:
|
||||
failed += 1
|
||||
attempted += 1
|
||||
errors.append(f"#{message.id}: account missing")
|
||||
continue
|
||||
try:
|
||||
retry_single_message(db, message.id)
|
||||
succeeded += 1
|
||||
except Exception as exc:
|
||||
failed += 1
|
||||
errors.append(f"#{message.id}: {exc}")
|
||||
attempted += 1
|
||||
|
||||
if not all_failed:
|
||||
break
|
||||
|
||||
return {
|
||||
"attempted": len(messages),
|
||||
"attempted": attempted,
|
||||
"succeeded": succeeded,
|
||||
"failed": failed,
|
||||
"errors": errors[:10],
|
||||
|
||||
Reference in new issue
Block a user