Skip to content

Commit dc87153

Browse files
authored
Merge pull request #793 from hchen2020/master
crontab only allow 1 minute.
2 parents 93f156d + 0f9a01c commit dc87153

File tree

11 files changed

+78
-57
lines changed

11 files changed

+78
-57
lines changed

src/Infrastructure/BotSharp.Abstraction/Crontab/Models/ScheduleTaskArgs.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ public class ScheduleTaskArgs
55
[JsonPropertyName("cron_expression")]
66
public string Cron { get; set; } = null!;
77

8+
[JsonPropertyName("less_than_60_seconds")]
9+
public bool LessThan60Seconds { get; set; } = false;
10+
811
[JsonPropertyName("title")]
912
public string Title { get; set; } = null!;
1013

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

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,37 @@ public ScheduleTaskFn(IServiceProvider services)
1818
public async Task<bool> Execute(RoleDialogModel message)
1919
{
2020
var args = JsonSerializer.Deserialize<ScheduleTaskArgs>(message.FunctionArgs);
21+
if (args.LessThan60Seconds)
22+
{
23+
message.Content = "Cron expression should not include seconds.";
24+
return false;
25+
}
2126

2227
var routing = _services.GetRequiredService<IRoutingContext>();
2328
var user = _services.GetRequiredService<IUserIdentity>();
24-
var crontabItem = new CrontabItem
29+
var db = _services.GetRequiredService<IBotSharpRepository>();
30+
31+
if (string.IsNullOrEmpty(args.Cron))
2532
{
26-
Title = args.Title,
27-
Description = args.Description,
28-
Cron = args.Cron,
29-
UserId = user.Id,
30-
AgentId = routing.EntryAgentId,
31-
ConversationId = routing.ConversationId,
32-
Tasks = args.Tasks,
33-
};
33+
var ret = db.DeleteCrontabItem(routing.ConversationId);
34+
message.Content = $"Task schedule canceled result: {ret}";
35+
}
36+
else
37+
{
38+
var crontabItem = new CrontabItem
39+
{
40+
Title = args.Title,
41+
Description = args.Description,
42+
Cron = args.Cron,
43+
UserId = user.Id,
44+
AgentId = routing.EntryAgentId,
45+
ConversationId = routing.ConversationId,
46+
Tasks = args.Tasks,
47+
};
3448

35-
var db = _services.GetRequiredService<IBotSharpRepository>();
36-
// var ret = db.UpsertCrontabItem(crontabItem);
49+
var ret = db.UpsertCrontabItem(crontabItem);
50+
message.Content = $"Task scheduled result: {ret}";
51+
}
3752

3853
return true;
3954
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using BotSharp.Abstraction.Repositories;
18+
using BotSharp.Core.Infrastructures;
1819
using Microsoft.Extensions.Logging;
1920

2021
namespace BotSharp.Core.Crontab.Services;
@@ -45,14 +46,9 @@ public async Task ScheduledTimeArrived(CrontabItem item)
4546
{
4647
_logger.LogDebug($"ScheduledTimeArrived {item}");
4748

48-
var hooks = _services.GetServices<ICrontabHook>();
49-
foreach(var hook in hooks)
50-
{
51-
await hook.OnCronTriggered(item);
52-
}
53-
/*await HookEmitter.Emit<ICrontabHook>(_services, async hook =>
49+
await HookEmitter.Emit<ICrontabHook>(_services, async hook =>
5450
await hook.OnCronTriggered(item)
55-
);*/
51+
);
5652
await Task.Delay(1000 * 10);
5753
}
5854
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ private async Task RunCronChecker(IServiceProvider services)
4848
{
4949
try
5050
{
51+
// strip seconds from cron expression
52+
item.Cron = string.Join(" ", item.Cron.Split(' ').TakeLast(5));
5153
var schedule = CrontabSchedule.Parse(item.Cron, new CrontabSchedule.ParseOptions
5254
{
53-
IncludingSeconds = true // Ensure you account for seconds
55+
IncludingSeconds = false // Ensure you account for seconds
5456
});
5557

5658
// Get the current time

src/Infrastructure/BotSharp.Core.Crontab/data/agents/6745151e-6d46-4a02-8de4-1c4f21c7da95/functions/util-crontab-schedule_task.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
{
22
"name": "util-crontab-schedule_task",
3-
"description": "Set up a scheduled task",
3+
"description": "Set up or cancel a scheduled task",
44
"parameters": {
55
"type": "object",
66
"properties": {
77
"cron_expression": {
88
"type": "string",
9-
"description": "cron expression include seconds"
9+
"description": "cron expression. Set value as empty if user wants to cancel schedule."
10+
},
11+
"less_than_60_seconds": {
12+
"type": "boolean",
13+
"description": "whether schedule interval is less than 60 seconds"
1014
},
1115
"title": {
1216
"type": "string",
@@ -39,6 +43,6 @@
3943
}
4044
}
4145
},
42-
"required": [ "cron_expression", "title", "description", "to_do_list" ]
46+
"required": [ "cron_expression", "include_seconds", "title", "description", "to_do_list" ]
4347
}
4448
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Call schedule_task if user needs to set up a scheduled task with appropriate programming script and language type.
1+
Call util-crontab-schedule_task if user needs to set up a scheduled task with appropriate programming script and language type.
2+
Set cron_expression as empty if user wants to cancel schedule.

src/Infrastructure/BotSharp.Core/data/agents/01fcc3e5-9af7-49e6-ad7a-a760bd12dc4a/agent.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
{
1818
"type": "planner",
19-
"field": "Sequential-Planner"
19+
"field": "Two-Stage-Planner"
2020
}
2121
]
2222
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Analyze the user's problem. Which prerequisite task needs to be completed? Output the next step of routing instructions.
2-
Check the job responsibilities of the routable Agent and do not transfer to an Agent that exceeds the scope of responsibility.
2+
Check the job responsibilities of the routable Agent and do not transfer to an Agent that exceeds the scope of responsibility.
3+
If the user request may require other tools or services, route to the planner agent.

src/Plugins/BotSharp.Plugin.Planner/data/agents/3e75e818-a139-48a8-9e22-4662548c13a3/agent.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "3e75e818-a139-48a8-9e22-4662548c13a3",
33
"name": "Sequential-Planner",
4-
"description": "Plan an ordered plan steps to execute some tasks in a predefined order by the user",
4+
"description": "Plan an execution steps for the user complex task, especially if the tasks are in a predefined order by the user",
55
"type": "planning",
66
"createdDateTime": "2023-08-27T10:39:00Z",
77
"updatedDateTime": "2023-08-27T14:39:00Z",

src/Plugins/BotSharp.Plugin.SqlDriver/Helpers/SqlDriverHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ internal static class SqlDriverHelper
55
internal static string GetDatabaseType(IServiceProvider services)
66
{
77
var settings = services.GetRequiredService<SqlDriverSetting>();
8-
var dbType = "MySQL";
8+
var dbType = "mysql";
99

1010
if (!string.IsNullOrWhiteSpace(settings?.SqlServerConnectionString))
1111
{
12-
dbType = "SQL Server";
12+
dbType = "sqlserver";
1313
}
1414
else if (!string.IsNullOrWhiteSpace(settings?.SqlLiteConnectionString))
1515
{
16-
dbType = "SQL Lite";
16+
dbType = "sqllite";
1717
}
1818
return dbType;
1919
}

0 commit comments

Comments
 (0)