Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some minor changes to code #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,4 @@ pip-log.txt

# Mac crap
.DS_Store
.vs
29 changes: 10 additions & 19 deletions CronNET/CronDaemon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,45 @@
using System.Collections.Generic;
using System.Timers;
using System.Threading;
using System.Threading.Tasks;
using CronNET.Interfaces;

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 readonly System.Timers.Timer _timer = new System.Timers.Timer(10000){AutoReset = true};
private readonly List<ICronJob> _cronJobs = new List<ICronJob>();
private DateTime _last= DateTime.Now;

public CronDaemon()
{
timer.AutoReset = true;
timer.Elapsed += timer_elapsed;
_timer.Elapsed += timer_elapsed;
}

public void AddJob(string schedule, ThreadStart action)
{
var cj = new CronJob(schedule, action);
cron_jobs.Add(cj);
_cronJobs.Add(cj);
}

public void Start()
{
timer.Start();
_timer.Start();
}

public void Stop()
{
timer.Stop();

foreach (CronJob job in cron_jobs)
job.abort();
_timer.Stop();
Parallel.ForEach(_cronJobs, job => 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);
Parallel.ForEach(_cronJobs, job => job.Execute(DateTime.Now));
}
}
}
Expand Down
35 changes: 15 additions & 20 deletions CronNET/CronJob.cs
Original file line number Diff line number Diff line change
@@ -1,47 +1,42 @@
using System;
using System.Threading;
using CronNET.Interfaces;

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 readonly ICronSchedule _cronSchedule;

private readonly object _lock = new object();
private readonly ThreadStart _threadStart;
private Thread _thread;

public CronJob(string schedule, ThreadStart thread_start)
public CronJob(string schedule, ThreadStart threadStart)
{
_cron_schedule = new CronSchedule(schedule);
_thread_start = thread_start;
_thread = new Thread(thread_start);
_cronSchedule = new CronSchedule(schedule);
_threadStart = threadStart;
_thread = new Thread(threadStart);
}

private object _lock = new object();
public void execute(DateTime date_time)
public void Execute(DateTime dateTime)
{
lock (_lock)
{
if (!_cron_schedule.isTime(date_time))
if (!_cronSchedule.IsTime(dateTime))
return;

if (_thread.ThreadState == ThreadState.Running)
return;

_thread = new Thread(_thread_start);
_thread = new Thread(_threadStart);
_thread.Start();
}
}

public void abort()
public void Abort()
{
_thread.Abort();
_thread.Abort();
}

}
}
}
3 changes: 3 additions & 0 deletions CronNET/CronNET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<Compile Include="CronDaemon.cs" />
<Compile Include="CronJob.cs" />
<Compile Include="CronSchedule.cs" />
<Compile Include="Interfaces\ICronSchedule.cs" />
<Compile Include="Interfaces\ICronDaemon.cs" />
<Compile Include="Interfaces\ICronJob.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
Expand Down
124 changes: 58 additions & 66 deletions CronNET/CronSchedule.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using CronNET.Interfaces;

namespace CronNET
{
public interface ICronSchedule
{
bool isValid(string expression);
bool isTime(DateTime date_time);
}

public class CronSchedule : ICronSchedule
{
#region Readonly Class Members

readonly static Regex divided_regex = new Regex(@"(\*/\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);
static readonly Regex DividedRegex = new Regex(@"(\*/\d+)");
static readonly Regex RangeRegex = new Regex(@"(\d+\-\d+)\/?(\d+)?");
static readonly Regex WildRegex = new Regex(@"(\*)");
static readonly Regex ListRegex = new Regex(@"(((\d+,)*\d+)+)");
static readonly Regex ValidationRegex = new Regex(DividedRegex + "|" + RangeRegex + "|" + WildRegex + "|" + ListRegex);

#endregion

#region Private Instance Members

private readonly string _expression;
public List<int> minutes;
public List<int> hours;
public List<int> days_of_month;
public List<int> months;
public List<int> days_of_week;
private List<int> _minutes;
private List<int> _hours;
private List<int> _daysOfMonth;
private List<int> _months;
private List<int> _daysOfWeek;

#endregion

Expand All @@ -41,101 +36,99 @@ public CronSchedule()

public CronSchedule(string expressions)
{
this._expression = expressions;
generate();
_expression = expressions;
Generate();
}

public List<int> Minutes => _minutes;

public List<int> Hours => _hours;

public List<int> DaysOfMonth => _daysOfMonth;

public List<int> Months => _months;

public List<int> DaysOfWeek => _daysOfWeek;

#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);
MatchCollection matches = ValidationRegex.Matches(expression);
return matches.Count > 0;//== 5;
}

public bool isTime(DateTime date_time)
public bool IsTime(DateTime dateTime)
{
return minutes.Contains(date_time.Minute) &&
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);
return _minutes.Contains(dateTime.Minute) &&
_hours.Contains(dateTime.Hour) &&
_daysOfMonth.Contains(dateTime.Day) &&
_months.Contains(dateTime.Month) &&
_daysOfWeek.Contains((int)dateTime.DayOfWeek);
}

private void generate()
private void Generate()
{
if (!isValid()) return;
if (!IsValid()) return;

MatchCollection matches = validation_regex.Matches(this._expression);
MatchCollection matches = ValidationRegex.Matches(_expression);

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("*");
generate_hours(matches.Count > 1 ? matches[1].ToString() : "*");

generate_days_of_month(matches.Count > 2 ? matches[2].ToString() : "*");

generate_months(matches.Count > 3 ? matches[3].ToString() : "*");

generate_days_of_weeks(matches.Count > 4 ? matches[4].ToString() : "*");
}

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);
_daysOfMonth = 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);
_daysOfWeek = generate_values(match, 0, 7);
}

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);
if (wild_regex.IsMatch(configuration)) return wild_array(configuration, start, max);
if (list_regex.IsMatch(configuration)) return list_array(configuration);
if (DividedRegex.IsMatch(configuration)) return divided_array(configuration, start, max);
if (RangeRegex.IsMatch(configuration)) return range_array(configuration);
if (WildRegex.IsMatch(configuration)) return wild_array(configuration, start, max);
if (ListRegex.IsMatch(configuration)) return list_array(configuration);

return new List<int>();
}

private List<int> divided_array(string configuration, int start, int max)
{
if (!divided_regex.IsMatch(configuration))
if (!DividedRegex.IsMatch(configuration))
return new List<int>();

List<int> ret = new List<int>();
Expand All @@ -151,13 +144,13 @@ private List<int> divided_array(string configuration, int start, int max)

private List<int> range_array(string configuration)
{
if (!range_regex.IsMatch(configuration))
if (!RangeRegex.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 = 0;
int end;
if (split[1].Contains("/"))
{
split = split[1].Split("/".ToCharArray());
Expand All @@ -169,8 +162,7 @@ private List<int> range_array(string configuration)
ret.Add(i);
return ret;
}
else
end = int.Parse(split[1]);
end = int.Parse(split[1]);

for (int i = start; i <= end; ++i)
ret.Add(i);
Expand All @@ -180,7 +172,7 @@ private List<int> range_array(string configuration)

private List<int> wild_array(string configuration, int start, int max)
{
if (!wild_regex.IsMatch(configuration))
if (!WildRegex.IsMatch(configuration))
return new List<int>();

List<int> ret = new List<int>();
Expand All @@ -193,7 +185,7 @@ private List<int> wild_array(string configuration, int start, int max)

private List<int> list_array(string configuration)
{
if (!list_regex.IsMatch(configuration))
if (!ListRegex.IsMatch(configuration))
return new List<int>();

List<int> ret = new List<int>();
Expand Down
11 changes: 11 additions & 0 deletions CronNET/Interfaces/ICronDaemon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading;

namespace CronNET.Interfaces
{
public interface ICronDaemon
{
void AddJob(string schedule, ThreadStart action);
void Start();
void Stop();
}
}
Loading