Skip to content

Commit 615f531

Browse files
authored
Merge pull request #790 from iceljc/feature/refine-cronttab
Feature/refine cronttab
2 parents ba96443 + 57a275d commit 615f531

File tree

5 files changed

+77
-20
lines changed

5 files changed

+77
-20
lines changed

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ public interface IBotSharpRepository : IHaveServiceProvider
147147
#endregion
148148

149149
#region Crontab
150-
bool InsertCrontabItem(CrontabItem cron) => throw new NotImplementedException();
150+
bool UpsertCrontabItem(CrontabItem cron) => throw new NotImplementedException();
151+
bool DeleteCrontabItem(string conversationId) => throw new NotImplementedException();
151152
PagedItems<CrontabItem> GetCrontabItems(CrontabItemFilter filter) => throw new NotImplementedException();
152153
#endregion
153154
}

src/Infrastructure/BotSharp.Core.Crontab/Functions/ScheduleTaskFn.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public async Task<bool> Execute(RoleDialogModel message)
3333
};
3434

3535
var db = _services.GetRequiredService<IBotSharpRepository>();
36-
// var ret = db.InsertCrontabItem(crontabItem);
36+
// var ret = db.UpsertCrontabItem(crontabItem);
3737

3838
return true;
3939
}

src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,32 @@ private async Task RunCronChecker(IServiceProvider services)
4646
var crons = await cron.GetCrontable();
4747
foreach (var item in crons)
4848
{
49-
var schedule = CrontabSchedule.Parse(item.Cron, new CrontabSchedule.ParseOptions
49+
try
5050
{
51-
IncludingSeconds = true // Ensure you account for seconds
52-
});
51+
var schedule = CrontabSchedule.Parse(item.Cron, new CrontabSchedule.ParseOptions
52+
{
53+
IncludingSeconds = true // Ensure you account for seconds
54+
});
5355

54-
// Get the current time
55-
var currentTime = DateTime.UtcNow;
56+
// Get the current time
57+
var currentTime = DateTime.UtcNow;
5658

57-
// Get the next occurrence from the schedule
58-
var nextOccurrence = schedule.GetNextOccurrence(currentTime.AddSeconds(-1));
59+
// Get the next occurrence from the schedule
60+
var nextOccurrence = schedule.GetNextOccurrence(currentTime.AddSeconds(-1));
5961

60-
// Check if the current time matches the schedule
61-
bool matches = currentTime >= nextOccurrence && currentTime < nextOccurrence.AddSeconds(1);
62+
// Check if the current time matches the schedule
63+
bool matches = currentTime >= nextOccurrence && currentTime < nextOccurrence.AddSeconds(1);
6264

63-
if (matches)
65+
if (matches)
66+
{
67+
_logger.LogDebug($"The current time matches the cron expression {item}");
68+
cron.ScheduledTimeArrived(item);
69+
}
70+
}
71+
catch (Exception ex)
6472
{
65-
_logger.LogDebug($"The current time matches the cron expression {item}");
66-
cron.ScheduledTimeArrived(item);
73+
_logger.LogWarning($"Error when running cron task ({item.ConversationId}, {item.Title}, {item.Cron}): {ex.Message}\r\n{ex.InnerException}");
74+
continue;
6775
}
6876
}
6977
}

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Crontab.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
using BotSharp.Abstraction.Crontab.Models;
21
using System.IO;
32

43
namespace BotSharp.Core.Repository;
54

65
public partial class FileRepository
76
{
8-
public bool InsertCrontabItem(CrontabItem cron)
7+
public bool UpsertCrontabItem(CrontabItem cron)
98
{
109
if (cron == null || string.IsNullOrWhiteSpace(cron.ConversationId))
1110
{
@@ -32,6 +31,37 @@ public bool InsertCrontabItem(CrontabItem cron)
3231
}
3332
}
3433

34+
public bool DeleteCrontabItem(string conversationId)
35+
{
36+
if (string.IsNullOrWhiteSpace(conversationId))
37+
{
38+
return false;
39+
}
40+
41+
try
42+
{
43+
var baseDir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversationId);
44+
if (!Directory.Exists(baseDir))
45+
{
46+
return false;
47+
}
48+
49+
var cronFile = Path.Combine(baseDir, CRON_FILE);
50+
if (!File.Exists(cronFile))
51+
{
52+
return false;
53+
}
54+
55+
File.Delete(cronFile);
56+
return true;
57+
}
58+
catch (Exception ex)
59+
{
60+
_logger.LogError($"Error when deleting crontab item (${conversationId}): {ex.Message}\r\n{ex.InnerException}");
61+
return false;
62+
}
63+
}
64+
3565

3666
public PagedItems<CrontabItem> GetCrontabItems(CrontabItemFilter filter)
3767
{

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Crontab.cs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace BotSharp.Plugin.MongoStorage.Repository;
55

66
public partial class MongoRepository
77
{
8-
public bool InsertCrontabItem(CrontabItem item)
8+
public bool UpsertCrontabItem(CrontabItem item)
99
{
10-
if (item == null)
10+
if (item == null || string.IsNullOrWhiteSpace(item.ConversationId))
1111
{
1212
return false;
1313
}
@@ -16,7 +16,12 @@ public bool InsertCrontabItem(CrontabItem item)
1616
{
1717
var cronDoc = CrontabItemDocument.ToMongoModel(item);
1818
cronDoc.Id = Guid.NewGuid().ToString();
19-
_dc.CrontabItems.InsertOne(cronDoc);
19+
20+
var filter = Builders<CrontabItemDocument>.Filter.Eq(x => x.ConversationId, item.ConversationId);
21+
var result = _dc.CrontabItems.ReplaceOne(filter, cronDoc, new ReplaceOptions
22+
{
23+
IsUpsert = true
24+
});
2025
return true;
2126
}
2227
catch (Exception ex)
@@ -27,6 +32,19 @@ public bool InsertCrontabItem(CrontabItem item)
2732
}
2833

2934

35+
public bool DeleteCrontabItem(string conversationId)
36+
{
37+
if (string.IsNullOrWhiteSpace(conversationId))
38+
{
39+
return false;
40+
}
41+
42+
var filter = Builders<CrontabItemDocument>.Filter.Eq(x => x.ConversationId, conversationId);
43+
var result = _dc.CrontabItems.DeleteMany(filter);
44+
return result.DeletedCount > 0;
45+
}
46+
47+
3048
public PagedItems<CrontabItem> GetCrontabItems(CrontabItemFilter filter)
3149
{
3250
if (filter == null)
@@ -37,7 +55,7 @@ public PagedItems<CrontabItem> GetCrontabItems(CrontabItemFilter filter)
3755
var cronBuilder = Builders<CrontabItemDocument>.Filter;
3856
var cronFilters = new List<FilterDefinition<CrontabItemDocument>>() { cronBuilder.Empty };
3957

40-
// Filter conversations
58+
// Filter cron
4159
if (filter?.AgentIds != null)
4260
{
4361
cronFilters.Add(cronBuilder.In(x => x.AgentId, filter.AgentIds));

0 commit comments

Comments
 (0)