@@ -120,10 +120,58 @@ public static IServiceCollection AddNCronJob(
120120 /// </code>
121121 /// </example>
122122 /// <returns>The modified service collection.</returns>
123+ [ Obsolete ( "This method will be dropped in the next major version. Register untyped jobs using AddNCronJob(Delegate, Action<JobOptionBuilder>) overload instead." ) ]
123124 public static IServiceCollection AddNCronJob ( this IServiceCollection services , Delegate jobDelegate , string cronExpression , TimeZoneInfo ? timeZoneInfo = null )
124- #pragma warning disable CS0618 // Type or member is obsolete
125- => services . AddNCronJob ( builder => builder . AddJob ( jobDelegate , cronExpression , timeZoneInfo ) ) ;
126- #pragma warning restore CS0618 // Type or member is obsolete
125+ => services . AddNCronJob ( jobDelegate , p => p . WithCronExpression ( cronExpression , timeZoneInfo ) ) ;
126+
127+ /// <summary>
128+ /// Adds a job using an anonymous delegate to the service collection that gets executed based on the given cron expression.
129+ /// This method allows for the scheduling of either synchronous or asynchronous tasks which are defined using lambda expressions.
130+ /// The delegate can depend on services registered in the dependency injection container, which are resolved at runtime.
131+ /// </summary>
132+ /// <param name="services">The service collection used to register the services.</param>
133+ /// <param name="jobDelegate">The delegate that represents the job to be executed. This delegate must return either void or Task.</param>
134+ /// <param name="options">The options that define how the job should be executed.</param>
135+ /// <example>
136+ /// Synchronous job example:
137+ /// <code>
138+ /// builder.Services.AddNCronJob((ILogger<Program> logger, TimeProvider timeProvider) =>
139+ /// {
140+ /// logger.LogInformation("Hello World - The current date and time is {Time}", timeProvider.GetLocalNow());
141+ /// }, p => p.WithCronExpression("*/40 * * * * *"));
142+ /// </code>
143+ /// Asynchronous job example:
144+ /// <code>
145+ /// builder.Services.AddNCronJob(async (ILogger<Program> logger, TimeProvider timeProvider, CancellationToken ct) =>
146+ /// {
147+ /// logger.LogInformation("Hello World - The current date and time is {Time}", timeProvider.GetLocalNow());
148+ /// await Task.Delay(1000, ct);
149+ /// }, p => p.WithCronExpression("*/40 * * * * *"));
150+ /// </code>
151+ /// Synchronous job with retry policy example:
152+ /// <code>
153+ /// builder.Services.AddNCronJob([RetryPolicy(retryCount: 4)] (JobExecutionContext context, ILogger<Program> logger) =>
154+ /// {
155+ /// var attemptCount = context.Attempts;
156+ /// if (attemptCount <= 4)
157+ /// {
158+ /// logger.LogWarning("TestRetryJob simulating failure.");
159+ /// throw new InvalidOperationException("Simulated operation failure in TestRetryJob.");
160+ /// }
161+ /// logger.LogInformation($"Job ran after {attemptCount} attempts");
162+ /// }, p => p.WithCronExpression("*/5 * * * * *"));
163+ /// </code>
164+ /// Synchronous job example with TimeZone:
165+ /// <code>
166+ /// builder.Services.AddNCronJob((ILogger<Program> logger, TimeProvider timeProvider) =>
167+ /// {
168+ /// logger.LogInformation("Hello World - The current date and time is {Time}", timeProvider.GetLocalNow());
169+ /// }, p => p.WithCronExpression("*/40 * * * * *", TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time")));
170+ /// </code>
171+ /// </example>
172+ /// <returns>The modified service collection.</returns>
173+ public static IServiceCollection AddNCronJob ( this IServiceCollection services , Delegate jobDelegate , Action < JobOptionBuilder > options )
174+ => services . AddNCronJob ( builder => builder . AddJob ( jobDelegate , options ) ) ;
127175
128176 /// <summary>
129177 /// Configures the host to use NCronJob. This will also start any given startup jobs and their dependencies.
0 commit comments