-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7888822
commit 16f371f
Showing
31 changed files
with
202 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,38 @@ | ||
namespace TokenPay.BgServices | ||
{ | ||
public abstract class BaseScheduledService : IHostedService, IDisposable | ||
public abstract class BaseScheduledService : BackgroundService | ||
{ | ||
private readonly Timer _timer; | ||
protected readonly string jobName; | ||
private readonly TimeSpan _period; | ||
protected readonly ILogger Logger; | ||
private readonly PeriodicTimer _timer; | ||
|
||
protected BaseScheduledService(string JobName, TimeSpan period, ILogger logger) | ||
{ | ||
Logger = logger; | ||
jobName = JobName; | ||
_period = period; | ||
_timer = new Timer(Execute, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan); | ||
_timer = new PeriodicTimer(period); | ||
} | ||
|
||
public void Execute(object? state = null) | ||
protected override async Task ExecuteAsync(CancellationToken stoppingToken) | ||
{ | ||
try | ||
{ | ||
|
||
ExecuteAsync().Wait(); | ||
//Logger.LogInformation("Begin execute service"); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.LogError(ex, $"定时任务[{jobName}]执行出现错误"); | ||
} | ||
finally | ||
Logger.LogInformation("Service {JobName} is starting.", jobName); | ||
do | ||
{ | ||
//Logger.LogInformation("Execute finished"); | ||
_timer.Change(_period, Timeout.InfiniteTimeSpan); | ||
} | ||
try | ||
{ | ||
await ExecuteAsync(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.LogError(ex, $"定时任务[{jobName}]执行出现错误"); | ||
} | ||
} while (!stoppingToken.IsCancellationRequested && await _timer.WaitForNextTickAsync(stoppingToken)); | ||
} | ||
|
||
protected abstract Task ExecuteAsync(); | ||
|
||
public virtual void Dispose() | ||
{ | ||
_timer?.Dispose(); | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
Logger.LogInformation("Service {JobName} is starting.", jobName); | ||
_timer.Change(TimeSpan.FromSeconds(3), Timeout.InfiniteTimeSpan); | ||
return Task.CompletedTask; | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
public override Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
Logger.LogInformation("Service {JobName} is stopping.", jobName); | ||
|
||
_timer?.Change(Timeout.Infinite, 0); | ||
|
||
return Task.CompletedTask; | ||
_timer.Dispose(); | ||
return base.StopAsync(cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.