diff --git a/src/DotNetCore.CAP.Dashboard/DashboardRoutes.cs b/src/DotNetCore.CAP.Dashboard/DashboardRoutes.cs index bfb7aa1..35105cf 100644 --- a/src/DotNetCore.CAP.Dashboard/DashboardRoutes.cs +++ b/src/DotNetCore.CAP.Dashboard/DashboardRoutes.cs @@ -128,7 +128,7 @@ namespace DotNetCore.CAP.Dashboard Routes.AddRazorPage("/nodes", x => { - var id = x.Request.Cookies["cap.node"]; + var id = x.Request.Cookies.ContainsKey("cap.node") ? x.Request.Cookies["cap.node"] : string.Empty; return new NodePage(id); }); diff --git a/src/DotNetCore.CAP.MySql/IDataStorage.MySql.cs b/src/DotNetCore.CAP.MySql/IDataStorage.MySql.cs index 4db2b94..b0aced0 100644 --- a/src/DotNetCore.CAP.MySql/IDataStorage.MySql.cs +++ b/src/DotNetCore.CAP.MySql/IDataStorage.MySql.cs @@ -141,22 +141,11 @@ namespace DotNetCore.CAP.MySql new MySqlParameter("@timeout", timeout), new MySqlParameter("@batchCount", batchCount)); } - public async Task> GetPublishedMessagesOfNeedRetry() - { - var fourMinAgo = DateTime.Now.AddMinutes(-4).ToString("O"); - var sql = $"SELECT * FROM `{_pubName}` WHERE `Retries`<{_capOptions.Value.FailedRetryCount} AND `Version`='{_capOptions.Value.Version}' AND `Added`<'{fourMinAgo}' AND (`StatusName` = '{StatusName.Failed}' OR `StatusName` = '{StatusName.Scheduled}') LIMIT 200;"; - - return await GetMessagesOfNeedRetryAsync(sql); - } - - public async Task> GetReceivedMessagesOfNeedRetry() - { - var fourMinAgo = DateTime.Now.AddMinutes(-4).ToString("O"); - var sql = - $"SELECT * FROM `{_recName}` WHERE `Retries`<{_capOptions.Value.FailedRetryCount} AND `Version`='{_capOptions.Value.Version}' AND `Added`<'{fourMinAgo}' AND (`StatusName` = '{StatusName.Failed}' OR `StatusName` = '{StatusName.Scheduled}') LIMIT 200;"; + public async Task> GetPublishedMessagesOfNeedRetry() => + await GetMessagesOfNeedRetryAsync(_pubName); - return await GetMessagesOfNeedRetryAsync(sql); - } + public async Task> GetReceivedMessagesOfNeedRetry() => + await GetMessagesOfNeedRetryAsync(_recName); public IMonitoringApi GetMonitoringApi() { @@ -189,8 +178,13 @@ namespace DotNetCore.CAP.MySql connection.ExecuteNonQuery(sql, sqlParams: sqlParams); } - private async Task> GetMessagesOfNeedRetryAsync(string sql) + private async Task> GetMessagesOfNeedRetryAsync(string tableName) { + var fourMinAgo = DateTime.Now.AddMinutes(-4).ToString("O"); + var sql = + $"SELECT `Id`,`Content`,`Retries`,`Added` FROM `{tableName}` WHERE `Retries`<{_capOptions.Value.FailedRetryCount} " + + $"AND `Version`='{_capOptions.Value.Version}' AND `Added`<'{fourMinAgo}' AND (`StatusName` = '{StatusName.Failed}' OR `StatusName` = '{StatusName.Scheduled}') LIMIT 200;"; + await using var connection = new MySqlConnection(_options.Value.ConnectionString); var result = connection.ExecuteReader(sql, reader => { diff --git a/src/DotNetCore.CAP.PostgreSql/IDataStorage.PostgreSql.cs b/src/DotNetCore.CAP.PostgreSql/IDataStorage.PostgreSql.cs index 1cf0ce7..aa55d34 100644 --- a/src/DotNetCore.CAP.PostgreSql/IDataStorage.PostgreSql.cs +++ b/src/DotNetCore.CAP.PostgreSql/IDataStorage.PostgreSql.cs @@ -143,23 +143,11 @@ namespace DotNetCore.CAP.PostgreSql return await Task.FromResult(count); } - public async Task> GetPublishedMessagesOfNeedRetry() - { - var fourMinAgo = DateTime.Now.AddMinutes(-4).ToString("O"); - var sql = - $"SELECT * FROM {_pubName} WHERE \"Retries\"<{_capOptions.Value.FailedRetryCount} AND \"Version\"='{_capOptions.Value.Version}' AND \"Added\"<'{fourMinAgo}' AND (\"StatusName\"='{StatusName.Failed}' OR \"StatusName\"='{StatusName.Scheduled}') LIMIT 200;"; + public async Task> GetPublishedMessagesOfNeedRetry() => + await GetMessagesOfNeedRetryAsync(_pubName); - return await GetMessagesOfNeedRetryAsync(sql); - } - - public async Task> GetReceivedMessagesOfNeedRetry() - { - var fourMinAgo = DateTime.Now.AddMinutes(-4).ToString("O"); - var sql = - $"SELECT * FROM {_recName} WHERE \"Retries\"<{_capOptions.Value.FailedRetryCount} AND \"Version\"='{_capOptions.Value.Version}' AND \"Added\"<'{fourMinAgo}' AND (\"StatusName\"='{StatusName.Failed}' OR \"StatusName\"='{StatusName.Scheduled}') LIMIT 200;"; - - return await GetMessagesOfNeedRetryAsync(sql); - } + public async Task> GetReceivedMessagesOfNeedRetry() => + await GetMessagesOfNeedRetryAsync(_recName); public IMonitoringApi GetMonitoringApi() { @@ -195,8 +183,13 @@ namespace DotNetCore.CAP.PostgreSql connection.ExecuteNonQuery(sql, sqlParams: sqlParams); } - private async Task> GetMessagesOfNeedRetryAsync(string sql) + private async Task> GetMessagesOfNeedRetryAsync(string tableName) { + var fourMinAgo = DateTime.Now.AddMinutes(-4).ToString("O"); + var sql = + $"SELECT \"Id\",\"Content\",\"Retries\",\"Added\" FROM {tableName} WHERE \"Retries\"<{_capOptions.Value.FailedRetryCount} " + + $"AND \"Version\"='{_capOptions.Value.Version}' AND \"Added\"<'{fourMinAgo}' AND (\"StatusName\"='{StatusName.Failed}' OR \"StatusName\"='{StatusName.Scheduled}') LIMIT 200;"; + await using var connection = new NpgsqlConnection(_options.Value.ConnectionString); var result = connection.ExecuteReader(sql, reader => { diff --git a/src/DotNetCore.CAP.SqlServer/IDataStorage.SqlServer.cs b/src/DotNetCore.CAP.SqlServer/IDataStorage.SqlServer.cs index 20db036..dc83dec 100644 --- a/src/DotNetCore.CAP.SqlServer/IDataStorage.SqlServer.cs +++ b/src/DotNetCore.CAP.SqlServer/IDataStorage.SqlServer.cs @@ -142,24 +142,11 @@ namespace DotNetCore.CAP.SqlServer return await Task.FromResult(count); } - public async Task> GetPublishedMessagesOfNeedRetry() - { - var fourMinAgo = DateTime.Now.AddMinutes(-4).ToString("O"); - var sql = $"SELECT TOP (200) Id, Content, Retries, Added FROM {_pubName} WITH (readpast) WHERE Retries<{_capOptions.Value.FailedRetryCount} " + - $"AND Version='{_capOptions.Value.Version}' AND Added<'{fourMinAgo}' AND (StatusName = '{StatusName.Failed}' OR StatusName = '{StatusName.Scheduled}')"; - - return await GetMessagesOfNeedRetryAsync(sql); - } - - public async Task> GetReceivedMessagesOfNeedRetry() - { - var fourMinAgo = DateTime.Now.AddMinutes(-4).ToString("O"); - var sql = - $"SELECT TOP (200) Id, Content, Retries, Added FROM {_recName} WITH (readpast) WHERE Retries<{_capOptions.Value.FailedRetryCount} " + - $"AND Version='{_capOptions.Value.Version}' AND Added<'{fourMinAgo}' AND (StatusName = '{StatusName.Failed}' OR StatusName = '{StatusName.Scheduled}')"; + public async Task> GetPublishedMessagesOfNeedRetry() => + await GetMessagesOfNeedRetryAsync(_pubName); - return await GetMessagesOfNeedRetryAsync(sql); - } + public async Task> GetReceivedMessagesOfNeedRetry() => + await GetMessagesOfNeedRetryAsync(_recName); public IMonitoringApi GetMonitoringApi() { @@ -195,8 +182,13 @@ namespace DotNetCore.CAP.SqlServer connection.ExecuteNonQuery(sql, sqlParams: sqlParams); } - private async Task> GetMessagesOfNeedRetryAsync(string sql) + private async Task> GetMessagesOfNeedRetryAsync(string tableName) { + var fourMinAgo = DateTime.Now.AddMinutes(-4).ToString("O"); + var sql = + $"SELECT TOP (200) Id, Content, Retries, Added FROM {tableName} WITH (readpast) WHERE Retries<{_capOptions.Value.FailedRetryCount} " + + $"AND Version='{_capOptions.Value.Version}' AND Added<'{fourMinAgo}' AND (StatusName = '{StatusName.Failed}' OR StatusName = '{StatusName.Scheduled}')"; + List result; using (var connection = new SqlConnection(_options.Value.ConnectionString)) { diff --git a/src/DotNetCore.CAP.SqlServer/IMonitoringApi.SqlServer.cs b/src/DotNetCore.CAP.SqlServer/IMonitoringApi.SqlServer.cs index 3e908d5..0bbfc58 100644 --- a/src/DotNetCore.CAP.SqlServer/IMonitoringApi.SqlServer.cs +++ b/src/DotNetCore.CAP.SqlServer/IMonitoringApi.SqlServer.cs @@ -193,7 +193,7 @@ with aggr as ( where StatusName = @statusName group by replace(convert(varchar, Added, 111), '/','-') + '-' + CONVERT(varchar, DATEPART(hh, Added)) ) -select [Key], [Count] from aggr with (nolock) where [Key] in @keys;"; +select [Key], [Count] from aggr with (nolock) where [Key] >= @minKey and [Key] <= @maxKey;"; //SQL Server 2012+ var sqlQuery = $@"