add delete cron
This commit is contained in:
parent
275aea422c
commit
0c5c0c81c5
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
Loading…
Reference in a new issue