| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Text;
- using Quadarax.Foundation.Core.Business.Exceptions;
- using Quadarax.Foundation.Core.Object;
- using Quadarax.Foundation.Core.Value.Extensions;
- namespace Quadarax.Foundation.Core.Business.ProcessQueue
- {
- public class QueueItem : DisposableObject, IQueueItem
- {
- public QueueItemStateEnum State { get; protected set;}
- public DateTime Created { get; }
- public DateTime? Modified { get; }
- public DateTime? Started { get; }
- public DateTime? Finished { get; }
- public DateTime StartFrom { get; }
- public DateTime? StartTo { get; }
- public string Affinity { get; protected set; }
- public int Priority { get; }
- public void Assign(string assignTo)
- {
- if (!string.IsNullOrEmpty(Affinity) && !string.IsNullOrEmpty(assignTo))
- throw new InvalidStateException(
- $"Query item '{GetIdentifier()}' has already set Affinity to '{Affinity}'");
- ValidateState("set Affinity", QueueItemStateEnum.New, QueueItemStateEnum.Paused);
- ends here
- Affinity = assignTo;
-
- }
- public void Start()
- {
- throw new NotImplementedException();
- }
- public void Stop()
- {
- throw new NotImplementedException();
- }
- public void Pause()
- {
- throw new NotImplementedException();
- }
- public string GetIdentifier()
- {
- return OnGetIdentifer();
- }
- protected void ValidateState(string operationName, params QueueItemStateEnum[] allowedStates)
- {
- if (string.IsNullOrEmpty(operationName)) throw new ArgumentNullException(nameof(operationName));
- if (allowedStates.Length==0)
- return;
- if (!State.In(allowedStates))
- throw new InvalidStateException(
- $"Query item '{GetIdentifier()}' has invalid state to {operationName} (expecting. {string.Join(",", allowedStates.Select(x=>x.ToString()))}");
- }
- protected virtual string OnGetIdentifer()
- {
- var sb = new StringBuilder();
- sb.Append("QI").Append(Created.ToFileTimeUtc()).Append("_").Append(StartFrom.ToFileTimeUtc()).Append("_")
- .Append(string.IsNullOrEmpty(Affinity) ? 0 : Affinity.GetHashCode());
- return sb.ToString();
- }
- protected override void OnDisposing()
- {
- }
- }
- }
|