Merge pull request #790 from iceljc/feature/refine-cronttab

Feature/refine cronttab
This commit is contained in:
iceljc 2024-12-10 15:17:34 -06:00 committed by GitHub
commit 615f53136e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 77 additions and 20 deletions

View file

@ -147,7 +147,8 @@ public interface IBotSharpRepository : IHaveServiceProvider
#endregion
#region Crontab
bool InsertCrontabItem(CrontabItem cron) => throw new NotImplementedException();
bool UpsertCrontabItem(CrontabItem cron) => throw new NotImplementedException();
bool DeleteCrontabItem(string conversationId) => throw new NotImplementedException();
PagedItems<CrontabItem> GetCrontabItems(CrontabItemFilter filter) => throw new NotImplementedException();
#endregion
}

View file

@ -33,7 +33,7 @@ public class ScheduleTaskFn : IFunctionCallback
};
var db = _services.GetRequiredService<IBotSharpRepository>();
// var ret = db.InsertCrontabItem(crontabItem);
// var ret = db.UpsertCrontabItem(crontabItem);
return true;
}

View file

@ -46,24 +46,32 @@ public class CrontabWatcher : BackgroundService
var crons = await cron.GetCrontable();
foreach (var item in crons)
{
var schedule = CrontabSchedule.Parse(item.Cron, new CrontabSchedule.ParseOptions
try
{
IncludingSeconds = true // Ensure you account for seconds
});
var schedule = CrontabSchedule.Parse(item.Cron, new CrontabSchedule.ParseOptions
{
IncludingSeconds = true // Ensure you account for seconds
});
// Get the current time
var currentTime = DateTime.UtcNow;
// Get the current time
var currentTime = DateTime.UtcNow;
// Get the next occurrence from the schedule
var nextOccurrence = schedule.GetNextOccurrence(currentTime.AddSeconds(-1));
// Get the next occurrence from the schedule
var nextOccurrence = schedule.GetNextOccurrence(currentTime.AddSeconds(-1));
// Check if the current time matches the schedule
bool matches = currentTime >= nextOccurrence && currentTime < nextOccurrence.AddSeconds(1);
// Check if the current time matches the schedule
bool matches = currentTime >= nextOccurrence && currentTime < nextOccurrence.AddSeconds(1);
if (matches)
if (matches)
{
_logger.LogDebug($"The current time matches the cron expression {item}");
cron.ScheduledTimeArrived(item);
}
}
catch (Exception ex)
{
_logger.LogDebug($"The current time matches the cron expression {item}");
cron.ScheduledTimeArrived(item);
_logger.LogWarning($"Error when running cron task ({item.ConversationId}, {item.Title}, {item.Cron}): {ex.Message}\r\n{ex.InnerException}");
continue;
}
}
}

View file

@ -1,11 +1,10 @@
using BotSharp.Abstraction.Crontab.Models;
using System.IO;
namespace BotSharp.Core.Repository;
public partial class FileRepository
{
public bool InsertCrontabItem(CrontabItem cron)
public bool UpsertCrontabItem(CrontabItem cron)
{
if (cron == null || string.IsNullOrWhiteSpace(cron.ConversationId))
{
@ -32,6 +31,37 @@ public partial class FileRepository
}
}
public bool DeleteCrontabItem(string conversationId)
{
if (string.IsNullOrWhiteSpace(conversationId))
{
return false;
}
try
{
var baseDir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversationId);
if (!Directory.Exists(baseDir))
{
return false;
}
var cronFile = Path.Combine(baseDir, CRON_FILE);
if (!File.Exists(cronFile))
{
return false;
}
File.Delete(cronFile);
return true;
}
catch (Exception ex)
{
_logger.LogError($"Error when deleting crontab item (${conversationId}): {ex.Message}\r\n{ex.InnerException}");
return false;
}
}
public PagedItems<CrontabItem> GetCrontabItems(CrontabItemFilter filter)
{

View file

@ -5,9 +5,9 @@ namespace BotSharp.Plugin.MongoStorage.Repository;
public partial class MongoRepository
{
public bool InsertCrontabItem(CrontabItem item)
public bool UpsertCrontabItem(CrontabItem item)
{
if (item == null)
if (item == null || string.IsNullOrWhiteSpace(item.ConversationId))
{
return false;
}
@ -16,7 +16,12 @@ public partial class MongoRepository
{
var cronDoc = CrontabItemDocument.ToMongoModel(item);
cronDoc.Id = Guid.NewGuid().ToString();
_dc.CrontabItems.InsertOne(cronDoc);
var filter = Builders<CrontabItemDocument>.Filter.Eq(x => x.ConversationId, item.ConversationId);
var result = _dc.CrontabItems.ReplaceOne(filter, cronDoc, new ReplaceOptions
{
IsUpsert = true
});
return true;
}
catch (Exception ex)
@ -27,6 +32,19 @@ public partial class MongoRepository
}
public bool DeleteCrontabItem(string conversationId)
{
if (string.IsNullOrWhiteSpace(conversationId))
{
return false;
}
var filter = Builders<CrontabItemDocument>.Filter.Eq(x => x.ConversationId, conversationId);
var result = _dc.CrontabItems.DeleteMany(filter);
return result.DeletedCount > 0;
}
public PagedItems<CrontabItem> GetCrontabItems(CrontabItemFilter filter)
{
if (filter == null)
@ -37,7 +55,7 @@ public partial class MongoRepository
var cronBuilder = Builders<CrontabItemDocument>.Filter;
var cronFilters = new List<FilterDefinition<CrontabItemDocument>>() { cronBuilder.Empty };
// Filter conversations
// Filter cron
if (filter?.AgentIds != null)
{
cronFilters.Add(cronBuilder.In(x => x.AgentId, filter.AgentIds));