From da74b0ece154f592b7dd220e26e3078a26448d1b Mon Sep 17 00:00:00 2001 From: sbwalker Date: Fri, 18 Aug 2023 13:34:38 -0400 Subject: [PATCH] fix #3157 - provide support for asynchronous scheduled jobs --- .../Infrastructure/Jobs/HostedServiceBase.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs b/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs index 1c9eb377..be8e8c69 100644 --- a/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs +++ b/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs @@ -23,9 +23,6 @@ namespace Oqtane.Infrastructure _serviceScopeFactory = serviceScopeFactory; } - // abstract method must be overridden - public abstract string ExecuteJob(IServiceProvider provider); - // public properties which can be overridden and are used during auto registration of job public string Name { get; set; } = ""; public string Frequency { get; set; } = "d"; // day @@ -35,6 +32,17 @@ namespace Oqtane.Infrastructure public int RetentionHistory { get; set; } = 10; public bool IsEnabled { get; set; } = false; + // one of the following methods must be overridden + public virtual string ExecuteJob(IServiceProvider provider) + { + return ""; + } + + public virtual Task ExecuteJobAsync(IServiceProvider provider) + { + return Task.FromResult(string.Empty); + } + protected async Task ExecuteAsync(CancellationToken stoppingToken) { await Task.Yield(); // required so that this method does not block startup @@ -102,6 +110,7 @@ namespace Oqtane.Infrastructure // set tenant and execute job tenantManager.SetTenant(tenant.TenantId); notes += ExecuteJob(scope.ServiceProvider); + notes += await ExecuteJobAsync(scope.ServiceProvider); } log.Notes = notes; log.Succeeded = true;