mirror of
https://github.com/kevincolyar/CronNET.git
synced 2026-03-10 15:39:05 +01:00
Merges changes from Mehdi Gholam (mgholam@hotmail.com)
This commit is contained in:
22
CronNET/CronDaemon.cs
Normal file → Executable file
22
CronNET/CronDaemon.cs
Normal file → Executable file
@@ -1,30 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Timers;
|
||||
using System.Threading;
|
||||
|
||||
namespace CronNET
|
||||
{
|
||||
public class CronDaemon
|
||||
{
|
||||
private readonly Timer timer = new Timer(60000);
|
||||
private readonly System.Timers.Timer timer = new System.Timers.Timer(30000);
|
||||
private readonly List<CronJob> cron_jobs = new List<CronJob>();
|
||||
private DateTime _last= DateTime.Now;
|
||||
|
||||
public CronDaemon()
|
||||
{
|
||||
timer.AutoReset = true;
|
||||
timer.Elapsed += timer_elapsed;
|
||||
}
|
||||
|
||||
public void add_job(CronJob cron_job)
|
||||
public void AddJob(string schedule, ThreadStart action)
|
||||
{
|
||||
cron_jobs.Add(cron_job);
|
||||
var cj = new CronJob(schedule, action);
|
||||
cron_jobs.Add(cj);
|
||||
}
|
||||
|
||||
public void start()
|
||||
public void Start()
|
||||
{
|
||||
timer.Start();
|
||||
}
|
||||
|
||||
public void stop()
|
||||
public void Stop()
|
||||
{
|
||||
timer.Stop();
|
||||
|
||||
@@ -34,8 +38,12 @@ namespace CronNET
|
||||
|
||||
private void timer_elapsed(object sender, ElapsedEventArgs e)
|
||||
{
|
||||
foreach (CronJob job in cron_jobs)
|
||||
job.execute(DateTime.Now);
|
||||
if (DateTime.Now.Minute != _last.Minute)
|
||||
{
|
||||
_last = DateTime.Now;
|
||||
foreach (CronJob job in cron_jobs)
|
||||
job.execute(DateTime.Now);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
16
CronNET/CronJob.cs
Normal file → Executable file
16
CronNET/CronJob.cs
Normal file → Executable file
@@ -16,16 +16,20 @@ namespace CronNET
|
||||
_thread = new Thread(thread_start);
|
||||
}
|
||||
|
||||
private object _lock = new object();
|
||||
public void execute(DateTime date_time)
|
||||
{
|
||||
if (!_cron_schedule.is_time(date_time))
|
||||
return;
|
||||
lock (_lock)
|
||||
{
|
||||
if (!_cron_schedule.isTime(date_time))
|
||||
return;
|
||||
|
||||
if (_thread.ThreadState == ThreadState.Running)
|
||||
return;
|
||||
if (_thread.ThreadState == ThreadState.Running)
|
||||
return;
|
||||
|
||||
_thread = new Thread(_thread_start);
|
||||
_thread.Start();
|
||||
_thread = new Thread(_thread_start);
|
||||
_thread.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public void abort()
|
||||
|
||||
9
CronNET/CronNET.csproj
Normal file → Executable file
9
CronNET/CronNET.csproj
Normal file → Executable file
@@ -1,4 +1,5 @@
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<?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>
|
||||
@@ -9,6 +10,12 @@
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>Cron</RootNamespace>
|
||||
<AssemblyName>Cron</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
||||
<FileUpgradeFlags>
|
||||
</FileUpgradeFlags>
|
||||
<UpgradeBackupLocation>
|
||||
</UpgradeBackupLocation>
|
||||
<OldToolsVersion>2.0</OldToolsVersion>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
|
||||
115
CronNET/CronSchedule.cs
Normal file → Executable file
115
CronNET/CronSchedule.cs
Normal file → Executable file
@@ -9,7 +9,7 @@ namespace CronNET
|
||||
#region Readonly Class Members
|
||||
|
||||
readonly static Regex divided_regex = new Regex(@"(\*/\d+)");
|
||||
readonly static Regex range_regex = new Regex(@"(\d+\-\d+)");
|
||||
readonly static Regex range_regex = new Regex(@"(\d+\-\d+)\/?(\d+)?");
|
||||
readonly static Regex wild_regex = new Regex(@"(\*)");
|
||||
readonly static Regex list_regex = new Regex(@"(((\d+,)*\d+)+)");
|
||||
readonly static Regex validation_regex = new Regex(divided_regex + "|" + range_regex + "|" + wild_regex + "|" + list_regex);
|
||||
@@ -17,9 +17,9 @@ namespace CronNET
|
||||
#endregion
|
||||
|
||||
#region Private Instance Members
|
||||
|
||||
|
||||
private readonly string _expression;
|
||||
public List<int> minutes;
|
||||
public List<int> minutes;
|
||||
public List<int> hours;
|
||||
public List<int> days_of_month;
|
||||
public List<int> months;
|
||||
@@ -43,65 +43,81 @@ namespace CronNET
|
||||
|
||||
#region Public Methods
|
||||
|
||||
public bool is_valid()
|
||||
private bool isValid()
|
||||
{
|
||||
return is_valid(this._expression);
|
||||
return isValid(this._expression);
|
||||
}
|
||||
|
||||
public bool is_valid(string expression)
|
||||
{
|
||||
MatchCollection matches = validation_regex.Matches(expression);
|
||||
return matches.Count == 5;
|
||||
public bool isValid(string expression)
|
||||
{
|
||||
MatchCollection matches = validation_regex.Matches(expression);
|
||||
return matches.Count > 0;//== 5;
|
||||
}
|
||||
|
||||
public bool is_time(DateTime date_time)
|
||||
public bool isTime(DateTime date_time)
|
||||
{
|
||||
return minutes.Contains(date_time.Minute) &&
|
||||
hours.Contains(date_time.Hour) &&
|
||||
hours.Contains(date_time.Hour) &&
|
||||
days_of_month.Contains(date_time.Day) &&
|
||||
months.Contains(date_time.Month) &&
|
||||
days_of_week.Contains((int)date_time.DayOfWeek);
|
||||
}
|
||||
|
||||
public void generate()
|
||||
private void generate()
|
||||
{
|
||||
if(!is_valid()) return;
|
||||
if (!isValid()) return;
|
||||
|
||||
MatchCollection matches = validation_regex.Matches(this._expression);
|
||||
MatchCollection matches = validation_regex.Matches(this._expression);
|
||||
|
||||
generate_minutes(matches[0]);
|
||||
generate_hours(matches[1]);
|
||||
generate_days_of_month(matches[2]);
|
||||
generate_months(matches[3]);
|
||||
generate_days_of_weeks(matches[4]);
|
||||
generate_minutes(matches[0].ToString());
|
||||
|
||||
if (matches.Count > 1)
|
||||
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
|
||||
generate_days_of_weeks("*");
|
||||
}
|
||||
|
||||
public void generate_minutes(Match match)
|
||||
private void generate_minutes(string match)
|
||||
{
|
||||
this.minutes = generate_values(match.ToString(), 0, 60);
|
||||
this.minutes = generate_values(match, 0, 60);
|
||||
}
|
||||
|
||||
public void generate_hours(Match match)
|
||||
private void generate_hours(string match)
|
||||
{
|
||||
this.hours = generate_values(match.ToString(), 0, 24);
|
||||
this.hours = generate_values(match, 0, 24);
|
||||
}
|
||||
|
||||
public void generate_days_of_month(Match match)
|
||||
private void generate_days_of_month(string match)
|
||||
{
|
||||
this.days_of_month = generate_values(match.ToString(), 1, 32);
|
||||
this.days_of_month = generate_values(match, 1, 32);
|
||||
}
|
||||
|
||||
public void generate_months(Match match)
|
||||
private void generate_months(string match)
|
||||
{
|
||||
this.months = generate_values(match.ToString(), 1, 13);
|
||||
this.months = generate_values(match, 1, 13);
|
||||
}
|
||||
|
||||
public void generate_days_of_weeks(Match match)
|
||||
private void generate_days_of_weeks(string match)
|
||||
{
|
||||
this.days_of_week = generate_values(match.ToString(), 0, 7);
|
||||
this.days_of_week = generate_values(match, 0, 7);
|
||||
}
|
||||
|
||||
public List<int> generate_values(string configuration, int start, int max)
|
||||
private List<int> generate_values(string configuration, int start, int max)
|
||||
{
|
||||
if (divided_regex.IsMatch(configuration)) return divided_array(configuration, start, max);
|
||||
if (range_regex.IsMatch(configuration)) return range_array(configuration);
|
||||
@@ -111,31 +127,44 @@ namespace CronNET
|
||||
return new List<int>();
|
||||
}
|
||||
|
||||
public List<int> divided_array(string configuration, int start, int max)
|
||||
private List<int> divided_array(string configuration, int start, int max)
|
||||
{
|
||||
if(!divided_regex.IsMatch(configuration))
|
||||
if (!divided_regex.IsMatch(configuration))
|
||||
return new List<int>();
|
||||
|
||||
List<int> ret = new List<int>();
|
||||
string[] split = configuration.Split("/".ToCharArray());
|
||||
int divisor = int.Parse(split[1]);
|
||||
|
||||
for(int i=start; i < max; ++i)
|
||||
if(i % divisor == 0)
|
||||
ret.Add(i);
|
||||
for (int i = start; i < max; ++i)
|
||||
if (i % divisor == 0)
|
||||
ret.Add(i);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
public List<int> range_array(string configuration)
|
||||
private List<int> range_array(string configuration)
|
||||
{
|
||||
if(!range_regex.IsMatch(configuration))
|
||||
if (!range_regex.IsMatch(configuration))
|
||||
return new List<int>();
|
||||
|
||||
List<int> ret = new List<int>();
|
||||
string[] split = configuration.Split("-".ToCharArray());
|
||||
int start = int.Parse(split[0]);
|
||||
int end = int.Parse(split[1]);
|
||||
int end = 0;
|
||||
if (split[1].Contains("/"))
|
||||
{
|
||||
split = split[1].Split("/".ToCharArray());
|
||||
end = int.Parse(split[0]);
|
||||
int divisor = int.Parse(split[1]);
|
||||
|
||||
for (int i = start; i < end; ++i)
|
||||
if (i % divisor == 0)
|
||||
ret.Add(i);
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
end = int.Parse(split[1]);
|
||||
|
||||
for (int i = start; i <= end; ++i)
|
||||
ret.Add(i);
|
||||
@@ -143,9 +172,9 @@ namespace CronNET
|
||||
return ret;
|
||||
}
|
||||
|
||||
public List<int> wild_array(string configuration, int start, int max)
|
||||
private List<int> wild_array(string configuration, int start, int max)
|
||||
{
|
||||
if(!wild_regex.IsMatch(configuration))
|
||||
if (!wild_regex.IsMatch(configuration))
|
||||
return new List<int>();
|
||||
|
||||
List<int> ret = new List<int>();
|
||||
@@ -156,16 +185,16 @@ namespace CronNET
|
||||
return ret;
|
||||
}
|
||||
|
||||
public List<int> list_array(string configuration)
|
||||
private List<int> list_array(string configuration)
|
||||
{
|
||||
if(!list_regex.IsMatch(configuration))
|
||||
if (!list_regex.IsMatch(configuration))
|
||||
return new List<int>();
|
||||
|
||||
List<int> ret = new List<int>();
|
||||
|
||||
string[] split = configuration.Split(",".ToCharArray());
|
||||
|
||||
foreach(string s in split)
|
||||
foreach (string s in split)
|
||||
ret.Add(int.Parse(s));
|
||||
|
||||
return ret;
|
||||
|
||||
0
CronNET/Properties/AssemblyInfo.cs
Normal file → Executable file
0
CronNET/Properties/AssemblyInfo.cs
Normal file → Executable file
Reference in New Issue
Block a user