diff --git a/src/Hangfire.Mongo.Tests/MongoConnectionFacts.cs b/src/Hangfire.Mongo.Tests/MongoConnectionFacts.cs index f26e0ad..28312f0 100644 --- a/src/Hangfire.Mongo.Tests/MongoConnectionFacts.cs +++ b/src/Hangfire.Mongo.Tests/MongoConnectionFacts.cs @@ -127,10 +127,10 @@ public void CreateExpiredJob_CreatesAJobInTheStorage_AndSetsItsParameters() Assert.NotNull(jobId); Assert.NotEmpty(jobId); - var databaseJob = _dbContext.JobGraph + var document = _dbContext.JobGraph .Find(new BsonDocument("_t", nameof(JobDto))) - .Project(b => new JobDto(b)) - .ToList().Single(); + .Single(); + var databaseJob = new JobDto(document); Assert.Equal(jobId, databaseJob.Id.ToString()); Assert.Equal(createdAt, databaseJob.CreatedAt); Assert.Null(databaseJob.StateName); @@ -148,10 +148,9 @@ public void CreateExpiredJob_CreatesAJobInTheStorage_AndSetsItsParameters() var parameters = _dbContext .JobGraph - .Find(new BsonDocument("_t", nameof(JobDto)){ ["_id"] = ObjectId.Parse(jobId) }) - .Project(j => new JobDto(j)) + .Find(new BsonDocument("_t", nameof(JobDto)) { ["_id"] = ObjectId.Parse(jobId) }) .ToList() - .SelectMany(j => j.Parameters) + .SelectMany(j => new JobDto(j).Parameters) .ToDictionary(p => p.Key, x => x.Value); Assert.NotNull(parameters); @@ -326,11 +325,7 @@ public void SetParameters_CreatesNewParameter_WhenParameterWithTheGivenNameDoesN ["_id"] = jobId, ["_t"] = nameof(JobDto), }; - var job = _dbContext - .JobGraph - .Find(filter) - .Project(j => new JobDto(j)) - .FirstOrDefault(); + var job = new JobDto(_dbContext.JobGraph.Find(filter).First()); Assert.NotNull(job.Parameters); Assert.Equal("Value", job.Parameters["Name"]); @@ -357,11 +352,7 @@ public void SetParameter_UpdatesValue_WhenParameterWithTheGivenName_AlreadyExist ["_id"] = jobId, ["_t"] = nameof(JobDto), }; - var job = _dbContext - .JobGraph - .Find(filter) - .Project(j => new JobDto(j)) - .FirstOrDefault(); + var job = new JobDto(_dbContext.JobGraph.Find(filter).FirstOrDefault()); Assert.NotNull(job.Parameters); Assert.Equal("AnotherValue", job.Parameters["Name"]); @@ -387,11 +378,7 @@ public void SetParameter_CanAcceptNulls_AsValues() ["_id"] = jobId, ["_t"] = nameof(JobDto), }; - var job = _dbContext - .JobGraph - .Find(filter) - .Project(j => new JobDto(j)) - .FirstOrDefault(); + var job = new JobDto(_dbContext.JobGraph.Find(filter).FirstOrDefault()); Assert.NotNull(job.Parameters); Assert.Null(job.Parameters["Name"]); @@ -563,7 +550,7 @@ public void AnnounceServer_CreatesOrUpdatesARecord() }; _connection.AnnounceServer("server", context1); - var server = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).Single(); + var server = new ServerDto(_dbContext.Server.AsQueryable().Single()); Assert.Equal("server", server.Id); Assert.Equal(context1.WorkerCount, server.WorkerCount); Assert.Equal(context1.Queues, server.Queues); @@ -576,7 +563,7 @@ public void AnnounceServer_CreatesOrUpdatesARecord() WorkerCount = 1000 }; _connection.AnnounceServer("server", context2); - var sameServer = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).Single(); + var sameServer = new ServerDto(_dbContext.Server.AsQueryable().Single()); Assert.Equal("server", sameServer.Id); Assert.Equal(context2.WorkerCount, sameServer.WorkerCount); } @@ -603,7 +590,7 @@ public void RemoveServer_RemovesAServerRecord() _connection.RemoveServer("Server1"); - var server = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).ToList().Single(); + var server = new ServerDto(_dbContext.Server.AsQueryable().Single()); Assert.NotEqual("Server1", server.Id, StringComparer.OrdinalIgnoreCase); } @@ -639,7 +626,7 @@ public void Heartbeat_UpdatesLastHeartbeat_OfTheServerWithGivenId() _connection.Heartbeat("server1"); - var servers = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).ToList() + var servers = _dbContext.Server.AsQueryable().ToList().Select(b => new ServerDto(b)) .ToDictionary(x => x.Id, x => x.LastHeartbeat); Assert.True(servers.ContainsKey("server1")); @@ -675,7 +662,7 @@ public void RemoveTimedOutServers_DoItsWorkPerfectly() _connection.RemoveTimedOutServers(TimeSpan.FromHours(15)); - var liveServer = _dbContext.Server.Find(new BsonDocument()).Project(b => new ServerDto(b)).Single(); + var liveServer = new ServerDto(_dbContext.Server.AsQueryable().Single()); Assert.Equal("server2", liveServer.Id); } @@ -824,11 +811,7 @@ public void SetRangeInHash_MergesAllRecords() [nameof(HashDto.Key)] = "some-hash", ["_t"] = nameof(HashDto) }; - var result = _dbContext.JobGraph - .Find(filter) - .Project(b => new HashDto(b)) - .First() - .Fields; + var result = new HashDto(_dbContext.JobGraph.Find(filter).First()).Fields; Assert.Equal("Value1", result["Key1"]); Assert.Equal("Value2", result["Key2"]); diff --git a/src/Hangfire.Mongo.Tests/MongoDiscriminatorTests.cs b/src/Hangfire.Mongo.Tests/MongoDiscriminatorTests.cs index fe91a50..f291971 100644 --- a/src/Hangfire.Mongo.Tests/MongoDiscriminatorTests.cs +++ b/src/Hangfire.Mongo.Tests/MongoDiscriminatorTests.cs @@ -53,9 +53,8 @@ public void AddJob_FromWriteOnly_FetchOK() { ["_t"] = nameof(JobDto), ["_id"] = ObjectId.Parse(jobId) - }) - .Project(b => new JobDto(b)) - .FirstOrDefault(); + }).FirstOrDefault(); + // ASSERT Assert.NotNull(jobDto); diff --git a/src/Hangfire.Mongo.Tests/MongoDistributedLockFacts.cs b/src/Hangfire.Mongo.Tests/MongoDistributedLockFacts.cs index a415db8..e9636da 100644 --- a/src/Hangfire.Mongo.Tests/MongoDistributedLockFacts.cs +++ b/src/Hangfire.Mongo.Tests/MongoDistributedLockFacts.cs @@ -168,11 +168,9 @@ public void Ctor_SetLockExpireAtWorks_WhenResourceIsNotLocked() DateTime initialExpireAt = DateTime.UtcNow; Thread.Sleep(TimeSpan.FromSeconds(5)); - DistributedLockDto lockEntry = _database.DistributedLock - .Find(filter) - .Project(b => new DistributedLockDto(b)).FirstOrDefault(); + var lockEntry = _database.DistributedLock.Find(filter).FirstOrDefault(); Assert.NotNull(lockEntry); - Assert.True(lockEntry.ExpireAt > initialExpireAt); + Assert.True(new DistributedLockDto(lockEntry).ExpireAt > initialExpireAt); } } @@ -192,11 +190,7 @@ public void Ctor_AcquireLock_WhenLockExpired() }; using (lock1.AcquireLock()) { - var lockEntry = _database - .DistributedLock - .Find(filter) - .Project(b => new DistributedLockDto(b)) - .Single(); + var lockEntry = new DistributedLockDto(_database.DistributedLock.Find(filter).Single()); Assert.True(lockEntry.ExpireAt > initialExpireAt); } diff --git a/src/Hangfire.Mongo.Tests/MongoJobQueueFacts.cs b/src/Hangfire.Mongo.Tests/MongoJobQueueFacts.cs index 83e4b24..d085566 100644 --- a/src/Hangfire.Mongo.Tests/MongoJobQueueFacts.cs +++ b/src/Hangfire.Mongo.Tests/MongoJobQueueFacts.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Threading; using Hangfire.Mongo.Database; using Hangfire.Mongo.Dto; @@ -142,13 +143,10 @@ public void Dequeue_ShouldLeaveJobInTheQueue_ButSetItsFetchedAtValue() ["_t"] = nameof(JobDto), ["_id"] = ObjectId.Parse(payload.JobId) }; - var fetchedAt = _hangfireDbContext.JobGraph - .Find(filter) - .Project(b => new JobDto(b)) - .FirstOrDefault() - .FetchedAt; + var document = _hangfireDbContext.JobGraph.Find(filter).FirstOrDefault(); + var fetchedAt = new JobDto(document).FetchedAt; - Assert.NotNull(fetchedAt); + Assert.NotNull(document); Assert.True(fetchedAt > DateTime.UtcNow.AddMinutes(-1)); _jobQueueSemaphoreMock.Verify(m => m.WaitNonBlock("default"), Times.Once); } @@ -248,11 +246,10 @@ public void Dequeue_ShouldSetFetchedAt_OnlyForTheFetchedJob() ["_t"] = nameof(JobDto), ["_id"] = new BsonDocument("$ne", ObjectId.Parse(payload.JobId)) }; - var otherJob = _hangfireDbContext - .JobGraph.Find(filter).Project(b => new JobDto(b)) - .FirstOrDefault(); - Assert.NotNull(otherJob); + var document = _hangfireDbContext.JobGraph.Find(filter).FirstOrDefault(); + Assert.NotNull(document); + var otherJob = new JobDto(document); var otherJobFetchedAt = otherJob.FetchedAt; Assert.Null(otherJobFetchedAt); diff --git a/src/Hangfire.Mongo.Tests/MongoWriteOnlyTransactionFacts.cs b/src/Hangfire.Mongo.Tests/MongoWriteOnlyTransactionFacts.cs index 74d8b8b..7a05407 100644 --- a/src/Hangfire.Mongo.Tests/MongoWriteOnlyTransactionFacts.cs +++ b/src/Hangfire.Mongo.Tests/MongoWriteOnlyTransactionFacts.cs @@ -6,7 +6,6 @@ using Hangfire.Mongo.Tests.Utils; using Hangfire.States; using MongoDB.Bson; -using MongoDB.Bson.Serialization.Conventions; using MongoDB.Driver; using Moq; using Xunit; @@ -142,10 +141,7 @@ public void SetJobState_AppendsAStateAndSetItToTheJob() Assert.Null(anotherTestJob.StateName); Assert.Empty(anotherTestJob.StateHistory); - var jobWithStates = _database.JobGraph - .Find(new BsonDocument("_t", nameof(JobDto))) - .Project(b => new JobDto(b)) - .FirstOrDefault(); + var jobWithStates = new JobDto(_database.JobGraph.Find(new BsonDocument("_t", nameof(JobDto))).First()); var jobState = jobWithStates.StateHistory.Single(); Assert.Equal("State", jobState.Name); @@ -178,10 +174,8 @@ public void AddJobState_JustAddsANewRecordInATable() var testJob = GetTestJob(_database, jobId); Assert.Null(testJob.StateName); - var jobWithStates = _database.JobGraph - .Find(new BsonDocument("_t", nameof(JobDto))) - .Project(b => new JobDto(b)) - .Single(); + var jobWithStates = new JobDto(_database.JobGraph.Find(new BsonDocument("_t", nameof(JobDto))).Single()); + var jobState = jobWithStates.StateHistory.Last(); Assert.Equal("State", jobState.Name); Assert.Equal("Reason", jobState.Reason); @@ -199,7 +193,7 @@ public void AddToQueue_CallsEnqueue_OnTargetPersistentQueue() Commit(x => x.AddToQueue("default", jobId.ToString())); - var jobDto = _database + var job = _database .JobGraph .Find(new BsonDocument { @@ -207,10 +201,9 @@ public void AddToQueue_CallsEnqueue_OnTargetPersistentQueue() [nameof(JobDto.Queue)] = "default", ["_id"] = jobId, }) - .Project(b => new JobDto(b)) .FirstOrDefault(); - Assert.NotNull(jobDto); + Assert.NotNull(job); } [Fact] @@ -218,12 +211,7 @@ public void IncrementCounter_AddsRecordToCounterTable_WithPositiveValue() { Commit(x => x.IncrementCounter("my-key")); - var record = _database.JobGraph - .Find(new BsonDocument - { - ["_t"] = nameof(CounterDto) - }) - .Project(b => new CounterDto(b)).ToList().Single(); + var record = new CounterDto(_database.JobGraph.Find(new BsonDocument { ["_t"] = nameof(CounterDto) }).Single()); Assert.Equal("my-key", record.Key); Assert.Equal(1L, record.Value); @@ -235,11 +223,7 @@ public void IncrementCounter_WithExpiry_AddsARecord_WithExpirationTimeSet() { Commit(x => x.IncrementCounter("my-key", TimeSpan.FromDays(1))); - var counter = _database.JobGraph.Find(new BsonDocument - { - ["_t"] = nameof(CounterDto) - }) - .Project(b => new CounterDto(b)).Single(); + var counter = new CounterDto(_database.JobGraph.Find(new BsonDocument { ["_t"] = nameof(CounterDto) }).Single()); Assert.Equal("my-key", counter.Key); Assert.Equal(1L, counter.Value); @@ -260,15 +244,14 @@ public void IncrementCounter_WithExistingKey_AddsAnotherRecord() x.IncrementCounter("my-key"); }); - var counter = _database.JobGraph.Find(new BsonDocument + var document = _database.JobGraph.Find(new BsonDocument { ["_t"] = nameof(CounterDto), [nameof(CounterDto.Key)] = "my-key" - }) - .Project(b => new CounterDto(b)).FirstOrDefault(); + }).FirstOrDefault(); + Assert.NotNull(document); - Assert.NotNull(counter); - Assert.Equal(2, counter.Value); + Assert.Equal(2, new CounterDto(document).Value); } [Fact] @@ -276,11 +259,7 @@ public void DecrementCounter_AddsRecordToCounterTable_WithNegativeValue() { Commit(x => x.DecrementCounter("my-key")); - var record = _database.JobGraph.Find(new BsonDocument - { - ["_t"] = nameof(CounterDto), - }) - .Project(b => new CounterDto(b)).Single(); + var record = new CounterDto(_database.JobGraph.Find(new BsonDocument { ["_t"] = nameof(CounterDto) }).Single()); Assert.Equal("my-key", record.Key); Assert.Equal(-1L, record.Value); @@ -292,11 +271,7 @@ public void DecrementCounter_WithExpiry_AddsARecord_WithExpirationTimeSet() { Commit(x => x.DecrementCounter("my-key", TimeSpan.FromDays(1))); - var counter = _database.JobGraph.Find(new BsonDocument - { - ["_t"] = nameof(CounterDto) - }) - .Project(b => new CounterDto(b)).Single(); + var counter = new CounterDto(_database.JobGraph.Find(new BsonDocument { ["_t"] = nameof(CounterDto) }).Single()); Assert.Equal("my-key", counter.Key); Assert.Equal(-1L, counter.Value); @@ -317,11 +292,7 @@ public void DecrementCounter_WithExistingKey_AddsAnotherRecord() x.DecrementCounter("my-key"); }); - var counter = _database.JobGraph.Find(new BsonDocument - { - ["_t"] = nameof(CounterDto) - }) - .Project(b => new CounterDto(b)).Single(); + var counter = new CounterDto(_database.JobGraph.Find(new BsonDocument { ["_t"] = nameof(CounterDto) }).Single()); Assert.Equal(-2, counter.Value); } @@ -331,11 +302,7 @@ public void AddToSet_AddsARecord_IfThereIsNo_SuchKeyAndValue() { Commit(x => x.AddToSet("my-key", "my-value")); - var record = _database.JobGraph.Find(new BsonDocument - { - ["_t"] = nameof(SetDto) - }) - .Project(b => new SetDto(b)).ToList().Single(); + var record = new SetDto(_database.JobGraph.Find(new BsonDocument {["_t"] = nameof(SetDto) }).Single()); Assert.Equal("my-key", record.Key); Assert.Equal(0.0, record.Score, 2); @@ -377,10 +344,7 @@ public void AddToSet_WithScore_AddsARecordWithScore_WhenBothKeyAndValueAreNotExi { Commit(x => x.AddToSet("my-key", "my-value", 3.2)); - var record = _database.JobGraph - .Find(new BsonDocument("_t", nameof(SetDto))) - .Project(b => new SetDto(b)) - .Single(); + var record = new SetDto(_database.JobGraph.Find(new BsonDocument("_t", nameof(SetDto))).Single()); Assert.Equal("my-key", record.Key); Assert.Equal(3.2, record.Score, 3); @@ -395,8 +359,7 @@ public void AddToSet_WithScore_UpdatesAScore_WhenBothKeyAndValueAreExist() x.AddToSet("my-key", "my-value", 3.2); }); - var record = _database.JobGraph.Find(new BsonDocument("_t", nameof(SetDto))) - .Project(b => new SetDto(b)).Single(); + var record = new SetDto(_database.JobGraph.Find(new BsonDocument("_t", nameof(SetDto))).Single()); Assert.Equal(3.2, record.Score, 3); } @@ -448,7 +411,8 @@ public void InsertToList_AddsARecord_WithGivenValues() { Commit(x => x.InsertToList("my-key", "my-value")); - var record = _database.JobGraph.Find(new BsonDocument("_t", nameof(ListDto))).Project(b => new ListDto(b)).Single(); + var record = new ListDto(_database.JobGraph.Find(new BsonDocument("_t", nameof(ListDto))).Single()); + Assert.Equal("my-key", record.Item); Assert.Equal("my-value", record.Value); } @@ -522,8 +486,7 @@ public void TrimList_TrimsAList_ToASpecifiedRange() x.TrimList("my-key", 1, 2); }); - var records = _database.JobGraph.Find(new BsonDocument("_t", nameof(ListDto))) - .Project(b => new ListDto(b)).ToList(); + var records = _database.JobGraph.Find(new BsonDocument("_t", nameof(ListDto))).ToList().Select(b => new ListDto(b)).ToList(); Assert.Equal(2, records.Count); Assert.Equal("1", records[0].Value); @@ -615,10 +578,7 @@ public void SetRangeInHash_MergesAllRecords() {"Key2", "Value2"} })); - var result = _database.JobGraph - .Find(new BsonDocument(nameof(KeyJobDto.Key), "some-hash") { ["_t"] = nameof(HashDto) }) - .Project(b => new HashDto(b)) - .FirstOrDefault(); + var result = new HashDto(_database.JobGraph.Find(new BsonDocument(nameof(KeyJobDto.Key), "some-hash") { ["_t"] = nameof(HashDto) }).Single()); Assert.Equal("Value1", result.Fields["Key1"]); Assert.Equal("Value2", result.Fields["Key2"]); @@ -877,8 +837,8 @@ private static JobDto GetTestJob(HangfireDbContext database, string jobId) ["_id"] = ObjectId.Parse(jobId), ["_t"] = nameof(JobDto) }; - return database.JobGraph.Find(filter).Project(b => new JobDto(b)) - .FirstOrDefault(); + var document = database.JobGraph.Find(filter).FirstOrDefault(); + return document == null ? null : new JobDto(document); } private static IList GetTestSet(HangfireDbContext database, string key) @@ -888,7 +848,7 @@ private static IList GetTestSet(HangfireDbContext database, string key) [nameof(SetDto.SetType)] = key, ["_t"] = nameof(SetDto) }; - return database.JobGraph.Find(filter).Project(b => new SetDto(b)).ToList(); + return database.JobGraph.Find(filter).ToList().Select(b => new SetDto(b)).ToList(); } private static ListDto GetTestList(HangfireDbContext database, string key) @@ -898,7 +858,8 @@ private static ListDto GetTestList(HangfireDbContext database, string key) [nameof(ListDto.Item)] = key, ["_t"] = nameof(ListDto) }; - return database.JobGraph.Find(filter).Project(b => new ListDto(b)).FirstOrDefault(); + var document = database.JobGraph.Find(filter).FirstOrDefault(); + return document == null ? null : new ListDto(document); } private static HashDto GetTestHash(HangfireDbContext database, string key) @@ -908,7 +869,8 @@ private static HashDto GetTestHash(HangfireDbContext database, string key) [nameof(HashDto.Key)] = key, ["_t"] = nameof(HashDto) }; - return database.JobGraph.Find(filter).Project(b => new HashDto(b)).FirstOrDefault(); + var document = database.JobGraph.Find(filter).FirstOrDefault(); + return document == null ? null : new HashDto(document); } private void Commit(Action action) diff --git a/src/Hangfire.Mongo/Migration/MongoMigrationManager.cs b/src/Hangfire.Mongo/Migration/MongoMigrationManager.cs index c19a3ac..74cf698 100644 --- a/src/Hangfire.Mongo/Migration/MongoMigrationManager.cs +++ b/src/Hangfire.Mongo/Migration/MongoMigrationManager.cs @@ -110,11 +110,8 @@ protected virtual bool Migrate(MongoBackupStrategy backupStrategy, MongoMigratio /// protected virtual SchemaDto GetCurrentSchema(IMongoDatabase database) { - return database - .GetCollection(_storageOptions.Prefix + ".schema") - .Find(new BsonDocument()) - .Project(b => new SchemaDto(b)) - .FirstOrDefault(); + var document = database.GetCollection(_storageOptions.Prefix + ".schema").AsQueryable().FirstOrDefault(); + return document == null ? null : new SchemaDto(document); } } } diff --git a/src/Hangfire.Mongo/MongoConnection.cs b/src/Hangfire.Mongo/MongoConnection.cs index 644d385..009b652 100644 --- a/src/Hangfire.Mongo/MongoConnection.cs +++ b/src/Hangfire.Mongo/MongoConnection.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text.RegularExpressions; using System.Threading; using Hangfire.Common; using Hangfire.Logging; @@ -22,9 +21,9 @@ public class MongoConnection : JobStorageConnection private static readonly ILog Logger = LogProvider.For(); private readonly MongoStorageOptions _storageOptions; private readonly MongoJobFetcher _jobFetcher; - + private readonly HangfireDbContext _dbContext; - + #pragma warning disable 1591 public MongoConnection( HangfireDbContext database, @@ -44,7 +43,7 @@ public override IDisposable AcquireDistributedLock(string resource, TimeSpan tim { var distributedLock = _storageOptions.Factory.CreateMongoDistributedLock(resource, timeout, _dbContext, _storageOptions); - + return distributedLock.AcquireLock(); } @@ -92,18 +91,18 @@ public override string GetJobParameter(string id, string name) throw new ArgumentNullException(nameof(name)); } var objectId = ObjectId.Parse(id); - var jobDto = _dbContext + var job = _dbContext .JobGraph .Find(new BsonDocument { ["_id"] = objectId, ["_t"] = nameof(JobDto) }) - .Project(b => new JobDto(b)) .FirstOrDefault(); + var jobDto = new JobDto(job); string value = null; - jobDto?.Parameters?.TryGetValue(name, out value); + jobDto.Parameters?.TryGetValue(name, out value); return value; } @@ -116,22 +115,22 @@ public override JobData GetJobData(string jobId) } var objectId = ObjectId.Parse(jobId); - var jobDto = _dbContext + var document = _dbContext .JobGraph .Find(new BsonDocument { ["_id"] = objectId, ["_t"] = nameof(JobDto) }) - .Project(b => new JobDto(b)) .FirstOrDefault(); - if (jobDto == null) + if (document == null) { return null; } // TODO: conversion exception could be thrown. + var jobDto = new JobDto(document); var invocationData = JobHelper.FromJson(jobDto.InvocationData); invocationData.Arguments = jobDto.Arguments; @@ -164,21 +163,21 @@ public override StateData GetStateData(string jobId) } var objectId = ObjectId.Parse(jobId); - var jobDto = _dbContext + var document = _dbContext .JobGraph .Find(new BsonDocument { ["_id"] = objectId, ["_t"] = nameof(JobDto) }) - .Project(b => new JobDto(b)) .FirstOrDefault(); - if (jobDto == null) + if (document == null) { return null; } + var jobDto = new JobDto(document); var state = jobDto.StateHistory.LastOrDefault(); if (state == null) @@ -275,7 +274,7 @@ public override HashSet GetAllItemsFromSet(string key) { Logger.Trace($"GetAllItemsFromSet({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -306,7 +305,7 @@ public override List GetFirstByLowestScoreFromSet(string key, double fro { Logger.Trace($"GetFirstByLowestScoreFromSet({key}, {fromScore}, {toScore}, {count})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -343,7 +342,7 @@ public override void SetRangeInHash(string key, IEnumerable GetAllEntriesFromHash(string key) { Logger.Trace($"GetAllEntriesFromHash({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -381,7 +380,7 @@ public override long GetSetCount(string key) { Logger.Trace($"GetSetCount({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -403,7 +402,7 @@ public override List GetRangeFromSet(string key, int startingFrom, int e { Logger.Trace($"GetRangeFromSet({key}, {startingFrom}, {endingAt})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -430,7 +429,7 @@ public override TimeSpan GetSetTtl(string key) { Logger.Trace($"GetSetTtl({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -461,7 +460,7 @@ public override long GetCounter(string key) { Logger.Trace($"GetCounter({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -474,10 +473,9 @@ public override long GetCounter(string key) [nameof(CounterDto.Key)] = key, ["_t"] = nameof(CounterDto) }) - .Project(b => new CounterDto(b)) .FirstOrDefault(); - return counter?.Value ?? 0; + return counter == null ? 0 : new CounterDto(counter).Value; } public override long GetHashCount(string key) @@ -486,7 +484,7 @@ public override long GetHashCount(string key) { Logger.Trace($"GetHashCount({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -499,10 +497,9 @@ public override long GetHashCount(string key) [nameof(HashDto.Key)] = key, ["_t"] = nameof(HashDto) }) - .Project(b => new HashDto(b)) .FirstOrDefault(); - return hash?.Fields.Count ?? 0; + return hash == null ? 0 : new HashDto(hash).Fields.Count; } public override TimeSpan GetHashTtl(string key) @@ -511,7 +508,7 @@ public override TimeSpan GetHashTtl(string key) { Logger.Trace($"GetHashTtl({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -542,7 +539,7 @@ public override string GetValueFromHash(string key, string name) { Logger.Trace($"GetValueFromHash({key}, {name})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -559,14 +556,13 @@ public override string GetValueFromHash(string key, string name) new BsonDocument(nameof(KeyJobDto.Key), key), new BsonDocument($"{nameof(HashDto.Fields)}.{name}", new BsonDocument("$exists", true)) }); - + var result = _dbContext .JobGraph .Find(hashWithField) - .Project(b => new HashDto(b)) .FirstOrDefault(); - return result?.Fields[name]; + return result == null ? null : new HashDto(result).Fields[name]; } public override long GetListCount(string key) @@ -575,7 +571,7 @@ public override long GetListCount(string key) { Logger.Trace($"GetListCount({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -597,7 +593,7 @@ public override TimeSpan GetListTtl(string key) { Logger.Trace($"GetListTtl({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -613,7 +609,7 @@ public override TimeSpan GetListTtl(string key) .Sort(new BsonDocument(nameof(ListDto.ExpireAt), 1)) .Project(new BsonDocument(nameof(ListDto.ExpireAt), 1)) .FirstOrDefault(); - + if(listDto == null) { return TimeSpan.FromSeconds(-1); @@ -628,7 +624,7 @@ public override List GetRangeFromList(string key, int startingFrom, int { Logger.Trace($"GetRangeFromList({key}, {startingFrom}, {endingAt})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); @@ -654,7 +650,7 @@ public override List GetAllItemsFromList(string key) { Logger.Trace($"GetAllItemsFromList({key})"); } - + if (key == null) { throw new ArgumentNullException(nameof(key)); diff --git a/src/Hangfire.Mongo/MongoMonitoringApi.cs b/src/Hangfire.Mongo/MongoMonitoringApi.cs index 4dd880e..e05a6eb 100644 --- a/src/Hangfire.Mongo/MongoMonitoringApi.cs +++ b/src/Hangfire.Mongo/MongoMonitoringApi.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.Design; using System.Linq; using System.Threading.Tasks; using Hangfire.Common; @@ -49,7 +50,7 @@ public virtual IList Queues() public virtual IList Servers() { - var servers = _dbContext.Server.Find(new BsonDocument()).Project(b => new Dto.ServerDto(b)).ToList(); + var servers = _dbContext.Server.AsQueryable().ToList().Select(b => new Dto.ServerDto(b)); var result = new List(); @@ -424,8 +425,8 @@ public virtual JobList EnqueuedJobs(IEnumerable jobIds) var jobs = _dbContext .JobGraph .Find(jobsByIdsFilter) - .Project(b => new JobDto(b)) - .ToList(); + .ToList() + .Select(b => new JobDto(b)); var enqueuedJobs = jobs .Select(job => @@ -509,8 +510,8 @@ public virtual JobList FetchedJobs(HangfireDbContext connection, var jobs = connection .JobGraph .Find(filter) - .Project(b => new JobDto(b)) - .ToList(); + .ToList() + .Select(b => new JobDto(b)); var fetcedJobs = jobs .Select(job => @@ -563,8 +564,8 @@ public virtual JobList GetJobs(int from, int count, string stateName .Sort(new BsonDocument("_id", -1)) .Skip(from) .Limit(count) - .Project(b => new JobDto(b)) - .ToList(); + .ToList() + .Select(b => new JobDto(b)); var joinedJobs = jobs .Select(job => @@ -645,8 +646,8 @@ public virtual Dictionary CreateTimeLineStats(IEnumerable new CounterDto(b)) .ToList() + .Select(b => new CounterDto(b)) .GroupBy(counter => counter.Key, counter => counter) .ToDictionary(counter => counter.Key, grouping => grouping.Sum(c => c.Value));