Skip to content

Conversation

@visagang
Copy link
Contributor

@visagang visagang commented Sep 9, 2025

PR Type

Enhancement


Description

  • Add configurable lock name for distributed locking

  • Update constructor to inject CrontabSettings dependency

  • Replace hardcoded lock key with dynamic key


Diagram Walkthrough

flowchart LR
  A["CrontabSettings"] --> B["LockName property"]
  B --> C["CrontabWatcher constructor"]
  C --> D["Dynamic DIST_KEY"]
  D --> E["Distributed lock operation"]
Loading

File Walkthrough

Relevant files
Configuration changes
CrontabSettings.cs
Add LockName configuration property                                           

src/Infrastructure/BotSharp.Abstraction/Crontab/Settings/CrontabSettings.cs

  • Add LockName property to CrontabSettings class
+1/-0     
Enhancement
CrontabWatcher.cs
Implement configurable distributed lock key                           

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

  • Add CrontabSettings dependency injection in constructor
  • Create dynamic DIST_KEY using configurable lock name
  • Replace hardcoded lock key with dynamic key
+6/-2     

@qodo-merge-pro
Copy link

qodo-merge-pro bot commented Sep 9, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Null Handling

If CrontabSettings is not registered or LockName is empty/whitespace, DIST_KEY falls back to "default" but whitespace isn't handled. Consider guarding against null DI and trimming/validating LockName to avoid unexpected key collisions.

public CrontabWatcher(IServiceProvider services, ILogger<CrontabWatcher> logger, CrontabSettings cronSettings)
{
    _logger = logger;
    _services = services;
    _cronSettings = cronSettings;
    DIST_KEY = $"CrontabWatcher:locker-{_cronSettings.LockName ?? "default"}";
}
Scope Reuse

The same IServiceScope is reused across the entire loop while awaiting delays and lock calls. If scoped services inside RunCronChecker rely on per-iteration lifetimes, consider creating a new scope per iteration to prevent stale dependencies or memory retention.

using (var scope = _services.CreateScope())
{
    var locker = scope.ServiceProvider.GetRequiredService<IDistributedLocker>();

    while (!stoppingToken.IsCancellationRequested)
    {
        var delay = Task.Delay(1000, stoppingToken);

        await locker.LockAsync(DIST_KEY, async () =>
        {
            await RunCronChecker(scope.ServiceProvider);
        });

        await delay;

{
public CrontabBaseSetting EventSubscriber { get; set; } = new();
public CrontabBaseSetting Watcher { get; set; } = new();
public string LockName { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you set a default name in case no settings is found.

@qodo-merge-pro
Copy link

qodo-merge-pro bot commented Sep 9, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Guard against null settings

Guard against a null cronSettings to avoid a potential NullReferenceException
when building the lock key. Initialize a default CrontabSettings when the
injected instance is null. This ensures DIST_KEY is always constructed safely.

src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabWatcher.cs [18-21]

 _logger = logger;
 _services = services;
-_cronSettings = cronSettings;
+_cronSettings = cronSettings ?? new CrontabSettings();
 DIST_KEY = $"CrontabWatcher:locker-{_cronSettings.LockName ?? "default"}";
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies that cronSettings could be null if not properly configured in the DI container, which would cause a NullReferenceException. The proposed change makes the service more robust by providing a default value.

Medium
  • Update

@Oceania2018 Oceania2018 merged commit 7a301dc into SciSharp:master Sep 9, 2025
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants