Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
fix(publishing): count stuck and retried posts in the overdue alert
spacesdrivedaeb8f8c786e
diff --git a/frontend/src/features/publishing/OverdueAlert.tsx b/frontend/src/features/publishing/OverdueAlert.tsxindex 3bcc916..ded3de7 100644--- a/frontend/src/features/publishing/OverdueAlert.tsx+++ b/frontend/src/features/publishing/OverdueAlert.tsx@@ -13,7 +13,7 @@ export default function OverdueAlert({ count }: OverdueAlertProps) {<Alert variant="warning"><AlertCircle /><AlertDescription>- {overdueLead(count)} more than 30 minutes past their time and still pending, so the scheduler on this server is not running. Email the administrator at{' '}+ {overdueLead(count)} more than 30 minutes late and not yet published, so the scheduler on this server is not running. Email the administrator at{' '}<a href={adminMailto('Scheduled posts are not publishing on Twiligent')} className="font-semibold underline underline-offset-4">{ADMIN_EMAIL}</a>.If you run this instance, follow the checks in <Link to="/settings#scheduling" className="font-semibold underline underline-offset-4">Settings, Scheduled publishing</Link>.</AlertDescription>diff --git a/frontend/src/features/publishing/ScheduledPostRow.tsx b/frontend/src/features/publishing/ScheduledPostRow.tsxindex 0739dcb..df83033 100644--- a/frontend/src/features/publishing/ScheduledPostRow.tsx+++ b/frontend/src/features/publishing/ScheduledPostRow.tsx@@ -7,7 +7,7 @@ import { cn } from '@/lib/utils';import ScheduledPostEditRow from './ScheduledPostEditRow';import StatusBadge from './StatusBadge';import { POST_STATUS, contentTypeOf } from './publishConstants';-import { formatScheduleDate, isPastDue } from './scheduleHelpers.ts';+import { formatScheduleDate, isPastDue, isRetrying } from './scheduleHelpers.ts';import type { ScheduledPost } from '@shared/types/domain.ts';import type { AppAccount } from '../../utils/formatters';import type { ScheduledPostEditor } from './useScheduledPostEdit';@@ -88,7 +88,10 @@ export default function ScheduledPostRow({ post, position, account, editor, onDe)}</span></TableCell>- <TableCell><StatusBadge status={post.status} map={POST_STATUS} /></TableCell>+ <TableCell>+ <StatusBadge status={post.status} map={POST_STATUS} />+ {isRetrying(post) && <p className="mt-1 text-caption text-ink-faint">Trying again on the next run</p>}+ </TableCell><TableCell>{post.error ? (<Tooltip>diff --git a/frontend/src/features/publishing/scheduleHelpers.ts b/frontend/src/features/publishing/scheduleHelpers.tsindex 4db2221..1990e83 100644--- a/frontend/src/features/publishing/scheduleHelpers.ts+++ b/frontend/src/features/publishing/scheduleHelpers.ts@@ -8,7 +8,7 @@ export interface StatusChip {count: number;}-export type ScheduleTiming = Pick<ScheduledPost, 'status' | 'scheduledAt'>;+export type ScheduleTiming = Pick<ScheduledPost, 'status' | 'scheduledAt' | 'claimedAt' | 'attempts'>;export interface ScheduleFieldValues {scheduleMode: boolean;@@ -18,6 +18,9 @@ export interface ScheduleFieldValues {export const OVERDUE_MS = 30 * 60 * 1000;+// the schedulers' claim lease (backend/lib/scheduledPublishing.ts): a claim this old is taken over by the next run+export const PUBLISH_LEASE_MS = 20 * 60 * 1000;+function pad(value: number): string {return String(value).padStart(2, '0');}@@ -39,8 +42,22 @@ export function filterScheduledPosts<T extends ScheduleTiming>(posts: T[], statu.sort((first, second) => new Date(second.scheduledAt).getTime() - new Date(first.scheduledAt).getTime());}+export function isRetrying(post: ScheduleTiming): boolean {+ return post.status === 'pending' && (post.attempts ?? 0) > 0;+}++// a retried post waits from its last claim, and a claim no run took over in time means no scheduler is runningexport function overduePostsOf<T extends ScheduleTiming>(posts: T[], now: number = Date.now()): T[] {- return posts.filter(post => post.status === 'pending' && now - new Date(post.scheduledAt).getTime() > OVERDUE_MS);+ return posts.filter(post => {+ if (post.status === 'pending') {+ const waitingSince = isRetrying(post) && post.claimedAt ? post.claimedAt : post.scheduledAt;+ return now - new Date(waitingSince).getTime() > OVERDUE_MS;+ }+ if (post.status === 'publishing' && post.claimedAt) {+ return now - new Date(post.claimedAt).getTime() > PUBLISH_LEASE_MS + OVERDUE_MS;+ }+ return false;+ });}export function isPastDue(post: ScheduleTiming, now: Date = new Date()): boolean {diff --git a/frontend/src/features/settings/SchedulingSection.tsx b/frontend/src/features/settings/SchedulingSection.tsxindex 56db497..b3e816c 100644--- a/frontend/src/features/settings/SchedulingSection.tsx+++ b/frontend/src/features/settings/SchedulingSection.tsx@@ -23,7 +23,7 @@ export interface SchedulingSectionProps {const HOW_IT_WORKS: HowItWorksStep[] = [{ title: 'Schedule a post', body: 'In Publish, choose an account, the content type and the file, write the caption, pick Schedule, then a date and time, and choose Schedule post. The file uploads straight away.' },- { title: 'The server publishes it', body: 'Within 15 minutes after the time you picked, the server publishes the post. Your browser can be closed and your computer can be off.' },+ { title: 'The server publishes it', body: 'Within 15 minutes after the time you picked, the server publishes the post. If Instagram or the network has a passing problem, it tries again on its next two runs. Your browser can be closed and your computer can be off.' },{ title: 'Keep the account connected', body: 'The post uses the Instagram account you picked. If that account is removed or its access expires before the time, the post fails and shows the reason under Publish, Scheduled posts. Refresh the account in Accounts if Instagram asks you to reconnect.' },];@@ -47,7 +47,7 @@ function Steps({ steps }: StepsProps) {export default function SchedulingSection({ github }: SchedulingSectionProps) {const pendingStep: HowItWorksStep = {title: 'If a post stays Pending',- body: <>A post still Pending 30 minutes after its time means the scheduler on this server is not running. Email the administrator at <a href={adminMailto('Scheduled posts are not publishing on Twiligent')}>{ADMIN_EMAIL}</a> with the time of the post. You can edit or delete a Pending post at any time.</>,+ body: <>A post still Pending 30 minutes after its time, or still Publishing 50 minutes after it started, means the scheduler on this server is not running. Email the administrator at <a href={adminMailto('Scheduled posts are not publishing on Twiligent')}>{ADMIN_EMAIL}</a> with the time of the post. You can edit or delete a Pending post at any time.</>,};return (diff --git a/frontend/src/features/settings/SchedulingSetupGuide.tsx b/frontend/src/features/settings/SchedulingSetupGuide.tsxindex b10f48b..10ebdab 100644--- a/frontend/src/features/settings/SchedulingSetupGuide.tsx+++ b/frontend/src/features/settings/SchedulingSetupGuide.tsx@@ -16,10 +16,11 @@ function SetupNotes() {return (<div className="flex flex-col gap-2 rounded-field border border-line p-4"><p className="text-sm font-semibold text-ink">That is it</p>- <p className="text-sm text-ink-muted">GitHub now checks every 15 minutes. You do not need to keep anything open. Two things to know:</p>+ <p className="text-sm text-ink-muted">GitHub now asks for a run every 15 minutes, and you do not need to keep anything open. Three things to know:</p><ul className="flex list-disc flex-col gap-1 pl-5 text-sm text-ink-muted marker:text-ink-faint">+ <li>GitHub starts scheduled runs when it has room, often hours late and sometimes not at all, so this backup catches posts the server missed later, not within 15 minutes.</li><li>If nothing changes in your copy for 60 days, GitHub pauses the schedule and emails you. Repeat step 4 to switch it back on.</li>- <li>If you made your copy private, GitHub's free minutes run out. Keep it public, or change the schedule in <code className="font-mono text-small text-ink">.github/workflows/publish-scheduled.yml</code> to every 30 minutes.</li>+ <li>If you made your copy private, GitHub's free minutes run out. Keep it public, or change the schedule in <code className="font-mono text-small text-ink">.github/workflows/publish-scheduled.yml</code> to <code className="font-mono text-small text-ink">'7,37 * * * *'</code>, every 30 minutes.</li></ul></div>);@@ -52,7 +53,7 @@ export default function SchedulingSetupGuide({ owner, repo, onOwnerChange, onRep><div className="flex flex-col gap-5"><p className="text-sm text-ink-muted">- A second copy of the publisher that runs on GitHub every 15 minutes, for free, so posts still go out if the server is ever paused. Step 2 needs the secret key of the Supabase project behind this Twiligent, which only its administrator has. If you only sign in here, skip this: the server already publishes your posts.+ A second copy of the publisher that runs on GitHub for free, so posts still go out if the server is ever paused, though later: GitHub often starts these runs hours after their time. Step 2 needs the secret key of the Supabase project behind this Twiligent, which only its administrator has. If you only sign in here, skip this: the server already publishes your posts.</p><div className="flex flex-wrap items-center gap-2"><Button variant="outline" size="sm" render={<a href={docsUrl('/self-host/github')} target="_blank" rel="noopener noreferrer" />}>diff --git a/frontend/tests/features/publishing/scheduleHelpers.test.ts b/frontend/tests/features/publishing/scheduleHelpers.test.tsindex 723ca54..0b16361 100644--- a/frontend/tests/features/publishing/scheduleHelpers.test.ts+++ b/frontend/tests/features/publishing/scheduleHelpers.test.ts@@ -1,7 +1,7 @@import { test } from 'node:test';import assert from 'node:assert/strict';import {- OVERDUE_MS, scheduledStatusChips, filterScheduledPosts, overduePostsOf, isPastDue, overdueLead, deleteAllMessage,+ OVERDUE_MS, PUBLISH_LEASE_MS, isRetrying, scheduledStatusChips, filterScheduledPosts, overduePostsOf, isPastDue, overdueLead, deleteAllMessage,formatScheduleDate, toLocalDateInput, toLocalTimeInput, utcDateInput, defaultBulkStartDate, goesLiveLabel,} from '../../../src/features/publishing/scheduleHelpers.ts';import type { ScheduleTiming } from '../../../src/features/publishing/scheduleHelpers.ts';@@ -32,6 +32,17 @@ test('a post is overdue only when more than 30 minutes late', () => {assert.deepEqual(overduePostsOf(posts, due + OVERDUE_MS + 1).map(post => post.id), [1]);});+test('a retried post is late from its last claim, and a stuck claim only once the lease and the grace have passed', () => {+ const claimed = new Date('2030-05-01T09:00:00Z').getTime();+ const retried: ScheduleTiming = { status: 'pending', scheduledAt: '2030-05-01T08:00:00Z', claimedAt: '2030-05-01T09:00:00Z', attempts: 1 };+ const stuck: ScheduleTiming = { status: 'publishing', scheduledAt: '2030-05-01T08:00:00Z', claimedAt: '2030-05-01T09:00:00Z', attempts: 1 };+ assert.equal(isRetrying(retried), true);+ assert.equal(overduePostsOf([retried], claimed + OVERDUE_MS).length, 0);+ assert.equal(overduePostsOf([retried], claimed + OVERDUE_MS + 1).length, 1);+ assert.equal(overduePostsOf([stuck], claimed + PUBLISH_LEASE_MS + OVERDUE_MS).length, 0);+ assert.equal(overduePostsOf([stuck], claimed + PUBLISH_LEASE_MS + OVERDUE_MS + 1).length, 1);+});+test('only pending posts can be past due', () => {const now = new Date('2030-06-01T00:00:00Z');assert.equal(isPastDue(posts[0], now), true);