Compare commits

..
Author SHA1 Message Date
timothyandClaude Opus 4.8 7d71fb9f94 fix(410): log scan cancellation below ERROR (user-initiated, not a failure)
Build ErsatzTV Image / CI image pin matches docker/ci (pull_request) Successful in 6s
Build ErsatzTV Image / Docs update reminder (pull_request) Successful in 5s
Build ErsatzTV Image / decisions.md append-only (pull_request) Successful in 8s
Build ErsatzTV Image / Build & test (.NET) (pull_request) Successful in 6m56s
Build ErsatzTV Image / API docs in sync (OpenAPI + endpoint index) (pull_request) Successful in 7s
Build ErsatzTV Image / Formatting (changed .cs conform to .editorconfig) (pull_request) Successful in 39s
Build ErsatzTV Image / Functional E2E (curl contracts) (pull_request) Successful in 14m20s
Build ErsatzTV Image / EF migration integrity (SQLite + MySql) (pull_request) Successful in 19m2s
Build ErsatzTV Image / Build & push image (amd64) (pull_request) Has been skipped
Scan cancellation surfaces as ScanCanceled, an ordinary BaseError, so all
four scan handlers (Jellyfin/Emby/Plex/local) logged it at ERROR alongside
genuine failures. A user cancelling a scan (or a container restart mid-scan)
is not an error; this was training operators to ignore scanner ERROR lines,
corrosive precisely because #264 showed scanner failures can be silent.

Each handler's Left-result loop now branches on `error is ScanCanceled`:
logs Information ("Scan of {Name} was canceled") for cancellation, keeps
LogError for every other BaseError. No behavior change to LastScan
stamping (still correctly skipped on any Left, cancellation included).

fixes #410

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-18 16:42:13 +02:00
9 changed files with 150 additions and 104 deletions
@@ -1,5 +1,6 @@
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Jellyfin;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Core.Jellyfin;
@@ -95,5 +96,83 @@ public class SynchronizeJellyfinLibraryByIdHandlerTests
Arg.Any<CancellationToken>());
await libraryRepository.Received(1).UpdateLastScan(library);
}
[Test]
public async Task Should_Not_Log_Error_When_Scan_Is_Canceled()
{
var scannerProxy = Substitute.For<IScannerProxy>();
var mediaSourceRepository = Substitute.For<IMediaSourceRepository>();
var jellyfinSecretStore = Substitute.For<IJellyfinSecretStore>();
var jellyfinMovieLibraryScanner = Substitute.For<IJellyfinMovieLibraryScanner>();
var jellyfinTelevisionLibraryScanner = Substitute.For<IJellyfinTelevisionLibraryScanner>();
var jellyfinMusicVideoLibraryScanner = Substitute.For<IJellyfinMusicVideoLibraryScanner>();
var libraryRepository = Substitute.For<ILibraryRepository>();
var configElementRepository = Substitute.For<IConfigElementRepository>();
var logger = Substitute.For<ILogger<SynchronizeJellyfinLibraryByIdHandler>>();
var library = new JellyfinLibrary
{
Id = 42,
Name = "Concerts",
MediaKind = LibraryMediaKind.MusicVideos,
MediaSourceId = 7
};
var mediaSource = new JellyfinMediaSource
{
Id = 7,
Connections =
[
new JellyfinConnection
{
Address = "http://jellyfin.example",
JellyfinMediaSourceId = 7
}
]
};
mediaSourceRepository.GetJellyfinByLibraryId(library.Id).Returns(Some(mediaSource).AsTask());
mediaSourceRepository.GetJellyfinLibrary(library.Id).Returns(Some(library).AsTask());
jellyfinSecretStore.ReadSecrets().Returns(new JellyfinSecrets
{
Address = "http://jellyfin.example",
ApiKey = "abc"
});
configElementRepository.GetValue<int>(
Arg.Is<ConfigElementKey>(key => key.Key == ConfigElementKey.LibraryRefreshInterval.Key),
Arg.Any<CancellationToken>())
.Returns(Task.FromResult<Option<int>>(Some(0)));
jellyfinMusicVideoLibraryScanner.ScanLibrary(
Arg.Any<JellyfinConnectionParameters>(),
library,
true,
Arg.Any<CancellationToken>())
.Returns(Left<BaseError, Unit>(new ScanCanceled()).AsTask());
var handler = new SynchronizeJellyfinLibraryByIdHandler(
scannerProxy,
mediaSourceRepository,
jellyfinSecretStore,
jellyfinMovieLibraryScanner,
jellyfinTelevisionLibraryScanner,
jellyfinMusicVideoLibraryScanner,
libraryRepository,
configElementRepository,
logger);
await handler.Handle(
new SynchronizeJellyfinLibraryById("http://ersatztv.example", library.Id, true, true),
CancellationToken.None);
// a user-initiated cancellation is not a failure and must not be logged at ERROR (#410)
await libraryRepository.DidNotReceive().UpdateLastScan(Arg.Any<Library>());
logger.ReceivedCalls()
.Any(call => call.GetArguments().ElementAtOrDefault(2)?.ToString()
== "Error synchronizing jellyfin library: Scan was canceled")
.ShouldBeFalse();
logger.ReceivedCalls()
.Any(call => call.GetArguments().ElementAtOrDefault(2)?.ToString()
== "Scan of jellyfin library Concerts was canceled")
.ShouldBeTrue();
}
}
}
@@ -1,5 +1,6 @@
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Scanner.Application.MediaSources;
using ErsatzTV.Scanner.Core.Interfaces;
@@ -96,6 +97,27 @@ public class ScanLocalLibraryHandlerTests
ShouldHaveLogged("Error scanning local library path /movies: scan failed");
}
[Test]
public async Task Should_Not_Log_Error_When_A_Path_Scan_Is_Canceled()
{
ScanResult(Left<BaseError, Unit>(new ScanCanceled()));
await Handler().Handle(
new ScanLocalLibrary("http://ersatztv.example", _library.Id, true),
CancellationToken.None);
// a user-initiated cancellation is not a failure and must not be logged at ERROR (#410);
// it's still correctly excluded from the "last scan" stamp, same as any other failure
_library.LastScan.ShouldBeNull();
await _libraryRepository.DidNotReceive().UpdateLastScan(Arg.Any<Library>());
_logger.ReceivedCalls()
.Any(call => call.GetArguments().ElementAtOrDefault(2)?.ToString()
== "Error scanning local library path /movies: Scan was canceled")
.ShouldBeFalse();
ShouldHaveLogged("Scan of local library path /movies was canceled");
}
[Test]
public async Task Should_Not_Set_Library_LastScan_When_A_Later_Path_Fails()
{
@@ -1,6 +1,7 @@
using ErsatzTV.Core;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Emby;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Emby;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Scanner.Core.Interfaces;
@@ -86,7 +87,14 @@ public class SynchronizeEmbyLibraryByIdHandler : IRequestHandler<SynchronizeEmby
foreach (BaseError error in result.LeftToSeq())
{
_logger.LogError("Error synchronizing emby library: {Error}", error);
if (error is ScanCanceled)
{
_logger.LogInformation("Scan of emby library {Name} was canceled", parameters.Library.Name);
}
else
{
_logger.LogError("Error synchronizing emby library: {Error}", error);
}
}
return result.Map(_ => parameters.Library.Name);
@@ -1,5 +1,6 @@
using ErsatzTV.Core;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Jellyfin;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Core.Jellyfin;
@@ -95,7 +96,16 @@ public class
foreach (BaseError error in result.LeftToSeq())
{
_logger.LogError("Error synchronizing jellyfin library: {Error}", error);
if (error is ScanCanceled)
{
_logger.LogInformation(
"Scan of jellyfin library {Name} was canceled",
parameters.Library.Name);
}
else
{
_logger.LogError("Error synchronizing jellyfin library: {Error}", error);
}
}
return result.Map(_ => parameters.Library.Name);
@@ -1,6 +1,7 @@
using System.Diagnostics;
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Scanner.Core.Interfaces;
using ErsatzTV.Scanner.Core.Interfaces.Metadata;
@@ -152,13 +153,23 @@ public class ScanLocalLibraryHandler : IRequestHandler<ScanLocalLibrary, Either<
// a failed path now suppresses the library-level scan time below, so without this the
// user sees "Never scanned" with nothing explaining why. The remote scanners log the
// same way.
// same way. A cancellation is user-initiated, not a failure, so it's logged separately
// at a lower level; genuine errors still log at ERROR.
foreach (BaseError error in result.LeftToSeq())
{
_logger.LogError(
"Error scanning local library path {Path}: {Error}",
libraryPath.Path,
error);
if (error is ScanCanceled)
{
_logger.LogInformation(
"Scan of local library path {Path} was canceled",
libraryPath.Path);
}
else
{
_logger.LogError(
"Error scanning local library path {Path}: {Error}",
libraryPath.Path,
error);
}
}
}
}
@@ -1,5 +1,6 @@
using ErsatzTV.Core;
using ErsatzTV.Core.Domain;
using ErsatzTV.Core.Errors;
using ErsatzTV.Core.Interfaces.Plex;
using ErsatzTV.Core.Interfaces.Repositories;
using ErsatzTV.Core.Plex;
@@ -96,7 +97,14 @@ public class SynchronizePlexLibraryByIdHandler : IRequestHandler<SynchronizePlex
foreach (BaseError error in result.LeftToSeq())
{
_logger.LogError("Error synchronizing plex library: {Error}", error);
if (error is ScanCanceled)
{
_logger.LogInformation("Scan of plex library {Name} was canceled", parameters.Library.Name);
}
else
{
_logger.LogError("Error synchronizing plex library: {Error}", error);
}
}
return result.Map(_ => parameters.Library.Name);
+1 -1
View File
@@ -57,7 +57,7 @@ const MESSAGES: Record<PinFlowStatus, string> = {
success: 'Connected to Plex.',
'authorized-no-servers': 'Signed in to Plex, but no eligible servers were discovered.',
timeout: 'Plex sign-in timed out — try again.',
'budget-exhausted': 'Still working — this can take a while on a large first sync. Check again to see if it finished.'
'budget-exhausted': 'Still working — this can take a while on a large first sync. Use Refresh to check again.'
};
const TERMINAL: ReadonlySet<PinFlowStatus> = new Set<PinFlowStatus>([
-42
View File
@@ -266,46 +266,4 @@ describe('PlexSourceScreen', () => {
expect(screen.queryByText(/Your Plex account is authorized/i)).toBeNull();
expect(screen.queryByText(/no eligible servers were discovered/i)).toBeNull();
});
// #367: budget-exhausted told the user to "Use Refresh to check again", but the Servers card (the
// only place Refresh lives) doesn't render with zero servers, so the instruction pointed at nothing.
// The message now describes checking again generically, and a global "Check again" button re-checks
// GET /api/v1/media-sources/plex without resuming the (already spent) timed poll loop.
it('budget-exhausted with no servers offers a working "Check again" control instead of a dead Refresh reference', async () => {
const stateRef = { current: { isAuthorized: false, isLocked: false, servers: [] } as PlexState };
const counter = { state: 0 };
installFetch(stateRef, counter);
vi.spyOn(window, 'open').mockReturnValue({} as Window);
vi.useFakeTimers();
render(<PlexSourceScreen />);
await vi.waitFor(() => expect(screen.getByRole('button', { name: 'Sign in to Plex' })).toBeTruthy());
stateRef.current = { isAuthorized: true, isLocked: true, servers: [] };
fireEvent.click(screen.getByRole('button', { name: 'Sign in to Plex' }));
await vi.waitFor(() => expect(screen.getByText(/discovering your Plex servers|Waiting/i)).toBeTruthy());
await vi.advanceTimersByTimeAsync(152_000); // consume the whole 150s budget
await vi.waitFor(() => expect(screen.getByText(/Still working/i)).toBeTruthy());
// The message no longer references a "Refresh" control that isn't rendered (no Servers card yet).
expect(screen.queryByText(/Use Refresh/i)).toBeNull();
expect(screen.queryByRole('heading', { name: 'Servers' })).toBeNull();
// A real, working "Check again" control is offered instead.
const checkAgainButton = screen.getByRole('button', { name: 'Check again' });
expect(checkAgainButton).toBeTruthy();
const callsBeforeCheck = counter.state;
// The next sync completes and a server is discovered by the time the user checks again.
stateRef.current = { isAuthorized: true, isLocked: false, servers: [{ id: 3, name: 'Attic Server', address: 'http://plex:32400' }] };
fireEvent.click(checkAgainButton);
// Check again performs its own fetch (does not rely on the exhausted timed poll loop resuming).
await vi.waitFor(() => expect(counter.state).toBeGreaterThan(callsBeforeCheck));
await vi.waitFor(() => expect(screen.getByRole('heading', { name: 'Servers' })).toBeTruthy());
expect(screen.getByText('Attic Server')).toBeTruthy();
expect(screen.getByText('Connected to Plex.')).toBeTruthy();
});
});
+1 -51
View File
@@ -28,16 +28,13 @@ type BootState =
// — the poll runs against GET /api/v1/media-sources/plex every 2s and, crucially, keeps polling while
// authorized-but-still-locked ("finalizing / discovering servers"); the terminal success signal is
// the lock RELEASING (see pinFlowPoll.ts). Server rows offer Refresh / Edit Libraries / Edit Path
// Replacements; Refresh is disabled while the Plex lock is held. The budget-exhausted terminal (no
// servers may exist yet) instead offers a global "Check again" affordance (#367) so its message
// always points at a control that's actually rendered.
// Replacements. Refresh is disabled while the Plex lock is held.
export function PlexSourceScreen() {
const [boot, setBoot] = useState<BootState>({ status: 'loading' });
const [actionError, setActionError] = useState<string | null>(null);
const [confirmSignOut, setConfirmSignOut] = useState(false);
const [signingOut, setSigningOut] = useState(false);
const [refreshingId, setRefreshingId] = useState<number | null>(null);
const [checkingAgain, setCheckingAgain] = useState(false);
// Pin-flow UI state.
const [pinState, setPinState] = useState<PinFlowState | null>(null);
@@ -165,41 +162,6 @@ export function PlexSourceScreen() {
});
};
// The global "Check again" affordance for the budget-exhausted terminal (#367): that state has no
// per-server Refresh (the Servers card only renders once a server is discovered), so the message
// must point at something that actually exists. This does a single one-off re-check of
// GET /api/v1/media-sources/plex rather than resuming the timed poll loop — the 150s budget is
// spent; re-arming it silently would contradict "exhausted" and could spin forever on a sync that
// never finishes.
const checkAgain = () => {
if (checkingAgain) {
return;
}
setCheckingAgain(true);
setActionError(null);
getPlexState()
.then((state) => {
if (!activeRef.current) {
return;
}
setBoot({ status: 'ready', state });
const elapsed = Date.now() - startedAtRef.current;
const next = evaluatePinFlow(
{ isLocked: state.isLocked, isAuthorized: state.isAuthorized, hasServers: (state.servers ?? []).length > 0 },
{ budgetExhausted: isPinFlowBudgetExhausted(elapsed) }
);
setPinState(next);
setCheckingAgain(false);
})
.catch((error: unknown) => {
if (!activeRef.current) {
return;
}
setCheckingAgain(false);
setActionError(messageFromMediaSourcesError(error, 'Unable to check Plex status'));
});
};
const refresh = (serverId: number) => {
setRefreshingId(serverId);
setActionError(null);
@@ -322,18 +284,6 @@ export function PlexSourceScreen() {
{(popupBlocked || (polling && authUrl)) && authUrl && (
<a href={authUrl} target="_blank" rel="noreferrer noopener">Open the Plex sign-in page</a>
)}
{pinState?.status === 'budget-exhausted' && (
<Button
disabled={checkingAgain}
loading={checkingAgain}
onClick={checkAgain}
size="sm"
startIcon={<RefreshCw aria-hidden="true" size={14} />}
variant="secondary"
>
Check again
</Button>
)}
</div>
)}
{!polling && !pinState && !hasPlexAccount && (