diff --git a/MyApp.ServiceInterface/Recurring/LogCommand.cs b/MyApp.ServiceInterface/Recurring/LogCommand.cs new file mode 100644 index 0000000..014f38e --- /dev/null +++ b/MyApp.ServiceInterface/Recurring/LogCommand.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Logging; +using ServiceStack; + +namespace MyApp.ServiceInterface.Recurring; + +public class LogCommand(ILogger log) : SyncCommand +{ + private static long count = 0; + protected override void Run() + { + Interlocked.Increment(ref count); + log.LogInformation("Log {Count}: Hello from Recurring Command", count); + } +} diff --git a/MyApp.ServiceInterface/App/LogCommand.cs b/MyApp.ServiceInterface/Recurring/SendEmailCommand.cs similarity index 63% rename from MyApp.ServiceInterface/App/LogCommand.cs rename to MyApp.ServiceInterface/Recurring/SendEmailCommand.cs index d34a1f4..4b07af8 100644 --- a/MyApp.ServiceInterface/App/LogCommand.cs +++ b/MyApp.ServiceInterface/Recurring/SendEmailCommand.cs @@ -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 log) : IAsyncCommand -{ - 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 log, SmtpConfig config) : IAsyncCommand +public abstract class SendEmailCommand(ILogger log, SmtpConfig config) : SyncCommand { - 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); @@ -55,6 +37,5 @@ public Task ExecuteAsync(SendEmail request) } client.Send(msg); - return Task.CompletedTask; } } diff --git a/MyApp.ServiceInterface/TimeHostedService.cs b/MyApp.ServiceInterface/TimeHostedService.cs deleted file mode 100644 index d849ba2..0000000 --- a/MyApp.ServiceInterface/TimeHostedService.cs +++ /dev/null @@ -1,51 +0,0 @@ -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; -using MyApp.Data; -using MyApp.ServiceModel; -using ServiceStack.Messaging; - -namespace MyApp.ServiceInterface; - -public class TimedHostedService(ILogger logger, IMessageService mqServer) : IHostedService, IDisposable -{ - private int EveryMins = 60; - private int executionCount = 0; - private Timer? timer = null; - - public Task StartAsync(CancellationToken stoppingToken) - { - logger.LogInformation("Timed Hosted Service running"); - - timer = new Timer(DoWork, null, TimeSpan.Zero, - TimeSpan.FromMinutes(EveryMins)); - - return Task.CompletedTask; - } - - private void DoWork(object? state) - { - var count = Interlocked.Increment(ref executionCount); - logger.LogInformation("Timed Hosted Service is working. Count: {Count}", count); - - if (logger.IsEnabled(LogLevel.Debug)) - logger.LogInformation("MQ Worker running at: {Stats}\n", mqServer.GetStatsDescription()); - - var frequentTasks = new PeriodicTasks { PeriodicFrequency = PeriodicFrequency.Hourly }; - using var mq = mqServer.MessageFactory.CreateMessageProducer(); - mq.Publish(new DbWrites { PeriodicTasks = frequentTasks }); - } - - public Task StopAsync(CancellationToken stoppingToken) - { - logger.LogInformation("Timed Hosted Service is stopping"); - - timer?.Change(Timeout.Infinite, 0); - - return Task.CompletedTask; - } - - public void Dispose() - { - timer?.Dispose(); - } -} diff --git a/MyApp/Configure.BackgroundJobs.cs b/MyApp/Configure.BackgroundJobs.cs index d47e88a..6f1b6a7 100644 --- a/MyApp/Configure.BackgroundJobs.cs +++ b/MyApp/Configure.BackgroundJobs.cs @@ -1,5 +1,6 @@ using MyApp.Data; using MyApp.ServiceInterface.App; +using MyApp.ServiceInterface.Recurring; using MyApp.ServiceModel; using ServiceStack.Jobs; @@ -18,8 +19,7 @@ public void Configure(IWebHostBuilder builder) => builder { var jobs = appHost.Resolve(); - jobs.RecurringCommand("Every Minute", Schedule.EveryMinute, - new LogRequest { Message = "Hello from Recurring Command" }); + jobs.RecurringCommand("Every Minute", Schedule.EveryMinute); jobs.RecurringCommand("Every 8 hours", Schedule.Interval(TimeSpan.FromHours(8)), new SendEmail @@ -33,7 +33,7 @@ public void Configure(IWebHostBuilder builder) => builder new DbWrites { PeriodicTasks = new PeriodicTasks { PeriodicFrequency = PeriodicFrequency.Hourly } }); - + }); }