Skip to content

Commit

Permalink
Use new SyncCommand helpers + remove old TimeHostedService
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Aug 17, 2024
1 parent e9504b2 commit f26405e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 78 deletions.
14 changes: 14 additions & 0 deletions MyApp.ServiceInterface/Recurring/LogCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.Extensions.Logging;
using ServiceStack;

namespace MyApp.ServiceInterface.Recurring;

public class LogCommand(ILogger<LogCommand> log) : SyncCommand
{
private static long count = 0;
protected override void Run()
{
Interlocked.Increment(ref count);
log.LogInformation("Log {Count}: Hello from Recurring Command", count);
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
using System.Net.Mail;
using Microsoft.Extensions.Logging;
using MyApp.Data;
using ServiceStack;
using MyApp.Data;

namespace MyApp.ServiceInterface.App;

public class LogRequest
{
public string Message { get; set; }
}

public class LogCommand(ILogger<LogCommand> log) : IAsyncCommand<LogRequest>
{
private static int count = 0;

public Task ExecuteAsync(LogRequest request)
{
Interlocked.Increment(ref count);
log.LogInformation("Log {Count}: {Message}", count, request.Message);
return Task.CompletedTask;
}
}
namespace MyApp.ServiceInterface.Recurring;

public class SendEmailCommand(ILogger<LogCommand> log, SmtpConfig config) : IAsyncCommand<SendEmail>
public abstract class SendEmailCommand(ILogger<LogCommand> log, SmtpConfig config) : SyncCommand<SendEmail>
{
private static int count = 0;

public Task ExecuteAsync(SendEmail request)
private static long count = 0;
protected override void Run(SendEmail request)
{
Interlocked.Increment(ref count);
log.LogInformation("Sending {Count} email to {Email} with subject {Subject}", count, request.To, request.Subject);
Expand Down Expand Up @@ -55,6 +37,5 @@ public Task ExecuteAsync(SendEmail request)
}

client.Send(msg);
return Task.CompletedTask;
}
}
51 changes: 0 additions & 51 deletions MyApp.ServiceInterface/TimeHostedService.cs

This file was deleted.

6 changes: 3 additions & 3 deletions MyApp/Configure.BackgroundJobs.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MyApp.Data;
using MyApp.ServiceInterface.App;
using MyApp.ServiceInterface.Recurring;
using MyApp.ServiceModel;
using ServiceStack.Jobs;

Expand All @@ -18,8 +19,7 @@ public void Configure(IWebHostBuilder builder) => builder
{
var jobs = appHost.Resolve<IBackgroundJobs>();

jobs.RecurringCommand<LogCommand>("Every Minute", Schedule.EveryMinute,
new LogRequest { Message = "Hello from Recurring Command" });
jobs.RecurringCommand<LogCommand>("Every Minute", Schedule.EveryMinute);

jobs.RecurringCommand<SendEmailCommand>("Every 8 hours", Schedule.Interval(TimeSpan.FromHours(8)),
new SendEmail
Expand All @@ -33,7 +33,7 @@ public void Configure(IWebHostBuilder builder) => builder
new DbWrites {
PeriodicTasks = new PeriodicTasks { PeriodicFrequency = PeriodicFrequency.Hourly }
});

});
}

Expand Down

0 comments on commit f26405e

Please sign in to comment.