Sessione salvata: terza revisione di S4b (#36)
La fine della guida da sola, dopo la copia, guarda di nuovo nello stesso passo in cui rilascia il permesso: se la vista remota, la finestra o un nuovo «Accedi» / «Usa il browser» sono tornati durante la copia, il permesso resta. Prima finiva comunque, e alla chiusura successiva il motore riprendeva subito, senza attesa e senza copia. Un «Ho finito» che prova davvero a copiare, o che chiude lui il permesso, e la chiusura della finestra che rinuncia dimenticano la copia di un abbandono precedente: un secondo «Ho finito» non dice più «era già stata copiata» dopo una copia appena rifiutata. Il registro non incolpa il browser quando il tempo della finestra finisce in attesa della pagina tenuta dalla ricerca dei profili; la riga di avvio del motore segue IsReadyToCollect e dice quando aspetta la copia; il riquadro «Se una copia nuova è incompleta» non mostra né accetta una scelta finché il server non dice quella salvata, non si fa cambiare da una risposta tardiva e torna alla scelta precedente se il salvataggio fallisce; singolare italiano nei messaggi («manca 1 valore che serve»). La guida e PROTOCOLLO.md dicono che in Docker copia solo «Ho finito»: lasciare la pagina vale «Annulla». Nuova riga in decisioni.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
74e67adbab
commit
b29b35a284
13 files changed
+343
-50
No files matched your search
@@ -214,11 +214,18 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
public async Task CopyOnWindowClosedAsync(BrowserWindowLetGo how, CancellationToken ct)
|
||||
{
|
||||
var what = how == BrowserWindowLetGo.MinimizedTooLong ? "Finestra del browser ridotta a icona troppo a lungo" : "Finestra del browser chiusa";
|
||||
if (_copier is null || !_browser.IsAvailable)
|
||||
if (_copier is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_browser.IsAvailable)
|
||||
{
|
||||
// A letting go that copies nothing: a late «Ho finito» must not report a copy of an earlier one.
|
||||
ForgetLetGoCopy();
|
||||
return;
|
||||
}
|
||||
|
||||
using var limit = CancellationTokenSource.CreateLinkedTokenSource(ct);
|
||||
limit.CancelAfter(LetGoWait);
|
||||
try
|
||||
@@ -227,6 +234,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
}
|
||||
catch (OperationCanceledException) when (limit.IsCancellationRequested)
|
||||
{
|
||||
ForgetLetGoCopy();
|
||||
_log.Info("collegamento", $"{what}: sessione non copiata, un altro comando del collegamento era ancora in corso.");
|
||||
return;
|
||||
}
|
||||
@@ -396,7 +404,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
{
|
||||
if (!_browser.IsAvailable)
|
||||
{
|
||||
ReleaseLease();
|
||||
EndUseWithHoFinito();
|
||||
return new FinishResult(false, "Il browser di Febe non è in funzione in questo momento, quindi Febe non può controllare il collegamento. Riprova fra qualche minuto; se il problema resta, guarda il registro.");
|
||||
}
|
||||
|
||||
@@ -409,7 +417,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
var copy = connected && _copier is not null ? await CopySessionAsync(ct).ConfigureAwait(false) : default;
|
||||
await RestIfAlbertosAsync().ConfigureAwait(false);
|
||||
await HideQuietlyAsync().ConfigureAwait(false);
|
||||
ReleaseLease();
|
||||
EndUseWithHoFinito();
|
||||
|
||||
var stopped = _engine?.Services.Breaker.State.Status is BreakerStatus.Challenge or BreakerStatus.Disconnected;
|
||||
string message;
|
||||
@@ -740,7 +748,10 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ReleaseLease(lease))
|
||||
// The copy may have lasted long (the page's turn, the browser, the database): the remote view, the window
|
||||
// or «Accedi» / «Usa il browser» may have come back meanwhile. Looked at again in the same step that ends
|
||||
// the lease, so that nothing slips in between.
|
||||
if (ReleaseIdleLease(lease, lastSign))
|
||||
{
|
||||
_log.Info("collegamento", $"Nessuna vista del browser aperta da {ItalianText.Duration(grace)} senza «Ho finito» né «Annulla»: il motore può riprendere.");
|
||||
return;
|
||||
@@ -843,10 +854,7 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
|
||||
// The last time this use of the browser let go decides what a late «Ho finito» says: a copy that does not
|
||||
// happen now (no session, the page busy, refused, an error) forgets one made at an earlier letting go.
|
||||
lock (_gate)
|
||||
{
|
||||
_letGoCopy = null;
|
||||
}
|
||||
ForgetLetGoCopy();
|
||||
|
||||
if (await CheckAsync(force: true, mayReport: false, ct).ConfigureAwait(false) != true)
|
||||
{
|
||||
@@ -857,10 +865,23 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
var hold = _browser.UserDriving.Begin();
|
||||
try
|
||||
{
|
||||
using var turn = await TakePageAsync(ct).ConfigureAwait(false);
|
||||
const string PageBusy = "sessione non copiata, la pagina era occupata dalla ricerca dei profili";
|
||||
PageTurn? taken;
|
||||
try
|
||||
{
|
||||
taken = await TakePageAsync(ct).ConfigureAwait(false);
|
||||
}
|
||||
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
||||
{
|
||||
// The time of the letting go ran out while a step of the search held the page: not the browser's fault.
|
||||
_log.Info("collegamento", $"{what}: {PageBusy}.");
|
||||
return;
|
||||
}
|
||||
|
||||
using var turn = taken;
|
||||
if (_engine is not null && turn is null)
|
||||
{
|
||||
_log.Info("collegamento", $"{what}: sessione non copiata, la pagina era occupata dalla ricerca dei profili.");
|
||||
_log.Info("collegamento", $"{what}: {PageBusy}.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -899,12 +920,12 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(copy);
|
||||
var missing = copy.MissingValues + copy.MissingDocIds;
|
||||
var clause = missing == 0 ? string.Empty : string.Create(CultureInfo.InvariantCulture, $", ma mancano {missing} valori che servono alle richieste");
|
||||
var clause = missing == 0 ? string.Empty : ", ma " + MissingNeeded(missing, past: false);
|
||||
return copy.Status switch
|
||||
{
|
||||
SessionCopyStatus.Saved => "sessione copiata" + clause,
|
||||
SessionCopyStatus.SavedInMemoryOnly => "sessione copiata, non ancora scritta su disco" + clause,
|
||||
SessionCopyStatus.KeptComplete => string.Create(CultureInfo.InvariantCulture, $"copia incompleta ({missing} valori mancanti), resta quella completa salvata prima"),
|
||||
SessionCopyStatus.KeptComplete => $"copia incompleta ({ItalianText.Count(missing, "valore mancante", "valori mancanti")}), resta quella completa salvata prima",
|
||||
SessionCopyStatus.Discarded => "sessione letta ma non salvata, il browser era tornato al motore",
|
||||
_ => $"sessione non copiata, {BrowserSessionCopier.Reason(copy.Refusal ?? CaptureRefusal.BrowserError)}; la sessione salvata resta com'era",
|
||||
};
|
||||
@@ -1038,6 +1059,9 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
return (new SessionCopyOutcome(SessionCopyStatus.Discarded, null, 0, 0, 0), false);
|
||||
}
|
||||
|
||||
// «Ho finito» copies again: from now on its own outcome is what a later «Ho finito» refers to, not an earlier copy
|
||||
// made when the browser was let go (that copy may be of another login).
|
||||
ForgetLetGoCopy();
|
||||
var hold = _browser.UserDriving.Begin();
|
||||
try
|
||||
{
|
||||
@@ -1078,27 +1102,24 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
const string Before = "La sessione per le richieste era già stata copiata quando il browser è tornato al motore";
|
||||
const string Again = "Riapri il browser dalla pagina Collegamento («Usa il browser» o «Pagina iniziale») e premi di nuovo «Ho finito» sulla tua pagina iniziale.";
|
||||
var missing = copy.MissingValues + copy.MissingDocIds;
|
||||
var needed = MissingNeeded(missing, past: true);
|
||||
var missingClause = missing == 0
|
||||
? string.Empty
|
||||
: string.Create(
|
||||
CultureInfo.InvariantCulture,
|
||||
$" Alla pagina però mancavano {missing} valori che servono alle richieste: la copia ha comunque sostituito quella salvata prima, e le richieste che li usano non partono. Alla prima Febe si ferma come disconnessione e chiede un nuovo accesso. {Again}");
|
||||
: $" Alla pagina però {needed}: la copia ha comunque sostituito quella salvata prima, e le richieste che {(missing == 1 ? "lo" : "li")} usano non partono. Alla prima Febe si ferma come disconnessione e chiede un nuovo accesso. {Again}";
|
||||
return copy.Status switch
|
||||
{
|
||||
SessionCopyStatus.Saved when earlier => Before + "." + (missing == 0 ? " Non serve rifare l'accesso." : missingClause),
|
||||
SessionCopyStatus.SavedInMemoryOnly when earlier =>
|
||||
Before + ", ma non ancora scritta su disco: Febe riprova alla prossima scrittura." + (missing == 0 ? " Non serve rifare l'accesso." : missingClause),
|
||||
SessionCopyStatus.KeptComplete when earlier => string.Create(
|
||||
CultureInfo.InvariantCulture,
|
||||
$"Quando il browser è tornato al motore alla pagina mancavano {missing} valori che servono alle richieste: come hai scelto nella pagina Collegamento («Tieni la copia completa»), Febe tiene la copia completa salvata prima. Se nel frattempo hai rifatto l'accesso o cambiato account, scegli «Sostituisci comunque». {Again}"),
|
||||
SessionCopyStatus.KeptComplete when earlier =>
|
||||
$"Quando il browser è tornato al motore alla pagina {needed}: come hai scelto nella pagina Collegamento («Tieni la copia completa»), Febe tiene la copia completa salvata prima. Se nel frattempo hai rifatto l'accesso o cambiato account, scegli «Sostituisci comunque». {Again}",
|
||||
SessionCopyStatus.Saved => "Sessione copiata per le richieste di Febe." + missingClause,
|
||||
SessionCopyStatus.SavedInMemoryOnly =>
|
||||
"Sessione copiata per le richieste di Febe, ma non ancora scritta su disco: Febe riprova alla prossima scrittura." + missingClause,
|
||||
SessionCopyStatus.Discarded =>
|
||||
"La sessione per le richieste non è stata copiata: nel frattempo il browser era tornato al motore. Rifai l'accesso dalla pagina Collegamento e premi «Ho finito» sulla tua pagina iniziale.",
|
||||
SessionCopyStatus.KeptComplete => string.Create(
|
||||
CultureInfo.InvariantCulture,
|
||||
$"Alla pagina mancavano {missing} valori che servono alle richieste: come hai scelto nella pagina Collegamento («Tieni la copia completa»), Febe tiene la copia completa salvata prima e non usa quella nuova. Se nel frattempo hai rifatto l'accesso o cambiato account, scegli «Sostituisci comunque». {Again}"),
|
||||
SessionCopyStatus.KeptComplete =>
|
||||
$"Alla pagina {needed}: come hai scelto nella pagina Collegamento («Tieni la copia completa»), Febe tiene la copia completa salvata prima e non usa quella nuova. Se nel frattempo hai rifatto l'accesso o cambiato account, scegli «Sostituisci comunque». {Again}",
|
||||
_ when copy.Refusal is CaptureRefusal.NotOnSite or CaptureRefusal.CheckpointPage =>
|
||||
$"La sessione per le richieste non è stata copiata: {BrowserSessionCopier.Reason(copy.Refusal.Value)}. La sessione salvata resta com'era. {Again}",
|
||||
_ =>
|
||||
@@ -1106,6 +1127,17 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// «mancano 3 valori che servono alle richieste», «manca 1 valore che serve alle richieste» (<paramref name="past"/>:
|
||||
/// «mancava», «mancavano»): the count with the Italian singular.
|
||||
/// </summary>
|
||||
internal static string MissingNeeded(int missing, bool past)
|
||||
{
|
||||
var one = missing == 1;
|
||||
var verb = past ? (one ? "mancava" : "mancavano") : (one ? "manca" : "mancano");
|
||||
return $"{verb} {ItalianText.Count(missing, "valore", "valori")} che {(one ? "serve" : "servono")} alle richieste";
|
||||
}
|
||||
|
||||
/// <summary>«Scollega» deletes the saved session (memory first, then the file); returns the error of the disk, if any.</summary>
|
||||
private async Task<Exception?> ForgetSavedSessionAsync()
|
||||
{
|
||||
@@ -1236,6 +1268,27 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Forgets the copy made when this use of the browser was let go (S4b): it no longer answers a late «Ho finito».</summary>
|
||||
private void ForgetLetGoCopy()
|
||||
{
|
||||
lock (_gate)
|
||||
{
|
||||
_letGoCopy = null;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// «Ho finito» ends the lease. When it ends one, that use of the browser is over: a further «Ho finito» (a double
|
||||
/// click, a second tab) must not report a copy made when that use was let go earlier.
|
||||
/// </summary>
|
||||
private void EndUseWithHoFinito()
|
||||
{
|
||||
if (ReleaseLease())
|
||||
{
|
||||
ForgetLetGoCopy();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Ends the lease (only <paramref name="expected"/>, when given). True when a lease ended.</summary>
|
||||
private bool ReleaseLease(IDisposable? expected = null)
|
||||
{
|
||||
@@ -1248,12 +1301,55 @@ public sealed class BrowserConnectionService : IConnectionService, IAsyncDisposa
|
||||
return false;
|
||||
}
|
||||
|
||||
lease = _lease;
|
||||
watch = _leaseWatch;
|
||||
_lease = null;
|
||||
_leaseWatch = null;
|
||||
(lease, watch) = TakeLeaseLocked();
|
||||
}
|
||||
|
||||
return EndLease(lease, watch);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ends <paramref name="expected"/> by itself only if, in the same look, it is still the only lease of the browser and
|
||||
/// no sign that Alberto uses the browser came after <paramref name="lastSign"/>: another lease (the remote view, the
|
||||
/// window, a hold of the connection) is a sign now. True when the lease ended.
|
||||
/// </summary>
|
||||
private bool ReleaseIdleLease(IDisposable expected, DateTimeOffset lastSign)
|
||||
{
|
||||
IDisposable? lease;
|
||||
CancellationTokenSource? watch;
|
||||
lock (_gate)
|
||||
{
|
||||
if (!ReferenceEquals(_lease, expected))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_browser.UserDriving.Leases > 1)
|
||||
{
|
||||
_lastDrivingSign = _clock.Now;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_lastDrivingSign > lastSign)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
(lease, watch) = TakeLeaseLocked();
|
||||
}
|
||||
|
||||
return EndLease(lease, watch);
|
||||
}
|
||||
|
||||
private (IDisposable? Lease, CancellationTokenSource? Watch) TakeLeaseLocked()
|
||||
{
|
||||
var taken = (_lease, _leaseWatch);
|
||||
_lease = null;
|
||||
_leaseWatch = null;
|
||||
return taken;
|
||||
}
|
||||
|
||||
private static bool EndLease(IDisposable? lease, CancellationTokenSource? watch)
|
||||
{
|
||||
if (watch is not null)
|
||||
{
|
||||
watch.Cancel();
|
||||
|
||||
Reference in new issue
Block a user