add jobs
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using Atomx.Utils.Json;
|
||||
using Atomx.Data;
|
||||
using Atomx.Data.CacheServices;
|
||||
using Atomx.Utils.Json;
|
||||
using Hangfire;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace Atomx.Core.Jos
|
||||
@@ -12,9 +15,13 @@ namespace Atomx.Core.Jos
|
||||
public class LocalizationJob
|
||||
{
|
||||
readonly ILogger<LocalizationJob> _logger;
|
||||
public LocalizationJob(ILogger<LocalizationJob> logger)
|
||||
readonly DataContext _dbContext;
|
||||
readonly ICacheService _cacheService;
|
||||
public LocalizationJob(ILogger<LocalizationJob> logger, DataContext dataContext, ICacheService cacheService)
|
||||
{
|
||||
_logger = logger;
|
||||
_dbContext = dataContext;
|
||||
_cacheService = cacheService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -30,46 +37,58 @@ namespace Atomx.Core.Jos
|
||||
|
||||
var fileName = $"{culture}.json";
|
||||
var filePath = Path.Combine(path, fileName);
|
||||
if (!Directory.Exists(filePath))
|
||||
if (!Directory.Exists(path))
|
||||
{
|
||||
Directory.CreateDirectory(filePath);
|
||||
_logger.LogInformation("Created Resources directory: {Path}", filePath);
|
||||
Directory.CreateDirectory(path);
|
||||
_logger.LogInformation("Created Resources directory: {Path}", path);
|
||||
}
|
||||
|
||||
var json = await File.ReadAllTextAsync(filePath);
|
||||
var fileData = JsonSerializer.Deserialize<Dictionary<string, string>>(json, new JsonSerializerOptions
|
||||
var fileData = new Dictionary<string, string>();
|
||||
if (File.Exists(filePath))
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
if (fileData == null)
|
||||
{
|
||||
fileData = new Dictionary<string, string>();
|
||||
var json = await File.ReadAllTextAsync(filePath);
|
||||
fileData = JsonSerializer.Deserialize<Dictionary<string, string>>(json, new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
}) ?? new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
foreach (var item in translations)
|
||||
{
|
||||
if (fileData.ContainsKey(item.Key))
|
||||
{
|
||||
fileData[item.Key] = item.Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
fileData.Add(item.Key, item.Value);
|
||||
}
|
||||
fileData[item.Key] = item.Value;
|
||||
}
|
||||
|
||||
var options = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = true
|
||||
};
|
||||
json = JsonSerializer.Serialize(fileData, options);
|
||||
await File.WriteAllTextAsync(filePath, json);
|
||||
var updatedJson = JsonSerializer.Serialize(fileData, options);
|
||||
await File.WriteAllTextAsync(filePath, updatedJson);
|
||||
|
||||
// 更新文件后,更新数据库中的资源版本
|
||||
string fileHash;
|
||||
using (var sha256 = SHA256.Create())
|
||||
using (var stream = File.OpenRead(filePath))
|
||||
{
|
||||
var hashBytes = sha256.ComputeHash(stream);
|
||||
fileHash = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
|
||||
}
|
||||
var language = _dbContext.Languages.FirstOrDefault(l => l.Culture == culture);
|
||||
if (language != null)
|
||||
{
|
||||
language.UpdateTime = DateTime.UtcNow;
|
||||
language.ResourceVersion = fileHash;
|
||||
await _dbContext.SaveChangesAsync();
|
||||
await _cacheService.GetLanguageById(language.Id, language);
|
||||
}
|
||||
|
||||
|
||||
|
||||
_logger.LogInformation("Saved localization file for culture: {Culture} with {Count} translations",
|
||||
culture, translations.Count);
|
||||
_logger.LogInformation("Saved localization file for culture: {Culture} with {Count} translations. File hash: {Hash}",
|
||||
culture, translations.Count, fileHash);
|
||||
}
|
||||
catch(Exception ex) {
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error saving localization file for culture: {Culture}", culture);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
using Hangfire;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Atomx.Core.Jos
|
||||
{
|
||||
public partial interface IBackgroundJobsService
|
||||
public partial interface IBackgroundJobService
|
||||
{
|
||||
/// <summary>
|
||||
/// 更新本地化文件
|
||||
@@ -19,12 +16,12 @@ namespace Atomx.Core.Jos
|
||||
string SendSMSVerificationCode(string phoneNumber, string code, TimeSpan validDuration);
|
||||
}
|
||||
|
||||
public partial class BackgroundJobsService : IBackgroundJobsService
|
||||
public partial class BackgroundJobService : IBackgroundJobService
|
||||
{
|
||||
readonly IBackgroundJobClient _backgroundJobClient;
|
||||
readonly IRecurringJobManager _recurringJobManager;
|
||||
readonly ILogger<BackgroundJobsService> _logger;
|
||||
public BackgroundJobsService(IBackgroundJobClient backgroundJobClient, IRecurringJobManager recurringJobManager, ILogger<BackgroundJobsService> logger)
|
||||
readonly ILogger<BackgroundJobService> _logger;
|
||||
public BackgroundJobService(IBackgroundJobClient backgroundJobClient, IRecurringJobManager recurringJobManager, ILogger<BackgroundJobService> logger)
|
||||
{
|
||||
_backgroundJobClient = backgroundJobClient;
|
||||
_recurringJobManager = recurringJobManager;
|
||||
Reference in New Issue
Block a user