Skip to content

Commit

Permalink
2.0.0 (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
OrnarasUS authored Feb 6, 2025
2 parents c3d93db + b65b00b commit 09ff997
Show file tree
Hide file tree
Showing 19 changed files with 181 additions and 573 deletions.
19 changes: 2 additions & 17 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,11 @@ jobs:
uses: actions/setup-dotnet@v4
with:
dotnet-version: 8.x
- name: Auth GitHub NuGet
run: dotnet nuget add source --username OrnarasUS --password ${{ secrets.TOKEN_GITHUB }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/OrnarasUS/index.json"
- name: Build project
run: dotnet build
- name: Set variables
id: vars
run: |
echo "nupkg=bin/Debug/$(ls bin/Debug | grep '\.nupkg')" >> $GITHUB_OUTPUT
echo "dll=bin/Debug/net8.0/$(ls bin/Debug/net8.0 | grep '\.dll')" >> $GITHUB_OUTPUT
run: echo "nupkg=bin/Debug/$(ls bin/Debug | grep '\.nupkg')" >> $GITHUB_OUTPUT
- name: Publish to NuGet
run: |
dotnet nuget push ${{ steps.vars.outputs.nupkg }} --source "github"
dotnet nuget push ${{ steps.vars.outputs.nupkg }} --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json
- name: Publish to GitHub Release
uses: softprops/action-gh-release@v2
with:
repository: OrnarasUS/OrnaLibs.Utils
token: ${{ secrets.PUBLISH_GITHUB_TOKEN }}
files: ${{ steps.vars.outputs.dll }}
name: ${{ github.event.pull_request.title }}
tag_name: ${{ github.event.pull_request.title }}
body: ${{ github.event.pull_request.body }}
run: dotnet nuget push ${{ steps.vars.outputs.nupkg }} --api-key ${{ secrets.NUGET_TOKEN }} --source https://api.nuget.org/v3/index.json

27 changes: 27 additions & 0 deletions ActionScheduler/ActionTypes/DailyAction.cs
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();
}
}
}
16 changes: 16 additions & 0 deletions ActionScheduler/Builders/ActionBuilder.cs
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;
}
}
28 changes: 28 additions & 0 deletions ActionScheduler/Builders/DailyBuilder.cs
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;
}
}
10 changes: 10 additions & 0 deletions ActionScheduler/IExpectedAction.cs
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();
}
}
59 changes: 59 additions & 0 deletions ActionScheduler/Scheduler.cs
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;
}
}
}
47 changes: 0 additions & 47 deletions Application/Base.cs

This file was deleted.

49 changes: 0 additions & 49 deletions Application/Properties.cs

This file was deleted.

38 changes: 0 additions & 38 deletions Application/Windows.cs

This file was deleted.

1 change: 0 additions & 1 deletion ConsoleUI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using OrnaLibs.DataTypes;
using System.Globalization;

namespace OrnaLibs
Expand Down
28 changes: 0 additions & 28 deletions DataTypes/RequiredDirectory.cs

This file was deleted.

1 change: 1 addition & 0 deletions Managers/TaskScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/// <summary>
/// Планировщик задач
/// </summary>
[Obsolete("Рекомендуется использовать ActionScheduler")]
public static class TaskScheduler
{
private static TimeOnly?[] times = null!;
Expand Down
Loading

0 comments on commit 09ff997

Please sign in to comment.