-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
181 additions
and
573 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace OrnaLibs.ActionScheduler.ActionTypes | ||
{ | ||
public class DailyAction : IExpectedAction | ||
{ | ||
Action IExpectedAction.Action => _action; | ||
DateTime IExpectedAction.LastRun { get => _last; set => _last = value; } | ||
private DateTime _dt => DateTime.Today.Add(_time.ToTimeSpan()); | ||
|
||
private readonly Action _action; | ||
private readonly TimeOnly _time; | ||
private DateTime _last; | ||
|
||
internal DailyAction(Action action, TimeOnly time) | ||
{ | ||
_action = action; | ||
_last = DateTime.MinValue; | ||
_time = time; | ||
} | ||
|
||
void IExpectedAction.TryExecute() | ||
{ | ||
if (_last >= _dt || DateTime.Now < _dt) return; | ||
_last = DateTime.Now; | ||
_action.Invoke(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace OrnaLibs.ActionScheduler.Builders | ||
{ | ||
public partial class ActionBuilder | ||
{ | ||
protected internal Action? _action = null!; | ||
|
||
|
||
public ActionBuilder Action(Action action) | ||
{ | ||
_action = action; | ||
return this; | ||
} | ||
|
||
protected internal virtual bool CheckDone() => _action is not null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using OrnaLibs.ActionScheduler.ActionTypes; | ||
|
||
namespace OrnaLibs.ActionScheduler.Builders | ||
{ | ||
public sealed class DailyBuilder: ActionBuilder | ||
{ | ||
private TimeOnly? _time = null; | ||
|
||
protected internal override bool CheckDone() => base.CheckDone() && _time is not null; | ||
|
||
public DailyBuilder Time(TimeOnly time) | ||
{ | ||
_time = time; | ||
return this; | ||
} | ||
|
||
public DailyAction Build() | ||
{ | ||
if (!CheckDone()) throw new Exception(); | ||
return new DailyAction(_action!, _needSave!.Value, _time!.Value); | ||
} | ||
} | ||
|
||
public partial class ActionBuilder | ||
{ | ||
public DailyBuilder Daily => (DailyBuilder)this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace OrnaLibs.ActionScheduler | ||
{ | ||
public interface IExpectedAction | ||
{ | ||
internal Action Action { get; } | ||
protected DateTime LastRun { get; set; } | ||
|
||
internal void TryExecute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
namespace OrnaLibs.ActionScheduler | ||
{ | ||
// todo добавить учитывание последного запуска | ||
// todo добавить запись и чтение даты и времени последного запуска | ||
public static class Scheduler | ||
{ | ||
private readonly static Dictionary<string, IExpectedAction> actions = []; | ||
|
||
private static Thread? thread = new(Loop) { IsBackground = true }; | ||
private static CancellationTokenSource? source; | ||
private static CancellationToken token; | ||
|
||
public static void Start() | ||
{ | ||
#pragma warning disable CA1513 | ||
if (thread is null) throw new ObjectDisposedException(nameof(Scheduler)); | ||
#pragma warning restore CA1513 | ||
source?.Dispose(); | ||
source = new CancellationTokenSource(); | ||
token = source.Token; | ||
thread.Start(); | ||
} | ||
|
||
private static void Loop() | ||
{ | ||
while (!token.IsCancellationRequested) | ||
{ | ||
try | ||
{ | ||
Thread.Sleep(1000); | ||
var _actions = new List<IExpectedAction>(actions.Values); | ||
if (_actions.Count == 0) return; | ||
foreach (var act in _actions) | ||
act.TryExecute(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (ex is not ThreadAbortException && ex is not ThreadInterruptedException) throw; | ||
} | ||
} | ||
} | ||
|
||
public static void Add(string id, IExpectedAction action) | ||
{ | ||
if (!actions.TryAdd(id, action)) | ||
throw new ArgumentException(null, nameof(id)); | ||
} | ||
|
||
public static void Clear() => actions.Clear(); | ||
|
||
public static void Stop() => source?.Cancel(); | ||
|
||
public static void Dispose() | ||
{ | ||
thread?.Interrupt(); | ||
thread = null; | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
using OrnaLibs.DataTypes; | ||
using System.Globalization; | ||
|
||
namespace OrnaLibs | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.