From 7fbb4dc0d3a5f18eaee37ef38fdbacf6654be0e8 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Thu, 3 Sep 2026 12:48:35 -0700 Subject: [PATCH] Adds the scheduled job domain model --- .../Common/Enums/Scheduling/ScheduleType.cs | 17 ++ .../Enums/Scheduling/ScheduledJobStatus.cs | 27 +++ .../Enums/Scheduling/ScheduledTaskType.cs | 17 ++ .../Common/Errors/Errors.Scheduling.cs | 28 +++ .../UserAggregate/UserId.cs | 51 ++++ .../Events/ScheduledJobAddedDomainEvent.cs | 21 ++ .../ScheduledJobCycleStartedDomainEvent.cs | 21 ++ .../ScheduledJobCycleStoppedDomainEvent.cs | 21 ++ ...heduledJobExecutionCompletedDomainEvent.cs | 34 +++ ...ScheduledJobExecutionStartedDomainEvent.cs | 34 +++ ...ScheduledJobExecutionStoppedDomainEvent.cs | 21 ++ .../Events/ScheduledJobFiredDomainEvent.cs | 21 ++ .../Events/ScheduledJobRemovedDomainEvent.cs | 21 ++ .../ScheduledJobAggregate/ScheduledJob.cs | 229 ++++++++++++++++++ .../ValueObjects/DailySchedule.cs | 86 +++++++ .../ValueObjects/IntervalSchedule.cs | 69 ++++++ .../ValueObjects/Schedule.cs | 27 +++ .../ValueObjects/ScheduledJobId.cs | 51 ++++ 18 files changed, 796 insertions(+) create mode 100644 src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduleType.cs create mode 100644 src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduledJobStatus.cs create mode 100644 src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduledTaskType.cs create mode 100644 src/Lumina.Domain/Common/Errors/Errors.Scheduling.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobAddedDomainEvent.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobCycleStartedDomainEvent.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobCycleStoppedDomainEvent.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionCompletedDomainEvent.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionStartedDomainEvent.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionStoppedDomainEvent.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobFiredDomainEvent.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobRemovedDomainEvent.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ScheduledJob.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/DailySchedule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/IntervalSchedule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/Schedule.cs create mode 100644 src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/ScheduledJobId.cs diff --git a/src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduleType.cs b/src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduleType.cs new file mode 100644 index 00000000..c8b274f8 --- /dev/null +++ b/src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduleType.cs @@ -0,0 +1,17 @@ +namespace Lumina.Domain.SharedKernel.Common.Enums.Scheduling; + +/// +/// Enumeration for the types of schedules of scheduled jobs. +/// +public enum ScheduleType +{ + /// + /// The scheduled job repeats with an interval measured in minutes. + /// + WithIntervalInMinutes, + + /// + /// The scheduled job repeats daily at a fixed hour and minute. + /// + DailyAtHourAndMinute +} diff --git a/src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduledJobStatus.cs b/src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduledJobStatus.cs new file mode 100644 index 00000000..f8d84191 --- /dev/null +++ b/src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduledJobStatus.cs @@ -0,0 +1,27 @@ +namespace Lumina.Domain.SharedKernel.Common.Enums.Scheduling; + +/// +/// Enumeration for the statuses of scheduled jobs. +/// +public enum ScheduledJobStatus +{ + /// + /// The scheduled job was added, but its execution cycle was not started. + /// + Added, + + /// + /// The execution cycle of the scheduled job is active, and the job is waiting for its next scheduled execution. + /// + Active, + + /// + /// The task of the scheduled job is currently being executed. + /// + Running, + + /// + /// The scheduled job finished its latest one time execution. + /// + Completed +} diff --git a/src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduledTaskType.cs b/src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduledTaskType.cs new file mode 100644 index 00000000..d6691e10 --- /dev/null +++ b/src/Lumina.Domain.SharedKernel/Common/Enums/Scheduling/ScheduledTaskType.cs @@ -0,0 +1,17 @@ +namespace Lumina.Domain.SharedKernel.Common.Enums.Scheduling; + +/// +/// Enumeration for the types of tasks a scheduled job can execute. +/// +public enum ScheduledTaskType +{ + /// + /// The scheduled job scans all enabled media libraries. + /// + ScanMediaLibraries, + + /// + /// The scheduled job cleans the temporary directory of the application. + /// + CleanTemporaryFiles +} diff --git a/src/Lumina.Domain/Common/Errors/Errors.Scheduling.cs b/src/Lumina.Domain/Common/Errors/Errors.Scheduling.cs new file mode 100644 index 00000000..1f2c5723 --- /dev/null +++ b/src/Lumina.Domain/Common/Errors/Errors.Scheduling.cs @@ -0,0 +1,28 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Primitives; +#endregion + +namespace Lumina.Domain.Common.Errors; + +/// +/// Scheduled job error types. +/// +public static partial class Errors +{ + public static class Scheduling + { + public static Error ScheduledJobNameCannotBeEmpty => Error.Validation(description: nameof(ScheduledJobNameCannotBeEmpty)); + public static Error IntervalMinutesMustBePositive => Error.Validation(description: nameof(IntervalMinutesMustBePositive)); + public static Error HourMustBeBetweenZeroAndTwentyThree => Error.Validation(description: nameof(HourMustBeBetweenZeroAndTwentyThree)); + public static Error MinuteMustBeBetweenZeroAndFiftyNine => Error.Validation(description: nameof(MinuteMustBeBetweenZeroAndFiftyNine)); + public static Error ScheduledJobAlreadyExists => Error.Conflict(description: nameof(ScheduledJobAlreadyExists)); + public static Error ScheduledJobNotFound => Error.NotFound(description: nameof(ScheduledJobNotFound)); + public static Error ScheduledJobExecutionAlreadyExists => Error.Conflict(description: nameof(ScheduledJobExecutionAlreadyExists)); + public static Error ScheduledJobExecutionNotFound => Error.NotFound(description: nameof(ScheduledJobExecutionNotFound)); + public static Error ScheduledJobCycleAlreadyStarted => Error.Forbidden(description: nameof(ScheduledJobCycleAlreadyStarted)); + public static Error ScheduledJobNotStarted => Error.Forbidden(description: nameof(ScheduledJobNotStarted)); + public static Error ScheduledJobAlreadyRunning => Error.Forbidden(description: nameof(ScheduledJobAlreadyRunning)); + public static Error CanOnlyCompleteRunningScheduledJob => Error.Forbidden(description: nameof(CanOnlyCompleteRunningScheduledJob)); + public static Error InvalidScheduleType => Error.Validation(description: nameof(InvalidScheduleType)); + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs new file mode 100644 index 00000000..8af9c718 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ExternalIdentifiers/UserManagementBoundedContext/UserAggregate/UserId.cs @@ -0,0 +1,51 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Models.Core; +using System; +using System.Collections.Generic; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate; + +/// +/// Value object for the Id of a user, duplicated from the UserManagement bounded context to keep the cross bounded context references decoupled. +/// +[DebuggerDisplay("{Value}")] +public sealed class UserId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private UserId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static UserId CreateUnique() + { + return new UserId(Guid.NewGuid()); + } + + /// + /// Creates a new instance of the class, from a pre-existing . + /// + /// The value used to create the instance. + /// The created instance. + public static UserId Create(Guid value) + { + return new UserId(value); + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return Value; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobAddedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobAddedDomainEvent.cs new file mode 100644 index 00000000..88430a4a --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobAddedDomainEvent.cs @@ -0,0 +1,21 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Events; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events; + +/// +/// Domain event raised when a scheduled job is added. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the scheduled job that was added. +/// The date and time when the domain event occurred. +[DebuggerDisplay("Id: {ScheduledJobId.Value}")] +public record ScheduledJobAddedDomainEvent( + Guid Id, + ScheduledJobId ScheduledJobId, + DateTime OccurredOnUtc +) : IDomainEvent; diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobCycleStartedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobCycleStartedDomainEvent.cs new file mode 100644 index 00000000..a361b456 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobCycleStartedDomainEvent.cs @@ -0,0 +1,21 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Events; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events; + +/// +/// Domain event raised when the execution cycle of a scheduled job is started. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the scheduled job whose execution cycle was started. +/// The date and time when the domain event occurred. +[DebuggerDisplay("Id: {ScheduledJobId.Value}")] +public record ScheduledJobCycleStartedDomainEvent( + Guid Id, + ScheduledJobId ScheduledJobId, + DateTime OccurredOnUtc +) : IDomainEvent; diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobCycleStoppedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobCycleStoppedDomainEvent.cs new file mode 100644 index 00000000..584f6246 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobCycleStoppedDomainEvent.cs @@ -0,0 +1,21 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Events; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events; + +/// +/// Domain event raised when the execution cycle of a scheduled job is stopped. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the scheduled job whose execution cycle was stopped. +/// The date and time when the domain event occurred. +[DebuggerDisplay("Id: {ScheduledJobId.Value}")] +public record ScheduledJobCycleStoppedDomainEvent( + Guid Id, + ScheduledJobId ScheduledJobId, + DateTime OccurredOnUtc +) : IDomainEvent; diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionCompletedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionCompletedDomainEvent.cs new file mode 100644 index 00000000..de7f97df --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionCompletedDomainEvent.cs @@ -0,0 +1,34 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Events; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.Scheduling; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events; + +/// +/// Domain event raised when the task of a scheduled job completes its execution. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the scheduled job whose task completed its execution. +/// The unique identifier of the current execution of the task of the scheduled job. +/// The type of the task that completed its execution. +/// when the execution was triggered by the execution cycle of the scheduled job, when it was a one time execution. +/// The date and time when the execution completed. +[DebuggerDisplay("Id: {ScheduledJobId.Value}, RunId: {RunId}")] +public record ScheduledJobExecutionCompletedDomainEvent( + Guid Id, + ScheduledJobId ScheduledJobId, + Guid RunId, + ScheduledTaskType TaskType, + bool IsCycleRun, + DateTime CompletedOnUtc +) : IDomainEvent +{ + /// + /// Gets the date and time when the domain event occurred. + /// + public DateTime OccurredOnUtc => CompletedOnUtc; +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionStartedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionStartedDomainEvent.cs new file mode 100644 index 00000000..e64611c0 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionStartedDomainEvent.cs @@ -0,0 +1,34 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Events; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.Scheduling; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events; + +/// +/// Domain event raised when the task of a scheduled job starts its execution. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the scheduled job whose task started executing. +/// The unique identifier of the current execution of the task of the scheduled job. +/// The type of the task that started executing. +/// when the execution was triggered by the execution cycle of the scheduled job, when it was a one time execution. +/// The date and time when the execution started. +[DebuggerDisplay("Id: {ScheduledJobId.Value}, RunId: {RunId}")] +public record ScheduledJobExecutionStartedDomainEvent( + Guid Id, + ScheduledJobId ScheduledJobId, + Guid RunId, + ScheduledTaskType TaskType, + bool IsCycleRun, + DateTime StartedOnUtc +) : IDomainEvent +{ + /// + /// Gets the date and time when the domain event occurred. + /// + public DateTime OccurredOnUtc => StartedOnUtc; +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionStoppedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionStoppedDomainEvent.cs new file mode 100644 index 00000000..fbf77e34 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobExecutionStoppedDomainEvent.cs @@ -0,0 +1,21 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Events; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events; + +/// +/// Domain event raised when the execution of the task of a scheduled job is stopped while it is running. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the scheduled job whose running execution was stopped. +/// The date and time when the domain event occurred. +[DebuggerDisplay("Id: {ScheduledJobId.Value}")] +public record ScheduledJobExecutionStoppedDomainEvent( + Guid Id, + ScheduledJobId ScheduledJobId, + DateTime OccurredOnUtc +) : IDomainEvent; diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobFiredDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobFiredDomainEvent.cs new file mode 100644 index 00000000..6f2f004d --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobFiredDomainEvent.cs @@ -0,0 +1,21 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Events; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events; + +/// +/// Domain event raised when the task of a scheduled job is fired once. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the scheduled job whose task was fired. +/// The date and time when the domain event occurred. +[DebuggerDisplay("Id: {ScheduledJobId.Value}")] +public record ScheduledJobFiredDomainEvent( + Guid Id, + ScheduledJobId ScheduledJobId, + DateTime OccurredOnUtc +) : IDomainEvent; diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobRemovedDomainEvent.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobRemovedDomainEvent.cs new file mode 100644 index 00000000..9587fff9 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/Events/ScheduledJobRemovedDomainEvent.cs @@ -0,0 +1,21 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Events; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events; + +/// +/// Domain event raised when a scheduled job is removed. +/// +/// The unique identifier of the domain event. +/// The unique identifier of the scheduled job that was removed. +/// The date and time when the domain event occurred. +[DebuggerDisplay("Id: {ScheduledJobId.Value}")] +public record ScheduledJobRemovedDomainEvent( + Guid Id, + ScheduledJobId ScheduledJobId, + DateTime OccurredOnUtc +) : IDomainEvent; diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ScheduledJob.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ScheduledJob.cs new file mode 100644 index 00000000..067f5fdc --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ScheduledJob.cs @@ -0,0 +1,229 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Errors; +using Lumina.Domain.Common.Models.Core; +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ExternalIdentifiers.UserManagementBoundedContext.UserAggregate; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.Events; +using Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; +using Lumina.Domain.SharedKernel.Common.Enums.Scheduling; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate; + +/// +/// Aggregate root for a scheduled job. +/// +[DebuggerDisplay("Id: {Id.Value}")] +public class ScheduledJob : AggregateRoot +{ + private Optional _currentExecutionRunId; + + /// + /// Gets the name of the scheduled job. + /// + public string Name { get; private set; } + + /// + /// Gets the type of the task executed by the scheduled job. + /// + public ScheduledTaskType TaskType { get; private set; } + + /// + /// Gets the schedule of the scheduled job. + /// + public Schedule Schedule { get; private set; } + + /// + /// Gets the status of the scheduled job. + /// + public ScheduledJobStatus Status { get; private set; } + + /// + /// Gets the object representing the unique identifier of the user that owns the scheduled job. + /// + public UserId OwnerUserId { get; private set; } + + /// + /// Gets the optional date and time when the task of the scheduled job last started its execution. + /// + public Optional LastStartedOnUtc { get; private set; } + + /// + /// Gets the optional date and time when the task of the scheduled job last completed its execution. + /// + public Optional LastCompletedOnUtc { get; private set; } + + /// + /// Initializes a new instance of the class. + /// + /// The object representing the unique identifier of the scheduled job. + /// The name of the scheduled job. + /// The type of the task executed by the scheduled job. + /// The schedule of the scheduled job. + /// The object representing the unique identifier of the user that owns the scheduled job. + /// The status of the scheduled job. + /// The optional date and time when the task of the scheduled job last started its execution. + /// The optional date and time when the task of the scheduled job last completed its execution. + private ScheduledJob(ScheduledJobId id, string name, ScheduledTaskType taskType, Schedule schedule, UserId ownerUserId, ScheduledJobStatus status, Optional lastStartedOnUtc, Optional lastCompletedOnUtc) + : base(id) + { + Name = name; + TaskType = taskType; + Schedule = schedule; + OwnerUserId = ownerUserId; + Status = status; + LastStartedOnUtc = lastStartedOnUtc; + LastCompletedOnUtc = lastCompletedOnUtc; + _currentExecutionRunId = Optional.None(); + } + + /// + /// Creates a new instance of the class. + /// + /// The name of the scheduled job. + /// The type of the task executed by the scheduled job. + /// The schedule of the scheduled job. + /// The object representing the unique identifier of the user that owns the scheduled job. + /// + /// An containing either a successfully created , or an error message. + /// + public static Result Create(string name, ScheduledTaskType taskType, Schedule schedule, UserId ownerUserId) + { + if (string.IsNullOrWhiteSpace(name)) + return Errors.Scheduling.ScheduledJobNameCannotBeEmpty; + return new ScheduledJob(ScheduledJobId.CreateUnique(), name, taskType, schedule, ownerUserId, ScheduledJobStatus.Added, Optional.None(), Optional.None()); + } + + /// + /// Creates a new instance of the class, with pre-existing data. + /// + /// The object representing the unique identifier of the scheduled job. + /// The name of the scheduled job. + /// The type of the task executed by the scheduled job. + /// The schedule of the scheduled job. + /// The object representing the unique identifier of the user that owns the scheduled job. + /// The status of the scheduled job. + /// The optional date and time when the task of the scheduled job last started its execution. + /// The optional date and time when the task of the scheduled job last completed its execution. + /// + /// An containing either a successfully created , or an error message. + /// + public static Result Create(ScheduledJobId id, string name, ScheduledTaskType taskType, Schedule schedule, UserId ownerUserId, ScheduledJobStatus status, Optional lastStartedOnUtc, Optional lastCompletedOnUtc) + { + if (string.IsNullOrWhiteSpace(name)) + return Errors.Scheduling.ScheduledJobNameCannotBeEmpty; + return new ScheduledJob(id, name, taskType, schedule, ownerUserId, status, lastStartedOnUtc, lastCompletedOnUtc); + } + + /// + /// Marks the scheduled job as added. + /// + /// An representing either a successful operation, or an error. + public Result Add() + { + if (Status != ScheduledJobStatus.Added) + return Errors.Scheduling.ScheduledJobCycleAlreadyStarted; + _domainEvents.Add(new ScheduledJobAddedDomainEvent(Guid.NewGuid(), Id, DateTime.UtcNow)); + return Result.Success; + } + + /// + /// Starts the execution cycle of the scheduled job. + /// + /// An representing either a successful operation, or an error. + public Result StartCycle() + { + if (Status == ScheduledJobStatus.Active || Status == ScheduledJobStatus.Running) + return Errors.Scheduling.ScheduledJobCycleAlreadyStarted; + + Status = ScheduledJobStatus.Active; + _domainEvents.Add(new ScheduledJobCycleStartedDomainEvent(Guid.NewGuid(), Id, DateTime.UtcNow)); + return Result.Success; + } + + /// + /// Stops the execution cycle of the scheduled job. + /// + /// An representing either a successful operation, or an error. + public Result StopCycle() + { + if (Status == ScheduledJobStatus.Added || Status == ScheduledJobStatus.Completed) + return Errors.Scheduling.ScheduledJobNotStarted; + + ScheduledJobStatus previousStatus = Status; + Status = ScheduledJobStatus.Added; + + // When the task of the scheduled job is currently executing, its execution is stopped as well, so its history row is closed. + if (previousStatus == ScheduledJobStatus.Running) + { + LastCompletedOnUtc = Optional.Some(DateTime.UtcNow); + _domainEvents.Add(new ScheduledJobExecutionStoppedDomainEvent(Guid.NewGuid(), Id, DateTime.UtcNow)); + } + else + _domainEvents.Add(new ScheduledJobCycleStoppedDomainEvent(Guid.NewGuid(), Id, DateTime.UtcNow)); + return Result.Success; + } + + /// + /// Marks the start of a one time execution of the task of the scheduled job. + /// + /// An representing either a successful operation, or an error. + public Result Fire() + { + if (Status == ScheduledJobStatus.Running) + return Errors.Scheduling.ScheduledJobAlreadyRunning; + _domainEvents.Add(new ScheduledJobFiredDomainEvent(Guid.NewGuid(), Id, DateTime.UtcNow)); + return Result.Success; + } + + /// + /// Marks the scheduled job as removed. + /// + /// An representing either a successful operation, or an error. + public Result Remove() + { + _domainEvents.Add(new ScheduledJobRemovedDomainEvent(Guid.NewGuid(), Id, DateTime.UtcNow)); + return Result.Success; + } + + /// + /// Marks the task of the scheduled job as started. + /// + /// Whether the execution was triggered by the execution cycle of the scheduled job, or it is a one time execution. + /// An representing either a successful operation, or an error. + public Result MarkExecutionStarted(bool isCycleRun) + { + if (Status == ScheduledJobStatus.Running) + return Errors.Scheduling.ScheduledJobAlreadyRunning; + + Guid runId = Guid.NewGuid(); + _currentExecutionRunId = Optional.Some(runId); + Status = ScheduledJobStatus.Running; + LastStartedOnUtc = Optional.Some(DateTime.UtcNow); + _domainEvents.Add(new ScheduledJobExecutionStartedDomainEvent(Guid.NewGuid(), Id, runId, TaskType, isCycleRun, DateTime.UtcNow)); + return Result.Success; + } + + /// + /// Marks the task of the scheduled job as completed. + /// + /// Whether the execution was triggered by the execution cycle of the scheduled job, or it is a one time execution. + /// An representing either a successful operation, or an error. + public Result MarkExecutionCompleted(bool isCycleRun) + { + if (Status != ScheduledJobStatus.Running) + return Errors.Scheduling.CanOnlyCompleteRunningScheduledJob; + if (!_currentExecutionRunId.HasValue) + return Errors.Scheduling.CanOnlyCompleteRunningScheduledJob; + + Guid runId = _currentExecutionRunId.Value; + _currentExecutionRunId = Optional.None(); + // A cycle run returns the scheduled job to its active state, where it waits for the next execution; a one time execution finishes the scheduled job. + Status = isCycleRun ? ScheduledJobStatus.Active : ScheduledJobStatus.Completed; + LastCompletedOnUtc = Optional.Some(DateTime.UtcNow); + _domainEvents.Add(new ScheduledJobExecutionCompletedDomainEvent(Guid.NewGuid(), Id, runId, TaskType, isCycleRun, DateTime.UtcNow)); + return Result.Success; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/DailySchedule.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/DailySchedule.cs new file mode 100644 index 00000000..083ad154 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/DailySchedule.cs @@ -0,0 +1,86 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Errors; +using Lumina.Domain.Common.Models.Core; +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.SharedKernel.Common.Enums.Scheduling; +using System; +using System.Collections.Generic; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; + +/// +/// Represents a daily schedule for a scheduled job. +/// +public class DailySchedule : Schedule +{ + /// + /// Gets the hour of the day at which the scheduled job runs. + /// + public int Hour { get; } + + /// + /// Gets the minute of the hour at which the scheduled job runs. + /// + public int Minute { get; } + + /// + /// Gets the type of the schedule. + /// + public override ScheduleType ScheduleType { get; } = ScheduleType.DailyAtHourAndMinute; + + /// + /// Initializes a new instance of the class. + /// + /// The hour of the day at which the scheduled job runs. + /// The minute of the hour at which the scheduled job runs. + private DailySchedule(int hour, int minute) + { + Hour = hour; + Minute = minute; + } + + /// + /// Creates a new instance of the class. + /// + /// The hour of the day at which the scheduled job runs. + /// The minute of the hour at which the scheduled job runs. + /// + /// An containing either a successfully created , or an error message. + /// + public static Result Create(int hour, int minute) + { + if (hour < 0 || hour > 23) + return Errors.Scheduling.HourMustBeBetweenZeroAndTwentyThree; + if (minute < 0 || minute > 59) + return Errors.Scheduling.MinuteMustBeBetweenZeroAndFiftyNine; + return new DailySchedule(hour, minute); + } + + /// + /// Calculates the delay until the next execution of the scheduled job, relative to . + /// + /// The current UTC date and time. + /// The time zone in which the daily hour and minute of the schedule are expressed. + /// The delay until the next execution of the scheduled job. + public override TimeSpan GetDelayUntilNextExecution(DateTime utcNow, TimeZoneInfo timeZone) + { + DateTime localNow = TimeZoneInfo.ConvertTimeFromUtc(utcNow, timeZone); + DateTime nextRunLocal = new(localNow.Year, localNow.Month, localNow.Day, Hour, Minute, 0, DateTimeKind.Unspecified); + DateTime nextRunUtc = TimeZoneInfo.ConvertTimeToUtc(nextRunLocal, timeZone); + // When the next run time already passed today, schedule the execution for the next day. + if (nextRunUtc <= utcNow) + nextRunUtc = nextRunUtc.AddDays(1); + return nextRunUtc - utcNow; + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return Hour; + yield return Minute; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/IntervalSchedule.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/IntervalSchedule.cs new file mode 100644 index 00000000..cd953c3b --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/IntervalSchedule.cs @@ -0,0 +1,69 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Errors; +using Lumina.Domain.Common.Models.Core; +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.SharedKernel.Common.Enums.Scheduling; +using System; +using System.Collections.Generic; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; + +/// +/// Represents an interval based schedule for a scheduled job. +/// +public class IntervalSchedule : Schedule +{ + /// + /// Gets the interval of the schedule, in minutes. + /// + public int IntervalMinutes { get; } + + /// + /// Gets the type of the schedule. + /// + public override ScheduleType ScheduleType { get; } = ScheduleType.WithIntervalInMinutes; + + /// + /// Initializes a new instance of the class. + /// + /// The interval of the schedule, in minutes. + private IntervalSchedule(int intervalMinutes) + { + IntervalMinutes = intervalMinutes; + } + + /// + /// Creates a new instance of the class. + /// + /// The interval of the schedule, in minutes. + /// + /// An containing either a successfully created , or an error message. + /// + public static Result Create(int intervalMinutes) + { + if (intervalMinutes <= 0) + return Errors.Scheduling.IntervalMinutesMustBePositive; + return new IntervalSchedule(intervalMinutes); + } + + /// + /// Calculates the delay until the next execution of the scheduled job, relative to . + /// + /// The current UTC date and time. + /// The time zone in which the daily hour and minute of the schedule are expressed. + /// The delay until the next execution of the scheduled job. + public override TimeSpan GetDelayUntilNextExecution(DateTime utcNow, TimeZoneInfo timeZone) + { + return TimeSpan.FromMinutes(IntervalMinutes); + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return IntervalMinutes; + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/Schedule.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/Schedule.cs new file mode 100644 index 00000000..fc60cc93 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/Schedule.cs @@ -0,0 +1,27 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Models.Core; +using Lumina.Domain.Common.Primitives; +using Lumina.Domain.SharedKernel.Common.Enums.Scheduling; +using System; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; + +/// +/// Represents an abstract schedule for a scheduled job. +/// +public abstract class Schedule : ValueObject +{ + /// + /// Gets the type of the schedule. + /// + public abstract ScheduleType ScheduleType { get; } + + /// + /// Calculates the delay until the next execution of the scheduled job, relative to . + /// + /// The current UTC date and time. + /// The time zone in which the daily hour and minute of the schedule are expressed. + /// The delay until the next execution of the scheduled job. + public abstract TimeSpan GetDelayUntilNextExecution(DateTime utcNow, TimeZoneInfo timeZone); +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/ScheduledJobId.cs b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/ScheduledJobId.cs new file mode 100644 index 00000000..67dc42bc --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/SchedulingBoundedContext/ScheduledJobAggregate/ValueObjects/ScheduledJobId.cs @@ -0,0 +1,51 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Models.Core; +using System; +using System.Collections.Generic; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.SchedulingBoundedContext.ScheduledJobAggregate.ValueObjects; + +/// +/// Value object for the Id of a scheduled job. +/// +[DebuggerDisplay("{Value}")] +public sealed class ScheduledJobId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private ScheduledJobId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static ScheduledJobId CreateUnique() + { + return new ScheduledJobId(Guid.NewGuid()); + } + + /// + /// Creates a new instance of the class, from a pre-existing . + /// + /// The value used to create the instance. + /// The created instance. + public static ScheduledJobId Create(Guid value) + { + return new ScheduledJobId(value); + } + + /// + /// Gets the list of items that define equality of the object. + /// + /// A list of items defining the equality. + public override IEnumerable GetEqualityComponents() + { + yield return Value; + } +}