Skip to content

Commit 4c4483c

Browse files
committed
Adding crontab with seconds
1 parent 5e5d4a8 commit 4c4483c

File tree

8 files changed

+52
-8
lines changed

8 files changed

+52
-8
lines changed

JobToolkit.Core.Standard/CronExpression.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,40 @@ public class CronExpression
1313

1414
public CronExpression(string exp)
1515
{
16-
NCrontab.Advanced.CrontabSchedule.Parse(exp);
1716
Expression = exp;
1817
}
19-
18+
19+
public DateTimeOffset GetNextTime()
20+
{
21+
return GetNextTime(DateTimeOffset.Now);
22+
}
23+
2024
public DateTimeOffset GetNextTime(DateTimeOffset after)
2125
{
22-
return NCrontab.Advanced.CrontabSchedule.Parse(Expression).GetNextOccurrence(after.DateTime);
26+
var tokens = Expression?.Split(new char[] { ' ' });
27+
if (tokens.Length == 5)
28+
return NCrontab.Advanced.CrontabSchedule.Parse(Expression).GetNextOccurrence(after.DateTime);
29+
else if (tokens.Length == 6)
30+
return NCrontab.Advanced.CrontabSchedule.Parse(Expression, NCrontab.Advanced.Enumerations.CronStringFormat.WithSeconds).GetNextOccurrence(after.DateTime);
31+
else if (tokens.Length == 7)
32+
return NCrontab.Advanced.CrontabSchedule.Parse(Expression, NCrontab.Advanced.Enumerations.CronStringFormat.WithSecondsAndYears).GetNextOccurrence(after.DateTime);
33+
else
34+
throw new ApplicationException("invalid Crontab expression.");
35+
2336
//NCrontab.CrontabSchedule.Parse("").GetNextOccurrence()
2437
}
2538

2639
public DateTimeOffset GetNextTime(DateTimeOffset after, DateTimeOffset before)
2740
{
28-
return NCrontab.Advanced.CrontabSchedule.Parse(Expression).GetNextOccurrence(after.DateTime, before.DateTime);
41+
var tokens = Expression?.Split(new char[] { ' ' });
42+
if (tokens.Length == 5)
43+
return NCrontab.Advanced.CrontabSchedule.Parse(Expression).GetNextOccurrence(after.DateTime, before.DateTime);
44+
else if (tokens.Length == 6)
45+
return NCrontab.Advanced.CrontabSchedule.Parse(Expression, NCrontab.Advanced.Enumerations.CronStringFormat.WithSeconds).GetNextOccurrence(after.DateTime, before.DateTime);
46+
else if (tokens.Length == 7)
47+
return NCrontab.Advanced.CrontabSchedule.Parse(Expression, NCrontab.Advanced.Enumerations.CronStringFormat.WithSecondsAndYears).GetNextOccurrence(after.DateTime, before.DateTime);
48+
else
49+
throw new ApplicationException("invalid Crontab expression.");
2950
}
3051

3152
public static CronExpression Parse(string exp)
@@ -49,6 +70,23 @@ public override string ToString()
4970
return Expression;
5071
}
5172

73+
/// <summary>
74+
/// Returns cron expression that fires every Second.
75+
/// </summary>
76+
public static string Secondly()
77+
{
78+
return "* * * * * *";
79+
}
80+
81+
/// <summary>
82+
/// Returns cron expression that fires every minute at the specified second.
83+
/// </summary>
84+
/// <param name="second">The second in which the schedule will be activated (0-59).</param>
85+
public static string Minutely(int second)
86+
{
87+
return String.Format("{0} * * * * *", second);
88+
}
89+
5290
/// <summary>
5391
/// Returns cron expression that fires every minute.
5492
/// </summary>

JobToolkit.Core.Standard/Job.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ public Job(JobTask task, DateTimeOffset scheduleTime, CronExpression cron, DateT
6464
this.CreateTime = DateTimeOffset.Now;
6565
}
6666

67+
public DateTimeOffset? GetNextScheduleTime()
68+
{
69+
return GetNextScheduleTime(DateTimeOffset.Now);
70+
}
71+
6772
public DateTimeOffset? GetNextScheduleTime(DateTimeOffset after)
6873
{
6974
DateTimeOffset? nextTime = null;

JobToolkit.Core.Standard/JobServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ private Task StartMainTaskAsync(CancellationToken cancellationToken)
8686
IList<Job> jobs = Repository.GetAll(criteria);
8787
if (jobs.Count <= 0)
8888
{
89-
System.Threading.Thread.Sleep(200);
89+
Task.Delay(200).GetAwaiter().GetResult();
90+
//System.Threading.Thread.Sleep(200);
9091
continue;
9192
}
9293

JobToolkit.Core.Standard/JobToolkit.Core.Standard.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<RepositoryType>GIT</RepositoryType>
1616
<PackageTags>csharp dotnet ASP.NET job dotnetcore job-scheduler background-jobs Long-Running Scheduler</PackageTags>
1717
<PackageReleaseNotes></PackageReleaseNotes>
18-
<Version>1.0.1</Version>
18+
<Version>1.0.2</Version>
1919
<RootNamespace>JobToolkit.Core</RootNamespace>
2020
<AssemblyName>JobToolkit.Core</AssemblyName>
2121
</PropertyGroup>
Binary file not shown.
Binary file not shown.

JobToolkit.NetCore.ConsoleApp/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static void Main(string[] args)
6868
Job retJob2 = jobManager.Get(job2.Id);
6969
//retJob2.DoAction();
7070

71-
Job job3 = jobManager.Schedule(() => Console.WriteLine("Exprission job {0} executed.", 3), DateTimeOffset.Now, "* * * * *", null);
71+
Job job3 = jobManager.Schedule(() => Console.WriteLine("Exprission job {0} executed.", 3), DateTimeOffset.Now, "*/30 * * * * *", null);
7272
Job retJob3 = jobManager.Get(job3.Id);
7373
//retJob3.DoAction();
7474

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Features
2222
- Custom storage support by writing custom repositories such as (MySql, PostGre Sql and ... )
2323
- Easy instalation and configuration
2424
- Supports any type of projects (Web, Win, Console, Win Service and ...)
25-
- Supports DotNet Framework, DotNet Core and Xamarin projects
25+
- Supports DotNet Framework, DotNet Core and Xamarin Forms projects
2626
- Allow Sepration between job producer and job executer on diffrent application or servers (Multiple producers and one executer)
2727

2828
Usage

0 commit comments

Comments
 (0)