Implement IDisposable for scheduled tasks

Added IDisposable interface to scheduled task classes to ensure proper resource cleanup. Specifically, added dispose methods to release timers and cancellation tokens where applicable.
This commit is contained in:
Sipke Schoorstra 2024-07-24 14:26:37 +02:00
parent 369263f9ca
commit e9f64f49c2
4 changed files with 28 additions and 4 deletions

View file

@ -10,7 +10,7 @@ namespace Elsa.Scheduling.ScheduledTasks;
/// <summary>
/// A task that is scheduled using a given cron expression.
/// </summary>
public class ScheduledCronTask : IScheduledTask
public class ScheduledCronTask : IScheduledTask, IDisposable
{
private readonly ISystemClock _systemClock;
private readonly ILogger _logger;
@ -97,4 +97,10 @@ public class ScheduledCronTask : IScheduledTask
if (!cancellationToken.IsCancellationRequested) Schedule();
};
}
void IDisposable.Dispose()
{
_timer?.Dispose();
_cancellationTokenSource.Dispose();
}
}

View file

@ -9,7 +9,7 @@ namespace Elsa.Scheduling.ScheduledTasks;
/// <summary>
/// A scheduled recurring task.
/// </summary>
public class ScheduledRecurringTask : IScheduledTask
public class ScheduledRecurringTask : IScheduledTask, IDisposable
{
private readonly ITask _task;
private readonly ISystemClock _systemClock;
@ -87,4 +87,10 @@ public class ScheduledRecurringTask : IScheduledTask
if (!cancellationToken.IsCancellationRequested) Schedule();
};
}
void IDisposable.Dispose()
{
_cancellationTokenSource.Dispose();
_timer?.Dispose();
}
}

View file

@ -9,7 +9,7 @@ namespace Elsa.Scheduling.ScheduledTasks;
/// <summary>
/// A task that is scheduled to execute at a specific instant.
/// </summary>
public class ScheduledSpecificInstantTask : IScheduledTask
public class ScheduledSpecificInstantTask : IScheduledTask, IDisposable
{
private readonly ITask _task;
private readonly ISystemClock _systemClock;
@ -58,4 +58,10 @@ public class ScheduledSpecificInstantTask : IScheduledTask
await commandSender.SendAsync(new RunScheduledTask(_task), cancellationToken);
};
}
void IDisposable.Dispose()
{
_cancellationTokenSource.Dispose();
_timer?.Dispose();
}
}

View file

@ -17,7 +17,7 @@ namespace Elsa.Workflows;
/// Represents the context of an activity execution.
/// </summary>
[PublicAPI]
public partial class ActivityExecutionContext : IExecutionContext
public partial class ActivityExecutionContext : IExecutionContext, IDisposable
{
private readonly ISystemClock _systemClock;
private readonly List<Bookmark> _bookmarks = new();
@ -676,4 +676,10 @@ public partial class ActivityExecutionContext : IExecutionContext
{
return ExpressionExecutionContext.TryGetBlock(locationBlockReference, out var memoryBlock) ? memoryBlock : default;
}
void IDisposable.Dispose()
{
_cancellationRegistration.Dispose();
_cancellationTokenSource.Dispose();
}
}