| | | 1 | | |
| | | 2 | | namespace Kestrun.Tasks; |
| | | 3 | | |
| | | 4 | | /// <summary> |
| | | 5 | | /// Represents the progress state of a task. |
| | | 6 | | /// </summary> |
| | | 7 | | public class ProgressiveKestrunTaskState |
| | | 8 | | { |
| | | 9 | | // Synchronizes concurrent updates coming from different runspaces. |
| | | 10 | | #if NET9_0_OR_GREATER |
| | 37 | 11 | | private readonly Lock _syncRoot = new(); |
| | | 12 | | #else |
| | | 13 | | private readonly object _syncRoot = new(); |
| | | 14 | | #endif |
| | | 15 | | private int _percentComplete = 0; |
| | 37 | 16 | | private string _statusMessage = "Not started"; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Percentage of task completed (0-100). |
| | | 20 | | /// </summary> |
| | | 21 | | public int PercentComplete |
| | | 22 | | { |
| | | 23 | | get |
| | 28 | 24 | | { |
| | | 25 | | lock (_syncRoot) |
| | | 26 | | { |
| | 28 | 27 | | return _percentComplete; |
| | | 28 | | } |
| | 28 | 29 | | } |
| | | 30 | | |
| | 3 | 31 | | set => SetState(value, StatusMessage, nameof(value)); |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Optional status message for the current progress. |
| | | 36 | | /// </summary> |
| | | 37 | | public string StatusMessage |
| | | 38 | | { |
| | | 39 | | get |
| | 15 | 40 | | { |
| | | 41 | | lock (_syncRoot) |
| | | 42 | | { |
| | 15 | 43 | | return _statusMessage; |
| | | 44 | | } |
| | 15 | 45 | | } |
| | 16 | 46 | | set => SetState(PercentComplete, value ?? throw new ArgumentNullException(nameof(value))); |
| | | 47 | | } |
| | | 48 | | /// <summary> |
| | | 49 | | /// Returns a string representation of the progress state. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <returns> A string representation of the progress state. </returns> |
| | | 52 | | public override string ToString() |
| | 2 | 53 | | { |
| | | 54 | | lock (_syncRoot) |
| | | 55 | | { |
| | 2 | 56 | | return $"{_percentComplete}% - {_statusMessage}"; |
| | | 57 | | } |
| | 2 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Resets the progress state to initial values. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="message"> Optional message to include with the reset status.</param> |
| | 5 | 64 | | public void Reset(string message = "Not started") => SetState(0, message); |
| | | 65 | | /// <summary> |
| | | 66 | | /// Marks the progress as complete with an optional message. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="message"> Optional message to include with the completion status.</param> |
| | 10 | 69 | | public void Complete(string message = "Completed") => SetState(100, message); |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Marks the progress as failed with an optional message. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="message"> Optional message to include with the failure status.</param> |
| | 2 | 75 | | public void Fail(string message = "Failed") => SetState(100, message); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Marks the progress as cancelled with an optional message. |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="message"> Optional message to include with the cancellation status.</param> |
| | 6 | 81 | | public void Cancel(string message = "Cancelled") => SetState(100, message); |
| | | 82 | | |
| | | 83 | | #pragma warning disable IDE0060 // Remove unused parameter |
| | | 84 | | private void SetState(int percentComplete, string statusMessage, string percentParameterName = nameof(PercentComplet |
| | | 85 | | #pragma warning restore IDE0060 // Remove unused parameter |
| | | 86 | | { |
| | 41 | 87 | | if (string.IsNullOrWhiteSpace(statusMessage)) |
| | | 88 | | { |
| | 0 | 89 | | throw new ArgumentNullException(nameof(statusMessage)); |
| | | 90 | | } |
| | | 91 | | |
| | 41 | 92 | | if (percentComplete < 0) |
| | | 93 | | { |
| | 0 | 94 | | percentComplete = 0; |
| | | 95 | | } |
| | 41 | 96 | | else if (percentComplete > 100) |
| | | 97 | | { |
| | 0 | 98 | | percentComplete = 100; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | |
| | | 102 | | lock (_syncRoot) |
| | | 103 | | { |
| | 41 | 104 | | _percentComplete = percentComplete; |
| | 41 | 105 | | _statusMessage = statusMessage; |
| | 41 | 106 | | } |
| | 41 | 107 | | } |
| | | 108 | | } |