mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-01-18 21:32:27 +01:00
Adding hosted service that will optionally check for new version once a day
This commit is contained in:
parent
b47b942e97
commit
ea7231ff26
@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using BTCPayServer.Services;
|
||||
using BTCPayServer.Services.Notifications;
|
||||
using BTCPayServer.Services.Notifications.Blobs;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace BTCPayServer.HostedServices
|
||||
{
|
||||
public class NewVersionCheckerHostedService : BaseAsyncService
|
||||
{
|
||||
private readonly SettingsRepository _settingsRepository;
|
||||
private readonly BTCPayServerEnvironment _env;
|
||||
private readonly NotificationSender _notificationSender;
|
||||
private readonly IVersionFetcher _versionFetcher;
|
||||
|
||||
public NewVersionCheckerHostedService(SettingsRepository settingsRepository, BTCPayServerEnvironment env,
|
||||
NotificationSender notificationSender, IVersionFetcher versionFetcher)
|
||||
{
|
||||
_settingsRepository = settingsRepository;
|
||||
_env = env;
|
||||
_notificationSender = notificationSender;
|
||||
_versionFetcher = versionFetcher;
|
||||
}
|
||||
|
||||
internal override Task[] InitializeTasks()
|
||||
{
|
||||
return new Task[] { CreateLoopTask(CheckForNewVersion) };
|
||||
}
|
||||
|
||||
protected async Task CheckForNewVersion()
|
||||
{
|
||||
var policies = await _settingsRepository.GetSettingAsync<PoliciesSettings>() ?? new PoliciesSettings();
|
||||
if (policies.CheckForNewVersions && !_env.IsDevelopping)
|
||||
{
|
||||
var tag = await _versionFetcher.Fetch(Cancellation);
|
||||
if (tag != null && tag != _env.Version)
|
||||
{
|
||||
var dh = await _settingsRepository.GetSettingAsync<NewVersionCheckerDataHolder>() ?? new NewVersionCheckerDataHolder();
|
||||
if (dh.LastVersion != tag)
|
||||
{
|
||||
await _notificationSender.SendNotification(new AdminScope(), new NewVersionNotification(tag));
|
||||
|
||||
dh.LastVersion = tag;
|
||||
await _settingsRepository.UpdateSetting(dh);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromDays(1));
|
||||
}
|
||||
|
||||
public class NewVersionCheckerDataHolder
|
||||
{
|
||||
public string LastVersion { get; set; }
|
||||
}
|
||||
|
||||
public interface IVersionFetcher
|
||||
{
|
||||
Task<string> Fetch(CancellationToken cancellation);
|
||||
}
|
||||
|
||||
public class GithubVersionFetcher : IVersionFetcher
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
public GithubVersionFetcher()
|
||||
{
|
||||
_httpClient = new HttpClient();
|
||||
_httpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.github.v3+json");
|
||||
_httpClient.DefaultRequestHeaders.Add("User-Agent", "BTCPayServer/NewVersionChecker");
|
||||
}
|
||||
|
||||
public async Task<string> Fetch(CancellationToken cancellation)
|
||||
{
|
||||
const string url = "https://api.github.com/repos/btcpayserver/btcpayserver/releases/latest";
|
||||
var resp = await _httpClient.GetAsync(url, cancellation);
|
||||
|
||||
if (resp.IsSuccessStatusCode)
|
||||
{
|
||||
var jobj = await resp.Content.ReadAsAsync<JObject>(cancellation);
|
||||
var tag = jobj["name"].ToString();
|
||||
return tag;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -239,7 +239,10 @@ namespace BTCPayServer.Hosting
|
||||
services.AddSingleton<IBackgroundJobClient, BackgroundJobClient>();
|
||||
services.AddScoped<IAuthorizationHandler, CookieAuthorizationHandler>();
|
||||
services.AddScoped<IAuthorizationHandler, BitpayAuthorizationHandler>();
|
||||
|
||||
services.AddSingleton<IHostedService, NewVersionCheckerHostedService>();
|
||||
services.AddSingleton<INotificationHandler, NewVersionNotification.Handler>();
|
||||
|
||||
services.AddSingleton<INotificationHandler, InvoiceEventNotification.Handler>();
|
||||
services.AddSingleton<INotificationHandler, PayoutNotification.Handler>();
|
||||
|
||||
|
@ -24,6 +24,8 @@ namespace BTCPayServer.Services
|
||||
public bool AllowHotWalletForAll { get; set; }
|
||||
[Display(Name = "Allow non-admins to import their hot wallets to the node wallet")]
|
||||
public bool AllowHotWalletRPCImportForAll { get; set; }
|
||||
[Display(Name = "Check releases on GitHub and alert when new BTCPayServer versions is available")]
|
||||
public bool CheckForNewVersions { get; set; }
|
||||
|
||||
[Display(Name = "Display app on website root")]
|
||||
public string RootAppId { get; set; }
|
||||
|
Loading…
Reference in New Issue
Block a user