优化了job的自动创建路由,并且删除了一些不必要的
This commit is contained in:
parent
48966fe84e
commit
4b77477008
|
@ -111,19 +111,11 @@ namespace ShardingCore.Bootstrapers
|
|||
if (virtualTableRoute is IJob routeJob && routeJob.StartJob())
|
||||
{
|
||||
var jobManager = ShardingContainer.GetService<IJobManager>();
|
||||
var jobEntries = JobTypeParser.Parse(virtualTableRoute.GetType());
|
||||
jobEntries.ForEach(o =>
|
||||
{
|
||||
o.JobName = $"{routeJob.JobName}:{o.JobName}";
|
||||
o.JobInstance = routeJob;
|
||||
});
|
||||
foreach (var jobEntry in jobEntries)
|
||||
{
|
||||
var jobEntry = JobEntryFactory.Create(routeJob);
|
||||
jobManager.AddJob(jobEntry);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private IVirtualDataSourceRoute<TEntity> CreateVirtualDataSourceRoute(Type virtualRouteType,EntityMetadata entityMetadata)
|
||||
{
|
||||
|
|
|
@ -17,7 +17,7 @@ namespace ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions
|
|||
* @Date: Saturday, 19 December 2020 19:55:24
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractShardingOperatorVirtualTableRoute<T, TKey> : AbstractShardingFilterVirtualTableRoute<T, TKey> where T : class
|
||||
public abstract class AbstractShardingOperatorVirtualTableRoute<TEntity, TKey> : AbstractShardingFilterVirtualTableRoute<TEntity, TKey> where TEntity : class
|
||||
{
|
||||
protected override List<IPhysicTable> DoRouteWithPredicate(List<IPhysicTable> allPhysicTables, IQueryable queryable)
|
||||
{
|
||||
|
|
|
@ -71,6 +71,11 @@ namespace ShardingCore
|
|||
|
||||
internal static IServiceCollection AddInternalShardingCore(this IServiceCollection services)
|
||||
{
|
||||
|
||||
//添加创建TActualDbContext创建者
|
||||
services.TryAddSingleton(typeof(IShardingDbContextCreatorConfig<>),typeof(DefaultShardingDbContextCreatorConfig<>));
|
||||
|
||||
|
||||
services.TryAddSingleton(typeof(ITrackerManager<>),typeof(TrackerManager<>));
|
||||
services.TryAddSingleton(typeof(IStreamMergeContextFactory<>),typeof(StreamMergeContextFactory<>));
|
||||
services.TryAddSingleton(typeof(IShardingTableCreator<>),typeof(ShardingTableCreator<>));
|
||||
|
@ -103,7 +108,7 @@ namespace ShardingCore
|
|||
services.TryAddSingleton<IShardingPageAccessor, ShardingPageAccessor>();
|
||||
services.TryAddSingleton<IShardingBootstrapper, ShardingBootstrapper>();
|
||||
|
||||
services.AddShardingJob();
|
||||
services.TryAddShardingJob();
|
||||
return services;
|
||||
}
|
||||
public static DbContextOptionsBuilder UseSharding<TShardingDbContext>(this DbContextOptionsBuilder optionsBuilder) where TShardingDbContext : DbContext, IShardingDbContext
|
||||
|
|
|
@ -47,6 +47,10 @@ namespace ShardingCore.DIExtensions
|
|||
ShardingConfigOption.ParallelQueryTimeOut = shardingCoreBeginOptions.ParallelQueryTimeOut;
|
||||
ShardingConfigOption.CreateShardingTableOnStart = shardingCoreBeginOptions.CreateShardingTableOnStart;
|
||||
ShardingConfigOption.IgnoreCreateTableError = shardingCoreBeginOptions.IgnoreCreateTableError;
|
||||
foreach (var entityType in shardingCoreBeginOptions.GetCreateTableEntities())
|
||||
{
|
||||
ShardingConfigOption.AddEntityTryCreateTable(entityType);
|
||||
}
|
||||
|
||||
return new ShardingTransactionBuilder<TShardingDbContext>(this);
|
||||
//return new ShardingQueryBuilder<TShardingDbContext>(this);
|
||||
|
@ -98,5 +102,21 @@ namespace ShardingCore.DIExtensions
|
|||
/// 忽略建表时的错误
|
||||
/// </summary>
|
||||
public bool? IgnoreCreateTableError { get; set; }
|
||||
|
||||
private readonly ISet<Type> _createTableEntities = new HashSet<Type>();
|
||||
|
||||
public void AddEntitiesTryCreateTable(params Type[] entityTypes)
|
||||
{
|
||||
foreach (var entityType in entityTypes)
|
||||
{
|
||||
_createTableEntities.Add(entityType);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public ISet<Type> GetCreateTableEntities()
|
||||
{
|
||||
return _createTableEntities;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -50,11 +50,6 @@ namespace ShardingCore.DIExtensions
|
|||
.AddSingleton<IShardingDbContextOptionsBuilderConfig<TShardingDbContext>,
|
||||
ShardingDbContextOptionsBuilderConfig<TShardingDbContext>>(sp => config);
|
||||
|
||||
//添加创建TActualDbContext创建者
|
||||
services
|
||||
.AddSingleton<IShardingDbContextCreatorConfig,
|
||||
DefaultShardingDbContextCreatorConfig<TShardingDbContext>>(sp =>
|
||||
new DefaultShardingDbContextCreatorConfig<TShardingDbContext>());
|
||||
if (_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparerFactory != null)
|
||||
{
|
||||
services.AddSingleton<IShardingComparer<TShardingDbContext>>(_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparerFactory);
|
||||
|
|
|
@ -1,12 +1,7 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.Exceptions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.RouteTails.Abstractions;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
|
||||
namespace ShardingCore.DbContexts
|
||||
{
|
||||
|
@ -18,14 +13,11 @@ namespace ShardingCore.DbContexts
|
|||
*/
|
||||
public class ShardingDbContextFactory<TShardingDbContext> : IShardingDbContextFactory<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly IShardingDbContextCreatorConfig _shardingDbContextCreatorConfig;
|
||||
private readonly IShardingDbContextCreatorConfig<TShardingDbContext> _shardingDbContextCreatorConfig;
|
||||
|
||||
public ShardingDbContextFactory(IEnumerable<IShardingDbContextCreatorConfig> shardingDbContextCreatorConfigs)
|
||||
public ShardingDbContextFactory(IShardingDbContextCreatorConfig<TShardingDbContext> shardingDbContextCreatorConfig)
|
||||
{
|
||||
_shardingDbContextCreatorConfig = shardingDbContextCreatorConfigs
|
||||
.FirstOrDefault(o => o.ShardingDbContextType == typeof(TShardingDbContext))
|
||||
??throw new ShardingCoreException(
|
||||
$"{typeof(TShardingDbContext).FullName} cant found DefaultShardingDbContextCreatorConfig<{typeof(TShardingDbContext).Name}> should use {nameof(DIExtension.AddShardingDbContext)}");
|
||||
_shardingDbContextCreatorConfig = shardingDbContextCreatorConfig;
|
||||
}
|
||||
public DbContext Create(ShardingDbContextOptions shardingDbContextOptions)
|
||||
{
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace ShardingCore
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class DefaultShardingDbContextCreatorConfig<TShardingDbContext> : IShardingDbContextCreatorConfig
|
||||
public class DefaultShardingDbContextCreatorConfig<TShardingDbContext> : IShardingDbContextCreatorConfig<TShardingDbContext>
|
||||
where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
private readonly Func<ShardingDbContextOptions, DbContext> _creator;
|
||||
|
|
|
@ -40,6 +40,18 @@ namespace ShardingCore
|
|||
/// </summary>
|
||||
public bool? CreateShardingTableOnStart { get; set; }
|
||||
/// <summary>
|
||||
/// 添加尝试建表的对象
|
||||
/// </summary>
|
||||
/// <param name="entityType"></param>
|
||||
/// <returns></returns>
|
||||
public bool AddEntityTryCreateTable(Type entityType);
|
||||
/// <summary>
|
||||
/// 是否需要启动创建表
|
||||
/// </summary>
|
||||
/// <param name="entityType"></param>
|
||||
/// <returns></returns>
|
||||
public bool NeedCreateTable(Type entityType);
|
||||
/// <summary>
|
||||
/// 忽略建表时的错误
|
||||
/// </summary>
|
||||
public bool? IgnoreCreateTableError { get; set; }
|
||||
|
|
|
@ -14,9 +14,8 @@ namespace ShardingCore
|
|||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public interface IShardingDbContextCreatorConfig
|
||||
public interface IShardingDbContextCreatorConfig<TShardingDbContext> where TShardingDbContext : DbContext, IShardingDbContext
|
||||
{
|
||||
Type ShardingDbContextType { get; }
|
||||
|
||||
DbContext Creator(ShardingDbContextOptions shardingDbContextOptions);
|
||||
}
|
||||
|
|
|
@ -1,10 +1,13 @@
|
|||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace ShardingCore.Jobs.Abstaractions
|
||||
{
|
||||
internal interface IJob
|
||||
{
|
||||
string JobName { get; }
|
||||
string[] GetCronExpressions();
|
||||
Task ExecuteAsync();
|
||||
bool StartJob();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +0,0 @@
|
|||
namespace ShardingCore.Jobs.Abstaractions
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Friday, 08 January 2021 22:19:57
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
internal interface IJobExecutor
|
||||
{
|
||||
void Run();
|
||||
bool IsRunning();
|
||||
bool StartJob();
|
||||
void Complete();
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace ShardingCore.Jobs.Abstaractions
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Friday, 08 January 2021 22:18:30
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public interface IJobTrigger
|
||||
{
|
||||
DateTime NextJobRunUtcTime();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls;
|
||||
|
||||
|
@ -13,12 +14,10 @@ namespace ShardingCore.Jobs
|
|||
*/
|
||||
internal static class DIExtension
|
||||
{
|
||||
public static IServiceCollection AddShardingJob(this IServiceCollection services, Action<JobGlobalOptions> config=null)
|
||||
public static IServiceCollection TryAddShardingJob(this IServiceCollection services)
|
||||
{
|
||||
var option = new JobGlobalOptions();
|
||||
config?.Invoke(option);
|
||||
services.AddSingleton(sp => option).AddSingleton<IJobManager, InMemoryJobManager>()
|
||||
.AddSingleton<JobRunnerService>();
|
||||
services.TryAddSingleton<JobRunnerService>();
|
||||
services.TryAddSingleton<IJobManager, InMemoryJobManager>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,28 +0,0 @@
|
|||
using System;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
|
||||
namespace ShardingCore.Jobs.Extensions
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Wednesday, 06 January 2021 13:08:55
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
internal static class CommonExtension
|
||||
{
|
||||
|
||||
public static bool IsJobType(this Type entityType)
|
||||
{
|
||||
if (entityType == null)
|
||||
throw new ArgumentNullException(nameof(entityType));
|
||||
return typeof(IJob).IsAssignableFrom(entityType);
|
||||
}
|
||||
public static bool IsAsyncMethod(this MethodInfo method)
|
||||
{
|
||||
return (typeof(Task).IsAssignableFrom(method.ReturnType));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
using System;
|
||||
|
||||
namespace ShardingCore.Jobs.Impls.Attributes
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Wednesday, 06 January 2021 13:04:57
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
|
||||
internal class JobRunAttribute:Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 任务名称
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间
|
||||
/// </summary>
|
||||
public DateTime BeginUtcTime => GetBeginUtcTime();
|
||||
/// <summary>
|
||||
/// 任务开始时间优先级最高,格式:yyyy/MM/dd HH:mm:ss、yyyy-MM-dd HH:mm:ss
|
||||
/// </summary>
|
||||
public string Begin { get; set; }
|
||||
|
||||
private DateTime GetBeginUtcTime()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(Begin))
|
||||
{
|
||||
var localTime = Convert.ToDateTime(Begin);
|
||||
var utcOffset = TimeZoneInfo.Local.BaseUtcOffset.TotalMilliseconds;
|
||||
return localTime.AddMilliseconds(-utcOffset);
|
||||
}
|
||||
else
|
||||
{
|
||||
return DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 默认每分钟执行
|
||||
/// </summary>
|
||||
public string Cron { get; set; } = "0 * * * * ? *";
|
||||
/// <summary>
|
||||
/// 如果正在运行就跳过
|
||||
/// </summary>
|
||||
public bool SkipIfRunning { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 是否从di容器中获取
|
||||
/// </summary>
|
||||
public bool CreateFromServiceProvider { get; set; } = false;
|
||||
/// <summary>
|
||||
/// 是否马上执行在程序启动后 需要优先满足Begin条件
|
||||
/// </summary>
|
||||
public bool RunOnceOnStart { get; set; } = false;
|
||||
}
|
||||
}
|
|
@ -1,7 +1,9 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Cron;
|
||||
|
||||
namespace ShardingCore.Jobs.Impls
|
||||
{
|
||||
|
@ -32,28 +34,10 @@ namespace ShardingCore.Jobs.Impls
|
|||
/// </summary>
|
||||
public IJob JobInstance { get; set; }
|
||||
|
||||
public Type JobClass { get; set; }
|
||||
public MethodInfo JobMethod { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 开始时间
|
||||
/// </summary>
|
||||
public DateTime BeginUtcTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 表达式
|
||||
/// </summary>
|
||||
public string Cron { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否跳过如果正在运行
|
||||
/// </summary>
|
||||
public bool SkipIfRunning { get; set; }
|
||||
/// <summary>
|
||||
/// 是否从di容器中获取
|
||||
/// </summary>
|
||||
|
||||
public bool CreateFromServiceProvider { get; set; }
|
||||
public bool SkipIfRunning { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 下次运行时间
|
||||
|
@ -80,5 +64,12 @@ namespace ShardingCore.Jobs.Impls
|
|||
if (SkipIfRunning)
|
||||
runStatus = unrunning;
|
||||
}
|
||||
/// <summary>
|
||||
/// 计算下一次执行时间
|
||||
/// </summary>
|
||||
public void CalcNextUtcTime()
|
||||
{
|
||||
this.NextUtcTime= JobInstance.GetCronExpressions().Select(cron => new CronExpression(cron).GetTimeAfter(DateTime.UtcNow)).Min();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,16 +0,0 @@
|
|||
namespace ShardingCore.Jobs.Impls
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Friday, 08 January 2021 08:16:08
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
internal class JobGlobalOptions
|
||||
{
|
||||
/// <summary>
|
||||
/// 延迟启动时间
|
||||
/// </summary>
|
||||
public int DelaySecondsOnStart { get; set; } = 10;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Cron;
|
||||
using ShardingCore.Jobs.Impls;
|
||||
|
||||
namespace ShardingCore.Jobs
|
||||
{
|
||||
internal class JobEntryFactory
|
||||
{
|
||||
private JobEntryFactory(){throw new InvalidOperationException("cant create instance"); }
|
||||
|
||||
public static JobEntry Create(IJob job)
|
||||
{
|
||||
|
||||
var jobEntry = new JobEntry()
|
||||
{
|
||||
JobInstance = job,
|
||||
JobName = job.JobName,
|
||||
};
|
||||
jobEntry.CalcNextUtcTime();
|
||||
return jobEntry;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,10 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Cron;
|
||||
using ShardingCore.Jobs.Extensions;
|
||||
using ShardingCore.Jobs.Impls;
|
||||
|
||||
namespace ShardingCore.Jobs
|
||||
{
|
||||
|
@ -20,7 +17,6 @@ namespace ShardingCore.Jobs
|
|||
internal class JobRunnerService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly JobGlobalOptions _jobGlobalOptions;
|
||||
private readonly IJobManager _jobManager;
|
||||
private readonly ILogger<JobRunnerService> _logger;
|
||||
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
|
||||
|
@ -31,18 +27,15 @@ namespace ShardingCore.Jobs
|
|||
/// </summary>
|
||||
private const long MAX_DELAY_MILLIS = 30000L;
|
||||
|
||||
public JobRunnerService(IServiceProvider serviceProvider, JobGlobalOptions jobGlobalOptions, IJobManager jobManager, ILogger<JobRunnerService> logger)
|
||||
public JobRunnerService(IServiceProvider serviceProvider,IJobManager jobManager, ILogger<JobRunnerService> logger)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_jobGlobalOptions = jobGlobalOptions;
|
||||
_jobManager = jobManager;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task StartAsync()
|
||||
{
|
||||
if (_jobGlobalOptions.DelaySecondsOnStart > 0)
|
||||
await Task.Delay(TimeSpan.FromSeconds(_jobGlobalOptions.DelaySecondsOnStart), _cts.Token);
|
||||
while (!_cts.Token.IsCancellationRequested)
|
||||
{
|
||||
var delayMs = 0L;
|
||||
|
@ -115,31 +108,18 @@ namespace ShardingCore.Jobs
|
|||
Task.Factory.StartNew(async () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var scope = _serviceProvider.CreateScope())
|
||||
{
|
||||
var job = jobEntry.JobInstance;
|
||||
if (job == null)
|
||||
_logger.LogWarning($"### job [{jobEntry.JobName}] cant created, create method :{(jobEntry.CreateFromServiceProvider ? "from service provider" : "activator")}");
|
||||
var method = jobEntry.JobMethod;
|
||||
var @params = method.GetParameters().Select(x => scope.ServiceProvider.GetService(x.ParameterType)).ToArray();
|
||||
_logger.LogWarning($"### job [{jobEntry.JobName}] is null ");
|
||||
|
||||
_logger.LogInformation($"### job [{jobEntry.JobName}] start success.");
|
||||
if (method.IsAsyncMethod())
|
||||
{
|
||||
if (method.Invoke(job, @params) is Task task)
|
||||
await task;
|
||||
}
|
||||
else
|
||||
{
|
||||
method.Invoke(job, @params);
|
||||
}
|
||||
await job.ExecuteAsync();
|
||||
_logger.LogInformation($"### job [{jobEntry.JobName}] invoke complete.");
|
||||
jobEntry.NextUtcTime = new CronExpression(jobEntry.Cron).GetTimeAfter(DateTime.UtcNow);
|
||||
jobEntry.CalcNextUtcTime();
|
||||
if (!jobEntry.NextUtcTime.HasValue)
|
||||
_logger.LogWarning($"### job [{jobEntry.JobName}] is stopped.");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.LogError($"### job [{jobEntry.JobName}] invoke fail : {e}.");
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using ShardingCore.Jobs.Cron;
|
||||
using ShardingCore.Jobs.Extensions;
|
||||
using ShardingCore.Jobs.Impls;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
|
||||
namespace ShardingCore.Jobs
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: Wednesday, 06 January 2021 15:48:59
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
internal class JobTypeParser
|
||||
{
|
||||
private JobTypeParser(){}
|
||||
|
||||
public static List<JobEntry> Parse(Type type)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException(nameof(type));
|
||||
if (!type.IsJobType())
|
||||
throw new NotSupportedException(type.FullName);
|
||||
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
|
||||
var list = new List<JobEntry>();
|
||||
foreach (var method in methods)
|
||||
{
|
||||
var jobRun = method.GetCustomAttribute<JobRunAttribute>();
|
||||
if ( jobRun!= null)
|
||||
{
|
||||
var jobEntry = new JobEntry()
|
||||
{
|
||||
BeginUtcTime = jobRun.BeginUtcTime,
|
||||
Cron = jobRun.Cron,
|
||||
JobName = jobRun.Name ?? (type.Name + "." + method.Name),
|
||||
NextUtcTime = GetNextRunUtcTime(jobRun),
|
||||
SkipIfRunning = jobRun.SkipIfRunning,
|
||||
CreateFromServiceProvider = jobRun.CreateFromServiceProvider,
|
||||
JobClass = type,
|
||||
JobMethod = method
|
||||
};
|
||||
list.Add(jobEntry);
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断时间是否满足启动时间如果满足就判断是需要在程序启动时就执行一次,如果需要就返回当前时间,如果不需要就按启动时间生成下次执行时间
|
||||
/// </summary>
|
||||
/// <param name="jobRun"></param>
|
||||
/// <returns></returns>
|
||||
private static DateTime? GetNextRunUtcTime(JobRunAttribute jobRun)
|
||||
{
|
||||
var utcNow = DateTime.UtcNow;
|
||||
if (utcNow >= jobRun.BeginUtcTime)
|
||||
{
|
||||
if (jobRun.RunOnceOnStart)
|
||||
{
|
||||
return utcNow;
|
||||
}
|
||||
}
|
||||
return new CronExpression(jobRun.Cron).GetTimeAfter(jobRun.BeginUtcTime);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -23,6 +23,9 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
, IAsyncDisposable
|
||||
#endif
|
||||
{
|
||||
/// <summary>
|
||||
/// is default data source connection string
|
||||
/// </summary>
|
||||
bool IsDefault { get; }
|
||||
int DbContextCount { get; }
|
||||
DbContext CreateDbContext(IRouteTail routeTail);
|
||||
|
|
|
@ -44,8 +44,6 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
private readonly ActualConnectionStringManager<TShardingDbContext> _actualConnectionStringManager;
|
||||
private readonly IEntityMetadataManager<TShardingDbContext> _entityMetadataManager;
|
||||
|
||||
private bool IsBeginTransaction => _shardingDbContext.Database.CurrentTransaction != null;
|
||||
|
||||
public int ReadWriteSeparationPriority
|
||||
{
|
||||
get => _actualConnectionStringManager.ReadWriteSeparationPriority;
|
||||
|
@ -80,7 +78,7 @@ namespace ShardingCore.Sharding.ShardingDbContextExecutors
|
|||
|
||||
}
|
||||
/// <summary>
|
||||
/// 是否有多个dcontext
|
||||
/// has more db context
|
||||
/// </summary>
|
||||
public bool IsMultiDbContext =>
|
||||
_dbContextCaches.Count > 1 || _dbContextCaches.Sum(o => o.Value.DbContextCount) > 1;
|
||||
|
|
|
@ -133,7 +133,7 @@ namespace ShardingCore.Sharding.ShardingQueryExecutors
|
|||
// private readonly IStreamMergeContextFactory _streamMergeContextFactory;
|
||||
|
||||
|
||||
var streamMergeContextMethod = streamMergeContextFactory.GetType().GetMethod("Create");
|
||||
var streamMergeContextMethod = streamMergeContextFactory.GetType().GetMethod(nameof(IStreamMergeContextFactory.Create));
|
||||
if (streamMergeContextMethod == null)
|
||||
throw new ShardingCoreException("cant found IStreamMergeContextFactory method [Create]");
|
||||
var streamMergeContext = streamMergeContextMethod.MakeGenericMethod(new Type[] { queryEntityType }).Invoke(streamMergeContextFactory, new[] { queryable, shardingDbContext });
|
||||
|
|
|
@ -79,6 +79,10 @@ namespace ShardingCore.Sharding
|
|||
_source = source;
|
||||
_shardingDbContext = shardingDbContext;
|
||||
_routeTailFactory = routeTailFactory;
|
||||
DataSourceRouteResult = dataSourceRouteResult;
|
||||
TableRouteResults = tableRouteResults;
|
||||
IsCrossDataSource = dataSourceRouteResult.IntersectDataSources.Count > 1;
|
||||
IsCrossTable = tableRouteResults.Count() > 1;
|
||||
var reWriteResult = new ReWriteEngine<T>(source).ReWrite();
|
||||
Skip = reWriteResult.Skip;
|
||||
Take = reWriteResult.Take;
|
||||
|
@ -88,10 +92,6 @@ namespace ShardingCore.Sharding
|
|||
GroupByContext = reWriteResult.GroupByContext;
|
||||
_reWriteSource = reWriteResult.ReWriteQueryable;
|
||||
QueryEntities = source.ParseQueryableRoute();
|
||||
DataSourceRouteResult = dataSourceRouteResult;
|
||||
TableRouteResults = tableRouteResults;
|
||||
IsCrossDataSource = dataSourceRouteResult.IntersectDataSources.Count > 1;
|
||||
IsCrossTable = tableRouteResults.Count() > 1;
|
||||
_trackerManager =
|
||||
(ITrackerManager)ShardingContainer.GetService(
|
||||
typeof(ITrackerManager<>).GetGenericType0(shardingDbContext.GetType()));
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.DbContexts.ShardingDbContexts;
|
||||
|
||||
namespace ShardingCore
|
||||
{
|
||||
/*
|
||||
* @Author: xjm
|
||||
* @Description:
|
||||
* @Date: 2021/3/3 16:30:21
|
||||
* @Ver: 1.0
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public class ShardingConfigEntry
|
||||
{
|
||||
public ShardingConfigEntry(Func<ShardingDbContextOptions, DbContext> creator, Type dbContextType, Action<ShardingDbConfigOptions> shardingDbConfigConfigure)
|
||||
{
|
||||
Creator = creator;
|
||||
DbContextType = dbContextType;
|
||||
DbConfigOptions = new ShardingDbConfigOptions();
|
||||
shardingDbConfigConfigure?.Invoke(DbConfigOptions);
|
||||
}
|
||||
|
||||
public Type DbContextType { get; }
|
||||
public Func<ShardingDbContextOptions, DbContext> Creator { get; }
|
||||
public ShardingDbConfigOptions DbConfigOptions { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 连接字符串
|
||||
/// </summary>
|
||||
public string ConnectionString { get; set; }
|
||||
}
|
||||
}
|
|
@ -24,6 +24,7 @@ namespace ShardingCore
|
|||
{
|
||||
private readonly Dictionary<Type, Type> _virtualDataSourceRoutes = new Dictionary<Type, Type>();
|
||||
private readonly Dictionary<Type, Type> _virtualTableRoutes = new Dictionary<Type, Type>();
|
||||
private readonly ISet<Type> _createTableEntities = new HashSet<Type>();
|
||||
|
||||
public Action<DbConnection, DbContextOptionsBuilder> SameConnectionConfigure { get; private set; }
|
||||
public Action<string, DbContextOptionsBuilder> DefaultQueryConfigure { get; private set; }
|
||||
|
@ -200,6 +201,16 @@ namespace ShardingCore
|
|||
/// </summary>
|
||||
public bool? CreateShardingTableOnStart { get; set; }
|
||||
|
||||
public bool AddEntityTryCreateTable(Type entityType)
|
||||
{
|
||||
return _createTableEntities.Add(entityType);
|
||||
}
|
||||
|
||||
public bool NeedCreateTable(Type entityType)
|
||||
{
|
||||
return _createTableEntities.Contains(entityType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 忽略建表时的错误
|
||||
/// </summary>
|
||||
|
@ -212,7 +223,7 @@ namespace ShardingCore
|
|||
/// <summary>
|
||||
/// 单次查询并发线程数目(最小1)
|
||||
/// </summary>
|
||||
public int ParallelQueryMaxThreadCount { get; set; } = 65536;
|
||||
public int ParallelQueryMaxThreadCount { get; set; } = Environment.ProcessorCount*2;
|
||||
/// <summary>
|
||||
/// 默认30秒超时
|
||||
/// </summary>
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.TableCreator;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Abstractions
|
||||
{
|
||||
public abstract class AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey> : AbstractShardingOperatorVirtualTableRoute<TEntity, TKey>, IJob where TEntity : class
|
||||
{
|
||||
private readonly ILogger<AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey>> _logger;
|
||||
|
||||
protected AbstractShardingAutoCreateOperatorVirtualTableRoute()
|
||||
{
|
||||
_logger = ShardingContainer
|
||||
.GetService<ILogger<AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, TKey>>>();
|
||||
}
|
||||
public virtual string JobName =>
|
||||
$"{EntityMetadata?.ShardingDbContextType?.Name}:{EntityMetadata?.EntityType?.Name}";
|
||||
|
||||
public virtual bool StartJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
/// <summary>
|
||||
/// 显示错误日志
|
||||
/// </summary>
|
||||
public virtual bool ShowErrorLog => false;
|
||||
|
||||
public abstract string[] GetCronExpressions();
|
||||
|
||||
public Task ExecuteAsync()
|
||||
{
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(TEntity));
|
||||
_logger.LogDebug($"get {typeof(TEntity).Name}'s virtualTable ");
|
||||
if (virtualTable == null)
|
||||
{
|
||||
_logger.LogDebug($" {typeof(TEntity).Name}'s virtualTable is null");
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(typeof(IVirtualDataSource<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var tableCreator = (IShardingTableCreator)ShardingContainer.GetService(typeof(IShardingTableCreator<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var now = DateTime.Now.AddMinutes(10);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
ISet<string> dataSources = new HashSet<string>();
|
||||
if (entityMetadataManager.IsShardingDataSource(typeof(TEntity)))
|
||||
{
|
||||
var virtualDataSourceRoute = virtualDataSource.GetRoute(typeof(TEntity));
|
||||
foreach (var dataSourceName in virtualDataSourceRoute.GetAllDataSourceNames())
|
||||
{
|
||||
dataSources.Add(dataSourceName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
dataSources.Add(virtualDataSource.DefaultDataSourceName);
|
||||
}
|
||||
_logger.LogInformation($"auto create table data source names:[{string.Join(",", dataSources)}]");
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"begin table tail:[{tail}],entity:[{typeof(TEntity).Name}]");
|
||||
tableCreator.CreateTable(dataSource, typeof(TEntity), tail);
|
||||
_logger.LogInformation($"succeed table tail:[{tail}],entity:[{typeof(TEntity).Name}]");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
_logger.LogInformation($"warning table tail:[{tail}],entity:[{typeof(TEntity).Name}]");
|
||||
if (ShowErrorLog)
|
||||
_logger.LogError(e, $"{dataSource} {typeof(TEntity).Name}'s create table error ");
|
||||
}
|
||||
}
|
||||
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -14,8 +14,8 @@ namespace ShardingCore.VirtualRoutes.Abstractions
|
|||
/// <summary>
|
||||
/// time type is date time
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class AbstractShardingTimeKeyDateTimeVirtualTableRoute<T> : AbstractShardingOperatorVirtualTableRoute<T, DateTime> where T : class
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
public abstract class AbstractShardingTimeKeyDateTimeVirtualTableRoute<TEntity> : AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, DateTime> where TEntity : class
|
||||
{
|
||||
/// <summary>
|
||||
/// how convert object to date time
|
||||
|
|
|
@ -13,8 +13,8 @@ namespace ShardingCore.VirtualRoutes.Abstractions
|
|||
/// <summary>
|
||||
/// sharding table route by time stamp (ms)
|
||||
/// </summary>
|
||||
/// <typeparam name="T">entity</typeparam>
|
||||
public abstract class AbstractShardingTimeKeyLongVirtualTableRoute<T> : AbstractShardingOperatorVirtualTableRoute<T, long> where T : class
|
||||
/// <typeparam name="TEntity">entity</typeparam>
|
||||
public abstract class AbstractShardingTimeKeyLongVirtualTableRoute<TEntity> : AbstractShardingAutoCreateOperatorVirtualTableRoute<TEntity, long> where TEntity : class
|
||||
{
|
||||
/// <summary>
|
||||
/// how convert object to long
|
||||
|
|
|
@ -1,17 +1,8 @@
|
|||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
using ShardingCore.TableCreator;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Days
|
||||
{
|
||||
|
@ -21,7 +12,7 @@ namespace ShardingCore.VirtualRoutes.Days
|
|||
* @Date: Wednesday, 27 January 2021 08:41:05
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<T>:AbstractShardingTimeKeyDateTimeVirtualTableRoute<T>,IJob where T:class
|
||||
public abstract class AbstractSimpleShardingDayKeyDateTimeVirtualTableRoute<TEntity>:AbstractShardingTimeKeyDateTimeVirtualTableRoute<TEntity> where TEntity:class
|
||||
{
|
||||
/// <summary>
|
||||
/// begin time use fixed time eg.new DateTime(20xx,xx,xx)
|
||||
|
@ -84,74 +75,15 @@ namespace ShardingCore.VirtualRoutes.Days
|
|||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每天5点执行,启动的时候执行以下
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时创建表", Cron = "0 0 5 * * ?", RunOnceOnStart = true)]
|
||||
public virtual void AutoShardingTableCreate()
|
||||
|
||||
public override string[] GetCronExpressions()
|
||||
{
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(typeof(IVirtualDataSource<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var tableCreator = (IShardingTableCreator)ShardingContainer.GetService(typeof(IShardingTableCreator<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
return new[]
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(1);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
ISet<string> dataSources = new HashSet<string>()
|
||||
{
|
||||
virtualDataSource.DefaultDataSourceName
|
||||
"0 59 23 * * ?",
|
||||
"0 0 0 * * ?",
|
||||
"0 1 0 * * ?",
|
||||
};
|
||||
if (entityMetadataManager.IsShardingDataSource(typeof(T)))
|
||||
{
|
||||
var virtualDataSourceRoute = virtualDataSource.GetRoute(typeof(T));
|
||||
foreach (var dataSourceName in virtualDataSourceRoute.GetAllDataSourceNames())
|
||||
{
|
||||
dataSources.Add(dataSourceName);
|
||||
}
|
||||
|
||||
}
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
try
|
||||
{
|
||||
tableCreator.CreateTable(dataSource, typeof(T), tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每天晚上23点59分执行
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时添加虚拟表", Cron = "0 55 23 * * ?")]
|
||||
public virtual void AutoShardingTableAdd()
|
||||
{
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(1);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
|
||||
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public virtual string JobName =>
|
||||
$"{EntityMetadata?.ShardingDbContextType?.Name}:{EntityMetadata?.EntityType?.Name}";
|
||||
|
||||
public virtual bool StartJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,20 +1,9 @@
|
|||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
using ShardingCore.Sharding.Abstractions;
|
||||
using ShardingCore.TableCreator;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Days
|
||||
{
|
||||
|
@ -24,7 +13,7 @@ namespace ShardingCore.VirtualRoutes.Days
|
|||
* @Date: Wednesday, 27 January 2021 08:56:38
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractSimpleShardingDayKeyLongVirtualTableRoute<T>:AbstractShardingTimeKeyLongVirtualTableRoute<T>,IJob where T:class
|
||||
public abstract class AbstractSimpleShardingDayKeyLongVirtualTableRoute<TEntity>:AbstractShardingTimeKeyLongVirtualTableRoute<TEntity> where TEntity:class
|
||||
{
|
||||
public abstract DateTime GetBeginTime();
|
||||
public override List<string> GetAllTails()
|
||||
|
@ -82,74 +71,14 @@ namespace ShardingCore.VirtualRoutes.Days
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每天5点执行,启动的时候执行以下
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时创建表", Cron = "0 0 5 * * ?", RunOnceOnStart = true)]
|
||||
public virtual void AutoShardingTableCreate()
|
||||
public override string[] GetCronExpressions()
|
||||
{
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(typeof(IVirtualDataSource<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var tableCreator = (IShardingTableCreator)ShardingContainer.GetService(typeof(IShardingTableCreator<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
return new[]
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(1);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
ISet<string> dataSources = new HashSet<string>()
|
||||
{
|
||||
virtualDataSource.DefaultDataSourceName
|
||||
"0 59 23 * * ?",
|
||||
"0 0 0 * * ?",
|
||||
"0 1 0 * * ?",
|
||||
};
|
||||
if (entityMetadataManager.IsShardingDataSource(typeof(T)))
|
||||
{
|
||||
var virtualDataSourceRoute = virtualDataSource.GetRoute(typeof(T));
|
||||
foreach (var dataSourceName in virtualDataSourceRoute.GetAllDataSourceNames())
|
||||
{
|
||||
dataSources.Add(dataSourceName);
|
||||
}
|
||||
|
||||
}
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
try
|
||||
{
|
||||
tableCreator.CreateTable(dataSource, typeof(T), tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每天晚上23点59分执行
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时添加虚拟表", Cron = "0 55 23 * * ?")]
|
||||
public virtual void AutoShardingTableAdd()
|
||||
{
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(1);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
|
||||
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
|
||||
|
||||
}
|
||||
|
||||
public virtual string JobName =>
|
||||
$"{EntityMetadata?.ShardingDbContextType?.Name}:{EntityMetadata?.EntityType?.Name}";
|
||||
|
||||
public virtual bool StartJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,8 +18,8 @@ namespace ShardingCore.VirtualRoutes.Mods
|
|||
/// <summary>
|
||||
/// 分表字段为int的取模分表
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class AbstractSimpleShardingModKeyIntVirtualTableRoute<T>: AbstractShardingOperatorVirtualTableRoute<T,int> where T:class
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
public abstract class AbstractSimpleShardingModKeyIntVirtualTableRoute<TEntity>: AbstractShardingOperatorVirtualTableRoute<TEntity,int> where TEntity:class
|
||||
{
|
||||
protected readonly int Mod;
|
||||
protected readonly int TailLength;
|
||||
|
|
|
@ -1,19 +1,10 @@
|
|||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
|
||||
using ShardingCore.Helpers;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Core.VirtualRoutes.TableRoutes.Abstractions;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
using ShardingCore.TableCreator;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Mods
|
||||
{
|
||||
|
@ -26,8 +17,8 @@ namespace ShardingCore.VirtualRoutes.Mods
|
|||
/// <summary>
|
||||
/// 分表字段为string的取模分表
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public abstract class AbstractSimpleShardingModKeyStringVirtualTableRoute<T>: AbstractShardingOperatorVirtualTableRoute<T,string> where T:class
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
public abstract class AbstractSimpleShardingModKeyStringVirtualTableRoute<TEntity>: AbstractShardingOperatorVirtualTableRoute<TEntity,string> where TEntity:class
|
||||
{
|
||||
protected readonly int Mod;
|
||||
protected readonly int TailLength;
|
||||
|
|
|
@ -1,18 +1,9 @@
|
|||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
using ShardingCore.TableCreator;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Months
|
||||
{
|
||||
|
@ -22,7 +13,7 @@ namespace ShardingCore.VirtualRoutes.Months
|
|||
* @Date: Wednesday, 27 January 2021 12:27:09
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<T> : AbstractShardingTimeKeyDateTimeVirtualTableRoute<T>,IJob where T : class
|
||||
public abstract class AbstractSimpleShardingMonthKeyDateTimeVirtualTableRoute<TEntity> : AbstractShardingTimeKeyDateTimeVirtualTableRoute<TEntity> where TEntity : class
|
||||
{
|
||||
public abstract DateTime GetBeginTime();
|
||||
public override List<string> GetAllTails()
|
||||
|
@ -76,76 +67,14 @@ namespace ShardingCore.VirtualRoutes.Months
|
|||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每个月20号5点会创建对应的表
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时创建表", Cron = "0 0 5 20 * ?", RunOnceOnStart = true)]
|
||||
public virtual void AutoShardingTableCreate()
|
||||
public override string[] GetCronExpressions()
|
||||
{
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(typeof(IVirtualDataSource<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var tableCreator = (IShardingTableCreator)ShardingContainer.GetService(typeof(IShardingTableCreator<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
return new[]
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(3);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
ISet<string> dataSources = new HashSet<string>()
|
||||
{
|
||||
virtualDataSource.DefaultDataSourceName
|
||||
"0 59 23 28,29,30,31 * ?",
|
||||
"0 0 0 1 * ?",
|
||||
"0 1 0 1 * ?",
|
||||
};
|
||||
if (entityMetadataManager.IsShardingDataSource(typeof(T)))
|
||||
{
|
||||
var virtualDataSourceRoute = virtualDataSource.GetRoute(typeof(T));
|
||||
foreach (var dataSourceName in virtualDataSourceRoute.GetAllDataSourceNames())
|
||||
{
|
||||
dataSources.Add(dataSourceName);
|
||||
}
|
||||
|
||||
}
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
try
|
||||
{
|
||||
tableCreator.CreateTable(dataSource, typeof(T), tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每月最后一天需要程序自己判断是不是最后一天
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时添加虚拟表", Cron = "0 55 23 28,29,30,31 * ?")]
|
||||
public virtual void AutoShardingTableAdd()
|
||||
{
|
||||
var nowDateTime = DateTime.Now.Date;
|
||||
var lastMonthDay = ShardingCoreHelper.GetNextMonthFirstDay(nowDateTime).AddDays(-1);
|
||||
if (lastMonthDay.Date != nowDateTime)
|
||||
return;
|
||||
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(7);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
}
|
||||
|
||||
public virtual string JobName =>
|
||||
$"{EntityMetadata?.ShardingDbContextType?.Name}:{EntityMetadata?.EntityType?.Name}";
|
||||
|
||||
public virtual bool StartJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,18 +1,9 @@
|
|||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
using ShardingCore.TableCreator;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Months
|
||||
{
|
||||
|
@ -22,7 +13,7 @@ namespace ShardingCore.VirtualRoutes.Months
|
|||
* @Date: Wednesday, 27 January 2021 13:09:52
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractSimpleShardingMonthKeyLongVirtualTableRoute<T>:AbstractShardingTimeKeyLongVirtualTableRoute<T>,IJob where T:class
|
||||
public abstract class AbstractSimpleShardingMonthKeyLongVirtualTableRoute<TEntity>:AbstractShardingTimeKeyLongVirtualTableRoute<TEntity> where TEntity:class
|
||||
{
|
||||
public abstract DateTime GetBeginTime();
|
||||
public override List<string> GetAllTails()
|
||||
|
@ -79,77 +70,14 @@ namespace ShardingCore.VirtualRoutes.Months
|
|||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每个月20号5点会创建对应的表
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时创建表", Cron = "0 0 5 20 * ?", RunOnceOnStart = true)]
|
||||
public virtual void AutoShardingTableCreate()
|
||||
public override string[] GetCronExpressions()
|
||||
{
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(typeof(IVirtualDataSource<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var tableCreator = (IShardingTableCreator)ShardingContainer.GetService(typeof(IShardingTableCreator<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
return new[]
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(3);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
ISet<string> dataSources = new HashSet<string>()
|
||||
{
|
||||
virtualDataSource.DefaultDataSourceName
|
||||
"0 59 23 28,29,30,31 * ?",
|
||||
"0 0 0 1 * ?",
|
||||
"0 1 0 1 * ?",
|
||||
};
|
||||
if (entityMetadataManager.IsShardingDataSource(typeof(T)))
|
||||
{
|
||||
var virtualDataSourceRoute = virtualDataSource.GetRoute(typeof(T));
|
||||
foreach (var dataSourceName in virtualDataSourceRoute.GetAllDataSourceNames())
|
||||
{
|
||||
dataSources.Add(dataSourceName);
|
||||
}
|
||||
|
||||
}
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
try
|
||||
{
|
||||
tableCreator.CreateTable(dataSource, typeof(T), tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每月最后一天需要程序自己判断是不是最后一天
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时添加虚拟表", Cron = "0 55 23 28,29,30,31 * ?")]
|
||||
public virtual void AutoShardingTableAdd()
|
||||
{
|
||||
var nowDateTime = DateTime.Now.Date;
|
||||
var lastMonthDay = ShardingCoreHelper.GetNextMonthFirstDay(nowDateTime).AddDays(-1);
|
||||
if (lastMonthDay.Date != nowDateTime)
|
||||
return;
|
||||
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(7);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
}
|
||||
|
||||
public virtual string JobName =>
|
||||
$"{EntityMetadata?.ShardingDbContextType?.Name}:{EntityMetadata?.EntityType?.Name}";
|
||||
|
||||
public virtual bool StartJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,18 +1,9 @@
|
|||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
using ShardingCore.TableCreator;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Weeks
|
||||
{
|
||||
|
@ -22,7 +13,7 @@ namespace ShardingCore.VirtualRoutes.Weeks
|
|||
* @Date: Wednesday, 27 January 2021 12:40:27
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractSimpleShardingWeekKeyDateTimeVirtualTableRoute<T> : AbstractShardingTimeKeyDateTimeVirtualTableRoute<T>, IJob where T : class
|
||||
public abstract class AbstractSimpleShardingWeekKeyDateTimeVirtualTableRoute<TEntity> : AbstractShardingTimeKeyDateTimeVirtualTableRoute<TEntity> where TEntity : class
|
||||
{
|
||||
public abstract DateTime GetBeginTime();
|
||||
public override List<string> GetAllTails()
|
||||
|
@ -78,70 +69,14 @@ namespace ShardingCore.VirtualRoutes.Weeks
|
|||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每周五 5点
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时创建表", Cron = "0 0 5 ? * 6", RunOnceOnStart = true)]
|
||||
public virtual void AutoShardingTableCreate()
|
||||
public override string[] GetCronExpressions()
|
||||
{
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(typeof(IVirtualDataSource<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var tableCreator = (IShardingTableCreator)ShardingContainer.GetService(typeof(IShardingTableCreator<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
return new[]
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(3);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
ISet<string> dataSources = new HashSet<string>()
|
||||
{
|
||||
virtualDataSource.DefaultDataSourceName
|
||||
"0 59 23 ? * 1",
|
||||
"0 0 0 ? * 2",
|
||||
"0 1 0 ? * 2",
|
||||
};
|
||||
if (entityMetadataManager.IsShardingDataSource(typeof(T)))
|
||||
{
|
||||
var virtualDataSourceRoute = virtualDataSource.GetRoute(typeof(T));
|
||||
foreach (var dataSourceName in virtualDataSourceRoute.GetAllDataSourceNames())
|
||||
{
|
||||
dataSources.Add(dataSourceName);
|
||||
}
|
||||
}
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
try
|
||||
{
|
||||
tableCreator.CreateTable(dataSource, typeof(T), tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每周末晚上23点55分添加数据
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时添加虚拟表", Cron = "0 55 23 ? * 1")]
|
||||
public virtual void AutoShardingTableAdd()
|
||||
{
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(3);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
}
|
||||
|
||||
public virtual string JobName =>
|
||||
$"{EntityMetadata?.ShardingDbContextType?.Name}:{EntityMetadata?.EntityType?.Name}";
|
||||
|
||||
public virtual bool StartJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,3 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
|
@ -9,10 +5,11 @@ using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
|||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
using ShardingCore.TableCreator;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Weeks
|
||||
{
|
||||
|
@ -22,7 +19,7 @@ namespace ShardingCore.VirtualRoutes.Weeks
|
|||
* @Date: Wednesday, 27 January 2021 13:12:50
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractSimpleShardingWeekKeyLongVirtualTableRoute<T> : AbstractShardingTimeKeyLongVirtualTableRoute<T>,IJob where T : class
|
||||
public abstract class AbstractSimpleShardingWeekKeyLongVirtualTableRoute<TEntity> : AbstractShardingTimeKeyLongVirtualTableRoute<TEntity> where TEntity : class
|
||||
{
|
||||
|
||||
public abstract DateTime GetBeginTime();
|
||||
|
@ -82,70 +79,14 @@ namespace ShardingCore.VirtualRoutes.Weeks
|
|||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每周五 5点
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时创建表", Cron = "0 0 5 ? * 6", RunOnceOnStart = true)]
|
||||
public virtual void AutoShardingTableCreate()
|
||||
public override string[] GetCronExpressions()
|
||||
{
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(typeof(IVirtualDataSource<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var tableCreator = (IShardingTableCreator)ShardingContainer.GetService(typeof(IShardingTableCreator<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
return new[]
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(3);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
ISet<string> dataSources = new HashSet<string>()
|
||||
{
|
||||
virtualDataSource.DefaultDataSourceName
|
||||
"0 59 23 ? * 1",
|
||||
"0 0 0 ? * 2",
|
||||
"0 1 0 ? * 2",
|
||||
};
|
||||
if (entityMetadataManager.IsShardingDataSource(typeof(T)))
|
||||
{
|
||||
var virtualDataSourceRoute = virtualDataSource.GetRoute(typeof(T));
|
||||
foreach (var dataSourceName in virtualDataSourceRoute.GetAllDataSourceNames())
|
||||
{
|
||||
dataSources.Add(dataSourceName);
|
||||
}
|
||||
}
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
try
|
||||
{
|
||||
tableCreator.CreateTable(dataSource, typeof(T), tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每周末晚上23点55分添加数据
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时添加虚拟表", Cron = "0 55 23 ? * 1")]
|
||||
public virtual void AutoShardingTableAdd()
|
||||
{
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(3);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
}
|
||||
|
||||
public virtual string JobName =>
|
||||
$"{EntityMetadata?.ShardingDbContextType?.Name}:{EntityMetadata?.EntityType?.Name}";
|
||||
|
||||
public virtual bool StartJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,17 +1,8 @@
|
|||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
using ShardingCore.TableCreator;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Years
|
||||
{
|
||||
|
@ -21,7 +12,7 @@ namespace ShardingCore.VirtualRoutes.Years
|
|||
* @Date: Wednesday, 27 January 2021 13:00:57
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractSimpleShardingYearKeyDateTimeVirtualTableRoute<T> : AbstractShardingTimeKeyDateTimeVirtualTableRoute<T>,IJob where T : class
|
||||
public abstract class AbstractSimpleShardingYearKeyDateTimeVirtualTableRoute<TEntity> : AbstractShardingTimeKeyDateTimeVirtualTableRoute<TEntity> where TEntity : class
|
||||
{
|
||||
public abstract DateTime GetBeginTime();
|
||||
public override List<string> GetAllTails()
|
||||
|
@ -74,70 +65,14 @@ namespace ShardingCore.VirtualRoutes.Years
|
|||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每年12月20号自动创建表
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时创建表", Cron = "0 0 5 20,21,22,23,24,25,26,27,28,29,30,31 12 ?", RunOnceOnStart = true)]
|
||||
public virtual void AutoShardingTableCreate()
|
||||
public override string[] GetCronExpressions()
|
||||
{
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(typeof(IVirtualDataSource<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var tableCreator = (IShardingTableCreator)ShardingContainer.GetService(typeof(IShardingTableCreator<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
return new[]
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddMonths(1);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
ISet<string> dataSources = new HashSet<string>()
|
||||
{
|
||||
virtualDataSource.DefaultDataSourceName
|
||||
"0 59 23 31 12 ?",
|
||||
"0 0 0 1 1 ?",
|
||||
"0 1 0 1 1 ?",
|
||||
};
|
||||
if (entityMetadataManager.IsShardingDataSource(typeof(T)))
|
||||
{
|
||||
var virtualDataSourceRoute = virtualDataSource.GetRoute(typeof(T));
|
||||
foreach (var dataSourceName in virtualDataSourceRoute.GetAllDataSourceNames())
|
||||
{
|
||||
dataSources.Add(dataSourceName);
|
||||
}
|
||||
}
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
try
|
||||
{
|
||||
tableCreator.CreateTable(dataSource, typeof(T), tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每年12月最后一天添加下一年的数据
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时添加虚拟表", Cron = "0 55 23 31 12 ?")]
|
||||
public virtual void AutoShardingTableAdd()
|
||||
{
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(7);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
}
|
||||
|
||||
public virtual string JobName =>
|
||||
$"{EntityMetadata?.ShardingDbContextType?.Name}:{EntityMetadata?.EntityType?.Name}";
|
||||
|
||||
public virtual bool StartJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,18 +1,9 @@
|
|||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using ShardingCore.Core;
|
||||
using ShardingCore.Core.EntityMetadatas;
|
||||
using ShardingCore.Core.PhysicTables;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualDataSources;
|
||||
using ShardingCore.Core.VirtualDatabase.VirtualTables;
|
||||
using ShardingCore.Core.VirtualRoutes;
|
||||
using ShardingCore.Extensions;
|
||||
using ShardingCore.Helpers;
|
||||
using ShardingCore.Jobs.Abstaractions;
|
||||
using ShardingCore.Jobs.Impls.Attributes;
|
||||
using ShardingCore.TableCreator;
|
||||
using ShardingCore.VirtualRoutes.Abstractions;
|
||||
|
||||
namespace ShardingCore.VirtualRoutes.Years
|
||||
{
|
||||
|
@ -22,7 +13,7 @@ namespace ShardingCore.VirtualRoutes.Years
|
|||
* @Date: Wednesday, 27 January 2021 13:17:24
|
||||
* @Email: 326308290@qq.com
|
||||
*/
|
||||
public abstract class AbstractSimpleShardingYearKeyLongVirtualTableRoute<T> : AbstractShardingTimeKeyLongVirtualTableRoute<T>,IJob where T : class
|
||||
public abstract class AbstractSimpleShardingYearKeyLongVirtualTableRoute<TEntity> : AbstractShardingTimeKeyLongVirtualTableRoute<TEntity> where TEntity : class
|
||||
{
|
||||
public abstract DateTime GetBeginTime();
|
||||
public override List<string> GetAllTails()
|
||||
|
@ -42,6 +33,7 @@ namespace ShardingCore.VirtualRoutes.Years
|
|||
tails.Add(tail);
|
||||
currentTimeStamp = currentTimeStamp.AddYears(1);
|
||||
}
|
||||
|
||||
return tails;
|
||||
}
|
||||
protected override string TimeFormatToTail(long time)
|
||||
|
@ -79,71 +71,14 @@ namespace ShardingCore.VirtualRoutes.Years
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 每年12月20号自动创建表
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时创建表", Cron = "0 0 5 20,21,22,23,24,25,26,27,28,29,30,31 12 ?", RunOnceOnStart = true)]
|
||||
public virtual void AutoShardingTableCreate()
|
||||
public override string[] GetCronExpressions()
|
||||
{
|
||||
var entityMetadataManager = (IEntityMetadataManager)ShardingContainer.GetService(typeof(IEntityMetadataManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualDataSource = (IVirtualDataSource)ShardingContainer.GetService(typeof(IVirtualDataSource<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var tableCreator = (IShardingTableCreator)ShardingContainer.GetService(typeof(IShardingTableCreator<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
return new[]
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddMonths(1);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
ISet<string> dataSources = new HashSet<string>()
|
||||
{
|
||||
virtualDataSource.DefaultDataSourceName
|
||||
"0 59 23 31 12 ?",
|
||||
"0 0 0 1 1 ?",
|
||||
"0 1 0 1 1 ?",
|
||||
};
|
||||
if (entityMetadataManager.IsShardingDataSource(typeof(T)))
|
||||
{
|
||||
var virtualDataSourceRoute = virtualDataSource.GetRoute(typeof(T));
|
||||
foreach (var dataSourceName in virtualDataSourceRoute.GetAllDataSourceNames())
|
||||
{
|
||||
dataSources.Add(dataSourceName);
|
||||
}
|
||||
}
|
||||
foreach (var dataSource in dataSources)
|
||||
{
|
||||
try
|
||||
{
|
||||
tableCreator.CreateTable(dataSource, typeof(T), tail);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 每年12月最后一天添加下一年的数据
|
||||
/// </summary>
|
||||
[JobRun(Name = "定时添加虚拟表", Cron = "0 55 23 31 12 ?")]
|
||||
public virtual void AutoShardingTableAdd()
|
||||
{
|
||||
var virtualTableManager = (IVirtualTableManager)ShardingContainer.GetService(typeof(IVirtualTableManager<>).GetGenericType0(EntityMetadata.ShardingDbContextType));
|
||||
var virtualTable = virtualTableManager.GetVirtualTable(typeof(T));
|
||||
if (virtualTable == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
var now = DateTime.Now.Date.AddDays(7);
|
||||
var tail = virtualTable.GetVirtualRoute().ShardingKeyToTail(now);
|
||||
virtualTableManager.AddPhysicTable(virtualTable, new DefaultPhysicTable(virtualTable, tail));
|
||||
}
|
||||
|
||||
public virtual string JobName =>
|
||||
$"{EntityMetadata?.ShardingDbContextType?.Name}:{EntityMetadata?.EntityType?.Name}";
|
||||
|
||||
public virtual bool StartJob()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue