-
-
Notifications
You must be signed in to change notification settings - Fork 552
Feature/fix: Add Play/Pause control to inference generation queue #1623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| using System; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Collections.Immutable; | ||
| using System.ComponentModel.DataAnnotations; | ||
|
|
@@ -13,6 +13,7 @@ | |
| using AsyncAwaitBestPractices; | ||
| using Avalonia.Controls.Notifications; | ||
| using Avalonia.Threading; | ||
| using CommunityToolkit.Mvvm.ComponentModel; | ||
| using CommunityToolkit.Mvvm.Input; | ||
| using ExifLibrary; | ||
| using FluentAvalonia.UI.Controls; | ||
|
|
@@ -75,6 +76,115 @@ public abstract partial class InferenceGenerationViewModelBase | |
| [JsonIgnore] | ||
| public IInferenceClientManager ClientManager { get; } | ||
|
|
||
| private readonly List<InferenceProjectDocument> _generationQueue = []; | ||
| private bool _isProcessingQueue; | ||
|
|
||
| [ObservableProperty] | ||
| [NotifyPropertyChangedFor(nameof(QueueToggleIcon))] | ||
| [NotifyPropertyChangedFor(nameof(QueueToggleToolTip))] | ||
| [property: JsonIgnore] | ||
| private bool isQueuePaused = true; | ||
|
|
||
| public string QueueToggleIcon => IsQueuePaused ? "fa-solid fa-play" : "fa-solid fa-pause"; | ||
| public string QueueToggleToolTip => IsQueuePaused ? "Start Queue" : "Pause Queue"; | ||
|
|
||
| [ObservableProperty] | ||
| [NotifyPropertyChangedFor(nameof(QueueGenerationText))] | ||
| [NotifyPropertyChangedFor(nameof(IsQueueClearable))] | ||
| [property: JsonIgnore] | ||
| private int queuedGenerationsCount; | ||
|
|
||
| public string QueueGenerationText => | ||
| QueuedGenerationsCount > 0 ? $"Queue Generation ({QueuedGenerationsCount})" : "Queue Generation"; | ||
|
|
||
| public bool IsQueueClearable => QueuedGenerationsCount > 0; | ||
|
|
||
| [RelayCommand] | ||
| private void ClearQueue() | ||
| { | ||
| _generationQueue.Clear(); | ||
| QueuedGenerationsCount = 0; | ||
| IsQueuePaused = true; | ||
| Logger.Info("Generation queue cleared"); | ||
| } | ||
|
|
||
| [RelayCommand] | ||
| private void ToggleQueueState() | ||
| { | ||
| IsQueuePaused = !IsQueuePaused; | ||
| if (!IsQueuePaused) | ||
| { | ||
| ProcessQueueAsync().SafeFireAndForget(ex => Logger.Error(ex, "Error processing generation queue")); | ||
| } | ||
| } | ||
|
|
||
| [RelayCommand] | ||
| private void QueueGeneration() | ||
| { | ||
| var doc = InferenceProjectDocument.FromLoadable(this); | ||
| _generationQueue.Add(doc); | ||
| QueuedGenerationsCount = _generationQueue.Count; | ||
| Logger.Info("Queued generation. Queue size: {QueueSize}", QueuedGenerationsCount); | ||
|
|
||
| if (!IsQueuePaused) | ||
| { | ||
| ProcessQueueAsync().SafeFireAndForget(ex => Logger.Error(ex, "Error processing generation queue")); | ||
| } | ||
| } | ||
|
|
||
| private async Task ProcessQueueAsync() | ||
| { | ||
| if (_isProcessingQueue) | ||
| return; | ||
|
|
||
| _isProcessingQueue = true; | ||
|
|
||
| try | ||
| { | ||
| while (_generationQueue.Count > 0 && !IsQueuePaused) | ||
| { | ||
| // Wait for any active generation to complete before starting the next | ||
| if (GenerateImageCommand.IsRunning) | ||
| { | ||
| var executionTask = GenerateImageCommand.ExecutionTask; | ||
| if (executionTask is not null) | ||
| { | ||
| await executionTask; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awaiting try
{
await executionTask;
}
catch (Exception ex)
{
Logger.Warn(ex, "Previous generation task failed, continuing queue");
} |
||
| } | ||
| } | ||
|
|
||
| // Re-check after awaiting — queue may have been cleared or paused | ||
| if (_generationQueue.Count == 0 || IsQueuePaused) | ||
| break; | ||
|
|
||
| // Dequeue and load state on UI thread | ||
| var nextDoc = _generationQueue[0]; | ||
| _generationQueue.RemoveAt(0); | ||
| QueuedGenerationsCount = _generationQueue.Count; | ||
|
|
||
| await Dispatcher.UIThread.InvokeAsync(() => LoadStateFromJsonObject(nextDoc.State)); | ||
|
|
||
| try | ||
| { | ||
| await GenerateImageCommand.ExecuteAsync(default(GenerateFlags)); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Logger.Error(ex, "Queued generation failed"); | ||
| } | ||
| } | ||
|
|
||
| if (_generationQueue.Count == 0) | ||
| { | ||
| IsQueuePaused = true; | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| _isProcessingQueue = false; | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected InferenceGenerationViewModelBase( | ||
| IServiceManager<ViewModelBase> vmFactory, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
_generationQueuelist is accessed and modified from both the UI thread (viaClearQueueandQueueGenerationcommands) and potentially a background thread (via theProcessQueueAsyncloop). While Avalonia'sDispatcherSynchronizationContextoften keeps continuations on the UI thread, relying on it for thread safety of a standardList<T>across multipleawaitpoints is risky. Consider using a thread-safe collection likeConcurrentQueue<InferenceProjectDocument>or adding a lock around all accesses to_generationQueue.