Добавьте файлы проекта.
This commit is contained in:
31
Pfr.Services.Scheduler.sln
Normal file
31
Pfr.Services.Scheduler.sln
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.0.32112.339
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pfr.Services.Scheduler", "Pfr.Services.Scheduler\Pfr.Services.Scheduler.csproj", "{7D0E3F8D-917D-41CF-B093-A5365C8F24F3}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Pfr.Services.SchedulerTests", "Pfr.Services.SchedulerTests\Pfr.Services.SchedulerTests.csproj", "{02192A90-645F-4DBC-866D-A1E890905C44}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{7D0E3F8D-917D-41CF-B093-A5365C8F24F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{7D0E3F8D-917D-41CF-B093-A5365C8F24F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{7D0E3F8D-917D-41CF-B093-A5365C8F24F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{7D0E3F8D-917D-41CF-B093-A5365C8F24F3}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{02192A90-645F-4DBC-866D-A1E890905C44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{02192A90-645F-4DBC-866D-A1E890905C44}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{02192A90-645F-4DBC-866D-A1E890905C44}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{02192A90-645F-4DBC-866D-A1E890905C44}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {5079223D-206D-44BE-8FB5-E7AB3657172D}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
11
Pfr.Services.Scheduler/Pfr.Services.Scheduler.csproj
Normal file
11
Pfr.Services.Scheduler/Pfr.Services.Scheduler.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
|
||||
<Version>$(VersionPrefix)1.2.3</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
86
Pfr.Services.Scheduler/Scheduler.cs
Normal file
86
Pfr.Services.Scheduler/Scheduler.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
namespace Pfr.Services.Scheduler
|
||||
{
|
||||
public class Scheduler
|
||||
{
|
||||
private long _Interval = 1000;
|
||||
private Timer _timer;
|
||||
private TimerCallback _timerCallback;
|
||||
private TimeExec _TimeExec;
|
||||
private DateTime _DateTimeExec;
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="pTimeExec"></param>
|
||||
public Scheduler(TimeExec pTimeExec)
|
||||
{
|
||||
_timerCallback = new TimerCallback(CheckTime);
|
||||
_timer = new Timer(_timerCallback, null, 0, _Interval);
|
||||
|
||||
_TimeExec = pTimeExec;
|
||||
if(DateTime.Now.Hour <= pTimeExec.Hour)
|
||||
{
|
||||
if(DateTime.Now.Minute < pTimeExec.Minute)
|
||||
{
|
||||
//Todo-OK устанавливаем время запуска сегодня
|
||||
_DateTimeExec = new DateTime(DateTime.Now.Year,
|
||||
DateTime.Now.Month,
|
||||
DateTime.Now.Day,
|
||||
pTimeExec.Hour,
|
||||
pTimeExec.Minute,
|
||||
pTimeExec.Second);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Todo-OK устанавливаем время запуска завтра
|
||||
_DateTimeExec = new DateTime(DateTime.Now.Year,
|
||||
DateTime.Now.Month,
|
||||
DateTime.Now.Day,
|
||||
pTimeExec.Hour,
|
||||
pTimeExec.Minute,
|
||||
pTimeExec.Second);
|
||||
_DateTimeExec = _DateTimeExec.AddDays(1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//Todo-OK устанавливаем время запуска завтра
|
||||
_DateTimeExec = new DateTime(DateTime.Now.Year,
|
||||
DateTime.Now.Month,
|
||||
DateTime.Now.Day,
|
||||
pTimeExec.Hour,
|
||||
pTimeExec.Minute,
|
||||
pTimeExec.Second);
|
||||
_DateTimeExec = _DateTimeExec.AddDays(1);
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void CheckTimeHandler(DateTime pDateTimeExec);
|
||||
//public delegate void CheckTimeHandler();
|
||||
|
||||
public event CheckTimeHandler? OnExecute;
|
||||
private void CheckTime(object obj)
|
||||
{
|
||||
if (Run())
|
||||
{
|
||||
OnExecute?.Invoke(_DateTimeExec);
|
||||
_DateTimeExec = _DateTimeExec.AddHours(24);
|
||||
}
|
||||
}
|
||||
|
||||
private bool Run()
|
||||
{
|
||||
bool _Exec = false;
|
||||
DateTime _curDateTime = DateTime.Now;
|
||||
_Exec = false;
|
||||
if ((_curDateTime.Day == _DateTimeExec.Day)
|
||||
&&(_curDateTime.Hour == _DateTimeExec.Hour)
|
||||
&& (_curDateTime.Minute == _DateTimeExec.Minute))
|
||||
{
|
||||
_Exec = true;
|
||||
}
|
||||
return _Exec;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
73
Pfr.Services.Scheduler/TimeExec.cs
Normal file
73
Pfr.Services.Scheduler/TimeExec.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pfr.Services.Scheduler
|
||||
{
|
||||
public class TimeExec
|
||||
{
|
||||
private readonly int DefaultHour = 0;
|
||||
private readonly int DefaultMinute = 0;
|
||||
private readonly int DefaultSecond = 0;
|
||||
|
||||
|
||||
private int _Hour;
|
||||
private int _Minute;
|
||||
private int _Second;
|
||||
public TimeExec()
|
||||
{
|
||||
_Hour = DefaultHour;
|
||||
_Minute = DefaultMinute;
|
||||
_Second = DefaultSecond;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Конструктор время выполнения
|
||||
/// </summary>
|
||||
/// <param name="hour"> час от 0 до 23</param>
|
||||
/// <param name="minute">мин от 0 до 59</param>
|
||||
/// <exception cref="ArgumentException"></exception>
|
||||
public TimeExec(int hour, int minute)
|
||||
{
|
||||
InitProp(hour, minute);
|
||||
Second = DefaultSecond;
|
||||
}
|
||||
|
||||
public TimeExec(int hour, int minute, int second)
|
||||
{
|
||||
InitProp(hour, minute);
|
||||
if ((second > 59) || (second < 0))
|
||||
{
|
||||
throw new ArgumentException(string.Format("second value from 0 to 59 this value {0}", second));
|
||||
}
|
||||
Second = second;
|
||||
}
|
||||
private void InitProp(int hour, int minute)
|
||||
{
|
||||
if ((hour > 23) || (hour < 0))
|
||||
{
|
||||
throw new ArgumentException(string.Format("hour value from 0 to 23 this value {0}", hour));
|
||||
}
|
||||
if ((minute > 59) || (minute < 0))
|
||||
{
|
||||
throw new ArgumentException(string.Format("minute value from 0 to 59 this value {0}", minute));
|
||||
}
|
||||
Hour = hour;
|
||||
Minute = minute;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// мин от 0 до 59
|
||||
/// </summary>
|
||||
public int Minute { get => _Minute; set => _Minute = value; }
|
||||
|
||||
/// <summary>
|
||||
/// час от 0 до 23
|
||||
/// </summary>
|
||||
public int Hour { get => _Hour; set => _Hour = value; }
|
||||
public int Second { get => _Second; set => _Second = value; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
|
||||
<PackageReference Include="MSTest.TestAdapter" Version="2.2.7" />
|
||||
<PackageReference Include="MSTest.TestFramework" Version="2.2.7" />
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.0" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Pfr.Services.Scheduler\Pfr.Services.Scheduler.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
64
Pfr.Services.SchedulerTests/SchedulerTests.cs
Normal file
64
Pfr.Services.SchedulerTests/SchedulerTests.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using Xunit;
|
||||
using Pfr.Services.Scheduler;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
|
||||
namespace Pfr.Services.Scheduler.Tests
|
||||
{
|
||||
public class SchedulerTests
|
||||
{
|
||||
private TimeExec _te;
|
||||
private int _CntExec = 0;
|
||||
|
||||
public TimeExec GetTimeExec(int pSec)
|
||||
{
|
||||
TimeExec _TimeExec = new TimeExec();
|
||||
DateTime _dateTime = DateTime.Now.AddSeconds(pSec);
|
||||
_TimeExec.Hour = _dateTime.Hour;
|
||||
_TimeExec.Minute = _dateTime.Minute;
|
||||
return _TimeExec;
|
||||
}
|
||||
|
||||
[Fact()]
|
||||
public void SchedulerTest()
|
||||
{
|
||||
_te = GetTimeExec(2);
|
||||
Scheduler _Scheduler = new Scheduler(_te);
|
||||
_Scheduler.OnExecute += OnExecMetod;
|
||||
Thread.Sleep(4000);
|
||||
}
|
||||
|
||||
private void OnExecMetod(DateTime pDateTimeExec)
|
||||
{
|
||||
Assert.Equal(pDateTimeExec.Hour, DateTime.Now.Hour);
|
||||
Assert.Equal(pDateTimeExec.Minute, DateTime.Now.Minute);
|
||||
}
|
||||
|
||||
[Fact()]
|
||||
public void Scheduler_Empty_OnExecute_Test()
|
||||
{
|
||||
_te = GetTimeExec(2);
|
||||
Scheduler _Scheduler = new Scheduler(_te);
|
||||
Thread.Sleep(4000);
|
||||
}
|
||||
|
||||
[Fact()]
|
||||
public void Scheduler_One_Exec_Test()
|
||||
{
|
||||
_te = GetTimeExec(60);
|
||||
Scheduler _Scheduler = new Scheduler(_te);
|
||||
_Scheduler.OnExecute += OnExec;
|
||||
Thread.Sleep(120000);
|
||||
Assert.Equal(_CntExec, 1);
|
||||
}
|
||||
|
||||
private void OnExec(DateTime pDateTimeExec)
|
||||
{
|
||||
_CntExec += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user