Skip to content

Commit bf08b3f

Browse files
committed
Cron Expression 1
1 parent 74fa792 commit bf08b3f

31 files changed

Lines changed: 587 additions & 67 deletions

src/Libraries/SmartStore.Core/Domain/Tasks/ScheduleTask.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public class ScheduleTask : BaseEntity, ICloneable<ScheduleTask>
1818
/// </summary>
1919
public string Alias { get; set; }
2020

21-
/// <summary>
22-
/// Gets or sets the run period (in seconds)
23-
/// </summary>
24-
public int Seconds { get; set; }
21+
/// <summary>
22+
/// Gets or sets the CRON expression used to calculate future schedules
23+
/// </summary>
24+
public string CronExpression { get; set; }
2525

2626
/// <summary>
2727
/// Gets or sets the type of appropriate ITask class

src/Libraries/SmartStore.Data/Mapping/Tasks/ScheduleTaskMap.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public ScheduleTaskMap()
1414
this.Property(t => t.Alias).HasMaxLength(500);
1515
this.Property(t => t.LastError).HasMaxLength(1000);
1616
this.Property(t => t.ProgressMessage).HasMaxLength(1000).IsOptional();
17+
this.Property(t => t.CronExpression).HasMaxLength(1000);
1718

1819
this.Ignore(t => t.IsRunning);
1920
this.Ignore(t => t.IsPending);

src/Libraries/SmartStore.Data/Migrations/201504270946381_TempFileCleanupTask.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void Seed(SmartObjectContext context)
2626
new ScheduleTask
2727
{
2828
Name = "Cleanup temporary files",
29-
Seconds = 86400,
29+
CronExpression = "30 3 * * *",
3030
Type = "SmartStore.Services.Common.TempFileCleanupTask, SmartStore.Services",
3131
Enabled = true,
3232
StopOnError = false

src/Libraries/SmartStore.Data/Migrations/201507102159496_TransientMedia.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Seed(SmartObjectContext context)
3838
new ScheduleTask
3939
{
4040
Name = "Clear transient uploads",
41-
Seconds = 43200, // 12 hours
41+
CronExpression = "30 1,13 * * *",
4242
Type = "SmartStore.Services.Media.TransientMediaClearTask, SmartStore.Services",
4343
Enabled = true,
4444
StopOnError = false,

src/Libraries/SmartStore.Data/Migrations/201507132241575_QueuedEmailAttachments.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void Seed(SmartObjectContext context)
5151
new ScheduleTask
5252
{
5353
Name = "Clear email queue",
54-
Seconds = 86400, // 1 day
54+
CronExpression = "0 2 * * *",
5555
Type = "SmartStore.Services.Messages.QueuedMessagesClearTask, SmartStore.Services",
5656
Enabled = true,
5757
StopOnError = false,

src/Libraries/SmartStore.Data/Migrations/201508142203054_CronExpressions.Designer.cs

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
namespace SmartStore.Data.Migrations
2+
{
3+
using System;
4+
using System.Linq;
5+
using System.Collections.Generic;
6+
using System.Data.Entity;
7+
using System.Data.Entity.Migrations;
8+
using SmartStore.Core.Domain.Tasks;
9+
using SmartStore.Data.Setup;
10+
11+
public partial class CronExpressions : DbMigration, ILocaleResourcesProvider, IDataSeeder<SmartObjectContext>
12+
{
13+
public override void Up()
14+
{
15+
AddColumn("dbo.ScheduleTask", "CronExpression", c => c.String(maxLength: 1000, defaultValue: "0 */1 * * *" /* Every hour */));
16+
DropColumn("dbo.ScheduleTask", "Seconds");
17+
}
18+
19+
public override void Down()
20+
{
21+
AddColumn("dbo.ScheduleTask", "Seconds", c => c.Int(nullable: false));
22+
DropColumn("dbo.ScheduleTask", "CronExpression");
23+
}
24+
25+
public bool RollbackOnFailure
26+
{
27+
get { return false; }
28+
}
29+
30+
public void Seed(SmartObjectContext context)
31+
{
32+
context.MigrateLocaleResources(MigrateLocaleResources);
33+
34+
// Seconds > CronExpressions
35+
var table = context.Set<ScheduleTask>();
36+
var tasks = table.ToList();
37+
38+
foreach (var task in tasks)
39+
{
40+
if (task.Type.Contains(".QueuedMessagesSendTask"))
41+
{
42+
task.CronExpression = "* * * * *"; // every Minute
43+
}
44+
else if (task.Type.Contains(".DeleteGuestsTask"))
45+
{
46+
task.CronExpression = "*/10 * * * *"; // every 10 Minutes
47+
}
48+
else if (task.Type.Contains(".ClearCacheTask"))
49+
{
50+
task.CronExpression = "0 */4 * * *"; // every 4 hrs
51+
}
52+
else if (task.Type.Contains(".UpdateExchangeRateTask"))
53+
{
54+
task.CronExpression = "0/15 * * * *"; // every 15 Minutes
55+
}
56+
else if (task.Type.Contains(".DeleteLogsTask"))
57+
{
58+
task.CronExpression = "0 1 * * *"; // At 01:00
59+
}
60+
else if (task.Type.Contains(".TransientMediaClearTask"))
61+
{
62+
task.CronExpression = "30 1,13 * * *"; // At 01:30 and 13:30
63+
}
64+
else if (task.Type.Contains(".QueuedMessagesClearTask"))
65+
{
66+
task.CronExpression = "0 2 * * *"; // At 02:00
67+
}
68+
else if (task.Type.Contains(".UpdateRatingWidgetStateTask"))
69+
{
70+
task.CronExpression = "0 3 * * *"; // At 03:00
71+
}
72+
else if (task.Type.Contains(".MailChimpSynchronizationTask"))
73+
{
74+
task.CronExpression = "0 */1 * * *"; // Every hour
75+
}
76+
else if (task.Type.Contains(".AmazonPay.DataPollingTask"))
77+
{
78+
task.CronExpression = "*/30 * * * *"; // Every 30 minutes
79+
}
80+
else if (task.Type.Contains(".NewsImportTask"))
81+
{
82+
task.CronExpression = "30 */12 * * *"; // At 30 minutes past the hour, every 12 hours
83+
}
84+
else if (task.Type.Contains(".TempFileCleanupTask"))
85+
{
86+
task.CronExpression = "30 3 * * *"; // At 03:30
87+
}
88+
else if (task.Type.Contains(".BMEcat.FileImportTask"))
89+
{
90+
task.CronExpression = "30 2 * * *"; // At 02:30
91+
}
92+
else if (task.Type.Contains(".StaticFileGenerationTask"))
93+
{
94+
task.CronExpression = "0 */6 * * *"; // Every 06 hours
95+
}
96+
else
97+
{
98+
task.CronExpression = "0 */1 * * *"; // Every hour
99+
}
100+
}
101+
102+
context.SaveChanges();
103+
}
104+
105+
public void MigrateLocaleResources(LocaleResourcesBuilder builder)
106+
{
107+
// ...
108+
}
109+
}
110+
}

src/Libraries/SmartStore.Data/Migrations/201508142203054_CronExpressions.resx

Lines changed: 126 additions & 0 deletions
Large diffs are not rendered by default.

src/Libraries/SmartStore.Data/Setup/SeedData/InvariantSeedData.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4771,59 +4771,67 @@ public IList<ScheduleTask> ScheduleTasks()
47714771
new ScheduleTask
47724772
{
47734773
Name = "Send emails",
4774-
Seconds = 60,
4774+
CronExpression = "* * * * *", // every Minute
47754775
Type = "SmartStore.Services.Messages.QueuedMessagesSendTask, SmartStore.Services",
47764776
Enabled = true,
47774777
StopOnError = false,
47784778
},
47794779
new ScheduleTask
47804780
{
47814781
Name = "Delete guests",
4782-
Seconds = 600,
4782+
CronExpression = "*/10 * * * *", // Every 10 minutes
47834783
Type = "SmartStore.Services.Customers.DeleteGuestsTask, SmartStore.Services",
47844784
Enabled = true,
47854785
StopOnError = false,
47864786
},
47874787
new ScheduleTask
47884788
{
47894789
Name = "Delete logs",
4790-
Seconds = 86400, // 1 day
4790+
CronExpression = "0 1 * * *", // At 01:00
47914791
Type = "SmartStore.Services.Logging.DeleteLogsTask, SmartStore.Services",
47924792
Enabled = true,
47934793
StopOnError = false,
47944794
},
47954795
new ScheduleTask
47964796
{
47974797
Name = "Clear cache",
4798-
Seconds = 14400, // 4 hrs
4798+
CronExpression = "0 */4 * * *", // Every 04 hours
47994799
Type = "SmartStore.Services.Caching.ClearCacheTask, SmartStore.Services",
48004800
Enabled = false,
48014801
StopOnError = false,
48024802
},
48034803
new ScheduleTask
48044804
{
48054805
Name = "Update currency exchange rates",
4806-
Seconds = 900,
4806+
CronExpression = "0/15 * * * *", // Every 15 minutes
48074807
Type = "SmartStore.Services.Directory.UpdateExchangeRateTask, SmartStore.Services",
48084808
Enabled = true,
48094809
StopOnError = false,
48104810
},
48114811
new ScheduleTask
48124812
{
48134813
Name = "Clear transient uploads",
4814-
Seconds = 43200, // 12 hours
4814+
CronExpression = "30 1,13 * * *", // At 01:30 and 13:30
48154815
Type = "SmartStore.Services.Media.TransientMediaClearTask, SmartStore.Services",
48164816
Enabled = true,
48174817
StopOnError = false,
48184818
},
48194819
new ScheduleTask
48204820
{
48214821
Name = "Clear email queue",
4822-
Seconds = 86400, // 1 day
4822+
CronExpression = "0 2 * * *", // At 02:00
48234823
Type = "SmartStore.Services.Messages.QueuedMessagesClearTask, SmartStore.Services",
48244824
Enabled = true,
48254825
StopOnError = false,
48264826
},
4827+
new ScheduleTask
4828+
{
4829+
Name = "Cleanup temporary files",
4830+
CronExpression = "30 3 * * *", // At 03:30
4831+
Type = "SmartStore.Services.Common.TempFileCleanupTask, SmartStore.Services",
4832+
Enabled = true,
4833+
StopOnError = false
4834+
}
48274835
};
48284836
this.Alter(entities);
48294837
return entities;

src/Libraries/SmartStore.Data/SmartStore.Data.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@
340340
<Compile Include="Migrations\201508121735397_AddSyncMapping.Designer.cs">
341341
<DependentUpon>201508121735397_AddSyncMapping.cs</DependentUpon>
342342
</Compile>
343+
<Compile Include="Migrations\201508142203054_CronExpressions.cs" />
344+
<Compile Include="Migrations\201508142203054_CronExpressions.Designer.cs">
345+
<DependentUpon>201508142203054_CronExpressions.cs</DependentUpon>
346+
</Compile>
343347
<Compile Include="Setup\Builder\SettingsBuilder.cs" />
344348
<Compile Include="Setup\Builder\SettingsMigrator.cs" />
345349
<Compile Include="Setup\DbMigrationContext.cs" />
@@ -642,6 +646,9 @@
642646
<EmbeddedResource Include="Migrations\201508121735397_AddSyncMapping.resx">
643647
<DependentUpon>201508121735397_AddSyncMapping.cs</DependentUpon>
644648
</EmbeddedResource>
649+
<EmbeddedResource Include="Migrations\201508142203054_CronExpressions.resx">
650+
<DependentUpon>201508142203054_CronExpressions.cs</DependentUpon>
651+
</EmbeddedResource>
645652
<EmbeddedResource Include="Sql\Indexes.sql" />
646653
<EmbeddedResource Include="Sql\StoredProcedures.sql" />
647654
</ItemGroup>

0 commit comments

Comments
 (0)