mirror of
https://github.com/kevincolyar/CronNET.git
synced 2024-05-17 13:12:36 +02:00
commit
81a6e64c5d
|
@ -1,56 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Timers;
|
||||
using System.Threading;
|
||||
|
||||
namespace CronNET
|
||||
{
|
||||
public interface ICronDaemon
|
||||
{
|
||||
void AddJob(string schedule, ThreadStart action);
|
||||
void Start();
|
||||
void Stop();
|
||||
}
|
||||
|
||||
public class CronDaemon : ICronDaemon
|
||||
{
|
||||
private readonly System.Timers.Timer timer = new System.Timers.Timer(30000);
|
||||
private readonly List<ICronJob> cron_jobs = new List<ICronJob>();
|
||||
private DateTime _last= DateTime.Now;
|
||||
|
||||
public CronDaemon()
|
||||
{
|
||||
timer.AutoReset = true;
|
||||
timer.Elapsed += timer_elapsed;
|
||||
}
|
||||
|
||||
public void AddJob(string schedule, ThreadStart action)
|
||||
{
|
||||
var cj = new CronJob(schedule, action);
|
||||
cron_jobs.Add(cj);
|
||||
}
|
||||
|
||||
public void Start()
|
||||
{
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
timer.Stop();
|
||||
|
||||
foreach (CronJob job in cron_jobs)
|
||||
job.abort();
|
||||
}
|
||||
|
||||
private void timer_elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
if (DateTime.Now.Minute != _last.Minute)
|
||||
{
|
||||
_last = DateTime.Now;
|
||||
foreach (ICronJob job in cron_jobs)
|
||||
job.execute(DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
|
||||
namespace CronNET
|
||||
{
|
||||
public interface ICronJob
|
||||
{
|
||||
void execute(DateTime date_time);
|
||||
void abort();
|
||||
}
|
||||
|
||||
public class CronJob : ICronJob
|
||||
{
|
||||
private readonly ICronSchedule _cron_schedule = new CronSchedule();
|
||||
private readonly ThreadStart _thread_start;
|
||||
private Thread _thread;
|
||||
|
||||
public CronJob(string schedule, ThreadStart thread_start)
|
||||
{
|
||||
_cron_schedule = new CronSchedule(schedule);
|
||||
_thread_start = thread_start;
|
||||
_thread = new Thread(thread_start);
|
||||
}
|
||||
|
||||
private object _lock = new object();
|
||||
public void execute(DateTime date_time)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_cron_schedule.isTime(date_time))
|
||||
return;
|
||||
|
||||
if (_thread.ThreadState == ThreadState.Running)
|
||||
return;
|
||||
|
||||
_thread = new Thread(_thread_start);
|
||||
_thread.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public void abort()
|
||||
{
|
||||
_thread.Abort();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0;net40;net48;net5.0</TargetFrameworks>
|
||||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>CronNET</RootNamespace>
|
||||
<AssemblyName>CronNET</AssemblyName>
|
||||
<PackageReleaseNotes>Migrated from .NET 4.0 to .NET 5.0</PackageReleaseNotes>
|
||||
<Version>8.0</Version>
|
||||
<Authors>kevinkolyar, iminet</Authors>
|
||||
<Product>C# library for running cron jobs on .NET</Product>
|
||||
<PackageId>Iminetsoft.CronNET</PackageId>
|
||||
<PackageProjectUrl>https://github.com/iminet/CronNET</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/iminet/CronNET</RepositoryUrl>
|
||||
<PackageTags>dotnet Cron Crondaemon Crontab</PackageTags>
|
||||
<AssemblyVersion>8.0</AssemblyVersion>
|
||||
<FileVersion>8.0</FileVersion>
|
||||
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -1,35 +0,0 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("CronNET")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("CronNET")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2008")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("aac36739-a1a8-433d-88f6-8834bbd6655f")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Revision and Build Numbers
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("1.0.0.1")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.1")]
|
|
@ -1,106 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProductVersion>8.0.50727</ProductVersion>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>{6FCFBDF4-ECB7-4FC2-A376-E962B11D487D}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>CronTests</RootNamespace>
|
||||
<AssemblyName>CronTests</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileUpgradeFlags>
|
||||
</FileUpgradeFlags>
|
||||
<UpgradeBackupLocation>
|
||||
</UpgradeBackupLocation>
|
||||
<OldToolsVersion>2.0</OldToolsVersion>
|
||||
<PublishUrl>publish\</PublishUrl>
|
||||
<Install>true</Install>
|
||||
<InstallFrom>Disk</InstallFrom>
|
||||
<UpdateEnabled>false</UpdateEnabled>
|
||||
<UpdateMode>Foreground</UpdateMode>
|
||||
<UpdateInterval>7</UpdateInterval>
|
||||
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
|
||||
<UpdatePeriodically>false</UpdatePeriodically>
|
||||
<UpdateRequired>false</UpdateRequired>
|
||||
<MapFileExtensions>true</MapFileExtensions>
|
||||
<ApplicationRevision>0</ApplicationRevision>
|
||||
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
|
||||
<IsWebBootstrapper>false</IsWebBootstrapper>
|
||||
<UseApplicationTrust>false</UseApplicationTrust>
|
||||
<BootstrapperEnabled>true</BootstrapperEnabled>
|
||||
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
|
||||
<RestorePackages>true</RestorePackages>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="nunit.framework">
|
||||
<HintPath>..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CronScheduleTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\CronNET\CronNET.csproj">
|
||||
<Project>{F31D7AF3-FDFA-44F1-9C63-305BAF11D002}</Project>
|
||||
<Name>CronNET</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>Microsoft .NET Framework 4 %28x86 and x64%29</ProductName>
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
|
||||
<Install>false</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>.NET Framework 3.5 SP1</ProductName>
|
||||
<Install>false</Install>
|
||||
</BootstrapperPackage>
|
||||
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
|
||||
<Visible>False</Visible>
|
||||
<ProductName>Windows Installer 3.1</ProductName>
|
||||
<Install>true</Install>
|
||||
</BootstrapperPackage>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
-->
|
||||
</Project>
|
|
@ -1,35 +0,0 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("CronNETTests")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("CronNETTests")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2008")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("6a430089-ab18-4d72-9d7a-5a4ee0883153")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Revision and Build Numbers
|
||||
// by using the '*' as shown below:
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
|
@ -1,4 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="NUnit" version="2.6.3" targetFramework="net40" />
|
||||
</packages>
|
|
@ -1,29 +1,26 @@
|
|||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using CronNET;
|
||||
using NUnit.Framework;
|
||||
using System.Threading;
|
||||
|
||||
|
||||
namespace CronTests
|
||||
namespace crondotnet.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public class CronScheduleTests
|
||||
{
|
||||
|
||||
[Test]
|
||||
public void is_valid_test()
|
||||
{
|
||||
var cron_schedule = new CronSchedule();
|
||||
Assert.IsTrue(cron_schedule.isValid("*/2"));
|
||||
Assert.IsTrue(cron_schedule.isValid("* * * * *"));
|
||||
Assert.IsTrue(cron_schedule.isValid("0 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.isValid("0,1,2 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.isValid("*/2 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.isValid("1-4 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.isValid("1-55/3 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.isValid("1,10,20 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.isValid("* 1,10,20 * * *"));
|
||||
Assert.IsTrue(cron_schedule.IsValid("*/2"));
|
||||
Assert.IsTrue(cron_schedule.IsValid("* * * * *"));
|
||||
Assert.IsTrue(cron_schedule.IsValid("0 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.IsValid("0,1,2 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.IsValid("*/2 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.IsValid("1-4 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.IsValid("1-55/3 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.IsValid("1,10,20 * * * *"));
|
||||
Assert.IsTrue(cron_schedule.IsValid("* 1,10,20 * * *"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
@ -116,76 +113,76 @@ namespace CronTests
|
|||
public void is_time_minute_test()
|
||||
{
|
||||
var cron_schedule = new CronSchedule("0 * * * *");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:00 am")));
|
||||
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("8:01 am")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("8:00 am")));
|
||||
Assert.IsFalse(cron_schedule.IsTime(DateTime.Parse("8:01 am")));
|
||||
|
||||
cron_schedule = new CronSchedule("0-10 * * * *");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:00 am")));
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:03 am")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("8:00 am")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("8:03 am")));
|
||||
|
||||
cron_schedule = new CronSchedule("*/2 * * * *");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:00 am")));
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("8:02 am")));
|
||||
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("8:03 am")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("8:00 am")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("8:02 am")));
|
||||
Assert.IsFalse(cron_schedule.IsTime(DateTime.Parse("8:03 am")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void is_time_hour_test()
|
||||
{
|
||||
var cron_schedule = new CronSchedule("* 0 * * *");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00 am")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("12:00 am")));
|
||||
|
||||
cron_schedule = new CronSchedule("* 0,12 * * *");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00 am")));
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00 pm")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("12:00 am")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("12:00 pm")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void is_time_day_of_month_test()
|
||||
{
|
||||
var cron_schedule = new CronSchedule("* * 1 * *");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("2010/08/01")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("2010/08/01")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void is_time_month_test()
|
||||
{
|
||||
var cron_schedule = new CronSchedule("* * * 1 *");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("1/1/2008")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("1/1/2008")));
|
||||
|
||||
cron_schedule = new CronSchedule("* * * 12 *");
|
||||
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("1/1/2008")));
|
||||
Assert.IsFalse(cron_schedule.IsTime(DateTime.Parse("1/1/2008")));
|
||||
|
||||
cron_schedule = new CronSchedule("* * * */3 *");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("3/1/2008")));
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("6/1/2008")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("3/1/2008")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("6/1/2008")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void is_time_day_of_week_test()
|
||||
{
|
||||
var cron_schedule = new CronSchedule("* * * * 0");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("10/12/2008")));
|
||||
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("10/13/2008")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("10/12/2008")));
|
||||
Assert.IsFalse(cron_schedule.IsTime(DateTime.Parse("10/13/2008")));
|
||||
|
||||
cron_schedule = new CronSchedule("* * * * */2");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("10/14/2008")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("10/14/2008")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void is_time_test()
|
||||
{
|
||||
var cron_schedule = new CronSchedule("0 0 12 10 *");
|
||||
Assert.IsTrue(cron_schedule.isTime(DateTime.Parse("12:00:00 am 10/12/2008")));
|
||||
Assert.IsFalse(cron_schedule.isTime(DateTime.Parse("12:01:00 am 10/12/2008")));
|
||||
Assert.IsTrue(cron_schedule.IsTime(DateTime.Parse("12:00:00 am 10/12/2008")));
|
||||
Assert.IsFalse(cron_schedule.IsTime(DateTime.Parse("12:01:00 am 10/12/2008")));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void ppp()
|
||||
{
|
||||
var d = new CronDaemon();
|
||||
d.AddJob("*/1 * * * *", () => { Console.WriteLine(DateTime.Now.ToString()); });
|
||||
d.Start();
|
||||
d.AddJob("*/1 * * * *", async (token) => { Console.WriteLine(DateTime.Now.ToString()); });
|
||||
d.StartAsync(CancellationToken.None);
|
||||
//Thread.Sleep(60 * 1000);
|
||||
}
|
||||
}
|
22
crondotnet.Tests/crondotnet.Tests.csproj
Normal file
22
crondotnet.Tests/crondotnet.Tests.csproj
Normal file
|
@ -0,0 +1,22 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6</TargetFramework>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\crondotnet\crondotnet.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -3,9 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00
|
|||
# Visual Studio Version 16
|
||||
VisualStudioVersion = 16.0.33027.164
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CronNET", "CronNET\CronNET.csproj", "{F31D7AF3-FDFA-44F1-9C63-305BAF11D002}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "crondotnet", "crondotnet\crondotnet.csproj", "{F31D7AF3-FDFA-44F1-9C63-305BAF11D002}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CronNETTests", "CronNETTests\CronNETTests.csproj", "{6FCFBDF4-ECB7-4FC2-A376-E962B11D487D}"
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "crondotnet.Tests", "crondotnet.Tests\crondotnet.Tests.csproj", "{6FCFBDF4-ECB7-4FC2-A376-E962B11D487D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
66
crondotnet/CronDaemon.cs
Normal file
66
crondotnet/CronDaemon.cs
Normal file
|
@ -0,0 +1,66 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace crondotnet
|
||||
{
|
||||
public interface ICronDaemon
|
||||
{
|
||||
void AddJob(string schedule, ExecuteCronJob action);
|
||||
Task StartAsync(CancellationToken cancellationToken);
|
||||
Task StopAsync();
|
||||
}
|
||||
|
||||
public class CronDaemon : ICronDaemon
|
||||
{
|
||||
private readonly PeriodicTimer timer;
|
||||
private readonly List<ICronJob> cronJobs = new List<ICronJob>();
|
||||
private DateTime _last = DateTime.Now;
|
||||
private CancellationTokenSource tokenSource = null;
|
||||
private Task timerTask = null;
|
||||
|
||||
public CronDaemon()
|
||||
{
|
||||
timer = new PeriodicTimer(TimeSpan.FromSeconds(30));
|
||||
}
|
||||
|
||||
public void AddJob(string schedule, ExecuteCronJob action)
|
||||
{
|
||||
var cj = new CronJob(schedule, action);
|
||||
cronJobs.Add(cj);
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (timerTask == null)
|
||||
{
|
||||
tokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
|
||||
timerTask = InternalStart(tokenSource.Token);
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task StopAsync()
|
||||
{
|
||||
tokenSource.Cancel();
|
||||
await timerTask;
|
||||
}
|
||||
|
||||
private async Task InternalStart(CancellationToken cancellationToken)
|
||||
{
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
await timer.WaitForNextTickAsync(cancellationToken);
|
||||
|
||||
if (!cancellationToken.IsCancellationRequested && DateTime.Now.Minute != _last.Minute)
|
||||
{
|
||||
_last = DateTime.Now;
|
||||
foreach (ICronJob job in cronJobs)
|
||||
job.Execute(DateTime.Now, cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
36
crondotnet/CronJob.cs
Normal file
36
crondotnet/CronJob.cs
Normal file
|
@ -0,0 +1,36 @@
|
|||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace crondotnet
|
||||
{
|
||||
public delegate Task ExecuteCronJob(CancellationToken cancellationToken);
|
||||
|
||||
public interface ICronJob
|
||||
{
|
||||
Task Execute(DateTime startTime, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public class CronJob : ICronJob
|
||||
{
|
||||
private readonly ICronSchedule _cronSchedule;
|
||||
private readonly ExecuteCronJob _threadStart;
|
||||
private readonly SemaphoreSlim _semaphore = new(1);
|
||||
|
||||
public CronJob(string schedule, ExecuteCronJob threadStart)
|
||||
{
|
||||
_cronSchedule = new CronSchedule(schedule);
|
||||
_threadStart = threadStart;
|
||||
}
|
||||
|
||||
public async Task Execute(DateTime startTime, CancellationToken cancellationToken)
|
||||
{
|
||||
await _semaphore.WaitAsync(cancellationToken);
|
||||
|
||||
if (!_cronSchedule.IsTime(startTime))
|
||||
return;
|
||||
|
||||
await _threadStart(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,12 +2,12 @@ using System;
|
|||
using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace CronNET
|
||||
namespace crondotnet
|
||||
{
|
||||
public interface ICronSchedule
|
||||
{
|
||||
bool isValid(string expression);
|
||||
bool isTime(DateTime date_time);
|
||||
bool IsValid(string expression);
|
||||
bool IsTime(DateTime date_time);
|
||||
}
|
||||
|
||||
public class CronSchedule : ICronSchedule
|
||||
|
@ -41,26 +41,26 @@ namespace CronNET
|
|||
|
||||
public CronSchedule(string expressions)
|
||||
{
|
||||
this._expression = expressions;
|
||||
generate();
|
||||
_expression = expressions;
|
||||
Generate();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Public Methods
|
||||
|
||||
private bool isValid()
|
||||
private bool IsValid()
|
||||
{
|
||||
return isValid(this._expression);
|
||||
return IsValid(_expression);
|
||||
}
|
||||
|
||||
public bool isValid(string expression)
|
||||
public bool IsValid(string expression)
|
||||
{
|
||||
MatchCollection matches = validation_regex.Matches(expression);
|
||||
return matches.Count > 0;//== 5;
|
||||
}
|
||||
|
||||
public bool isTime(DateTime date_time)
|
||||
public bool IsTime(DateTime date_time)
|
||||
{
|
||||
return minutes.Contains(date_time.Minute) &&
|
||||
hours.Contains(date_time.Hour) &&
|
||||
|
@ -69,11 +69,11 @@ namespace CronNET
|
|||
days_of_week.Contains((int)date_time.DayOfWeek);
|
||||
}
|
||||
|
||||
private void generate()
|
||||
private void Generate()
|
||||
{
|
||||
if (!isValid()) return;
|
||||
if (!IsValid()) return;
|
||||
|
||||
MatchCollection matches = validation_regex.Matches(this._expression);
|
||||
MatchCollection matches = validation_regex.Matches(_expression);
|
||||
|
||||
generate_minutes(matches[0].ToString());
|
||||
|
||||
|
@ -81,17 +81,17 @@ namespace CronNET
|
|||
generate_hours(matches[1].ToString());
|
||||
else
|
||||
generate_hours("*");
|
||||
|
||||
|
||||
if (matches.Count > 2)
|
||||
generate_days_of_month(matches[2].ToString());
|
||||
else
|
||||
generate_days_of_month("*");
|
||||
|
||||
|
||||
if (matches.Count > 3)
|
||||
generate_months(matches[3].ToString());
|
||||
else
|
||||
generate_months("*");
|
||||
|
||||
|
||||
if (matches.Count > 4)
|
||||
generate_days_of_weeks(matches[4].ToString());
|
||||
else
|
||||
|
@ -100,27 +100,27 @@ namespace CronNET
|
|||
|
||||
private void generate_minutes(string match)
|
||||
{
|
||||
this.minutes = generate_values(match, 0, 60);
|
||||
minutes = generate_values(match, 0, 60);
|
||||
}
|
||||
|
||||
private void generate_hours(string match)
|
||||
{
|
||||
this.hours = generate_values(match, 0, 24);
|
||||
hours = generate_values(match, 0, 24);
|
||||
}
|
||||
|
||||
private void generate_days_of_month(string match)
|
||||
{
|
||||
this.days_of_month = generate_values(match, 1, 32);
|
||||
days_of_month = generate_values(match, 1, 32);
|
||||
}
|
||||
|
||||
private void generate_months(string match)
|
||||
{
|
||||
this.months = generate_values(match, 1, 13);
|
||||
months = generate_values(match, 1, 13);
|
||||
}
|
||||
|
||||
private void generate_days_of_weeks(string match)
|
||||
{
|
||||
this.days_of_week = generate_values(match, 0, 7);
|
||||
days_of_week = generate_values(match, 0, 7);
|
||||
}
|
||||
|
||||
private List<int> generate_values(string configuration, int start, int max)
|
||||
|
@ -207,5 +207,15 @@ namespace CronNET
|
|||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public static implicit operator CronSchedule(string pattern)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(pattern))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return new CronSchedule(pattern);
|
||||
}
|
||||
}
|
||||
}
|
14
crondotnet/crondotnet.csproj
Normal file
14
crondotnet/crondotnet.csproj
Normal file
|
@ -0,0 +1,14 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>net6;net7</TargetFrameworks>
|
||||
<OutputType>Library</OutputType>
|
||||
<RootNamespace>crondotnet</RootNamespace>
|
||||
<AssemblyName>crondotnet</AssemblyName>
|
||||
<Authors>kevinkolyar, iminet, tycoff</Authors>
|
||||
<Product>C# library for running cron jobs on .NET</Product>
|
||||
<PackageId>crondotnet</PackageId>
|
||||
<PackageProjectUrl>https://github.com/tycoff/crondotnet</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/tycoff/crondotnet</RepositoryUrl>
|
||||
<PackageTags>dotnet Cron Crondaemon Crontab</PackageTags>
|
||||
</PropertyGroup>
|
||||
</Project>
|
Loading…
Reference in New Issue
Block a user