Skip to content

Commit

Permalink
代码优化、编译优化、支持动态修改页面内容
Browse files Browse the repository at this point in the history
  • Loading branch information
Light committed Sep 4, 2024
1 parent 7888822 commit 16f371f
Show file tree
Hide file tree
Showing 31 changed files with 202 additions and 262 deletions.
65 changes: 31 additions & 34 deletions .github/workflows/dotnet-publish-prerelease.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: .NET
name: Build Pre-Release

on:
push:
Expand All @@ -17,11 +17,16 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.x
- name: Publish for Linux-x64
run: dotnet publish -c Release -r linux-x64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -p:PublishReadyToRun=true -o app/linux-x64
- name: Publish for Windows-x64
run: dotnet publish -c Release -r win-x64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -p:PublishReadyToRun=true -o app/win-x64
dotnet-version: 8.0.x
- name: Publish
run: |
dotnet publish -c Release -r linux-x64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -o app/linux-x64
dotnet publish -c Release -r win-x64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -o app/win-x64
dotnet publish -c Release -r linux-arm64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -o app/linux-arm64
cd app
find . -type f -name 'appsettings.Example.json' -execdir mv {} appsettings.json \;
find . -maxdepth 1 -type d ! -name '.' | xargs -I {} sh -c 'cd "{}" && zip -r "../${{ github.ref_name }}-$(basename {}).zip" *'
find . -maxdepth 1 -type d ! -name '.' -exec rm -rf {} +
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
Expand All @@ -39,14 +44,6 @@ jobs:
with:
name: app
path: app

- name: Zip artifact
run: |
cd app
mv linux-x64/appsettings.Example.json linux-x64/appsettings.json
zip -r linux-x64.zip linux-x64/*
mv win-x64/appsettings.Example.json win-x64/appsettings.json
zip -r win-x64.zip win-x64/*

- name: Create Release
id: create_release
Expand All @@ -59,27 +56,27 @@ jobs:
draft: false
prerelease: true

- name: Upload Release Asset for Linux-x64
id: upload_linux
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: app/linux-x64.zip
asset_name: linux-x64.zip
asset_content_type: application/zip

- name: Upload Release Asset for Windows-x64
id: upload_win
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload ZIP files to Release
id: upload_zip
uses: actions/github-script@v3
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: app/win-x64.zip
asset_name: win-x64.zip
asset_content_type: application/zip
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const path = require('path');
const fs = require('fs');
const release_id = '${{ steps.create_release.outputs.id }}';
for (let file of await fs.readdirSync('app/')) {
if (path.extname(file) === '.zip') {
console.log('uploadReleaseAsset', file);
await github.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release_id,
name: file,
data: await fs.readFileSync(`app/${file}`)
});
}
}
cleanup:
name: Cleanup
Expand Down
65 changes: 31 additions & 34 deletions .github/workflows/dotnet-publish-release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: .NET
name: Build Release

on:
push:
Expand All @@ -17,11 +17,16 @@ jobs:
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: 6.0.x
- name: Publish for Linux-x64
run: dotnet publish -c Release -r linux-x64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -p:PublishReadyToRun=true -o app/linux-x64
- name: Publish for Windows-x64
run: dotnet publish -c Release -r win-x64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -p:PublishReadyToRun=true -o app/win-x64
dotnet-version: 8.0.x
- name: Publish
run: |
dotnet publish -c Release -r linux-x64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -o app/linux-x64
dotnet publish -c Release -r win-x64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -o app/win-x64
dotnet publish -c Release -r linux-arm64 --self-contained -p:PublishTrimmed=true -p:PublishSingleFile=true -o app/linux-arm64
cd app
find . -type f -name 'appsettings.Example.json' -execdir mv {} appsettings.json \;
find . -maxdepth 1 -type d ! -name '.' | xargs -I {} sh -c 'cd "{}" && zip -r "../${{ github.ref_name }}-$(basename {}).zip" *'
find . -maxdepth 1 -type d ! -name '.' -exec rm -rf {} +
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
Expand All @@ -39,14 +44,6 @@ jobs:
with:
name: app
path: app

- name: Zip artifact
run: |
cd app
mv linux-x64/appsettings.Example.json linux-x64/appsettings.json
zip -r linux-x64.zip linux-x64/*
mv win-x64/appsettings.Example.json win-x64/appsettings.json
zip -r win-x64.zip win-x64/*

- name: Create Release
id: create_release
Expand All @@ -59,27 +56,27 @@ jobs:
draft: false
prerelease: false

- name: Upload Release Asset for Linux-x64
id: upload_linux
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: app/linux-x64.zip
asset_name: linux-x64.zip
asset_content_type: application/zip

- name: Upload Release Asset for Windows-x64
id: upload_win
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Upload ZIP files to Release
id: upload_zip
uses: actions/github-script@v3
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: app/win-x64.zip
asset_name: win-x64.zip
asset_content_type: application/zip
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const path = require('path');
const fs = require('fs');
const release_id = '${{ steps.create_release.outputs.id }}';
for (let file of await fs.readdirSync('app/')) {
if (path.extname(file) === '.zip') {
console.log('uploadReleaseAsset', file);
await github.repos.uploadReleaseAsset({
owner: context.repo.owner,
repo: context.repo.repo,
release_id: release_id,
name: file,
data: await fs.readFileSync(`app/${file}`)
});
}
}
cleanup:
name: Cleanup
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,4 @@ FodyWeavers.xsd
/src/TokenPay/EVMChains.json
/src/TokenPay/EVMChains.Development.json
/src/TokenPay/手续费钱包私钥.txt
/src/TokenPay/.config
1 change: 0 additions & 1 deletion src/TokenPay.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{D54245C4-8D70-442B-91C0-0053856152B0}"
ProjectSection(SolutionItems) = preProject
.dockerignore = .dockerignore
.editorconfig = .editorconfig
.gitattributes = .gitattributes
.gitignore = .gitignore
docker-compose.yml = docker-compose.yml
Expand Down
58 changes: 18 additions & 40 deletions src/TokenPay/BgServices/BaseScheduledService.cs
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);
}
}
}
10 changes: 4 additions & 6 deletions src/TokenPay/BgServices/CollectionTRONService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CollectionTRONService : BaseScheduledService
{
private readonly IConfiguration _configuration;
private readonly TelegramBot _bot;
private readonly IServiceProvider _serviceProvider;
private readonly IFreeSql freeSql;
private readonly ILogger<CollectionTRONService> _logger;
/// <summary>
/// 是否启用归集功能
Expand Down Expand Up @@ -63,14 +63,13 @@ public class CollectionTRONService : BaseScheduledService
public CollectionTRONService(
IConfiguration configuration,
TelegramBot bot,
IServiceProvider serviceProvider,
IFreeSql freeSql,
ILogger<CollectionTRONService> logger) : base("TRON归集任务", TimeSpan.FromHours(configuration.GetValue("Collection:CheckTime", 1)), logger)

Check warning on line 67 in src/TokenPay/BgServices/CollectionTRONService.cs

View workflow job for this annotation

GitHub Actions / Build And Publish

Using member 'Microsoft.Extensions.Configuration.ConfigurationBinder.GetValue<T>(IConfiguration, String, T)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. In case the type is non-primitive, the trimmer cannot statically analyze the object's type so its members may be trimmed.
{
this._configuration = configuration;
this._serviceProvider = serviceProvider;
this._logger = logger;
this._bot = bot;

this.freeSql = freeSql;
}
protected override async Task ExecuteAsync()
{
Expand Down Expand Up @@ -160,8 +159,7 @@ await _bot.SendTextMessageAsync(@$"归集收款地址余额
当前TRX余额:{trx} USDT
当前USDT余额:{usdt} USDT");
}
using IServiceScope scope = _serviceProvider.CreateScope();
var _repository = scope.ServiceProvider.GetRequiredService<IBaseRepository<Tokens>>();
var _repository = freeSql.GetRepository<Tokens>();
var list = await _repository.Where(x => x.Currency == TokenCurrency.TRX).Where(x => ForceCheckAllAddress || (x.USDT > MinUSDT || x.Value > 0.5m)).ToListAsync();
var count = 0;
foreach (var item in list)
Expand Down
17 changes: 4 additions & 13 deletions src/TokenPay/BgServices/OrderCheckEVMBaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,27 @@ public class OrderCheckEVMBaseService : BaseScheduledService
private readonly IHostEnvironment _env;
private readonly Channel<TokenOrders> _channel;
private readonly List<EVMChain> _chains;
private readonly IServiceProvider _serviceProvider;
private readonly FlurlClient client;
private readonly IFreeSql freeSql;
private bool UseDynamicAddress => _configuration.GetValue("UseDynamicAddress", true);
private bool UseDynamicAddressAmountMove => _configuration.GetValue("DynamicAddressConfig:AmountMove", false);
public OrderCheckEVMBaseService(ILogger<OrderCheckEVMBaseService> logger,
IConfiguration configuration,
IHostEnvironment env,
Channel<TokenOrders> channel,
List<EVMChain> Chains,
IServiceProvider serviceProvider) : base("ETH订单检测", TimeSpan.FromSeconds(15), logger)
IFreeSql freeSql) : base("ETH订单检测", TimeSpan.FromSeconds(15), logger)
{
_logger = logger;
this._configuration = configuration;
this._env = env;
this._channel = channel;
_chains = Chains;
_serviceProvider = serviceProvider;
var WebProxy = configuration.GetValue<string>("WebProxy");
client = new FlurlClient();
if (!string.IsNullOrEmpty(WebProxy))
{
client.Settings.HttpClientFactory = new ProxyHttpClientFactory(WebProxy);
}
this.freeSql = freeSql;
}

protected override async Task ExecuteAsync()
{
using IServiceScope scope = _serviceProvider.CreateScope();
var _repository = scope.ServiceProvider.GetRequiredService<IBaseRepository<TokenOrders>>();
var _repository = freeSql.GetRepository<TokenOrders>();
foreach (var chain in _chains)
{
if (chain == null || !chain.Enable) continue;
Expand Down Expand Up @@ -87,7 +79,6 @@ protected override async Task ExecuteAsync()
var req = BaseUrl
.AppendPathSegment($"api")
.SetQueryParams(query)
.WithClient(client)
.WithTimeout(15);
var result = await req
.GetJsonAsync<BaseResponse<EthTransaction>>();
Expand Down
Loading

0 comments on commit 16f371f

Please sign in to comment.