| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | |
| | | 3 | | namespace Pomodoro.Web.Services; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Provides safe fire-and-forget task execution with exception logging. |
| | | 7 | | /// Use this instead of discard pattern to ensure exceptions are logged. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class SafeTaskRunner |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Runs a task asynchronously without awaiting, logging any exceptions. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="task">The async task to execute.</param> |
| | | 15 | | /// <param name="logger">The logger to use for exception logging.</param> |
| | | 16 | | /// <param name="operationName">A descriptive name for the operation for logging purposes.</param> |
| | | 17 | | public static void RunAndForget( |
| | | 18 | | Func<Task> task, |
| | | 19 | | ILogger logger, |
| | | 20 | | string operationName = Constants.SafeTaskOperations.UnknownOperation) |
| | 485 | 21 | | { |
| | 485 | 22 | | _ = ExecuteSafelyAsync(task, logger, operationName); |
| | 485 | 23 | | } |
| | | 24 | | |
| | | 25 | | private static async Task ExecuteSafelyAsync(Func<Task> task, ILogger logger, string operationName) |
| | 485 | 26 | | { |
| | | 27 | | try |
| | 485 | 28 | | { |
| | 485 | 29 | | await task().ConfigureAwait(false); |
| | 474 | 30 | | } |
| | 7 | 31 | | catch (Exception ex) |
| | 7 | 32 | | { |
| | 7 | 33 | | logger.LogError(ex, Constants.SafeTaskOperations.ErrorInOperationLogFormat, operationName); |
| | 7 | 34 | | } |
| | 481 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Runs a task asynchronously without awaiting, logging any exceptions. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <typeparam name="T">The type of the task result.</typeparam> |
| | | 41 | | /// <param name="task">The async task to execute.</param> |
| | | 42 | | /// <param name="logger">The logger to use for exception logging.</param> |
| | | 43 | | /// <param name="operationName">A descriptive name for the operation for logging purposes.</param> |
| | | 44 | | /// <param name="onSuccess">Optional callback when the task succeeds.</param> |
| | | 45 | | public static void RunAndForget<T>( |
| | | 46 | | Func<Task<T>> task, |
| | | 47 | | ILogger logger, |
| | | 48 | | string operationName = Constants.SafeTaskOperations.UnknownOperation, |
| | | 49 | | Action<T>? onSuccess = null) |
| | 4 | 50 | | { |
| | 4 | 51 | | _ = ExecuteSafelyAsync(task, logger, operationName, onSuccess); |
| | 4 | 52 | | } |
| | | 53 | | |
| | | 54 | | private static async Task ExecuteSafelyAsync<T>(Func<Task<T>> task, ILogger logger, string operationName, Action<T>? |
| | 4 | 55 | | { |
| | | 56 | | try |
| | 4 | 57 | | { |
| | 4 | 58 | | var result = await task().ConfigureAwait(false); |
| | 2 | 59 | | onSuccess?.Invoke(result); |
| | 2 | 60 | | } |
| | 2 | 61 | | catch (Exception ex) |
| | 2 | 62 | | { |
| | 2 | 63 | | logger.LogError(ex, Constants.SafeTaskOperations.ErrorInOperationLogFormat, operationName); |
| | 2 | 64 | | } |
| | 4 | 65 | | } |
| | | 66 | | } |