optimze crontab identity · SciSharp/BotSharp@2a237fb · GitHub
Skip to content

Commit 2a237fb

Browse files
committed
optimze crontab identity
1 parent 0f0ac75 commit 2a237fb

6 files changed

Lines changed: 88 additions & 38 deletions

File tree

‎BotSharp.sln‎

Lines changed: 15 additions & 15 deletions

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,26 @@ public async Task ScheduledTimeArrived(CrontabItem item)
126126
return;
127127
}
128128

129-
await HookEmitter.Emit<ICrontabHook>(_services, async hook =>
129+
// Phase 1 - Authenticate. Use the SYNCHRONOUS Emit overload: OnAuthenticate runs as a plain
130+
// synchronous delegate call (no async state machine, so no ExecutionContext capture/restore).
131+
// The identity a hook writes to the ambient AsyncLocal (IHttpContextAccessor /
132+
// Thread.CurrentPrincipal) therefore persists in this method's context and flows DOWN into the
133+
// Phase 2 OnCronTriggered awaits below. If authentication ran inside the awaited (async) Emit
134+
// overload, the async state machine would restore the ExecutionContext on each hook's
135+
// completion and discard the identity before the work runs.
136+
HookEmitter.Emit<ICrontabHook>(_services, hook =>
130137
{
131138
if (hook.Triggers == null || hook.Triggers.Contains(item.Title))
132139
{
133140
hook.OnAuthenticate(item);
141+
}
142+
}, item.AgentId);
143+
144+
// Phase 2 - Execute.
145+
await HookEmitter.Emit<ICrontabHook>(_services, async hook =>
146+
{
147+
if (hook.Triggers == null || hook.Triggers.Contains(item.Title))
148+
{
134149
await hook.OnTaskExecuting(item);
135150
await hook.OnCronTriggered(item);
136151
await hook.OnTaskExecuted(item);

BotSharp.Core.UnitTests/BotSharp.Core.UnitTests.csproj renamed to tests/BotSharp.Core.UnitTests/BotSharp.Core.UnitTests.csproj

File renamed without changes.

BotSharp.Core.UnitTests/Crontab/CrontabIdentityFlowTests.cs renamed to tests/BotSharp.Core.UnitTests/Crontab/CrontabIdentityFlowTests.cs

Lines changed: 57 additions & 22 deletions

BotSharp.Core.UnitTests/Infrastructures/SettingServiceTests.cs renamed to tests/BotSharp.Core.UnitTests/Infrastructures/SettingServiceTests.cs

File renamed without changes.

BotSharp.Core.UnitTests/Routing/RoutingRuleTests.cs renamed to tests/BotSharp.Core.UnitTests/Routing/RoutingRuleTests.cs

File renamed without changes.

0 commit comments

Comments
 (0)