Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add property Name to ITask #24

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static void AddHostedScheduler(this IServiceCollection serviceCollection, IConfiguration configuration)
public static IServiceCollection AddHostedScheduler(this IServiceCollection serviceCollection, IConfiguration configuration)
{
// Register services
serviceCollection.AddScheduler(configuration);

// Add hosted service
serviceCollection.AddHostedService<HostedSchedulerService>();

return serviceCollection;
}

public static void AddHostedScheduler(this IServiceCollection serviceCollection, Action<SchedulerOptions> options = null)
public static IServiceCollection AddHostedScheduler(this IServiceCollection serviceCollection, Action<SchedulerOptions> options = null)
{
// Register services
serviceCollection.AddScheduler(options);

// Add hosted service
serviceCollection.AddHostedService<HostedSchedulerService>();

return serviceCollection;
}
}
}
34 changes: 23 additions & 11 deletions NCrontab.Scheduler/AsyncScheduledTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,41 @@

namespace NCrontab.Scheduler
{
public class AsyncScheduledTask : IAsyncScheduledTask
public class AsyncScheduledTask : TaskBase, IAsyncScheduledTask
{
private readonly Func<CancellationToken, Task> action;
private readonly Func<CancellationToken, Task> task;

public AsyncScheduledTask(CrontabSchedule cronExpression, Func<CancellationToken, Task> action)
: this(Guid.NewGuid(), cronExpression, action)
public AsyncScheduledTask(string cronExpression, Func<CancellationToken, Task> task = null)
: this(CrontabSchedule.Parse(cronExpression), task)
{
}

public AsyncScheduledTask(Guid id, CrontabSchedule crontabSchedule, Func<CancellationToken, Task> action)
public AsyncScheduledTask(CrontabSchedule crontabSchedule, Func<CancellationToken, Task> task = null)
: this(Guid.NewGuid(), crontabSchedule, task)
{
this.Id = id;
this.CrontabSchedule = crontabSchedule;
this.action = action;
}

public Guid Id { get; }
public AsyncScheduledTask(Guid id, CrontabSchedule crontabSchedule, Func<CancellationToken, Task> task = null)
: this(id, null, crontabSchedule, task)
{
this.task = task;
}

public CrontabSchedule CrontabSchedule { get; set; }
public AsyncScheduledTask(string name, CrontabSchedule crontabSchedule, Func<CancellationToken, Task> task = null)
: this(Guid.NewGuid(), name, crontabSchedule, task)
{
this.task = task;
}

public AsyncScheduledTask(Guid id, string name, CrontabSchedule crontabSchedule, Func<CancellationToken, Task> task = null)
: base(id, name, crontabSchedule)
{
this.task = task;
}

public Task RunAsync(CancellationToken cancellationToken)
{
return this.action(cancellationToken);
return this.task != null ? this.task(cancellationToken) : Task.CompletedTask;
}
}
}
66 changes: 53 additions & 13 deletions NCrontab.Scheduler/Extensions/SchedulerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ public static void Start(this IScheduler scheduler, CancellationToken cancellati
Task.Run(() => scheduler.StartAsync(cancellationToken));
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
/// <param name="cronExpression">The cron expression.</param>
/// <returns>The task identifier.</returns>
public static Guid AddTask(this IScheduler scheduler, string cronExpression)
{
return scheduler.AddTask(cronExpression, action: null);
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
Expand All @@ -32,6 +42,16 @@ public static Guid AddTask(this IScheduler scheduler, string cronExpression, Act
return scheduler.AddTask(crontabSchedule, action);
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
/// <param name="crontabSchedule">The crontab schedule.</param>
/// <returns>The task identifier.</returns>
public static Guid AddTask(this IScheduler scheduler, CrontabSchedule crontabSchedule)
{
return scheduler.AddTask(crontabSchedule, action: null);
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
Expand All @@ -47,18 +67,38 @@ public static Guid AddTask(this IScheduler scheduler, CrontabSchedule crontabSch
return taskId;
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
/// <param name="taskId">The task identifier.</param>
/// <param name="cronExpression">The cron expression.</param>
public static void AddTask(this IScheduler scheduler, Guid taskId, string cronExpression)
{
scheduler.AddTask(taskId, cronExpression, action: null);
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
/// <param name="taskId">The task identifier.</param>
/// <param name="cronExpression">The cron expression.</param>
/// <param name="action">The callback action which is called whenever the <paramref name="cronExpression"/> is planned to execute.</param>
public static void AddTask(this IScheduler scheduler, Guid taskId, string cronExpression, Action<CancellationToken> action)
public static void AddTask(this IScheduler scheduler, Guid taskId, string cronExpression, Action<CancellationToken> action = null)
{
var crontabSchedule = CrontabSchedule.Parse(cronExpression);
scheduler.AddTask(taskId, crontabSchedule, action);
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
/// <param name="taskId">The task identifier.</param>
/// <param name="crontabSchedule">The crontab schedule.</param>
public static void AddTask(this IScheduler scheduler, Guid taskId, CrontabSchedule crontabSchedule)
{
scheduler.AddTask(taskId, crontabSchedule, action: null);
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
Expand All @@ -75,25 +115,25 @@ public static void AddTask(this IScheduler scheduler, Guid taskId, CrontabSchedu
/// Adds a task to the scheduler.
/// </summary>
/// <param name="cronExpression">The cron expression.</param>
/// <param name="action">The callback action which is called whenever the <paramref name="cronExpression"/> is planned to execute.</param>
/// <param name="task">The callback action which is called whenever the <paramref name="cronExpression"/> is planned to execute.</param>
/// <returns>The task identifier.</returns>
public static Guid AddTask(this IScheduler scheduler, string cronExpression, Func<CancellationToken, Task> action)
public static Guid AddTask(this IScheduler scheduler, string cronExpression, Func<CancellationToken, Task> task)
{
var crontabSchedule = CrontabSchedule.Parse(cronExpression);
return scheduler.AddTask(crontabSchedule, action);
return scheduler.AddTask(crontabSchedule, task);
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
/// <param name="crontabSchedule">The crontab schedule.</param>
/// <param name="action">The callback action which is called whenever the <paramref name="crontabSchedule"/> is planned to execute.</param>
/// <param name="task">The callback action which is called whenever the <paramref name="crontabSchedule"/> is planned to execute.</param>
/// <returns>The task identifier.</returns>
public static Guid AddTask(this IScheduler scheduler, CrontabSchedule crontabSchedule, Func<CancellationToken, Task> action)
public static Guid AddTask(this IScheduler scheduler, CrontabSchedule crontabSchedule, Func<CancellationToken, Task> task)
{
var taskId = Guid.NewGuid();

scheduler.AddTask(taskId, crontabSchedule, action);
scheduler.AddTask(taskId, crontabSchedule, task);

return taskId;
}
Expand All @@ -103,22 +143,22 @@ public static Guid AddTask(this IScheduler scheduler, CrontabSchedule crontabSch
/// </summary>
/// <param name="taskId">The task identifier.</param>
/// <param name="cronExpression">The cron expression.</param>
/// <param name="action">The callback action which is called whenever the <paramref name="cronExpression"/> is planned to execute.</param>
public static void AddTask(this IScheduler scheduler, Guid taskId, string cronExpression, Func<CancellationToken, Task> action)
/// <param name="task">The callback action which is called whenever the <paramref name="cronExpression"/> is planned to execute.</param>
public static void AddTask(this IScheduler scheduler, Guid taskId, string cronExpression, Func<CancellationToken, Task> task)
{
var crontabSchedule = CrontabSchedule.Parse(cronExpression);
scheduler.AddTask(taskId, crontabSchedule, action);
scheduler.AddTask(taskId, crontabSchedule, task);
}

/// <summary>
/// Adds a task to the scheduler.
/// </summary>
/// <param name="taskId">The task identifier.</param>
/// <param name="crontabSchedule">The crontab schedule.</param>
/// <param name="action">The callback action which is called whenever the <paramref name="crontabSchedule"/> is planned to execute.</param>
public static void AddTask(this IScheduler scheduler, Guid taskId, CrontabSchedule crontabSchedule, Func<CancellationToken, Task> action)
/// <param name="task">The callback action which is called whenever the <paramref name="crontabSchedule"/> is planned to execute.</param>
public static void AddTask(this IScheduler scheduler, Guid taskId, CrontabSchedule crontabSchedule, Func<CancellationToken, Task> task)
{
var asyncScheduledTask = new AsyncScheduledTask(taskId, crontabSchedule, action);
var asyncScheduledTask = new AsyncScheduledTask(taskId, crontabSchedule, task);
scheduler.AddTask(asyncScheduledTask);
}

Expand Down
8 changes: 6 additions & 2 deletions NCrontab.Scheduler/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static void AddScheduler(this IServiceCollection serviceCollection, IConfiguration configuration)
public static IServiceCollection AddScheduler(this IServiceCollection serviceCollection, IConfiguration configuration)
{
// Configuration
serviceCollection.Configure<SchedulerOptions>(configuration);

// Register services
serviceCollection.AddScheduler();

return serviceCollection;
}

public static void AddScheduler(this IServiceCollection serviceCollection, Action<SchedulerOptions> options = null)
public static IServiceCollection AddScheduler(this IServiceCollection serviceCollection, Action<SchedulerOptions> options = null)
{
// Configuration
if (options != null)
Expand All @@ -33,6 +35,8 @@ public static void AddScheduler(this IServiceCollection serviceCollection, Actio
x.GetRequiredService<ILogger<Scheduler>>(),
x.GetRequiredService<IOptions<SchedulerOptions>>());
});

return serviceCollection;
}
}
}
2 changes: 2 additions & 0 deletions NCrontab.Scheduler/ISchedulerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ public interface ISchedulerOptions
/// consistent behavior across daylight savings time changes.
/// Log messages are formatted with UTC timestamps too.</remarks>
public DateTimeKind DateTimeKind { get; set; }

public LoggingOptions Logging { get; set; }
}
}
17 changes: 17 additions & 0 deletions NCrontab.Scheduler/ITask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@

namespace NCrontab.Scheduler
{
/// <summary>
/// Abstraction for any kind of task that can be scheduled with a <see cref="Scheduler"/> instance.
/// See also:
/// <seealso cref="ScheduledTask"/>
/// <seealso cref="AsyncScheduledTask"/>
/// </summary>
public interface ITask
{
/// <summary>
/// Unique identifier of the task.
/// </summary>
public Guid Id { get; }

/// <summary>
/// Display name of the task.
/// </summary>
public string Name { get; set; }

/// <summary>
/// The cron schedule expressions.
/// </summary>
CrontabSchedule CrontabSchedule { get; set; }
}
}
25 changes: 25 additions & 0 deletions NCrontab.Scheduler/LogIdentifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace NCrontab.Scheduler
{
public enum LogIdentifier
{
/// <summary>
/// Use <see cref="ITask.Id"/> in the log messages.
/// </summary>
TaskId,

/// <summary>
/// Use <see cref="ITask.Name"/> in the log messages.
/// </summary>
TaskName,

/// <summary>
/// Use <see cref="ITask.Id"/> and <see cref="ITask.Name"/> (if available) in the log messages.
/// </summary>
TaskIdAndName,

/// <summary>
/// Use <see cref="ITask.Name"/> (if available) and <see cref="ITask.Id"/> in the log messages.
/// </summary>
TaskNameAndId,
}
}
34 changes: 34 additions & 0 deletions NCrontab.Scheduler/LoggingOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;

namespace NCrontab.Scheduler
{
public class LoggingOptions
{
public LoggingOptions()
{
this.DateTimeKind = DateTimeKind.Utc;
this.LogIdentifier = LogIdentifier.TaskName;
this.TaskIdFormatter = "B";
}

public virtual DateTimeKind DateTimeKind { get; set; }

/// <summary>
/// Sets the formatting rule for a task identifier when written to the log output.
/// This option can improve the readability of the scheduler log output.
/// It has no impact on functionaly and/or performance of the scheduler.
/// Default is <c>LogIdentifier.TaskName</c>.
/// </summary>
/// <remarks>
/// Since <see cref="ITask.Name"/> is optional,
/// <see cref="ITask.Id"/> is used if <see cref="ITask.Name"/> is null or empty.
/// </remarks>
public virtual LogIdentifier LogIdentifier { get; set; }

/// <summary>
/// Formatter used when logging <see cref="ITask.Id"/>.
/// Default is <c>"B"</c>.
/// </summary>
public virtual string TaskIdFormatter { get; set; }
}
}
3 changes: 2 additions & 1 deletion NCrontab.Scheduler/NCrontab.Scheduler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- Initial release
</PackageReleaseNotes>
<Copyright>Copyright 2023</Copyright>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand All @@ -48,8 +49,8 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="[6.0.0,)" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="[6.0.1,)" />
<PackageReference Include="Microsoft.Extensions.Options" Version="[6.0.0,)" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="[6.0.1,)" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="[6.0.0,)" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="[6.0.0,)" />
<PackageReference Include="ncrontab" Version="[3.3.1,)" />
</ItemGroup>

Expand Down
2 changes: 2 additions & 0 deletions NCrontab.Scheduler/NullTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public NullTask(Guid taskId)

public Guid Id => this.taskId;

public string Name { get; set; }

public CrontabSchedule CrontabSchedule { get; set; }
}
}
Loading